Skip to content

Commit 8ad165a

Browse files
committed
add clean command and clean only generated files
1 parent caaa2b8 commit 8ad165a

File tree

6 files changed

+120
-17
lines changed

6 files changed

+120
-17
lines changed

bin/react-unity-scripts.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ const scriptIndex = args.findIndex(
2424
x === 'eject' ||
2525
x === 'start' ||
2626
x === 'test' ||
27+
x === 'clean' ||
2728
x === 'lint'
2829
);
2930
const script = scriptIndex === -1 ? args[0] : args[scriptIndex];
3031
const nodeArgs = scriptIndex > 0 ? args.slice(0, scriptIndex) : [];
3132

32-
if (['build', 'eject', 'start', 'test', 'lint'].includes(script)) {
33+
if (['build', 'eject', 'start', 'test', 'clean', 'lint'].includes(script)) {
3334
const result = spawn.sync(
3435
process.execPath,
3536
nodeArgs

config/paths.js

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const publicUrlOrPath = getPublicUrlOrPath(
3030
);
3131

3232
const buildPath = process.env.BUILD_PATH || '../Assets/Resources/react';
33+
const manifestPath = path.join(buildPath, 'asset-manifest.json');
3334

3435
const moduleFileExtensions = [
3536
'web.mjs',
@@ -63,6 +64,7 @@ module.exports = {
6364
dotenv: resolveApp('.env'),
6465
appPath: resolveApp('.'),
6566
appBuild: resolveApp(buildPath),
67+
appManifest: resolveApp(manifestPath),
6668
appPublic: resolveApp('public'),
6769
appPublicAlt: resolveApp('previewer'),
6870
appHtml: resolveApp('public/index.html'),
@@ -90,6 +92,7 @@ module.exports = {
9092
dotenv: resolveApp('.env'),
9193
appPath: resolveApp('.'),
9294
appBuild: resolveApp(buildPath),
95+
appManifest: resolveApp(manifestPath),
9396
appPublic: resolveApp('public'),
9497
appPublicAlt: resolveApp('previewer'),
9598
appHtml: resolveApp('public/index.html'),
@@ -130,6 +133,7 @@ if (
130133
dotenv: resolveOwn(`${templatePath}/.env`),
131134
appPath: resolveApp('.'),
132135
appBuild: resolveOwn(path.join('../..', buildPath)),
136+
appManifest: resolveOwn(path.join('../..', manifestPath)),
133137
appPublic: resolveOwn(`${templatePath}/public`),
134138
appPreviewer: resolveOwn(`${templatePath}/previewer`),
135139
appHtml: resolveOwn(`${templatePath}/public/index.html`),

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
"terser-webpack-plugin": "^5.3.1",
9696
"url-loader": "^4.1.1",
9797
"webpack": "^5.72.0",
98-
"webpack-dev-server": "^4.8.1",
98+
"webpack-dev-server": "^4.9.0",
9999
"webpack-manifest-plugin": "^5.0.0"
100100
},
101101
"devDependencies": {

scripts/build.js

+3-15
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,13 @@ const configFactory = require('../config/webpack.config');
3131
const paths = require('../config/paths');
3232
const checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');
3333
const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages');
34-
const printHostingInstructions = require('react-dev-utils/printHostingInstructions');
3534
const FileSizeReporter = require('react-dev-utils/FileSizeReporter');
3635
const printBuildError = require('react-dev-utils/printBuildError');
36+
const { cleanBuildDirectory } = require('./utils/cleanBuildDirectory');
3737

3838
const measureFileSizesBeforeBuild =
3939
FileSizeReporter.measureFileSizesBeforeBuild;
4040
const printFileSizesAfterBuild = FileSizeReporter.printFileSizesAfterBuild;
41-
const useYarn = fs.existsSync(paths.yarnLockFile);
4241

4342
// These sizes are pretty large. We'll warn for bundles exceeding them.
4443
const WARN_AFTER_BUNDLE_GZIP_SIZE = 512 * 1024;
@@ -67,9 +66,7 @@ checkBrowsers(paths.appPath, isInteractive)
6766
return measureFileSizesBeforeBuild(paths.appBuild);
6867
})
6968
.then(previousFileSizes => {
70-
// Remove all content but keep the directory so that
71-
// if you're in it, you don't end up in Trash
72-
fs.emptyDirSync(paths.appBuild);
69+
cleanBuildDirectory();
7370
// Merge with the public folder
7471
copyPublicFolder();
7572
// Start the webpack build
@@ -104,17 +101,8 @@ checkBrowsers(paths.appPath, isInteractive)
104101
);
105102
console.log();
106103

107-
const appPackage = require(paths.appPackageJson);
108-
const publicUrl = paths.publicUrlOrPath;
109-
const publicPath = config.output.publicPath;
110104
const buildFolder = path.relative(process.cwd(), paths.appBuild);
111-
printHostingInstructions(
112-
appPackage,
113-
publicUrl,
114-
publicPath,
115-
buildFolder,
116-
useYarn
117-
);
105+
console.log(`The ${chalk.blue(buildFolder)} folder is ready to be used in ReactUnity.`);
118106
},
119107
err => {
120108
const tscCompileOnError = process.env.TSC_COMPILE_ON_ERROR === 'true';

scripts/clean.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// @remove-on-eject-begin
2+
/**
3+
* Copyright (c) 2015-present, Facebook, Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
// @remove-on-eject-end
9+
'use strict';
10+
11+
// Do this as the first thing so that any code reading it knows the right env.
12+
process.env.BABEL_ENV = 'production';
13+
process.env.NODE_ENV = 'production';
14+
15+
// Makes the script crash on unhandled rejections instead of silently
16+
// ignoring them. In the future, promise rejections that are not handled will
17+
// terminate the Node.js process with a non-zero exit code.
18+
process.on('unhandledRejection', err => {
19+
throw err;
20+
});
21+
22+
// Ensure environment variables are read.
23+
require('../config/env');
24+
25+
const path = require('path');
26+
const chalk = require('react-dev-utils/chalk');
27+
const paths = require('../config/paths');
28+
const { cleanBuildDirectory } = require('./utils/cleanBuildDirectory');
29+
30+
Promise.resolve().then(() => {
31+
const buildFolder = path.relative(process.cwd(), paths.appBuild);
32+
console.log(`Cleaning build directory at ${chalk.blue(buildFolder)}`);
33+
cleanBuildDirectory();
34+
});

scripts/utils/cleanBuildDirectory.js

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// @remove-on-eject-begin
2+
/**
3+
* Copyright (c) 2015-present, Facebook, Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
// @remove-on-eject-end
9+
'use strict';
10+
11+
const path = require('path');
12+
const fs = require('fs-extra');
13+
const paths = require('../../config/paths');
14+
15+
function cleanBuildDirectory() {
16+
try {
17+
if (fs.existsSync(paths.appManifest)) {
18+
// Clean only the files inside the asset-manifest so that we avoid cleaning files belonging to user
19+
const manifestContent = fs.readFileSync(paths.appManifest, 'utf8');
20+
const manifest = JSON.parse(manifestContent);
21+
22+
const keys = Object.keys(manifest.files);
23+
24+
for (const key of keys) {
25+
const file = manifest.files[key];
26+
const filePath = path.join(paths.appBuild, file);
27+
const metaFile = filePath + '.meta';
28+
29+
fs.removeSync(filePath);
30+
fs.removeSync(metaFile);
31+
}
32+
33+
const manifestMeta = paths.appManifest + '.meta';
34+
fs.removeSync(paths.appManifest);
35+
fs.removeSync(manifestMeta);
36+
37+
cleanEmptyFoldersRecursively(null, paths.appBuild, true);
38+
39+
return;
40+
}
41+
} catch (err) { }
42+
43+
console.log('Skipped clearing output directory because asset manifest was not found. Please clean the output directory manually if necessary.');
44+
}
45+
46+
47+
function cleanEmptyFoldersRecursively(parent, folder, skipFirst) {
48+
if (!fs.existsSync(folder)) return;
49+
var isDir = fs.statSync(folder).isDirectory();
50+
if (!isDir) return;
51+
52+
var files = fs.readdirSync(folder);
53+
if (files.length > 0) {
54+
files.forEach(function (file) {
55+
var fullPath = path.join(folder, file);
56+
cleanEmptyFoldersRecursively(folder, fullPath, false);
57+
});
58+
59+
// re-evaluate files; after deleting subfolder
60+
// we may have parent folder empty now
61+
files = fs.readdirSync(folder);
62+
}
63+
64+
if (!skipFirst && files.length == 0) {
65+
fs.rmdirSync(folder);
66+
67+
if (parent) {
68+
const folderName = path.basename(folder);
69+
const dirMeta = path.join(parent, folderName + '.meta');
70+
fs.removeSync(dirMeta);
71+
}
72+
return;
73+
}
74+
}
75+
76+
module.exports.cleanBuildDirectory = cleanBuildDirectory;

0 commit comments

Comments
 (0)