Skip to content

Commit a04eca9

Browse files
committed
feat: support --watch-only
1 parent 515b136 commit a04eca9

File tree

4 files changed

+152
-143
lines changed

4 files changed

+152
-143
lines changed

src/builder/bundle/index.ts

+130-127
Original file line numberDiff line numberDiff line change
@@ -28,159 +28,162 @@ interface IBundleOpts {
2828
configProvider: BundleConfigProvider;
2929
buildDependencies?: string[];
3030
watch?: boolean;
31+
watchOnly?: boolean;
3132
}
3233

33-
function bundle(opts: Omit<IBundleOpts, 'watch'>): Promise<void>;
34+
function bundle(opts: Omit<IBundleOpts, 'watch' | 'watchOnly'>): Promise<void>;
3435
function bundle(opts: IBundleOpts): Promise<IBundleWatcher>;
3536
async function bundle(opts: IBundleOpts): Promise<void | IBundleWatcher> {
3637
const enableCache = process.env.FATHER_CACHE !== 'none';
3738
const closeHandlers: webpack.Watching['close'][] = [];
3839

39-
for (let i = 0; i < opts.configProvider.configs.length; i += 1) {
40-
const config = opts.configProvider.configs[i];
41-
const { plugins: extraPostCSSPlugins, ...postcssLoader } =
42-
config.postcssOptions || {};
43-
const babelSCOpts = getBabelStyledComponentsOpts(opts.configProvider.pkg);
44-
// workaround for combine continuous onBuildComplete log in watch mode
45-
const logStatus = lodash.debounce(
46-
() =>
47-
logger.info(
48-
`Bundle from ${chalk.yellow(config.entry)} to ${chalk.yellow(
49-
path.join(config.output.path, config.output.filename),
50-
)}`,
51-
),
52-
100,
53-
{ leading: true, trailing: false },
54-
);
55-
56-
// log for normal build
57-
!opts.watch && logStatus();
58-
await bundler.build({
59-
cwd: opts.cwd,
60-
watch: opts.watch,
61-
config: {
62-
alias: config.alias,
63-
autoprefixer: config.autoprefixer,
64-
chainWebpack: config.chainWebpack,
65-
define: config.define,
66-
devtool: config.sourcemap && 'source-map',
67-
externals: config.externals,
68-
outputPath: config.output.path,
69-
70-
// postcss config
71-
extraPostCSSPlugins,
72-
postcssLoader,
73-
74-
...(config.extractCSS !== false ? {} : { styleLoader: {} }),
75-
76-
// less config
77-
theme: config.theme,
78-
79-
// compatible with IE11 by default
80-
targets: getBundleTargets(config),
81-
jsMinifier: JSMinifier.terser,
82-
cssMinifier: CSSMinifier.cssnano,
83-
extraBabelIncludes: [/node_modules/],
84-
85-
// set cache parent directory, will join it with `bundler-webpack`
86-
// ref: https://github.com/umijs/umi/blob/8dad8c5af0197cd62db11f4b4c85d6bc1db57db1/packages/bundler-webpack/src/build.ts#L32
87-
cacheDirectoryPath: getCachePath(),
88-
},
89-
entry: {
90-
[path.parse(config.output.filename).name]: path.join(
91-
opts.cwd,
92-
config.entry,
93-
),
94-
},
95-
babelPreset: [
96-
require.resolve('@umijs/babel-preset-umi'),
97-
{
98-
presetEnv: {
99-
targets: getBundleTargets(config),
100-
},
101-
presetReact: getBabelPresetReactOpts(
102-
opts.configProvider.pkg,
40+
if (!opts.watchOnly) {
41+
for (let i = 0; i < opts.configProvider.configs.length; i += 1) {
42+
const config = opts.configProvider.configs[i];
43+
const { plugins: extraPostCSSPlugins, ...postcssLoader } =
44+
config.postcssOptions || {};
45+
const babelSCOpts = getBabelStyledComponentsOpts(opts.configProvider.pkg);
46+
// workaround for combine continuous onBuildComplete log in watch mode
47+
const logStatus = lodash.debounce(
48+
() =>
49+
logger.info(
50+
`Bundle from ${chalk.yellow(config.entry)} to ${chalk.yellow(
51+
path.join(config.output.path, config.output.filename),
52+
)}`,
53+
),
54+
100,
55+
{ leading: true, trailing: false },
56+
);
57+
58+
// log for normal build
59+
!opts.watch && logStatus();
60+
await bundler.build({
61+
cwd: opts.cwd,
62+
watch: opts.watch,
63+
config: {
64+
alias: config.alias,
65+
autoprefixer: config.autoprefixer,
66+
chainWebpack: config.chainWebpack,
67+
define: config.define,
68+
devtool: config.sourcemap && 'source-map',
69+
externals: config.externals,
70+
outputPath: config.output.path,
71+
72+
// postcss config
73+
extraPostCSSPlugins,
74+
postcssLoader,
75+
76+
...(config.extractCSS !== false ? {} : { styleLoader: {} }),
77+
78+
// less config
79+
theme: config.theme,
80+
81+
// compatible with IE11 by default
82+
targets: getBundleTargets(config),
83+
jsMinifier: JSMinifier.terser,
84+
cssMinifier: CSSMinifier.cssnano,
85+
extraBabelIncludes: [/node_modules/],
86+
87+
// set cache parent directory, will join it with `bundler-webpack`
88+
// ref: https://github.com/umijs/umi/blob/8dad8c5af0197cd62db11f4b4c85d6bc1db57db1/packages/bundler-webpack/src/build.ts#L32
89+
cacheDirectoryPath: getCachePath(),
90+
},
91+
entry: {
92+
[path.parse(config.output.filename).name]: path.join(
10393
opts.cwd,
94+
config.entry,
10495
),
105-
presetTypeScript: {},
106-
pluginTransformRuntime: {},
107-
pluginLockCoreJS: {},
108-
pluginDynamicImportNode: false,
10996
},
110-
],
111-
beforeBabelPlugins: [
112-
require.resolve('babel-plugin-dynamic-import-node'),
113-
...(babelSCOpts
114-
? [[require.resolve('babel-plugin-styled-components'), babelSCOpts]]
115-
: []),
116-
],
117-
extraBabelPresets: config.extraBabelPresets,
118-
extraBabelPlugins: config.extraBabelPlugins,
119-
120-
// configure library related options
121-
chainWebpack(memo: any) {
122-
memo.output.libraryTarget('umd');
123-
124-
if (config?.name) {
125-
memo.output.library(config.name);
126-
}
127-
128-
// modify webpack target
129-
if (config.platform === 'node') {
130-
memo.target('node');
131-
}
132-
133-
if (enableCache) {
134-
// use father version as cache version
135-
memo.merge({
136-
cache: { version: require('../../../package.json').version },
137-
});
138-
}
139-
140-
// also bundle svg as asset, because father force disable svgr
141-
const imgRule = memo.module.rule('asset').oneOf('image');
142-
143-
imgRule.test(
144-
new RegExp(imgRule.get('test').source.replace(/(\|png)/, '$1|svg')),
145-
);
97+
babelPreset: [
98+
require.resolve('@umijs/babel-preset-umi'),
99+
{
100+
presetEnv: {
101+
targets: getBundleTargets(config),
102+
},
103+
presetReact: getBabelPresetReactOpts(
104+
opts.configProvider.pkg,
105+
opts.cwd,
106+
),
107+
presetTypeScript: {},
108+
pluginTransformRuntime: {},
109+
pluginLockCoreJS: {},
110+
pluginDynamicImportNode: false,
111+
},
112+
],
113+
beforeBabelPlugins: [
114+
require.resolve('babel-plugin-dynamic-import-node'),
115+
...(babelSCOpts
116+
? [[require.resolve('babel-plugin-styled-components'), babelSCOpts]]
117+
: []),
118+
],
119+
extraBabelPresets: config.extraBabelPresets,
120+
extraBabelPlugins: config.extraBabelPlugins,
121+
122+
// configure library related options
123+
chainWebpack(memo: any) {
124+
memo.output.libraryTarget('umd');
125+
126+
if (config?.name) {
127+
memo.output.library(config.name);
128+
}
146129

147-
// disable progress bar
148-
memo.plugins.delete('progress-plugin');
130+
// modify webpack target
131+
if (config.platform === 'node') {
132+
memo.target('node');
133+
}
149134

150-
// auto bump analyze port
151-
/* istanbul ignore if -- @preserve */
152-
if (process.env.ANALYZE) {
153-
memo.plugin('webpack-bundle-analyzer').tap((args: any) => {
154-
args[0].analyzerPort += i;
135+
if (enableCache) {
136+
// use father version as cache version
137+
memo.merge({
138+
cache: { version: require('../../../package.json').version },
139+
});
140+
}
155141

156-
return args;
157-
});
158-
}
142+
// also bundle svg as asset, because father force disable svgr
143+
const imgRule = memo.module.rule('asset').oneOf('image');
159144

160-
return memo;
161-
},
145+
imgRule.test(
146+
new RegExp(imgRule.get('test').source.replace(/(\|png)/, '$1|svg')),
147+
);
148+
149+
// disable progress bar
150+
memo.plugins.delete('progress-plugin');
151+
152+
// auto bump analyze port
153+
/* istanbul ignore if -- @preserve */
154+
if (process.env.ANALYZE) {
155+
memo.plugin('webpack-bundle-analyzer').tap((args: any) => {
156+
args[0].analyzerPort += i;
157+
158+
return args;
159+
});
160+
}
161+
162+
return memo;
163+
},
162164

163-
// enable webpack persistent cache
164-
...(enableCache
165-
? {
165+
// enable webpack persistent cache
166+
...(enableCache
167+
? {
166168
cache: {
167169
buildDependencies: opts.buildDependencies,
168170
},
169171
}
170-
: {}),
172+
: {}),
171173

172-
// collect close handlers for watch mode
173-
...(opts.watch
174-
? {
174+
// collect close handlers for watch mode
175+
...(opts.watch
176+
? {
175177
onBuildComplete({ isFirstCompile, close }: any) {
176178
if (isFirstCompile) closeHandlers.push(close);
177179
// log for watch mode
178180
else logStatus();
179181
},
180182
}
181-
: {}),
182-
disableCopy: true,
183-
});
183+
: {}),
184+
disableCopy: true,
185+
});
186+
}
184187
}
185188

186189
// return watching closer for watch mode

src/builder/bundless/index.ts

+16-14
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ async function transformFiles(
3939
cwd: string;
4040
configProvider: BundlessConfigProvider;
4141
watch?: true;
42+
watchOnly?: boolean;
4243
},
4344
) {
4445
try {
@@ -102,8 +103,7 @@ async function transformFiles(
102103
}
103104

104105
logger.quietExpect.event(
105-
`Bundless ${chalk.gray(item)} to ${chalk.gray(itemDistPath)}${
106-
result?.options.declaration ? ' (with declaration)' : ''
106+
`Bundless ${chalk.gray(item)} to ${chalk.gray(itemDistPath)}${result?.options.declaration ? ' (with declaration)' : ''
107107
}`,
108108
);
109109
count += 1;
@@ -164,18 +164,20 @@ async function bundless(
164164
logger.info(statusText);
165165

166166
const startTime = Date.now();
167-
const matches = glob.sync(`${opts.configProvider.input}/**`, {
168-
cwd: opts.cwd,
169-
ignore: DEFAULT_BUNDLESS_IGNORES,
170-
nodir: true,
171-
});
172-
const count = await transformFiles(matches, opts);
167+
let count = 0;
168+
if (!opts.watchOnly) {
169+
const matches = glob.sync(`${opts.configProvider.input}/**`, {
170+
cwd: opts.cwd,
171+
ignore: DEFAULT_BUNDLESS_IGNORES,
172+
nodir: true,
173+
});
174+
count = await transformFiles(matches, opts);
175+
}
173176

174177
if (!opts.watch) {
175178
// output result for normal mode
176179
logger.quietExpect.event(
177-
`Transformed successfully in ${
178-
Date.now() - startTime
180+
`Transformed successfully in ${Date.now() - startTime
179181
} ms (${count} files)`,
180182
);
181183
} else {
@@ -227,10 +229,10 @@ async function bundless(
227229
// TODO: collect real emit files
228230
const relatedFiles = isTsFile
229231
? [
230-
replacePathExt(fileDistAbsPath, '.js'),
231-
replacePathExt(fileDistAbsPath, '.d.ts'),
232-
replacePathExt(fileDistAbsPath, '.d.ts.map'),
233-
]
232+
replacePathExt(fileDistAbsPath, '.js'),
233+
replacePathExt(fileDistAbsPath, '.d.ts'),
234+
replacePathExt(fileDistAbsPath, '.d.ts.map'),
235+
]
234236
: [fileDistAbsPath];
235237
const relatedMainFile = relatedFiles.find((item) =>
236238
fs.existsSync(item),

src/builder/index.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ interface IWatchBuilderResult {
4141
// overload normal/watch mode
4242
function builder(opts: IBuilderOpts): Promise<void>;
4343
function builder(
44-
opts: IBuilderOpts & { watch: true },
44+
opts: IBuilderOpts & { watch: true, watchOnly?: boolean },
4545
): Promise<IWatchBuilderResult>;
4646

4747
async function builder(
48-
opts: IBuilderOpts & { watch?: true },
48+
opts: IBuilderOpts & { watch?: true, watchOnly?: boolean },
4949
): Promise<IWatchBuilderResult | void> {
5050
const configProviders = createConfigProviders(
5151
opts.userConfig,
@@ -69,6 +69,7 @@ async function builder(
6969
configProvider: configProviders.bundle,
7070
buildDependencies: opts.buildDependencies,
7171
watch: opts.watch,
72+
watchOnly: opts.watchOnly,
7273
});
7374

7475
opts.watch && watchers.push(watcher);
@@ -79,6 +80,7 @@ async function builder(
7980
cwd: opts.cwd,
8081
configProvider: configProviders.bundless.esm,
8182
watch: opts.watch,
83+
watchOnly: opts.watchOnly,
8284
});
8385

8486
opts.watch && watchers.push(watcher);
@@ -89,6 +91,7 @@ async function builder(
8991
cwd: opts.cwd,
9092
configProvider: configProviders.bundless.cjs,
9193
watch: opts.watch,
94+
watchOnly: opts.watchOnly,
9295
});
9396

9497
opts.watch && watchers.push(watcher);

src/commands/dev.ts

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export default (api: IApi) => {
1616
cwd: api.cwd,
1717
pkg: api.pkg,
1818
watch: true,
19+
watchOnly: args.watchOnly,
1920
clean: args.clean,
2021
});
2122

0 commit comments

Comments
 (0)