diff --git a/README.md b/README.md index 320ede9..4fd3172 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ export default { // All options are optional include: /\.[jt]sx?$/, // default, inferred from `loaders` option exclude: /node_modules/, // default - sourceMap: false, // by default inferred from rollup's `output.sourcemap` option + sourceMap: true, // default minify: process.env.NODE_ENV === 'production', target: 'es2017', // default, or 'es20XX', 'esnext' jsx: 'transform', // default, or 'preserve' diff --git a/src/index.ts b/src/index.ts index 870f521..77d925e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -50,7 +50,7 @@ export type Options = Omit< export default ({ include, exclude, - sourceMap: _sourceMap, + sourceMap = true, optimizeDeps, tsconfig, loaders: _loaders, @@ -95,7 +95,6 @@ export default ({ let optimizeDepsResult: OptimizeDepsResult | undefined let cwd = process.cwd() - let sourceMap = false return { name: 'esbuild', @@ -106,10 +105,6 @@ export default ({ } return null }, - outputOptions({ sourcemap }) { - sourceMap = _sourceMap ?? !!sourcemap - return null - }, async buildStart() { if (!optimizeDeps || optimizeDepsResult) return diff --git a/src/minify.ts b/src/minify.ts index 8957338..3460e0f 100644 --- a/src/minify.ts +++ b/src/minify.ts @@ -18,7 +18,7 @@ export type Options = Omit & { } export const getRenderChunk = ({ - sourceMap, + sourceMap = true, ...options }: Options): RenderChunkHook => async function (code, _, rollupOptions) { @@ -32,7 +32,7 @@ export const getRenderChunk = ({ const result = await transform(code, { format, loader: 'js', - sourcemap: sourceMap !== false, + sourcemap: sourceMap, ...options, }) await warn(this, result.warnings) @@ -46,16 +46,13 @@ export const getRenderChunk = ({ return null } -export const minify = (options: Options = {}): Plugin => { - let sourceMap = false +export const minify = ({ + sourceMap = true, + ...options +}: Options = {}): Plugin => { return { name: 'esbuild-minify', - outputOptions({ sourcemap }) { - sourceMap = options.sourceMap ?? !!sourcemap - return null - }, - renderChunk: getRenderChunk({ minify: true, ...options, diff --git a/test/index.test.ts b/test/index.test.ts index 3c1c5a5..620a94e 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -3,6 +3,19 @@ import fs from 'fs' import { rollup, Plugin as RollupPlugin, ModuleFormat } from 'rollup' import esbuild, { minify } from '../src' +const mockEsbuildTransform = jest.fn() + +jest.mock('esbuild', () => { + const originalModule = jest.requireActual('esbuild') + return { + ...originalModule, + transform: (...args: any[]) => { + mockEsbuildTransform(...args) + return originalModule.transform(...args) + }, + } +}) + const realFs = (folderName: string, files: Record) => { const tmpDir = path.join(__dirname, '.temp', `esbuild/${folderName}`) Object.keys(files).forEach((file) => { @@ -21,18 +34,21 @@ const build = async ({ rollupPlugins = [], dir = '.', format = 'esm', + external, }: { input?: string | string[] sourcemap?: boolean rollupPlugins?: RollupPlugin[] dir?: string format?: ModuleFormat + external?: string[] } = {}) => { const build = await rollup({ input: [...(Array.isArray(input) ? input : [input])].map((v) => path.resolve(dir, v) ), plugins: rollupPlugins, + external, }) const { output } = await build.generate({ format, @@ -42,6 +58,10 @@ const build = async ({ return output } +beforeEach(() => { + mockEsbuildTransform.mockClear() +}) + describe('esbuild plugin', () => { test('simple', async () => { const dir = realFs(getTestName(), { @@ -430,6 +450,7 @@ describe('esbuild plugin', () => { input: './fixture/index.jsx', dir, rollupPlugins: [esbuild({})], + external: ['react/jsx-runtime'], }) expect(output[0].code).toMatchInlineSnapshot(` "import { jsx } from 'react/jsx-runtime'; @@ -476,6 +497,56 @@ describe('esbuild plugin', () => { " `) }) + + test('sourcemap', async () => { + const dir = realFs(getTestName(), { + './fixture/index.js': `console.log('sourcemap')`, + }) + + // default: + await build({ + dir, + rollupPlugins: [esbuild({})], + }) + expect(mockEsbuildTransform).toHaveBeenCalledWith( + expect.any(String), + expect.objectContaining({ + sourcemap: true, + }) + ) + + // sourceMap: false + await build({ + dir, + rollupPlugins: [ + esbuild({ + sourceMap: false, + }), + ], + }) + expect(mockEsbuildTransform).toHaveBeenCalledWith( + expect.any(String), + expect.objectContaining({ + sourcemap: false, + }) + ) + + // sourceMap: true + await build({ + dir, + rollupPlugins: [ + esbuild({ + sourceMap: true, + }), + ], + }) + expect(mockEsbuildTransform).toHaveBeenCalledWith( + expect.any(String), + expect.objectContaining({ + sourcemap: true, + }) + ) + }) }) describe('minify plugin', () => {