From c06b5e14269c1b1cba28d6951896bae6709d4d1a Mon Sep 17 00:00:00 2001 From: EGOIST <0x142857@gmail.com> Date: Fri, 12 Aug 2022 13:48:19 +0800 Subject: [PATCH] fix: make --treeshake work with hashbang --- package.json | 2 +- pnpm-lock.yaml | 16 ++++++++-------- src/esbuild/index.ts | 23 ++++++++++++----------- src/plugins/tree-shaking.ts | 2 ++ test/index.test.ts | 29 +++++++++++++++++++++++++++-- 5 files changed, 50 insertions(+), 22 deletions(-) diff --git a/package.json b/package.json index 656ea603..0beb67a8 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c76745f8..2a1493dc 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -38,7 +38,7 @@ specifiers: 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 dependencies: @@ -81,7 +81,7 @@ devDependencies: tsconfig-paths: 3.12.0 tsup: 6.0.1_ien5tfzdggmpmrmtxysw6xj5lu typescript: 4.6.3 - vitest: 0.19.0 + vitest: 0.21.1 wait-for-expect: 3.0.2 packages: @@ -304,11 +304,11 @@ packages: /@types/chai-subset/1.3.3: resolution: {integrity: sha512-frBecisrNGz+F4T6bcc+NLeolfiojh5FxW2klu669+8BARtyQv2C/GkNW6FUodVe4BroGMP/wER/YDGc7rEllw==} dependencies: - '@types/chai': 4.3.1 + '@types/chai': 4.3.3 dev: true - /@types/chai/4.3.1: - resolution: {integrity: sha512-/zPMqDkzSZ8t3VtxOa4KPq7uzzW978M9Tvh+j7GHKuo6k6GTLxPJ4J5gE5cjfJ26pnXst0N5Hax8Sr0T2Mi9zQ==} + /@types/chai/4.3.3: + resolution: {integrity: sha512-hC7OMnszpxhZPduX+m+nrx+uFoLkWOMiR4oa/AZF3MuSETYTZmFfJAHqZEM8MVlvfG7BEUcgvtwoCTxBp6hm3g==} dev: true /@types/debug/4.1.7: @@ -1649,8 +1649,8 @@ packages: fsevents: 2.3.2 dev: true - /vitest/0.19.0: - resolution: {integrity: sha512-nU80Gm95tMchigHpAMukxv1LoWbBGgknX/1MqrXCOoJoJL7/wfq4h2aow61o2jwf5szQrahoNqBqaGb+fYdYrQ==} + /vitest/0.21.1: + resolution: {integrity: sha512-WBIxuFmIDPuK47GO6Lu9eNeRMqHj/FWL3dk73OHH3eyPPWPiu+UB3QHLkLK2PEggCqJW4FaWoWg8R68S7p9+9Q==} engines: {node: '>=v14.16.0'} hasBin: true peerDependencies: @@ -1674,7 +1674,7 @@ packages: jsdom: optional: true dependencies: - '@types/chai': 4.3.1 + '@types/chai': 4.3.3 '@types/chai-subset': 1.3.3 '@types/node': 14.18.12 chai: 4.3.6 diff --git a/src/esbuild/index.ts b/src/esbuild/index.ts index 4a052ea4..780eb007 100644 --- a/src/esbuild/index.ts +++ b/src/esbuild/index.ts @@ -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 || {} @@ -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 }), @@ -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, @@ -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) => { diff --git a/src/plugins/tree-shaking.ts b/src/plugins/tree-shaking.ts index 610d8f2a..63034f94 100644 --- a/src/plugins/tree-shaking.ts +++ b/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 = @@ -20,6 +21,7 @@ export const treeShakingPlugin = ({ const bundle = await rollup({ input: [info.path], plugins: [ + hashbang(), { name: 'tsup', resolveId(source) { diff --git a/test/index.test.ts b/test/index.test.ts index 3676025d..7eaec1b1 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -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') @@ -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); +}` ) }) @@ -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); + " + `) +})