diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 181dee9b..df855d61 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -336,7 +336,7 @@ packages: '@babel/compat-data': 7.21.4 '@babel/core': 7.18.2 '@babel/helper-validator-option': 7.21.0 - browserslist: 4.21.5 + browserslist: 4.24.3 semver: 6.3.0 dev: true @@ -4089,6 +4089,7 @@ packages: /acorn-import-assertions@1.8.0(acorn@8.7.1): resolution: {integrity: sha512-m7VZ3jwz4eK6A4Vtt8Ew1/mNbP24u0FhdyfA7fSvnJR6LMdfOYnmuIrrJAgrYfYJ10F/otaHTtrtrtmHdMNzEw==} + deprecated: package has been renamed to acorn-import-attributes peerDependencies: acorn: ^8 dependencies: @@ -4274,7 +4275,7 @@ packages: peerDependencies: postcss: ^8.1.0 dependencies: - browserslist: 4.21.5 + browserslist: 4.24.3 caniuse-lite: 1.0.30001466 fraction.js: 4.2.0 normalize-range: 0.1.2 @@ -4609,6 +4610,16 @@ packages: node-releases: 2.0.10 update-browserslist-db: 1.0.10(browserslist@4.21.5) + /browserslist@4.24.3: + resolution: {integrity: sha512-1CPmv8iobE2fyRMV97dAcMVegvvWKxmq94hkLiAkUGwKVTyDLw33K+ZxiFrREKmmps4rIw6grcCFCnTMSZ/YiA==} + engines: {node: ^6 || ^7 || ^8 || ^9 || ^10 || ^11 || ^12 || >=13.7} + hasBin: true + dependencies: + caniuse-lite: 1.0.30001690 + electron-to-chromium: 1.5.76 + node-releases: 2.0.19 + update-browserslist-db: 1.1.1(browserslist@4.24.3) + /bser@2.1.1: resolution: {integrity: sha512-gQxTNE/GAfIIrmHLUE3oJyp5FO6HRBfhjnw4/wMmA63ZGDJnWBmgY/lyQBpnDUkGmAhbSe39tx2d/iTOAfglwQ==} dependencies: @@ -4670,6 +4681,9 @@ packages: /caniuse-lite@1.0.30001466: resolution: {integrity: sha512-ewtFBSfWjEmxUgNBSZItFSmVtvk9zkwkl1OfRZlKA8slltRN+/C/tuGVrF9styXkN36Yu3+SeJ1qkXxDEyNZ5w==} + /caniuse-lite@1.0.30001690: + resolution: {integrity: sha512-5ExiE3qQN6oF8Clf8ifIDcMRCRE/dMGcETG/XGMD8/XiXm6HXQgQTh1yZYLXXpSOsEUlJm1Xr7kGULZTuGtP/w==} + /chalk@2.4.2: resolution: {integrity: sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==} engines: {node: '>=4'} @@ -5326,6 +5340,9 @@ packages: /electron-to-chromium@1.4.331: resolution: {integrity: sha512-tdtr9y9oJl8VDiS+HeB6e/JDJqdDGjIk3yRfEMHm5rDnWQ/D5SbafybAayInxolbfbH6pouV5g7ZUAwE/WVtHw==} + /electron-to-chromium@1.5.76: + resolution: {integrity: sha512-CjVQyG7n7Sr+eBXE86HIulnL5N8xZY1sgmOPGuq/F0Rr0FJq63lg0kEtOIDfZBk44FnDLf6FUJ+dsJcuiUDdDQ==} + /elliptic@6.5.7: resolution: {integrity: sha512-ESVCtTwiA+XhY3wyh24QqRGBoP3rEdDUl3EDUUo9tft074fi19IrdpH7hLCMMP3CIj7jb3W96rn8lt/BqIlt5Q==} dependencies: @@ -5582,6 +5599,10 @@ packages: resolution: {integrity: sha512-k0er2gUkLf8O0zKJiAhmkTnJlTvINGv7ygDNPbeIsX/TJjGJZHuh9B2UxbsaEkmlEo9MfhrSzmhIlhRlI2GXnw==} engines: {node: '>=6'} + /escalade@3.2.0: + resolution: {integrity: sha512-WUj2qlxaQtO4g6Pq5c29GTcWGDyd8itL8zTlipgECz3JesAiiOKotd8JU6otB3PACgG6xkJUyVhboMS+bje/jA==} + engines: {node: '>=6'} + /escape-string-regexp@1.0.5: resolution: {integrity: sha512-vbRorB5FUQWvla16U8R/qgaFIya2qGzwDrNmCZuYKrbdSUMG6I1ZCGQRefkRVhuOkIGVne7BQ35DSfo1qvJqFg==, tarball: https://registry.npmjs.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz} engines: {node: '>=0.8.0'} @@ -7787,6 +7808,9 @@ packages: /node-releases@2.0.10: resolution: {integrity: sha512-5GFldHPXVG/YZmFzJvKK2zDSzPKhEp0+ZR5SVaoSag9fsL5YgHbUHDfnG5494ISANDcK4KwPXAx2xqVEydmd7w==} + /node-releases@2.0.19: + resolution: {integrity: sha512-xxOWJsBKtzAq7DY0J+DTzuz58K8e7sJbdgwkbMWQe8UYB6ekmsQ45q0M/tJDsGaZmbC+l7n57UV8Hl5tHxO9uw==} + /normalize-package-data@2.5.0: resolution: {integrity: sha512-/5CMN3T0R4XTj4DcGaexo+roZSdSFW/0AOOTROrjxzCG1wrWXEsGbRKevjlIL+ZDE4sZlJr5ED4YW0yqmkK+eA==} dependencies: @@ -8060,6 +8084,9 @@ packages: /picocolors@1.0.0: resolution: {integrity: sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ==} + /picocolors@1.1.1: + resolution: {integrity: sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA==} + /picomatch@2.3.1: resolution: {integrity: sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==} engines: {node: '>=8.6'} @@ -8429,7 +8456,7 @@ packages: '@csstools/postcss-stepped-value-functions': 1.0.0(postcss@8.4.31) '@csstools/postcss-unset-value': 1.0.1(postcss@8.4.31) autoprefixer: 10.4.7(postcss@8.4.31) - browserslist: 4.21.5 + browserslist: 4.24.3 css-blank-pseudo: 3.0.3(postcss@8.4.31) css-has-pseudo: 3.0.4(postcss@8.4.31) css-prefers-color-scheme: 6.0.3(postcss@8.4.31) @@ -9810,6 +9837,16 @@ packages: escalade: 3.1.1 picocolors: 1.0.0 + /update-browserslist-db@1.1.1(browserslist@4.24.3): + resolution: {integrity: sha512-R8UzCaa9Az+38REPiJ1tXlImTJXlVfgHZsglwBD/k6nj76ctsH1E3q4doGrukiLQd3sGQYu56r5+lo5r94l29A==} + hasBin: true + peerDependencies: + browserslist: '>= 4.21.0' + dependencies: + browserslist: 4.24.3 + escalade: 3.2.0 + picocolors: 1.1.1 + /uri-js@4.4.1: resolution: {integrity: sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==} dependencies: @@ -9955,7 +9992,7 @@ packages: '@webassemblyjs/wasm-parser': 1.11.5 acorn: 8.7.1 acorn-import-assertions: 1.8.0(acorn@8.7.1) - browserslist: 4.21.5 + browserslist: 4.24.3 chrome-trace-event: 1.0.3 enhanced-resolve: 5.13.0 es-module-lexer: 1.2.1 diff --git a/src/builder/bundle/index.ts b/src/builder/bundle/index.ts index 2cf8d7ea..a4bc36ba 100644 --- a/src/builder/bundle/index.ts +++ b/src/builder/bundle/index.ts @@ -70,7 +70,6 @@ async function bundle(opts: IBundleOpts): Promise { devtool: config.sourcemap && 'source-map', externals: config.externals, outputPath: config.output.path, - // postcss config extraPostCSSPlugins, postcssLoader, @@ -82,7 +81,7 @@ async function bundle(opts: IBundleOpts): Promise { // compatible with IE11 by default targets: getBundleTargets(config), - jsMinifier: JSMinifier.terser, + jsMinifier: config.jsMinifier || JSMinifier.terser, cssMinifier: CSSMinifier.cssnano, extraBabelIncludes: [/node_modules/], diff --git a/src/builder/config.ts b/src/builder/config.ts index 38bb47d0..8463b58b 100644 --- a/src/builder/config.ts +++ b/src/builder/config.ts @@ -1,4 +1,6 @@ +import { JSMinifier } from '@umijs/bundler-webpack/dist/types'; import { winPath } from '@umijs/utils'; +import assert from 'assert'; import { Minimatch } from 'minimatch'; import path from 'path'; import { loadConfig } from 'tsconfig-paths'; @@ -24,6 +26,7 @@ export interface IBundleConfig Omit { type: IFatherBuildTypes.BUNDLE; bundler: 'webpack'; + jsMinifier: JSMinifier; entry: string; output: { filename: string; @@ -111,55 +114,100 @@ export function normalizeUserConfig( const entryConfig = umd.entry; const output = typeof umd.output === 'object' ? umd.output : { path: umd.output }; - const bundleConfig: Omit = { - type: IFatherBuildTypes.BUNDLE, - bundler: 'webpack', - ...baseConfig, + const bundleConfig: Omit = + { + type: IFatherBuildTypes.BUNDLE, + bundler: 'webpack', + ...baseConfig, - // override base configs from umd config - ...umd, - - // generate default output - output: { - // default to generate filename from package name - filename: - output.filename || `${getAutoBundleFilename(pkg.name)}.min.js`, - // default to output dist - path: output.path || 'dist/umd', - }, - }; + // override base configs from umd config + ...umd, + }; if (typeof entryConfig === 'object') { - // extract multiple entries to single configs - Object.keys(entryConfig).forEach((entry) => { - const outputConfig = entryConfig[entry].output; + Object.entries(entryConfig).forEach(([entry, singleConfig]) => { + const outputConfig = singleConfig.output; + const entryOutput = typeof outputConfig === 'object' ? outputConfig : { path: outputConfig }; + assert( + !( + singleConfig.generateUnminified && + entryOutput.filename?.includes('.min') + ), + 'if set generateUnminified enabled, you need to delete ".min" in the output filename config', + ); - configs.push({ + const minifiedConfig = { ...bundleConfig, - - // override all configs from entry config - ...entryConfig[entry], + ...singleConfig, entry, - - // override output + jsMinifier: JSMinifier.terser, output: { - filename: - entryOutput.filename || `${path.parse(entry).name}.min.js`, - path: entryOutput.path || bundleConfig.output.path, + filename: entryOutput.filename + ? entryOutput.filename.replace( + /(\.js)?$/, + singleConfig.generateUnminified ? '.min.js' : '.js', + ) + : `${path.parse(entry).name}.min.js`, + path: entryOutput.path || output.path || 'dist/umd', }, - }); + }; + + if (singleConfig.generateUnminified) { + const unminifiedConfig = { + ...bundleConfig, + ...singleConfig, + entry, + jsMinifier: JSMinifier.none, + sourcemap: false, + output: { + filename: entryOutput.filename || `${path.parse(entry).name}.js`, + path: entryOutput.path || output.path || 'dist/umd', + }, + }; + configs.push(unminifiedConfig, minifiedConfig); + } else { + configs.push(minifiedConfig); + } }); } else { - // generate single entry to single config + const defaultEntry = entryConfig || 'src/index'; + const defaultFileName = getAutoBundleFilename(pkg.name); + if (umd.generateUnminified) { + assert( + !output.filename?.includes('.min'), + 'if set generateUnminified enabled, you need to delete ".min" in the output filename config', + ); + configs.push({ + ...bundleConfig, + entry: defaultEntry, + jsMinifier: JSMinifier.none, + sourcemap: false, + output: { + filename: output.filename + ? `${output.filename}` + : `${defaultFileName}.js`, + path: output.path || 'dist/umd', + }, + }); + } + configs.push({ ...bundleConfig, - - // default to bundle src/index - entry: entryConfig || 'src/index', + entry: defaultEntry, + jsMinifier: JSMinifier.terser, + output: { + filename: output.filename + ? output.filename.replace( + /(\.js)?$/, + umd.generateUnminified ? '.min.js' : '.js', + ) + : `${defaultFileName}.min.js`, + path: output.path || 'dist/umd', + }, }); } } diff --git a/src/features/configPlugins/schema.ts b/src/features/configPlugins/schema.ts index dc19d91b..1bfc9dd4 100644 --- a/src/features/configPlugins/schema.ts +++ b/src/features/configPlugins/schema.ts @@ -64,6 +64,7 @@ export function getSchemas(): Record any> { Joi.string(), Joi.array(), ), + generateUnminified: Joi.boolean().optional(), chainWebpack: Joi.function().optional(), extractCSS: Joi.boolean().optional(), name: Joi.string().optional(), diff --git a/src/types.ts b/src/types.ts index 8d8f2557..7de151e4 100644 --- a/src/types.ts +++ b/src/types.ts @@ -221,6 +221,13 @@ export interface IFatherBundleConfig extends IFatherBaseConfig { * configure less variables */ theme?: Record; + + /** + * output unminified js file + * @default false + * @note When set to true, unminified js file will be generated in the same directory without sourcemap. + */ + generateUnminified?: boolean; } export interface IFatherPreBundleConfig {