Skip to content

Commit b88d665

Browse files
djgrantTimer
authored andcommitted
Modularise scripts (#1433)
* Refactor start script into modules * Move dev server config into config file * Replace eject file whitelist with a "remove-file-on-eject" flag * Move utils into scripts folder (for inclusion in ejection) * Add missed changes * Pass showInstructions as an argument * Fix eject bug * Don't eject babelTransform
1 parent 59cab8f commit b88d665

9 files changed

+304
-275
lines changed

packages/react-scripts/config/jest/babelTransform.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @remove-file-on-eject
12
/**
23
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
34
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
var config = require('./webpack.config.dev');
2+
var paths = require('./paths');
3+
4+
var protocol = process.env.HTTPS === 'true' ? 'https' : 'http';
5+
var host = process.env.HOST || 'localhost';
6+
7+
module.exports = {
8+
// Enable gzip compression of generated files.
9+
compress: true,
10+
// Silence WebpackDevServer's own logs since they're generally not useful.
11+
// It will still show compile warnings and errors with this setting.
12+
clientLogLevel: 'none',
13+
// By default WebpackDevServer serves physical files from current directory
14+
// in addition to all the virtual build products that it serves from memory.
15+
// This is confusing because those files won’t automatically be available in
16+
// production build folder unless we copy them. However, copying the whole
17+
// project directory is dangerous because we may expose sensitive files.
18+
// Instead, we establish a convention that only files in `public` directory
19+
// get served. Our build script will copy `public` into the `build` folder.
20+
// In `index.html`, you can get URL of `public` folder with %PUBLIC_URL%:
21+
// <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
22+
// In JavaScript code, you can access it with `process.env.PUBLIC_URL`.
23+
// Note that we only recommend to use `public` folder as an escape hatch
24+
// for files like `favicon.ico`, `manifest.json`, and libraries that are
25+
// for some reason broken when imported through Webpack. If you just want to
26+
// use an image, put it in `src` and `import` it from JavaScript instead.
27+
contentBase: paths.appPublic,
28+
// By default files from `contentBase` will not trigger a page reload.
29+
watchContentBase: true,
30+
// Enable hot reloading server. It will provide /sockjs-node/ endpoint
31+
// for the WebpackDevServer client so it can learn when the files were
32+
// updated. The WebpackDevServer client is included as an entry point
33+
// in the Webpack development configuration. Note that only changes
34+
// to CSS are currently hot reloaded. JS changes will refresh the browser.
35+
hot: true,
36+
// It is important to tell WebpackDevServer to use the same "root" path
37+
// as we specified in the config. In development, we always serve from /.
38+
publicPath: config.output.publicPath,
39+
// WebpackDevServer is noisy by default so we emit custom message instead
40+
// by listening to the compiler events with `compiler.plugin` calls above.
41+
quiet: true,
42+
// Reportedly, this avoids CPU overload on some systems.
43+
// https://github.com/facebookincubator/create-react-app/issues/293
44+
watchOptions: {
45+
ignored: /node_modules/
46+
},
47+
// Enable HTTPS if the HTTPS environment variable is set to 'true'
48+
https: protocol === 'https',
49+
host: host,
50+
overlay: false,
51+
};

packages/react-scripts/scripts/eject.js

+30-24
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @remove-file-on-eject
12
/**
23
* Copyright (c) 2015-present, Facebook, Inc.
34
* All rights reserved.
@@ -7,13 +8,14 @@
78
* of patent rights can be found in the PATENTS file in the same directory.
89
*/
910

10-
var createJestConfig = require('../utils/createJestConfig');
1111
var fs = require('fs-extra');
1212
var path = require('path');
13-
var paths = require('../config/paths');
14-
var prompt = require('react-dev-utils/prompt');
1513
var spawnSync = require('cross-spawn').sync;
1614
var chalk = require('chalk');
15+
var prompt = require('react-dev-utils/prompt');
16+
var paths = require('../config/paths');
17+
var createJestConfig = require('./utils/createJestConfig');
18+
1719
var green = chalk.green;
1820
var cyan = chalk.cyan;
1921

@@ -45,44 +47,48 @@ prompt(
4547

4648
var folders = [
4749
'config',
48-
path.join('config', 'jest'),
49-
'scripts'
50+
'config/jest',
51+
'scripts',
52+
'scripts/utils',
5053
];
5154

52-
var files = [
53-
path.join('config', 'env.js'),
54-
path.join('config', 'paths.js'),
55-
path.join('config', 'polyfills.js'),
56-
path.join('config', 'webpack.config.dev.js'),
57-
path.join('config', 'webpack.config.prod.js'),
58-
path.join('config', 'jest', 'cssTransform.js'),
59-
path.join('config', 'jest', 'fileTransform.js'),
60-
path.join('scripts', 'build.js'),
61-
path.join('scripts', 'start.js'),
62-
path.join('scripts', 'test.js')
63-
];
55+
// Make shallow array of files paths
56+
var files = folders.reduce(function (files, folder) {
57+
return files.concat(
58+
fs.readdirSync(path.join(ownPath, folder))
59+
// set full path
60+
.map(file => path.join(ownPath, folder, file))
61+
// omit dirs from file list
62+
.filter(file => fs.lstatSync(file).isFile())
63+
);
64+
}, []);
6465

6566
// Ensure that the app folder is clean and we won't override any files
6667
folders.forEach(verifyAbsent);
6768
files.forEach(verifyAbsent);
6869

69-
// Copy the files over
70+
console.log();
71+
console.log(cyan('Copying files into ' + appPath));
72+
7073
folders.forEach(function(folder) {
7174
fs.mkdirSync(path.join(appPath, folder))
7275
});
7376

74-
console.log();
75-
console.log(cyan('Copying files into ' + appPath));
7677
files.forEach(function(file) {
77-
console.log(' Adding ' + cyan(file) + ' to the project');
78-
var content = fs
79-
.readFileSync(path.join(ownPath, file), 'utf8')
78+
var content = fs.readFileSync(file, 'utf8');
79+
80+
// Skip flagged files
81+
if (content.match(/\/\/ @remove-file-on-eject/)) {
82+
return;
83+
}
84+
content = content
8085
// Remove dead code from .js files on eject
8186
.replace(/\/\/ @remove-on-eject-begin([\s\S]*?)\/\/ @remove-on-eject-end/mg, '')
8287
// Remove dead code from .applescript files on eject
8388
.replace(/-- @remove-on-eject-begin([\s\S]*?)-- @remove-on-eject-end/mg, '')
8489
.trim() + '\n';
85-
fs.writeFileSync(path.join(appPath, file), content);
90+
console.log(' Adding ' + cyan(file.replace(ownPath, '')) + ' to the project');
91+
fs.writeFileSync(file.replace(ownPath, appPath), content);
8692
});
8793
console.log();
8894

packages/react-scripts/scripts/init.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @remove-file-on-eject
12
/**
23
* Copyright (c) 2015-present, Facebook, Inc.
34
* All rights reserved.

0 commit comments

Comments
 (0)