Skip to content

Commit

Permalink
feat: support output bundler unminified (#772)
Browse files Browse the repository at this point in the history
* feat: support output bundler uncomporess
  • Loading branch information
Jinbao1001 authored Dec 30, 2024
1 parent e537130 commit 0cdea25
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 39 deletions.
45 changes: 41 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions src/builder/bundle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ async function bundle(opts: IBundleOpts): Promise<void | IBundleWatcher> {
devtool: config.sourcemap && 'source-map',
externals: config.externals,
outputPath: config.output.path,

// postcss config
extraPostCSSPlugins,
postcssLoader,
Expand All @@ -82,7 +81,7 @@ async function bundle(opts: IBundleOpts): Promise<void | IBundleWatcher> {

// compatible with IE11 by default
targets: getBundleTargets(config),
jsMinifier: JSMinifier.terser,
jsMinifier: config.jsMinifier || JSMinifier.terser,
cssMinifier: CSSMinifier.cssnano,
extraBabelIncludes: [/node_modules/],

Expand Down
114 changes: 81 additions & 33 deletions src/builder/config.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -24,6 +26,7 @@ export interface IBundleConfig
Omit<IFatherBundleConfig, 'entry' | 'output'> {
type: IFatherBuildTypes.BUNDLE;
bundler: 'webpack';
jsMinifier: JSMinifier;
entry: string;
output: {
filename: string;
Expand Down Expand Up @@ -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<IBundleConfig, 'entry'> = {
type: IFatherBuildTypes.BUNDLE,
bundler: 'webpack',
...baseConfig,
const bundleConfig: Omit<IBundleConfig, 'entry' | 'output' | 'jsMinifier'> =
{
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',
},
});
}
}
Expand Down
1 change: 1 addition & 0 deletions src/features/configPlugins/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export function getSchemas(): Record<string, (Joi: Root) => any> {
Joi.string(),
Joi.array(),
),
generateUnminified: Joi.boolean().optional(),
chainWebpack: Joi.function().optional(),
extractCSS: Joi.boolean().optional(),
name: Joi.string().optional(),
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,13 @@ export interface IFatherBundleConfig extends IFatherBaseConfig {
* configure less variables
*/
theme?: Record<string, string>;

/**
* 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 {
Expand Down

0 comments on commit 0cdea25

Please sign in to comment.