From 70fbaae0546486630b243fdcf3911b80c184d9ab Mon Sep 17 00:00:00 2001 From: 10Derozan Date: Wed, 18 Oct 2023 14:17:29 +0800 Subject: [PATCH] fix(module-tools): put terser at the end of renderChunk to ensure minify (#4803) --- .changeset/nasty-files-fetch.md | 6 ++++++ .../module-tools/src/builder/feature/index.ts | 8 ++++---- .../build/minify/__snapshots__/minify.test.ts.snap | 3 +++ .../module/fixtures/build/minify/minify.test.ts | 12 ++++++++++++ .../module/fixtures/build/minify/modern.config.ts | 11 +++++++++++ 5 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 .changeset/nasty-files-fetch.md create mode 100644 tests/integration/module/fixtures/build/minify/__snapshots__/minify.test.ts.snap create mode 100644 tests/integration/module/fixtures/build/minify/modern.config.ts diff --git a/.changeset/nasty-files-fetch.md b/.changeset/nasty-files-fetch.md new file mode 100644 index 000000000000..faca24059568 --- /dev/null +++ b/.changeset/nasty-files-fetch.md @@ -0,0 +1,6 @@ +--- +'@modern-js/module-tools': patch +--- + +fix(module-tools): put terser at the end of renderChunk to ensure minify +fix(module-tools): 把 terser 放在 renderChunk 的最后阶段来确保全部代码压缩 diff --git a/packages/solutions/module-tools/src/builder/feature/index.ts b/packages/solutions/module-tools/src/builder/feature/index.ts index f5104bab3d19..b8988db22869 100644 --- a/packages/solutions/module-tools/src/builder/feature/index.ts +++ b/packages/solutions/module-tools/src/builder/feature/index.ts @@ -25,10 +25,6 @@ export async function getInternalList(context: Context): Promise { internal.push(redirect, json); } - if (config.minify && config.minify !== 'esbuild') { - internal.push(minify); - } - const userTsconfig = await getProjectTsconfig(context.config.tsconfig); const emitDecoratorMetadata = userTsconfig?.compilerOptions?.emitDecoratorMetadata ?? false; @@ -54,5 +50,9 @@ export async function getInternalList(context: Context): Promise { internal.push(swcRenderChunk); } + if (config.minify && config.minify !== 'esbuild') { + internal.push(minify); + } + return internal; } diff --git a/tests/integration/module/fixtures/build/minify/__snapshots__/minify.test.ts.snap b/tests/integration/module/fixtures/build/minify/__snapshots__/minify.test.ts.snap new file mode 100644 index 000000000000..c0de0e27c52f --- /dev/null +++ b/tests/integration/module/fixtures/build/minify/__snapshots__/minify.test.ts.snap @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`minify usage umd + terser 1`] = `"var e,t;e=this,t=function(e,t){"use strict";function r(e){return e&&e.__esModule?e:{default:e}}Object.defineProperty(e,"__esModule",{value:!0}),Object.defineProperty(e,"debug",{enumerable:!0,get:function(){return l}}),t=r(t);var n,o=Object.defineProperty,i=Object.getOwnPropertyNames,u={};((e,t)=>{for(var r in t)o(e,r,{get:t[r],enumerable:!0})})(u,{addPrefix:()=>n});var a,d,f=(a={"src/common.ts"(){n=(e,t)=>\`\${e}:\${t}\`}},function(){return a&&(d=(0,a[i(a)[0]])(a=0)),d}),l=e=>{return r=void 0,n=null,o=function*(){const{addPrefix:r}=yield Promise.resolve().then((()=>(f(),u)));r("DEBUG:",t.default.join(e))},new Promise(((e,t)=>{var i=e=>{try{a(o.next(e))}catch(r){t(r)}},u=e=>{try{a(o.throw(e))}catch(r){t(r)}},a=t=>t.done?e(t.value):Promise.resolve(t.value).then(i,u);a((o=o.apply(r,n)).next())}));var r,n,o}},"object"===typeof module&&"object"===typeof module.exports?t(exports,require("path")):"function"===typeof define&&define.amd?define(["exports","path"],t):(e="undefined"!==typeof globalThis?globalThis:e||self)&&t(e.index={},e.path);"`; diff --git a/tests/integration/module/fixtures/build/minify/minify.test.ts b/tests/integration/module/fixtures/build/minify/minify.test.ts index 7c54766a807f..cc32b4e40caf 100644 --- a/tests/integration/module/fixtures/build/minify/minify.test.ts +++ b/tests/integration/module/fixtures/build/minify/minify.test.ts @@ -42,4 +42,16 @@ describe('minify usage', () => { expect((await esbuildMinifyStat).size).toBeLessThan((await stat).size); expect((await terserMinifyStat).size).toBeLessThan((await stat).size); }); + + it('umd + terser', async () => { + await runCli({ + argv: ['build'], + appDirectory: fixtureDir, + }); + const content = await fs.readFile( + path.join(fixtureDir, './dist/umd/index.js'), + 'utf-8', + ); + expect(content).toMatchSnapshot(); + }); }); diff --git a/tests/integration/module/fixtures/build/minify/modern.config.ts b/tests/integration/module/fixtures/build/minify/modern.config.ts new file mode 100644 index 000000000000..284b1d2b6069 --- /dev/null +++ b/tests/integration/module/fixtures/build/minify/modern.config.ts @@ -0,0 +1,11 @@ +import { defineConfig } from '@modern-js/module-tools/defineConfig'; + +export default defineConfig({ + buildConfig: { + buildType: 'bundle', + input: ['./src/index.ts'], + minify: 'terser', + format: 'umd', + outDir: './dist/umd', + }, +});