Skip to content
This repository was archived by the owner on Apr 24, 2024. It is now read-only.

Commit fa57b0c

Browse files
authored
feat(cli): adding args, bundle name, chunk naming
* feat(cli): adding args, bundle name, chunk naming Allow the user to overwrite the default output bundle and chunk filenaming that this package uses. This respects webpack placeholders such as [name] and [hash]. Add man text for the cli args. If args are not provided, we default to using what is used now. If the user omits '.js' extension from args, we apply it automatically. * feat(cli): update README.md with new args * feat(cli): more readable code, review suggestion
1 parent 88b0c6f commit fa57b0c

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ If those defaults do not work for you, the script accepts some arguments:
8787

8888
- `-b|--build-path`: expects either an absolute or relative path. If a relative path is given it will be prefixed by your project root path.
8989
- default: `yourProjectRoot/build`.
90+
- `--chunk-filename`: Set the naming you want to use for non-entry chunk files. Accepts webpack placeholders such as `[id]`, `[name]`, `[hash]`. Directories can be supplied.
91+
- default: `js/bundle.js`.
9092
- `--disable-chunks`: disable code-splitting / chunks so that only a single bundle.js file is generated. It only works with `react-scripts` >= `2.0.0`.
93+
- `-o|--output-filename`: Set the name to be used for the output bundle. Accepts webpack placeholders such as `[id]`, `[name]`, `[hash]`. Directories can be supplied.
94+
- default: `js/[name].chunk.js`
9195
- `--react-scripts-version`: expects the `react-scripts` version you are using in your project i.e `2.0.3`. If not given it will be implied from your `node_modules` and if it cannot be implied the version `2.1.2` will be the default. Consider setting it if you **ejected** and are not using the latest `react-scripts` version.
9296
- `-p|--public-path`: expects a relative URL where `/` is the root. If you serve your files using an external webserver this argument is to match with your web server configuration. More information can be found in [webpack configuration guide](https://webpack.js.org/configuration/output/#output-publicpath).
9397
- default: "".

scripts/index.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@ const ora = require('ora');
99
const assert = require('assert');
1010

1111
const {
12-
flags: { buildPath, publicPath, reactScriptsVersion, verbose, disableChunks },
12+
flags: {
13+
buildPath,
14+
publicPath,
15+
reactScriptsVersion,
16+
verbose,
17+
disableChunks,
18+
outputFilename,
19+
chunkFilename,
20+
},
1321
} = require('../utils/cliHandler');
1422
const { getReactScriptsVersion, isEjected } = require('../utils');
1523

@@ -57,8 +65,14 @@ const resolvedBuildPath = buildPath ? handleBuildPath(buildPath) : paths.appBuil
5765
// update the paths in config
5866
config.output.path = resolvedBuildPath;
5967
config.output.publicPath = publicPath || '';
60-
config.output.filename = `js/bundle.js`;
61-
config.output.chunkFilename = `js/[name].chunk.js`;
68+
69+
// Grab output names from cli args, otherwise use some default naming.
70+
const fileNameToUse = outputFilename || `js/bundle.js`;
71+
const chunkNameToUse = chunkFilename || `js/[name].chunk.js`;
72+
// If cli user adds .js, respect that, otherwise we add it ourself
73+
config.output.filename = fileNameToUse.slice(-3) !== '.js' ? `${fileNameToUse}.js` : fileNameToUse;
74+
config.output.chunkFilename =
75+
chunkNameToUse.slice(-3) !== '.js' ? `${chunkNameToUse}.js` : chunkNameToUse;
6276

6377
if (disableChunks) {
6478
assert(major >= 2, 'Split chunks optimization is only available in react-scripts >= 2.0.0');

utils/cliHandler.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,39 @@ module.exports = meow(
1010
Options
1111
-b, --build-path Path to the build folder. Absolute or relative path, if relative will be prefixed with project root folder path.
1212
13+
--chunk-filename Set the naming you want to use for non-entry chunk files. Accepts webpack placeholders such as [id], [name], [hash]. Directories can be supplied.
14+
1315
--disable-chunks Disable code-splitting / chunks so that only a single bundle.js file is generated. Only available in react-scripts >= 2.0.0.
1416
17+
-o, --output-filename Set the name to be used for the output bundle. Accepts webpack placeholders such as [id], [name], [hash]. Directories can be supplied.
18+
1519
-p, --public-path Public URL.
1620
1721
--react-scripts-version Version of the react-scripts package used in your project i.e 2.0.3. If not given it will be implied from your package.json and if it cannot be implied the major version 2 will be the default.
1822
1923
-v, --verbose
20-
24+
2125
Examples
2226
$ cra-build-watch -b dist/ -p /assets
27+
$ cra-build-watch --chunk-filename './js/[chunkhash].[name]' -o './js/myapp'
28+
$ cra-build-watch -b dist/ -p /assets --chunk-filename './js/[name]/[hash].js' -v
2329
`,
2430
{
2531
flags: {
2632
'build-path': {
2733
type: 'string',
2834
alias: 'b',
2935
},
36+
'chunk-filename': {
37+
type: 'string',
38+
},
39+
'disable-chunks': {
40+
type: 'boolean',
41+
},
42+
'output-filename': {
43+
type: 'string',
44+
alias: 'o',
45+
},
3046
'public-path': {
3147
type: 'string',
3248
alias: 'p',
@@ -38,9 +54,6 @@ module.exports = meow(
3854
type: 'boolean',
3955
alias: 'v',
4056
},
41-
'disable-chunks': {
42-
type: 'boolean',
43-
},
4457
},
4558
}
4659
);

0 commit comments

Comments
 (0)