Skip to content

Commit 143b8f4

Browse files
committed
Cleanup
1 parent f8a1faa commit 143b8f4

File tree

3 files changed

+54
-46
lines changed

3 files changed

+54
-46
lines changed

packages/react-scripts/config/webpack.config.dev.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// @remove-on-eject-begin
2+
// @remove-plugins-on-eject
23
/**
34
* Copyright (c) 2015-present, Facebook, Inc.
45
* All rights reserved.

packages/react-scripts/config/webpack.config.prod.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// @remove-on-eject-begin
2+
// @remove-plugins-on-eject
23
/**
34
* Copyright (c) 2015-present, Facebook, Inc.
45
* All rights reserved.

packages/react-scripts/scripts/eject.js

+52-46
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,37 @@ function getGitStatus() {
4040
}
4141
}
4242

43+
function ejectContent(content, { additionalDeps, pluginPaths }) {
44+
const { code, dependencies, paths: newPaths } = ejectFile({
45+
code: content,
46+
existingDependencies: additionalDeps,
47+
});
48+
for (const [key, value] of dependencies) {
49+
additionalDeps.set(key, value);
50+
}
51+
for (const newPath of newPaths) {
52+
pluginPaths.add(newPath);
53+
}
54+
return code;
55+
}
56+
57+
function addPlugins(pluginPaths) {
58+
if (pluginPaths.size < 1) {
59+
return;
60+
}
61+
62+
console.log(cyan('Adding plugins'));
63+
64+
for (const pluginPath of pluginPaths) {
65+
const pluginName = /.*react-scripts-plugin-([\w-]+)/.exec(pluginPath).pop();
66+
console.log(` Applying ${cyan(pluginName)}`);
67+
const { eject } = require(pluginPath);
68+
eject({ paths });
69+
}
70+
71+
console.log();
72+
}
73+
4374
inquirer
4475
.prompt({
4576
type: 'confirm',
@@ -115,28 +146,20 @@ inquirer
115146
fs.mkdirSync(path.join(appPath, folder));
116147
});
117148

118-
let addtlDeps = new Map();
119-
let pluginPaths = new Set();
149+
const additionalDeps = new Map(),
150+
pluginPaths = new Set();
120151
files.forEach(file => {
121152
let content = fs.readFileSync(file, 'utf8');
122153

123154
// Skip flagged files
124155
if (content.match(/\/\/ @remove-file-on-eject/)) {
125156
return;
126157
}
127-
// Inline plugins
128-
if (
129-
file.endsWith('webpack.config.dev.js') ||
130-
file.endsWith('webpack.config.prod.js')
131-
) {
132-
const { code, dependencies, paths: newPaths } = ejectFile({
133-
code: content,
134-
existingDependencies: addtlDeps,
135-
});
136-
content = code;
137-
addtlDeps = new Map([...addtlDeps, ...dependencies]);
138-
pluginPaths = new Set([...pluginPaths, ...newPaths]);
158+
// Remove plugins
159+
if (content.match(/\/\/ @remove-plugins-on-eject/)) {
160+
content = ejectContent(content, { additionalDeps, pluginPaths });
139161
}
162+
140163
content =
141164
content
142165
// Remove dead code from .js files on eject
@@ -155,39 +178,13 @@ inquirer
155178
});
156179
console.log();
157180

158-
if (pluginPaths.size > 0) {
159-
console.log(cyan('Adding plugins'));
160-
}
161-
for (const pluginPath of pluginPaths) {
162-
const pluginName = /.*react-scripts-plugin-([\w-]+)/
163-
.exec(pluginPath)
164-
.pop();
165-
console.log(` Applying ${cyan(pluginName)}`);
166-
const { eject } = require(pluginPath);
167-
eject({ paths });
168-
}
169-
if (pluginPaths.size > 0) {
170-
console.log();
171-
}
181+
addPlugins(pluginPaths);
172182

173-
const {
174-
name: ownPackageName,
175-
dependencies: _ownDependencies,
176-
optionalDependencies: ownOptionalDependencies,
177-
bin: ownBin,
178-
} = require(path.join(ownPath, 'package.json'));
183+
const ownPackage = require(path.join(ownPath, 'package.json'));
179184
const appPackage = require(path.join(appPath, 'package.json'));
180185

181-
const ownDependencies = Object.assign(
182-
{},
183-
_ownDependencies,
184-
Array.from(addtlDeps).reduce(
185-
(prev, [pkg, version]) => Object.assign(prev, { [pkg]: version }),
186-
{}
187-
)
188-
);
189-
190186
console.log(cyan('Updating the dependencies'));
187+
const ownPackageName = ownPackage.name;
191188
if (appPackage.devDependencies) {
192189
// We used to put react-scripts in devDependencies
193190
if (appPackage.devDependencies[ownPackageName]) {
@@ -200,9 +197,18 @@ inquirer
200197
console.log(` Removing ${cyan(ownPackageName)} from dependencies`);
201198
delete appPackage.dependencies[ownPackageName];
202199
}
200+
// Combine `react-scripts` dependencies with additional dependencies
201+
const ownDependencies = Object.assign(
202+
{},
203+
ownPackage.dependencies,
204+
Array.from(additionalDeps).reduce(
205+
(prev, [pkg, version]) => Object.assign(prev, { [pkg]: version }),
206+
{}
207+
)
208+
);
203209
Object.keys(ownDependencies).forEach(key => {
204210
// For some reason optionalDependencies end up in dependencies after install
205-
if (ownOptionalDependencies[key]) {
211+
if (ownPackage.optionalDependencies[key]) {
206212
return;
207213
}
208214
console.log(` Adding ${cyan(key)} to dependencies`);
@@ -219,7 +225,7 @@ inquirer
219225
console.log(cyan('Updating the scripts'));
220226
delete appPackage.scripts['eject'];
221227
Object.keys(appPackage.scripts).forEach(key => {
222-
Object.keys(ownBin).forEach(binKey => {
228+
Object.keys(ownPackage.bin).forEach(binKey => {
223229
const regex = new RegExp(binKey + ' (\\w+)', 'g');
224230
if (!regex.test(appPackage.scripts[key])) {
225231
return;
@@ -264,7 +270,7 @@ inquirer
264270
if (ownPath.indexOf(appPath) === 0) {
265271
try {
266272
// remove react-scripts and react-scripts binaries from app node_modules
267-
Object.keys(ownBin).forEach(binKey => {
273+
Object.keys(ownPackage.bin).forEach(binKey => {
268274
fs.removeSync(path.join(appPath, 'node_modules', '.bin', binKey));
269275
});
270276
fs.removeSync(ownPath);

0 commit comments

Comments
 (0)