Skip to content

Commit

Permalink
fix: make --treeshake work with hashbang
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Aug 12, 2022
1 parent c3dc8d8 commit c06b5e1
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -65,7 +65,7 @@
"tsconfig-paths": "3.12.0",
"tsup": "6.0.1",
"typescript": "4.6.3",
"vitest": "0.19.0",
"vitest": "0.21.1",
"wait-for-expect": "3.0.2"
},
"peerDependencies": {
Expand Down
16 changes: 8 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 12 additions & 11 deletions src/esbuild/index.ts
Expand Up @@ -103,8 +103,8 @@ export async function runEsbuild(
format === 'iife'
? false
: typeof options.splitting === 'boolean'
? options.splitting
: format === 'esm'
? options.splitting
: format === 'esm'

const platform = options.platform || 'node'
const loader = options.loader || {}
Expand All @@ -131,12 +131,12 @@ export async function runEsbuild(
// esbuild's `external` option doesn't support RegExp
// So here we use a custom plugin to implement it
format !== 'iife' &&
externalPlugin({
external,
noExternal: options.noExternal,
skipNodeModulesBundle: options.skipNodeModulesBundle,
tsconfigResolvePaths: options.tsconfigResolvePaths,
}),
externalPlugin({
external,
noExternal: options.noExternal,
skipNodeModulesBundle: options.skipNodeModulesBundle,
tsconfigResolvePaths: options.tsconfigResolvePaths,
}),
options.tsconfigDecoratorMetadata && swcPlugin({ logger }),
nativeNodeModulesPlugin(),
postcssPlugin({ css, inject: options.injectStyle }),
Expand All @@ -156,7 +156,8 @@ export async function runEsbuild(
try {
result = await esbuild({
entryPoints: options.entry,
format: format === 'cjs' && splitting ? 'esm' : format,
format:
(format === 'cjs' && splitting) || options.treeshake ? 'esm' : format,
bundle: typeof options.bundle === 'undefined' ? true : options.bundle,
platform,
globalName: options.globalName,
Expand Down Expand Up @@ -198,8 +199,8 @@ export async function runEsbuild(
TSUP_FORMAT: JSON.stringify(format),
...(format === 'cjs' && injectShims
? {
'import.meta.url': 'importMetaUrl',
}
'import.meta.url': 'importMetaUrl',
}
: {}),
...options.define,
...Object.keys(env).reduce((res, key) => {
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/tree-shaking.ts
@@ -1,4 +1,5 @@
import { rollup, TreeshakingOptions, TreeshakingPreset } from 'rollup'
import hashbang from 'rollup-plugin-hashbang'
import { Plugin } from '../plugin'

export type TreeshakingStrategy =
Expand All @@ -20,6 +21,7 @@ export const treeShakingPlugin = ({
const bundle = await rollup({
input: [info.path],
plugins: [
hashbang(),
{
name: 'tsup',
resolveId(source) {
Expand Down
29 changes: 27 additions & 2 deletions test/index.test.ts
Expand Up @@ -13,7 +13,10 @@ const cacheDir = path.resolve(__dirname, '.cache')
const bin = path.resolve(__dirname, '../dist/cli-default.js')

const getTestName = () => {
const name = expect.getState().currentTestName
const name = expect
.getState()
.currentTestName?.replace(/^[a-z]+/g, '_')
.replace(/-/g, '_')

if (!name) {
throw new Error('No test name')
Expand Down Expand Up @@ -944,7 +947,9 @@ test('use rollup for treeshaking', async () => {
}
)
expect(await getFileContent('dist/input.mjs')).toContain(
`import { inject } from 'vue'`
`function useRoute() {
return inject(routeLocationKey);
}`
)
})

Expand Down Expand Up @@ -1032,3 +1037,23 @@ test('remove unused code', async () => {
)
expect(await getFileContent('dist/input.js')).not.toContain('console.log(1)')
})

test('treeshake should work with hashbang', async () => {
const { getFileContent } = await run(
getTestName(),
{
'input.ts': '#!/usr/bin/node\nconsole.log(123)',
},
{
flags: ['--treeshake'],
}
)
expect(await getFileContent('dist/input.js')).toMatchInlineSnapshot(`
"#!/usr/bin/node
'use strict';
// input.ts
console.log(123);
"
`)
})

0 comments on commit c06b5e1

Please sign in to comment.