From dadb3632a0f824f6e6a1c246ed8788bea5cbcad6 Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 18 Jun 2024 18:12:26 +0800 Subject: [PATCH] build: use swc minify for prod builds --- package.json | 2 +- pnpm-lock.yaml | 169 ++++++++++++++++++++++++++++++++++++----------- rollup.config.js | 27 +++++--- 3 files changed, 150 insertions(+), 48 deletions(-) diff --git a/package.json b/package.json index 77ff894b99f..8c5b72f838c 100644 --- a/package.json +++ b/package.json @@ -67,7 +67,7 @@ "@rollup/plugin-json": "^6.1.0", "@rollup/plugin-node-resolve": "^15.2.3", "@rollup/plugin-replace": "5.0.4", - "@rollup/plugin-terser": "^0.4.4", + "@swc/core": "^1.6.1", "@types/hash-sum": "^1.0.2", "@types/minimist": "^1.2.5", "@types/node": "^20.14.2", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1df276b6dfb..624d2512e55 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -32,9 +32,9 @@ importers: '@rollup/plugin-replace': specifier: 5.0.4 version: 5.0.4(rollup@4.18.0) - '@rollup/plugin-terser': - specifier: ^0.4.4 - version: 0.4.4(rollup@4.18.0) + '@swc/core': + specifier: ^1.6.1 + version: 1.6.1 '@types/hash-sum': specifier: ^1.0.2 version: 1.0.2 @@ -807,15 +807,6 @@ packages: rollup: optional: true - '@rollup/plugin-terser@0.4.4': - resolution: {integrity: sha512-XHeJC5Bgvs8LfukDwWZp7yeqin6ns8RTl2B9avbejt6tZqsqvVoWI7ZTQrcNsfKEDWBTnTxM8nMDkO2IFFbd0A==} - engines: {node: '>=14.0.0'} - peerDependencies: - rollup: ^2.0.0||^3.0.0||^4.0.0 - peerDependenciesMeta: - rollup: - optional: true - '@rollup/pluginutils@5.1.0': resolution: {integrity: sha512-XTIWOPPcpvyKI6L1NHo0lFlCyznUEyPmPY1mc3KpPVDYulHSTvyeLNVW00QTLIAFNhR3kYnJTQHeGqU4M3n09g==} engines: {node: '>=14.0.0'} @@ -915,6 +906,81 @@ packages: resolution: {integrity: sha512-tlqY9xq5ukxTUZBmoOp+m61cqwQD5pHJtFY3Mn8CA8ps6yghLH/Hw8UPdqg4OLmFW3IFlcXnQNmo/dh8HzXYIQ==} engines: {node: '>=18'} + '@swc/core-darwin-arm64@1.6.1': + resolution: {integrity: sha512-u6GdwOXsOEdNAdSI6nWq6G2BQw5HiSNIZVcBaH1iSvBnxZvWbnIKyDiZKaYnDwTLHLzig2GuUjjE2NaCJPy4jg==} + engines: {node: '>=10'} + cpu: [arm64] + os: [darwin] + + '@swc/core-darwin-x64@1.6.1': + resolution: {integrity: sha512-/tXwQibkDNLVbAtr7PUQI0iQjoB708fjhDDDfJ6WILSBVZ3+qs/LHjJ7jHwumEYxVq1XA7Fv2Q7SE/ZSQoWHcQ==} + engines: {node: '>=10'} + cpu: [x64] + os: [darwin] + + '@swc/core-linux-arm-gnueabihf@1.6.1': + resolution: {integrity: sha512-aDgipxhJTms8iH78emHVutFR2c16LNhO+NTRCdYi+X4PyIn58/DyYTH6VDZ0AeEcS5f132ZFldU5AEgExwihXA==} + engines: {node: '>=10'} + cpu: [arm] + os: [linux] + + '@swc/core-linux-arm64-gnu@1.6.1': + resolution: {integrity: sha512-XkJ+eO4zUKG5g458RyhmKPyBGxI0FwfWFgpfIj5eDybxYJ6s4HBT5MoxyBLorB5kMlZ0XoY/usUMobPVY3nL0g==} + engines: {node: '>=10'} + cpu: [arm64] + os: [linux] + + '@swc/core-linux-arm64-musl@1.6.1': + resolution: {integrity: sha512-dr6YbLBg/SsNxs1hDqJhxdcrS8dGMlOXJwXIrUvACiA8jAd6S5BxYCaqsCefLYXtaOmu0bbx1FB/evfodqB70Q==} + engines: {node: '>=10'} + cpu: [arm64] + os: [linux] + + '@swc/core-linux-x64-gnu@1.6.1': + resolution: {integrity: sha512-A0b/3V+yFy4LXh3O9umIE7LXPC7NBWdjl6AQYqymSMcMu0EOb1/iygA6s6uWhz9y3e172Hpb9b/CGsuD8Px/bg==} + engines: {node: '>=10'} + cpu: [x64] + os: [linux] + + '@swc/core-linux-x64-musl@1.6.1': + resolution: {integrity: sha512-5dJjlzZXhC87nZZZWbpiDP8kBIO0ibis893F/rtPIQBI5poH+iJuA32EU3wN4/WFHeK4et8z6SGSVghPtWyk4g==} + engines: {node: '>=10'} + cpu: [x64] + os: [linux] + + '@swc/core-win32-arm64-msvc@1.6.1': + resolution: {integrity: sha512-HBi1ZlwvfcUibLtT3g/lP57FaDPC799AD6InolB2KSgkqyBbZJ9wAXM8/CcH67GLIP0tZ7FqblrJTzGXxetTJQ==} + engines: {node: '>=10'} + cpu: [arm64] + os: [win32] + + '@swc/core-win32-ia32-msvc@1.6.1': + resolution: {integrity: sha512-AKqHohlWERclexar5y6ux4sQ8yaMejEXNxeKXm7xPhXrp13/1p4/I3E5bPVX/jMnvpm4HpcKSP0ee2WsqmhhPw==} + engines: {node: '>=10'} + cpu: [ia32] + os: [win32] + + '@swc/core-win32-x64-msvc@1.6.1': + resolution: {integrity: sha512-0dLdTLd+ONve8kgC5T6VQ2Y5G+OZ7y0ujjapnK66wpvCBM6BKYGdT/OKhZKZydrC5gUKaxFN6Y5oOt9JOFUrOQ==} + engines: {node: '>=10'} + cpu: [x64] + os: [win32] + + '@swc/core@1.6.1': + resolution: {integrity: sha512-Yz5uj5hNZpS5brLtBvKY0L4s2tBAbQ4TjmW8xF1EC3YLFxQRrUjMP49Zm1kp/KYyYvTkSaG48Ffj2YWLu9nChw==} + engines: {node: '>=10'} + peerDependencies: + '@swc/helpers': '*' + peerDependenciesMeta: + '@swc/helpers': + optional: true + + '@swc/counter@0.1.3': + resolution: {integrity: sha512-e2BR4lsJkkRlKZ/qCHPw9ZaSxc0MVUd7gtbtaB7aMvHeJVYe8sOB8DBZkP2DtISHGSku9sCK6T6cnY0CtXrOCQ==} + + '@swc/types@0.1.8': + resolution: {integrity: sha512-RNFA3+7OJFNYY78x0FYwi1Ow+iF1eF5WvmfY1nXPOEH4R2p/D4Cr1vzje7dNAI2aLFqpv8Wyz4oKSWqIZArpQA==} + '@tootallnate/quickjs-emscripten@0.23.0': resolution: {integrity: sha512-C5Mc6rdnsaJDjO3UpGW/CQTHtCKaYlScZTly4JIu97Jxo/odCiH0ITnDXSJPTOrEKk/ycSZ0AOgTmkDtkOsvIA==} @@ -2757,9 +2823,6 @@ packages: queue-tick@1.0.1: resolution: {integrity: sha512-kJt5qhMxoszgU/62PLP1CJytzd2NKetjSRnyuj31fDd3Rlcz3fzlFdFLD1SItunPwyqEOkca6GbV612BWfaBag==} - randombytes@2.1.0: - resolution: {integrity: sha512-vYl3iOX+4CKUWuxGi9Ukhie6fsqXqS9FE2Zaic4tNFD2N2QQaXOMFbuKK4QmDHC0JO6B1Zp41J0LpT0oR68amQ==} - range-parser@1.2.0: resolution: {integrity: sha512-kA5WQoNVo4t9lNx2kQNFCxKeBl5IbbSNBl1M/tLkw9WCn+hxNBAW5Qh8gdhs63CJnhjJ2zQWFoqPJP2sK1AV5A==} engines: {node: '>= 0.6'} @@ -2900,9 +2963,6 @@ packages: engines: {node: '>=10'} hasBin: true - serialize-javascript@6.0.2: - resolution: {integrity: sha512-Saa1xPByTTq2gdeFZYLLo+RFE35NHZkAbqZeWNd3BpzppeVisAqpDjcp8dyf6uIvEqJRd46jemmyA4iFIeVk8g==} - serve-handler@6.1.5: resolution: {integrity: sha512-ijPFle6Hwe8zfmBxJdE+5fta53fdIY0lHISJvuikXB3VYFafRjMRpOffSPvCYsbKyBA7pvy9oYr/BT1O3EArlg==} @@ -2963,9 +3023,6 @@ packages: resolution: {integrity: sha512-94hK0Hh8rPqQl2xXc3HsaBoOXKV20MToPkcXvwbISWLEs+64sBq5kFgn2kJDHb1Pry9yrP0dxrCI9RRci7RXKg==} engines: {node: '>= 6.0.0', npm: '>= 3.0.0'} - smob@1.5.0: - resolution: {integrity: sha512-g6T+p7QO8npa+/hNx9ohv1E5pVCmWrVCUzUXJyLdMmftX6ER0oiWY/w9knEonLpnOp6b6FenKnMfR8gqwWdwig==} - socks-proxy-agent@8.0.3: resolution: {integrity: sha512-VNegTZKhuGq5vSD6XNKlbqWhyt/40CgoEw8XxD6dhnm8Jq9IEa3nIa4HwnM8XOqU0CdB0BwWVXusqiFXfHB3+A==} engines: {node: '>= 14'} @@ -3815,14 +3872,6 @@ snapshots: optionalDependencies: rollup: 4.18.0 - '@rollup/plugin-terser@0.4.4(rollup@4.18.0)': - dependencies: - serialize-javascript: 6.0.2 - smob: 1.5.0 - terser: 5.31.1 - optionalDependencies: - rollup: 4.18.0 - '@rollup/pluginutils@5.1.0(rollup@4.18.0)': dependencies: '@types/estree': 1.0.5 @@ -3885,6 +3934,58 @@ snapshots: '@sindresorhus/merge-streams@4.0.0': {} + '@swc/core-darwin-arm64@1.6.1': + optional: true + + '@swc/core-darwin-x64@1.6.1': + optional: true + + '@swc/core-linux-arm-gnueabihf@1.6.1': + optional: true + + '@swc/core-linux-arm64-gnu@1.6.1': + optional: true + + '@swc/core-linux-arm64-musl@1.6.1': + optional: true + + '@swc/core-linux-x64-gnu@1.6.1': + optional: true + + '@swc/core-linux-x64-musl@1.6.1': + optional: true + + '@swc/core-win32-arm64-msvc@1.6.1': + optional: true + + '@swc/core-win32-ia32-msvc@1.6.1': + optional: true + + '@swc/core-win32-x64-msvc@1.6.1': + optional: true + + '@swc/core@1.6.1': + dependencies: + '@swc/counter': 0.1.3 + '@swc/types': 0.1.8 + optionalDependencies: + '@swc/core-darwin-arm64': 1.6.1 + '@swc/core-darwin-x64': 1.6.1 + '@swc/core-linux-arm-gnueabihf': 1.6.1 + '@swc/core-linux-arm64-gnu': 1.6.1 + '@swc/core-linux-arm64-musl': 1.6.1 + '@swc/core-linux-x64-gnu': 1.6.1 + '@swc/core-linux-x64-musl': 1.6.1 + '@swc/core-win32-arm64-msvc': 1.6.1 + '@swc/core-win32-ia32-msvc': 1.6.1 + '@swc/core-win32-x64-msvc': 1.6.1 + + '@swc/counter@0.1.3': {} + + '@swc/types@0.1.8': + dependencies: + '@swc/counter': 0.1.3 + '@tootallnate/quickjs-emscripten@0.23.0': {} '@types/estree@1.0.5': {} @@ -5865,10 +5966,6 @@ snapshots: queue-tick@1.0.1: {} - randombytes@2.1.0: - dependencies: - safe-buffer: 5.2.1 - range-parser@1.2.0: {} rc@1.2.8: @@ -6028,10 +6125,6 @@ snapshots: semver@7.6.2: {} - serialize-javascript@6.0.2: - dependencies: - randombytes: 2.1.0 - serve-handler@6.1.5: dependencies: bytes: 3.0.0 @@ -6102,8 +6195,6 @@ snapshots: smart-buffer@4.2.0: {} - smob@1.5.0: {} - socks-proxy-agent@8.0.3: dependencies: agent-base: 7.1.1 diff --git a/rollup.config.js b/rollup.config.js index e8d5984184d..1253a42e0db 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -9,11 +9,11 @@ import pico from 'picocolors' import commonJS from '@rollup/plugin-commonjs' import polyfillNode from 'rollup-plugin-polyfill-node' import { nodeResolve } from '@rollup/plugin-node-resolve' -import terser from '@rollup/plugin-terser' import esbuild from 'rollup-plugin-esbuild' import alias from '@rollup/plugin-alias' import { entries } from './scripts/aliases.js' import { inlineEnums } from './scripts/inline-enums.js' +import { minify as minifySwc } from '@swc/core' /** * @template T @@ -364,14 +364,25 @@ function createMinifiedConfig(/** @type {PackageFormat} */ format) { format: outputConfigs[format].format, }, [ - terser({ - module: /^esm/.test(format), - compress: { - ecma: 2016, - pure_getters: true, + { + name: 'swc-minify', + + async renderChunk( + contents, + _, + { format, sourcemap, sourcemapExcludeSources }, + ) { + const { code, map } = await minifySwc(contents, { + module: format === 'es', + compress: true, + mangle: true, + sourceMap: !!sourcemap, + inlineSourcesContent: !sourcemapExcludeSources, + }) + + return { code, map: map || null } }, - safari10: true, - }), + }, ], ) }