这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
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: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ This will emit `./dist/index.js` and `./dist/index.d.ts`.

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

### Generate sourcemap file

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

This will emit `./dist/index.js` and `./dist/index.js.map`.

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

### Bundle formats

Supported format: `esm`, `cjs`, (default) and `iife`.
Expand Down
1 change: 1 addition & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ async function main() {
'Output different formats to different folder instead of using different extensions'
)
.option('--dts', 'Generate declaration file')
.option('--sourcemap', 'Generate sourcemap file')
.option('--watch', 'Watch mode')
.option('--env.* <value>', 'Define compile-time env variables')
.option('--define.* <value>', 'Define compile-time constants')
Expand Down
74 changes: 39 additions & 35 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export type Options = {
[k: string]: string
}
dts?: boolean
sourcemap?: boolean;
/** Don't bundle these packages */
external?: string[]
/** Transform the result with `@babel/core` */
Expand Down Expand Up @@ -107,6 +108,7 @@ export async function runEsbuild(
globalName: options.globalName,
jsxFactory: options.jsxFactory,
jsxFragment: options.jsxFragment,
sourcemap: options.sourcemap,
target: options.target === 'es5' ? 'es2016' : options.target,
define: {
...options.define,
Expand Down Expand Up @@ -152,51 +154,53 @@ export async function runEsbuild(
const dir = dirname(file.path)
const outPath = file.path
const ext = extname(outPath)
if (ext !== '.js' && ext !== outExtension['.js']) return
const comeFromSource = ext === '.js' || ext === outExtension['.js']
await fs.promises.mkdir(dir, { recursive: true })
let mode: number | undefined
if (file.contents[0] === 35 && file.contents[1] === 33) {
mode = 0o755
}
let contents = textDecoder.decode(file.contents)
if (options.babel) {
const babel = getBabel()
if (babel) {
contents = await babel
.transformAsync(contents, {
filename: file.path,
})
.then((res) => res?.code || contents)
} else {
throw new PrettyError(
`@babel/core is not found in ${process.cwd()}`
)
if (comeFromSource) {
if (options.babel) {
const babel = getBabel()
if (babel) {
contents = await babel
.transformAsync(contents, {
filename: file.path,
})
.then((res) => res?.code || contents)
} else {
throw new PrettyError(
`@babel/core is not found in ${process.cwd()}`
)
}
}
}
if (options.target === 'es5') {
try {
contents = transformToEs5(contents, {
source: file.path,
file: file.path,
transforms: {
modules: false,
arrow: true,
dangerousTaggedTemplateString: true,
spreadRest: true,
},
if (options.target === 'es5') {
try {
contents = transformToEs5(contents, {
source: file.path,
file: file.path,
transforms: {
modules: false,
arrow: true,
dangerousTaggedTemplateString: true,
spreadRest: true,
},
}).code
} catch (error) {
throw new PrettyError(
`Error compiling to es5 target:\n${error.snippet}`
)
}
}
// Cause we need to transform to code from esm to cjs first
if (format === 'cjs') {
contents = transform(contents, {
transforms: ['imports'],
}).code
} catch (error) {
throw new PrettyError(
`Error compiling to es5 target:\n${error.snippet}`
)
}
}
// Cause we need to transform to code from esm to cjs first
if (format === 'cjs') {
contents = transform(contents, {
transforms: ['imports'],
}).code
}
await fs.promises.writeFile(outPath, contents, {
encoding: 'utf8',
mode,
Expand Down
11 changes: 11 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ runTest(
}
)

runTest(
'bundle graphql-tools with --sourcemap flag',
{
'input.ts': `export { makeExecutableSchema } from 'graphql-tools'`,
},
{
snapshot: false,
flags: ['--sourcemap'],
}
)

runTest(
'es5 target',
{
Expand Down