Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(module-tools): put terser at the end of renderChunk to ensure minify #4803

Merged
merged 2 commits into from
Oct 18, 2023
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
6 changes: 6 additions & 0 deletions .changeset/nasty-files-fetch.md
Original file line number Diff line number Diff line change
@@ -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 的最后阶段来确保全部代码压缩
8 changes: 4 additions & 4 deletions packages/solutions/module-tools/src/builder/feature/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ export async function getInternalList(context: Context): Promise<HookList> {
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;
Expand All @@ -54,5 +50,9 @@ export async function getInternalList(context: Context): Promise<HookList> {
internal.push(swcRenderChunk);
}

if (config.minify && config.minify !== 'esbuild') {
internal.push(minify);
}

return internal;
}
Original file line number Diff line number Diff line change
@@ -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);"`;
12 changes: 12 additions & 0 deletions tests/integration/module/fixtures/build/minify/minify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
11 changes: 11 additions & 0 deletions tests/integration/module/fixtures/build/minify/modern.config.ts
Original file line number Diff line number Diff line change
@@ -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',
},
});