Skip to content

Commit fced96e

Browse files
committed
Multiple entries WIP
Just create an "pages" folder in the root of project and put there *.js files. You can override default src/index.js by creating pages/index.js.
1 parent 9663385 commit fced96e

File tree

4 files changed

+70
-28
lines changed

4 files changed

+70
-28
lines changed

packages/react-scripts/config/paths.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
var path = require('path');
1313
var fs = require('fs');
1414
var url = require('url');
15+
var glob = require('glob');
1516

1617
// Make sure any symlinks in the project folder are resolved:
1718
// https://github.com/facebookincubator/create-react-app/issues/637
@@ -78,8 +79,12 @@ module.exports = {
7879
appPublic: resolveApp('public'),
7980
appHtml: resolveApp('public/index.html'),
8081
appIndexJs: resolveApp('src/index.js'),
82+
appEntries: glob.sync(resolveApp('pages/*.js')).reduce((entries, page) => {
83+
entries[path.basename(page, '.js')] = resolveApp(page);
84+
return entries;
85+
}, {}),
8186
appPackageJson: resolveApp('package.json'),
82-
appSrc: resolveApp('src'),
87+
appSrc: [resolveApp('src'), resolveApp('pages')],
8388
yarnLockFile: resolveApp('yarn.lock'),
8489
testsSetup: resolveApp('src/setupTests.js'),
8590
appNodeModules: resolveApp('node_modules'),
@@ -102,8 +107,12 @@ module.exports = {
102107
appPublic: resolveApp('public'),
103108
appHtml: resolveApp('public/index.html'),
104109
appIndexJs: resolveApp('src/index.js'),
110+
appEntries: glob.sync(resolveApp('pages/*.js')).reduce((entries, page) => {
111+
entries[path.basename(page, '.js')] = resolveApp(page);
112+
return entries;
113+
}, {}),
105114
appPackageJson: resolveApp('package.json'),
106-
appSrc: resolveApp('src'),
115+
appSrc: [resolveApp('src'), resolveApp('pages')],
107116
yarnLockFile: resolveApp('yarn.lock'),
108117
testsSetup: resolveApp('src/setupTests.js'),
109118
appNodeModules: resolveApp('node_modules'),

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

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,21 @@ module.exports = {
4343
// These are the "entry points" to our application.
4444
// This means they will be the "root" imports that are included in JS bundle.
4545
// The first two entry points enable "hot" CSS and auto-refreshes for JS.
46-
entry: [
47-
// Include an alternative client for WebpackDevServer. A client's job is to
48-
// connect to WebpackDevServer by a socket and get notified about changes.
49-
// When you save a file, the client will either apply hot updates (in case
50-
// of CSS changes), or refresh the page (in case of JS changes). When you
51-
// make a syntax error, this client will display a syntax error overlay.
52-
// Note: instead of the default WebpackDevServer client, we use a custom one
53-
// to bring better experience for Create React App users. You can replace
54-
// the line below with these two lines if you prefer the stock client:
55-
// require.resolve('webpack-dev-server/client') + '?/',
56-
// require.resolve('webpack/hot/dev-server'),
57-
require.resolve('react-dev-utils/webpackHotDevClient'),
58-
// We ship a few polyfills by default:
59-
require.resolve('./polyfills'),
60-
// Finally, this is your app's code:
61-
paths.appIndexJs
62-
// We include the app code last so that if there is a runtime error during
63-
// initialization, it doesn't blow up the WebpackDevServer client, and
64-
// changing JS code would still trigger a refresh.
65-
],
46+
// todo comments
47+
entry: Object.assign({
48+
index: [
49+
require.resolve('react-dev-utils/webpackHotDevClient'),
50+
require.resolve('./polyfills'),
51+
paths.appIndexJs,
52+
],
53+
polyfills: [require.resolve('./polyfills')],
54+
}, Object.keys(paths.appEntries).reduce((entries, chunk) => {
55+
entries[chunk] = [
56+
require.resolve('react-dev-utils/webpackHotDevClient'),
57+
paths.appEntries[chunk],
58+
];
59+
return entries;
60+
}, {})),
6661
output: {
6762
// Next line is not used in dev but WebpackDevServer crashes without it:
6863
path: paths.appBuild,
@@ -71,7 +66,7 @@ module.exports = {
7166
// This does not produce a real file. It's just the virtual path that is
7267
// served by WebpackDevServer in development. This is the JS bundle
7368
// containing code from all our entry points, and the Webpack runtime.
74-
filename: 'static/js/bundle.js',
69+
filename: 'static/js/[name].js',
7570
// This is the URL that app is served from. We use "/" in development.
7671
publicPath: publicPath
7772
},
@@ -208,6 +203,17 @@ module.exports = {
208203
new HtmlWebpackPlugin({
209204
inject: true,
210205
template: paths.appHtml,
206+
chunks: ['index'],
207+
}),
208+
// todo comment
209+
...Object.keys(paths.appEntries).map((chunk) => {
210+
return new HtmlWebpackPlugin({
211+
inject: true,
212+
template: paths.appHtml,
213+
chunks: ['polyfills', chunk],
214+
chunksSortMode: 'none', // todo fix
215+
filename: chunk + '.html',
216+
});
211217
}),
212218
// Makes some environment variables available to the JS code, for example:
213219
// if (process.env.NODE_ENV === 'development') { ... }. See `./env.js`.

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,13 @@ module.exports = {
6565
// You can exclude the *.map files from the build during deployment.
6666
devtool: 'source-map',
6767
// In production, we only want to load the polyfills and the app code.
68-
entry: [
69-
require.resolve('./polyfills'),
70-
paths.appIndexJs
71-
],
68+
entry: Object.assign({
69+
index: [
70+
require.resolve('./polyfills'),
71+
paths.appIndexJs,
72+
],
73+
polyfills: [require.resolve('./polyfills')],
74+
}, paths.appEntries),
7275
output: {
7376
// The build folder.
7477
path: paths.appBuild,
@@ -223,6 +226,7 @@ module.exports = {
223226
new HtmlWebpackPlugin({
224227
inject: true,
225228
template: paths.appHtml,
229+
chunks: ['index'],
226230
minify: {
227231
removeComments: true,
228232
collapseWhitespace: true,
@@ -236,6 +240,28 @@ module.exports = {
236240
minifyURLs: true
237241
}
238242
}),
243+
// todo comment
244+
...Object.keys(paths.appEntries).map((chunk) => {
245+
return new HtmlWebpackPlugin({
246+
inject: true,
247+
template: paths.appHtml,
248+
chunks: ['polyfills', chunk],
249+
chunksSortMode: 'none', // todo fix
250+
filename: chunk + '.html',
251+
minify: {
252+
removeComments: true,
253+
collapseWhitespace: true,
254+
removeRedundantAttributes: true,
255+
useShortDoctype: true,
256+
removeEmptyAttributes: true,
257+
removeStyleLinkTypeAttributes: true,
258+
keepClosingSlash: true,
259+
minifyJS: true,
260+
minifyCSS: true,
261+
minifyURLs: true
262+
}
263+
});
264+
}),
239265
// Makes some environment variables available to the JS code, for example:
240266
// if (process.env.NODE_ENV === 'production') { ... }. See `./env.js`.
241267
// It is absolutely essential that NODE_ENV was set to production here.

packages/react-scripts/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "spiking-react-scripts",
3-
"version": "0.9.2-fork.1",
3+
"version": "0.9.2-fork.2",
44
"description": "Configuration and scripts for Create React App.",
55
"repository": "stockspiking/create-react-app",
66
"license": "BSD-3-Clause",
@@ -48,6 +48,7 @@
4848
"file-loader": "0.10.0",
4949
"filesize": "3.3.0",
5050
"fs-extra": "0.30.0",
51+
"glob": "^7.1.1",
5152
"gzip-size": "3.0.0",
5253
"html-webpack-plugin": "2.24.0",
5354
"http-proxy-middleware": "0.17.3",

0 commit comments

Comments
 (0)