这是indexloc提供的服务,不要输入任何密码
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export default defineConfig((options) => {
})
```

The `options` here is derived from CLI flags.
The `options` here are derived from CLI flags.

#### package.json

Expand Down Expand Up @@ -192,16 +192,16 @@ You can combine this command with Tsup [`onSuccess`](https://tsup.egoist.dev/#on
tsup index.ts --sourcemap
```

This will emit `./dist/index.js` and `./dist/index.js.map`.
This will emit `./dist/index.js` and `./dist/index.js.map`. Setting multiple entry files will result in each getting its corresponding `.map` file.

If you set multiple entry files, each entry will get a corresponding `.map` file.

If you want to inline sourcemap, you can try:
If you want to inline sourcemap, you can use:

```bash
tsup index.ts --sourcemap inline
```

Supported sourcemap options: `external`, `linked`, `inline`, `both`. [Esbuild sourcemap options](https://esbuild.github.io/api/#sourcemap)

> Warning: Note that inline sourcemap is solely used for development, e.g. when developing a browser extension and the access to `.map` file is not allowed, and it's not recommended for production.

> Warning: Source map is not supported in `--dts` build.
Expand Down
15 changes: 14 additions & 1 deletion src/esbuild/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from 'path'
import {
build as esbuild,
BuildResult,
CommonOptions,
formatMessages,
Plugin as EsbuildPlugin,
} from 'esbuild'
Expand Down Expand Up @@ -181,6 +182,18 @@ export async function runEsbuild(
? options.footer({ format })
: options.footer

// if sourcemap is set to true, select `external`
let sourcemap: CommonOptions['sourcemap'] =
options.sourcemap === true ? 'external' : false

// if sourcemap is any of the allowed options, select that one
const sourcemapOptions = ['external', 'linked', 'inline', 'both']
if (
typeof options.sourcemap === 'string' &&
sourcemapOptions.indexOf(options.sourcemap) !== -1
)
sourcemap = options.sourcemap

try {
result = await esbuild({
entryPoints: options.entry,
Expand All @@ -191,7 +204,7 @@ export async function runEsbuild(
globalName: options.globalName,
jsxFactory: options.jsxFactory,
jsxFragment: options.jsxFragment,
sourcemap: options.sourcemap ? 'external' : false,
sourcemap,
target: options.target,
banner,
footer,
Expand Down
2 changes: 1 addition & 1 deletion src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export type Options = {
[k: string]: string
}
dts?: boolean | string | DtsConfig
sourcemap?: boolean | 'inline'
sourcemap?: boolean | 'inline' | 'external' | 'linked' | 'both'
/** Always bundle modules matching given patterns */
noExternal?: (string | RegExp)[]
/** Don't bundle these modules */
Expand Down
86 changes: 77 additions & 9 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ test('not bundle `package/subpath` in dts (resolve)', async () => {
'node_modules/foo/bar.d.ts': `export type Foobar = { foo: 'foo', bar: 'bar' }`,
'node_modules/foo/package.json': `{ "name": "foo", "version": "0.0.0" }`,
},
{
{
flags: ['--dts', '--dts-resolve'],
}
)
Expand Down Expand Up @@ -957,6 +957,72 @@ test('native-node-module plugin should handle *.node(.js) import properly', asyn
)
})

test('support `inline` sourcemaps', async () => {
const { getFileContent, outFiles } = await run(
getTestName(),
{
'input.ts': `export const hi = 'hi'`,
},
{
entry: ['input.ts'],
flags: ['--sourcemap', 'inline'],
}
)
// outFiles should not contain input.js.map
expect(outFiles).toEqual(['input.js'])
const contents = await getFileContent('dist/input.js')
expect(contents).toContain(
'//# sourceMappingURL=data:application/json;base64'
)
})

test('support `external` sourcemaps', async () => {
const { getFileContent, outFiles } = await run(
getTestName(),
{
'input.ts': `export const hi = 'hi'`,
},
{
entry: ['input.ts'],
flags: ['--sourcemap', 'external'],
}
)
expect(outFiles).toEqual(['input.js', 'input.js.map'])
})

test('support `linked` sourcemaps', async () => {
const { getFileContent, outFiles } = await run(
getTestName(),
{
'input.ts': `export const hi = 'hi'`,
},
{
entry: ['input.ts'],
flags: ['--sourcemap', 'linked'],
}
)
expect(outFiles).toEqual(['input.js', 'input.js.map'])
})

test('support `both` sourcemaps', async () => {
const { getFileContent, outFiles } = await run(
getTestName(),
{
'input.ts': `export const hi = 'hi'`,
},
{
entry: ['input.ts'],
flags: ['--sourcemap', 'both'],
}
)
expect(outFiles).toEqual(['input.js', 'input.js.map'])
const contents = await getFileContent('dist/input.js')
expect(contents).toContain(
'//# sourceMappingURL=data:application/json;base64'
)
expect(contents).toContain('//# sourceMappingURL=input.js.map')
})

test('proper sourcemap sources path when swc is enabled', async () => {
const { getFileContent } = await run(
getTestName(),
Expand Down Expand Up @@ -1114,10 +1180,10 @@ test('support target in tsconfig.json', async () => {
"baseUrl":".",
"target": "esnext"
}
}`
},
}`,
},
{
flags: ['--format', 'esm', ],
flags: ['--format', 'esm'],
}
)
expect(await getFileContent('dist/input.mjs')).contains('await import(')
Expand All @@ -1135,11 +1201,13 @@ test('override target in tsconfig.json', async () => {
"baseUrl":".",
"target": "esnext"
}
}`
},
}`,
},
{
flags: ['--format', 'esm', '--target', 'es2018' ],
flags: ['--format', 'esm', '--target', 'es2018'],
}
)
).rejects.toThrowError(`Top-level await is not available in the configured target environment ("es2018")`)
})
).rejects.toThrowError(
`Top-level await is not available in the configured target environment ("es2018")`
)
})