Skip to content
This repository was archived by the owner on May 17, 2019. It is now read-only.

Commit f3bbef1

Browse files
author
Aleksey Smolenchuk
committed
Add brotli and svgo flags; allow babel excludes; overrideWebpackConfig option in .fusionrc.js; remove scary messages
1 parent d76797a commit f3bbef1

File tree

7 files changed

+101
-30
lines changed

7 files changed

+101
-30
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ node_modules/
44
.nyc_output/
55
yarn-error.log
66
coverage/
7+
.vscode

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ The CLI interface for Fusion.js
66

77
The `fusion-cli` package is responsible for orchestrating compile-time configuration for server and browser bundles, as well as development, test and production variations. It provides a standardized Babel configuration that includes async/await support as well as stage 3+ Ecmascript features.
88

9-
Due to the complexity involved in configuring many permutations of configurations, Fusion.js does not support custom `webpack.config`. This design decision allows Fusion.js to eventually move away from Webpack if faster and better bundlers become available. Additionally, it allows Fusion.js to make changes to the internal webpack configuration without the concern of breaking users customizations. If you run into a situation where you feel you need to make a webpack customization, please reach out to us on [slack](https://join.slack.com/t/fusionjs/shared_invite/enQtMzk3NjM0MTg0MTI4LWJhNzVjYjk5ZDVlYWIxZWViMjA3YzE5OTc4YWZkNzBkZmNkYmJkMDYyOGEzODEwMzRmMWExMzc1NDIzMmY2NDQ) or create an issue describing your use case.
10-
119
The CLI is also responsible for hot module reloading in development mode, and for running the web server.
1210

1311
### Installation
@@ -43,6 +41,9 @@ The CLI API can be most easily run through the Yarn or NPX CLI, e.g. `yarn fusio
4341
- `--log-level`: Log level to output to console `[default: "info"]`
4442
- `--forceLegacyBuild`: Force enable legacy build. By default not compiled in dev.
4543
- `--perserve-names`: Disable name mangling during script minification
44+
- `--no-zofpli`: Disable zopfli compression plugin
45+
- `--no-brotli`: Disable brotli compression plugin
46+
- `--no-svgo`: Disable svgo optimization plugin
4647

4748
<!--
4849
* `fusion profile [--environment] [--watch] [--file-count]`: Profile your application

build/compiler.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ function getStatsLogger({dir, logger, env}) {
6363

6464
if (err) {
6565
logger.error(err.stack || err);
66-
if (err.details) {
67-
logger.error(err.details);
66+
if ((err /*: any */).details) {
67+
logger.error((err /*: any */).details);
6868
}
6969
return;
7070
}
@@ -119,6 +119,8 @@ type CompilerOpts = {
119119
logger?: any,
120120
preserveNames?: boolean,
121121
zopfli?: boolean,
122+
brotli?: boolean,
123+
svgo?: boolean,
122124
minify?: boolean
123125
};
124126
*/
@@ -133,6 +135,8 @@ function Compiler(
133135
watch = false,
134136
logger = console,
135137
zopfli = true,
138+
brotli = true,
139+
svgo = true,
136140
minify = true,
137141
} /*: CompilerOpts */
138142
) /*: CompilerType */ {
@@ -170,6 +174,8 @@ function Compiler(
170174
legacyPkgConfig,
171175
preserveNames,
172176
zopfli,
177+
brotli,
178+
svgo,
173179
minify,
174180
};
175181

@@ -180,7 +186,9 @@ function Compiler(
180186

181187
const statsLogger = getStatsLogger({dir, logger, env});
182188

183-
this.on = (type, callback) => compiler.hooks[type].tap('compiler', callback);
189+
this.on = (type, callback) =>
190+
(compiler /*: any */).hooks[type]
191+
.tap('compiler', callback);
184192
this.start = cb => {
185193
cb = cb || function noop(err, stats) {};
186194
// Handler may be called multiple times by `watch`

build/get-webpack-config.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ import type {
7373
FusionRC
7474
} from "./load-fusionrc.js";
7575
76+
import type {
77+
WebpackOptions
78+
} from "webpack";
79+
7680
export type WebpackConfigOpts = {|
7781
id: $Keys<typeof COMPILATIONS>,
7882
dir: string,
@@ -81,6 +85,8 @@ export type WebpackConfigOpts = {|
8185
watch: boolean,
8286
preserveNames: boolean,
8387
zopfli: boolean,
88+
brotli: boolean,
89+
svgo: boolean,
8490
minify: boolean,
8591
state: {
8692
clientChunkMetadata: ClientChunkMetadataState,
@@ -108,9 +114,12 @@ function getWebpackConfig(opts /*: WebpackConfigOpts */) {
108114
state,
109115
fusionConfig,
110116
zopfli,
117+
brotli,
118+
svgo,
111119
minify,
112120
legacyPkgConfig = {},
113121
} = opts;
122+
114123
const main = 'src/main.js';
115124

116125
if (!fs.existsSync(path.join(dir, main))) {
@@ -197,7 +206,8 @@ function getWebpackConfig(opts /*: WebpackConfigOpts */) {
197206
target: runtime === 'server' ? 'node-bundled' : 'browser-legacy',
198207
specOnly: false,
199208
});
200-
return {
209+
210+
const webpackConfig = {
201211
name: runtime,
202212
target: {server: 'node', client: 'web', sw: 'webworker'}[runtime],
203213
entry: {
@@ -285,7 +295,9 @@ function getWebpackConfig(opts /*: WebpackConfigOpts */) {
285295
runtime === 'server' && {
286296
compiler: id => id === 'server',
287297
test: JS_EXT_PATTERN,
288-
exclude: EXCLUDE_TRANSPILATION_PATTERNS,
298+
exclude:
299+
(fusionConfig.babel && fusionConfig.babel.exclude) ||
300+
EXCLUDE_TRANSPILATION_PATTERNS,
289301
use: [
290302
{
291303
loader: babelLoader.path,
@@ -315,7 +327,9 @@ function getWebpackConfig(opts /*: WebpackConfigOpts */) {
315327
(runtime === 'client' || runtime === 'sw') && {
316328
compiler: id => id === 'client' || id === 'sw',
317329
test: JS_EXT_PATTERN,
318-
exclude: EXCLUDE_TRANSPILATION_PATTERNS,
330+
exclude:
331+
(fusionConfig.babel && fusionConfig.babel.exclude) ||
332+
EXCLUDE_TRANSPILATION_PATTERNS,
319333
use: [
320334
{
321335
loader: babelLoader.path,
@@ -345,7 +359,9 @@ function getWebpackConfig(opts /*: WebpackConfigOpts */) {
345359
runtime === 'client' && {
346360
compiler: id => id === 'client-legacy',
347361
test: JS_EXT_PATTERN,
348-
exclude: EXCLUDE_TRANSPILATION_PATTERNS,
362+
exclude:
363+
(fusionConfig.babel && fusionConfig.babel.exclude) ||
364+
EXCLUDE_TRANSPILATION_PATTERNS,
349365
use: [
350366
{
351367
loader: babelLoader.path,
@@ -457,8 +473,8 @@ function getWebpackConfig(opts /*: WebpackConfigOpts */) {
457473
state.i18nManifest
458474
),
459475
!dev && zopfli && zopfliWebpackPlugin,
460-
!dev && brotliWebpackPlugin,
461-
!dev && svgoWebpackPlugin,
476+
!dev && brotli && brotliWebpackPlugin,
477+
!dev && svgo && svgoWebpackPlugin,
462478
// In development, skip the emitting phase on errors to ensure there are
463479
// no assets emitted that include errors. This fixes an issue with hot reloading
464480
// server side code and recovering from errors correctly. We only want to do this
@@ -580,6 +596,12 @@ function getWebpackConfig(opts /*: WebpackConfigOpts */) {
580596
: undefined,
581597
},
582598
};
599+
600+
if (fusionConfig.overrideWebpackConfig) {
601+
return fusionConfig.overrideWebpackConfig(webpackConfig);
602+
}
603+
604+
return webpackConfig;
583605
}
584606

585607
// Allow overrides with a warning for `dev` command. In production builds, throw if NODE_ENV is not `production`.

build/load-fusionrc.js

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@ const chalk = require('chalk');
1616
let loggedNotice = false;
1717

1818
/*::
19+
import type {
20+
WebpackOptions
21+
} from "webpack";
22+
1923
export type FusionRC = {
20-
babel?: {plugins?: Array<any>, presets?: Array<any>},
24+
babel?: {plugins?: Array<any>, presets?: Array<any>, exclude?: mixed},
2125
assumeNoImportSideEffects?: boolean,
2226
experimentalCompile?: boolean,
2327
nodeBuiltins?: {[string]: any},
28+
overrideWebpackConfig:? (any) => any
2429
};
2530
*/
2631

@@ -33,17 +38,6 @@ module.exports = function validateConfig(dir /*: string */) /*: FusionRC */ {
3338
if (!isValid(config)) {
3439
throw new Error('.fusionrc.js is invalid');
3540
}
36-
if (!loggedNotice && config.babel) {
37-
console.log(chalk.dim('Using custom Babel config from .fusionrc.js'));
38-
console.warn(
39-
chalk.yellow(
40-
'Warning: custom Babel config is an',
41-
chalk.bold.underline('unstable API'),
42-
'and may be not be supported in future releases. Use at your own risk.'
43-
)
44-
);
45-
loggedNotice = true;
46-
}
4741
} else {
4842
config = {};
4943
}
@@ -62,6 +56,7 @@ function isValid(config) {
6256
'assumeNoImportSideEffects',
6357
'experimentalCompile',
6458
'nodeBuiltins',
59+
'overrideWebpackConfig',
6560
].includes(key)
6661
)
6762
) {
@@ -70,10 +65,12 @@ function isValid(config) {
7065

7166
if (
7267
config.babel &&
73-
!Object.keys(config.babel).every(el => ['plugins', 'presets'].includes(el))
68+
!Object.keys(config.babel).every(el =>
69+
['plugins', 'presets', 'exclude'].includes(el)
70+
)
7471
) {
7572
throw new Error(
76-
`Only "plugins" and "presets" are supported in fusionrc.js babel config`
73+
`Only "plugins", "presets", and "exclude" are supported in fusionrc.js babel config`
7774
);
7875
}
7976

commands/build.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@ exports.run = async function(
1919
preserveNames,
2020
logLevel,
2121
zopfli,
22+
brotli,
23+
svgo,
2224
minify,
2325
} /*: {
2426
dir: string,
2527
production: boolean,
2628
preserveNames: boolean,
2729
logLevel: string,
2830
zopfli: boolean,
31+
brotli: boolean,
32+
svgo: boolean,
2933
minify: boolean
3034
}*/
3135
) {
@@ -45,6 +49,8 @@ exports.run = async function(
4549
logger,
4650
preserveNames,
4751
zopfli,
52+
brotli,
53+
svgo,
4854
minify,
4955
});
5056

docs/fusionrc.md

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,20 @@ This configuration object supports the following fields:
66

77
## `babel`
88

9-
### Adding plugins/presets
9+
### Adding plugins/presets and excluding files from babel compilation
1010

1111
For example, to add your own Babel plugins/preset:
1212

1313
```js
1414
module.exports = {
1515
babel: {
1616
presets: ["some-babel-preset"],
17-
plugins: ["some-babel-plugin"]
17+
plugins: ["some-babel-plugin"],
18+
exclude: moduleName => /\/node_modules\//.test(moduleName)
1819
}
1920
};
2021
```
2122

22-
**Please note that custom Babel config is an unstable API and may not be supported in future releases.**
23-
24-
2523
## `assumeNoImportSideEffects`
2624

2725
By default this is `false`.
@@ -48,3 +46,41 @@ module.exports = {
4846
}
4947
};
5048
```
49+
50+
## `overrideWebpackConfig`
51+
52+
Allows to customize [webpack configuration](https://webpack.js.org/concepts).
53+
54+
Pass a function that takes a [`webpackConfig`](https://webpack.js.org/configuration/) object created by fusion and return a config with any modifications supported by webpack.
55+
56+
Example:
57+
58+
```js
59+
const SimpleProgressPlugin = require('simple-progress-webpack-plugin');
60+
61+
module.exports = {
62+
overrideWebpackConfig: (webpackConfig) => {
63+
// Debug things:
64+
console.log(webpackConfig);
65+
66+
// Pass your own plugins:
67+
webpackConfig.plugins = webpackConfig.plugins.map(plugin => {
68+
// console.log(plugin.constructor.name);
69+
70+
if (plugin.constructor.name === 'ProgressPlugin') {
71+
return new SimpleProgressPlugin({
72+
format: 'expanded'
73+
})
74+
}
75+
76+
return plugin;
77+
});
78+
79+
// Don't forget to return it
80+
return webpackConfig;
81+
}
82+
};
83+
```
84+
85+
86+

0 commit comments

Comments
 (0)