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

feat: support output bundler unminified #772

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
2 changes: 1 addition & 1 deletion boilerplate/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-father",
"version": "4.5.0",
"version": "4.5.1-beta.3",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个版本号是误提交的?

"description": "Creator for father boilerplate",
"homepage": "https://github.com/umijs/father/tree/master/boilerplate#readme",
"bugs": "https://github.com/umijs/father/issues",
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "father",
"version": "4.5.0",
"version": "4.5.1-beta.3",
"description": "A bundless/bundle build tool",
"homepage": "https://github.com/umijs/father#readme",
"bugs": "https://github.com/umijs/father/issues",
Expand Down Expand Up @@ -43,12 +43,12 @@
},
"dependencies": {
"@microsoft/api-extractor": "7.39.1",
"@umijs/babel-preset-umi": "^4.3.12",
"@umijs/bundler-utils": "^4.3.12",
"@umijs/bundler-webpack": "^4.3.12",
"@umijs/babel-preset-umi": "^4.3.14",
"@umijs/bundler-utils": "^4.3.14",
"@umijs/bundler-webpack": "^4.3.14",
"@umijs/case-sensitive-paths-webpack-plugin": "^1.0.1",
"@umijs/core": "^4.3.12",
"@umijs/utils": "^4.3.12",
"@umijs/core": "^4.3.14",
"@umijs/utils": "^4.3.14",
"@vercel/ncc": "0.33.3",
"babel-plugin-dynamic-import-node": "2.3.3",
"babel-plugin-module-resolver": "4.1.0",
Expand Down
76 changes: 38 additions & 38 deletions pnpm-lock.yaml

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

27 changes: 14 additions & 13 deletions src/builder/bundle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ interface IBundleOpts {
incremental?: boolean;
}

function bundle(opts: Omit<IBundleOpts, 'watch' | 'incremental'>): Promise<void>;
function bundle(
opts: Omit<IBundleOpts, 'watch' | 'incremental'>,
): Promise<void>;
function bundle(opts: IBundleOpts): Promise<IBundleWatcher>;
async function bundle(opts: IBundleOpts): Promise<void | IBundleWatcher> {
const enableCache = process.env.FATHER_CACHE !== 'none';
Expand Down Expand Up @@ -68,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 @@ -80,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,
cssMinifier: CSSMinifier.cssnano,
extraBabelIncludes: [/node_modules/],

Expand Down Expand Up @@ -165,21 +166,21 @@ async function bundle(opts: IBundleOpts): Promise<void | IBundleWatcher> {
// enable webpack persistent cache
...(enableCache
? {
cache: {
buildDependencies: opts.buildDependencies,
},
}
cache: {
buildDependencies: opts.buildDependencies,
},
}
: {}),

// collect close handlers for watch mode
...(opts.watch
? {
onBuildComplete({ isFirstCompile, close }: any) {
if (isFirstCompile) closeHandlers.push(close);
// log for watch mode
else logStatus();
},
}
onBuildComplete({ isFirstCompile, close }: any) {
if (isFirstCompile) closeHandlers.push(close);
// log for watch mode
else logStatus();
},
}
: {}),
disableCopy: true,
});
Expand Down
111 changes: 78 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,97 @@ 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`,
Jinbao1001 marked this conversation as resolved.
Show resolved Hide resolved
// default to output dist
path: output.path || 'dist/umd',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这部分还是可以继续放在公共配置里?

},
};
// 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 };
if (singleConfig.generateUnminified) {
assert(
Jinbao1001 marked this conversation as resolved.
Show resolved Hide resolved
!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}${
singleConfig.generateUnminified ? '.min' : ''
}`
: `${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) {
if (umd.generateUnminified) {
Jinbao1001 marked this conversation as resolved.
Show resolved Hide resolved
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}.js`
: `${defaultFileName}.js`,
path: output.path || 'dist/umd',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个默认值还是写了多次;可以 bundlerConfig.output.path 有个默认值,后续各配置生成的时候优先取配置里的,兜底到默认值

},
});
}

configs.push({
...bundleConfig,

// default to bundle src/index
entry: entryConfig || 'src/index',
entry: defaultEntry,
jsMinifier: JSMinifier.terser,
output: {
filename: output.filename
? `${output.filename}${umd.generateUnminified ? '.min' : ''}`
: `${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
Loading
Loading