Skip to content

Commit

Permalink
fix: fix sourcemap behavior (#349)
Browse files Browse the repository at this point in the history
Co-authored-by: EGOIST <[email protected]>
  • Loading branch information
KingSora and egoist authored Aug 26, 2022
1 parent ecbf5a7 commit 61df802
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
7 changes: 1 addition & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export type Options = Omit<
export default ({
include,
exclude,
sourceMap: _sourceMap,
sourceMap = true,
optimizeDeps,
tsconfig,
loaders: _loaders,
Expand Down Expand Up @@ -95,7 +95,6 @@ export default ({

let optimizeDepsResult: OptimizeDepsResult | undefined
let cwd = process.cwd()
let sourceMap = false

return {
name: 'esbuild',
Expand All @@ -106,10 +105,6 @@ export default ({
}
return null
},
outputOptions({ sourcemap }) {
sourceMap = _sourceMap ?? !!sourcemap
return null
},

async buildStart() {
if (!optimizeDeps || optimizeDepsResult) return
Expand Down
15 changes: 6 additions & 9 deletions src/minify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type Options = Omit<TransformOptions, 'sourcemap'> & {
}

export const getRenderChunk = ({
sourceMap,
sourceMap = true,
...options
}: Options): RenderChunkHook =>
async function (code, _, rollupOptions) {
Expand All @@ -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)
Expand All @@ -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,
Expand Down
71 changes: 71 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>) => {
const tmpDir = path.join(__dirname, '.temp', `esbuild/${folderName}`)
Object.keys(files).forEach((file) => {
Expand All @@ -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,
Expand All @@ -42,6 +58,10 @@ const build = async ({
return output
}

beforeEach(() => {
mockEsbuildTransform.mockClear()
})

describe('esbuild plugin', () => {
test('simple', async () => {
const dir = realFs(getTestName(), {
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit 61df802

Please sign in to comment.