From c4465e5d59d8bdcb3f6dea3b9f41c8e444156507 Mon Sep 17 00:00:00 2001 From: umutcanbolat <10065235+umutcanbolat@users.noreply.github.com> Date: Sun, 25 Oct 2020 03:24:28 +0100 Subject: [PATCH 1/2] feat(cli): add skip-cleanup parameter --- scripts/index.js | 85 ++++++++++++++++++++++++--------------------- utils/cliHandler.js | 5 ++- 2 files changed, 49 insertions(+), 41 deletions(-) diff --git a/scripts/index.js b/scripts/index.js index 2231fc7..6ba828e 100644 --- a/scripts/index.js +++ b/scripts/index.js @@ -21,6 +21,7 @@ const { chunkFilename, afterInitialBuildHook, afterRebuildHook, + skipCleanup, }, } = require('../utils/cliHandler'); const { getReactScriptsVersion, isEjected } = require('../utils'); @@ -129,49 +130,48 @@ config.plugins[htmlPluginIndex] = new HtmlWebpackPlugin({ }); spinner.succeed(); -spinner.start('Clear destination folder'); let inProgress = false; -fs.emptyDir(paths.appBuild) - .then(() => { - spinner.succeed(); - - return new Promise((resolve, reject) => { - const webpackCompiler = webpack(config); - new webpack.ProgressPlugin(() => { - if (!inProgress) { - spinner.start('Start webpack watch'); - inProgress = true; - } - }).apply(webpackCompiler); - - webpackCompiler.watch({}, (err, stats) => { - if (err) { - return reject(err); - } - - spinner.succeed(); - - runHook('after rebuild hook', spinner, afterRebuildHook); - - inProgress = false; - - if (verbose) { - console.log(); - console.log( - stats.toString({ - chunks: false, - colors: true, - }) - ); - console.log(); - } - - return resolve(); - }); - }); - }) +Promise.resolve() + .then(() => skipCleanup || clearDestinationFolder()) + .then( + () => + new Promise((resolve, reject) => { + const webpackCompiler = webpack(config); + new webpack.ProgressPlugin(() => { + if (!inProgress) { + spinner.start('Start webpack watch'); + inProgress = true; + } + }).apply(webpackCompiler); + + webpackCompiler.watch({}, (err, stats) => { + if (err) { + return reject(err); + } + + spinner.succeed(); + + runHook('after rebuild hook', spinner, afterRebuildHook); + + inProgress = false; + + if (verbose) { + console.log(); + console.log( + stats.toString({ + chunks: false, + colors: true, + }) + ); + console.log(); + } + + return resolve(); + }); + }) + ) .then(() => copyPublicFolder()) .then(() => runHook('after initial build hook', spinner, afterInitialBuildHook)); @@ -207,3 +207,8 @@ function handleBuildPath(userBuildPath) { return path.join(process.cwd(), userBuildPath); } + +function clearDestinationFolder() { + spinner.start('Clear destination folder'); + return fs.emptyDir(paths.appBuild).then(() => spinner.succeed()); +} diff --git a/utils/cliHandler.js b/utils/cliHandler.js index 18b2fff..8f04a8a 100644 --- a/utils/cliHandler.js +++ b/utils/cliHandler.js @@ -63,7 +63,10 @@ module.exports = meow( }, 'after-rebuild-hook': { type: 'string', - } + }, + 'skip-cleanup': { + type: 'boolean', + }, }, } ); From 37ff09ad1be6aa25696c8ca8f4a3b17f138a5b6c Mon Sep 17 00:00:00 2001 From: umutcanbolat <10065235+umutcanbolat@users.noreply.github.com> Date: Sun, 25 Oct 2020 03:39:34 +0100 Subject: [PATCH 2/2] docs(cli): add explanation of the skip-cleanup command --- README.md | 1 + utils/cliHandler.js | 3 +++ 2 files changed, 4 insertions(+) diff --git a/README.md b/README.md index 3ad0c86..bb4becf 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,7 @@ If those defaults do not work for you, the script accepts some arguments: - `--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. - `-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). - default: "". +- `--skip-cleanup`: Skips the initial cleanup of the build folder. Could be beneficial when the cleanup is handled beforehand and some other files are moved to the build folder before running this script. - `-v|--verbose`: display webpack build output. # Contributions diff --git a/utils/cliHandler.js b/utils/cliHandler.js index 8f04a8a..0f064f0 100644 --- a/utils/cliHandler.js +++ b/utils/cliHandler.js @@ -24,12 +24,15 @@ module.exports = meow( --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. + --skip-cleanup Skips the initial cleanup of the build folder. Could be beneficial when the cleanup is handled beforehand and some other files are moved to the build folder before running this script. + -v, --verbose Examples $ cra-build-watch -b dist/ -p /assets $ cra-build-watch --chunk-filename './js/[chunkhash].[name]' -o './js/myapp' $ cra-build-watch -b dist/ -p /assets --chunk-filename './js/[name]/[hash].js' -v + $ cra-build-watch --skip-cleanup `, { flags: {