Skip to content

Commit 69e2338

Browse files
committed
3.4.0 webpack config
1 parent 17e676c commit 69e2338

File tree

4 files changed

+148
-92
lines changed

4 files changed

+148
-92
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 fs = require('fs');
12+
const path = require('path');
13+
const crypto = require('crypto');
14+
const chalk = require('react-dev-utils/chalk');
15+
const paths = require('./paths');
16+
17+
// Ensure the certificate and key provided are valid and if not
18+
// throw an easy to debug error
19+
function validateKeyAndCerts({ cert, key, keyFile, crtFile }) {
20+
let encrypted;
21+
try {
22+
// publicEncrypt will throw an error with an invalid cert
23+
encrypted = crypto.publicEncrypt(cert, Buffer.from('test'));
24+
} catch (err) {
25+
throw new Error(
26+
`The certificate "${chalk.yellow(crtFile)}" is invalid.\n${err.message}`
27+
);
28+
}
29+
30+
try {
31+
// privateDecrypt will throw an error with an invalid key
32+
crypto.privateDecrypt(key, encrypted);
33+
} catch (err) {
34+
throw new Error(
35+
`The certificate key "${chalk.yellow(keyFile)}" is invalid.\n${
36+
err.message
37+
}`
38+
);
39+
}
40+
}
41+
42+
// Read file and throw an error if it doesn't exist
43+
function readEnvFile(file, type) {
44+
if (!fs.existsSync(file)) {
45+
throw new Error(
46+
`You specified ${chalk.cyan(
47+
type
48+
)} in your env, but the file "${chalk.yellow(file)}" can't be found.`
49+
);
50+
}
51+
return fs.readFileSync(file);
52+
}
53+
54+
// Get the https config
55+
// Return cert files if provided in env, otherwise just true or false
56+
function getHttpsConfig() {
57+
const { SSL_CRT_FILE, SSL_KEY_FILE, HTTPS } = process.env;
58+
const isHttps = HTTPS === 'true';
59+
60+
if (isHttps && SSL_CRT_FILE && SSL_KEY_FILE) {
61+
const crtFile = path.resolve(paths.appPath, SSL_CRT_FILE);
62+
const keyFile = path.resolve(paths.appPath, SSL_KEY_FILE);
63+
const config = {
64+
cert: readEnvFile(crtFile, 'SSL_CRT_FILE'),
65+
key: readEnvFile(keyFile, 'SSL_KEY_FILE'),
66+
};
67+
68+
validateKeyAndCerts({ ...config, keyFile, crtFile });
69+
return config;
70+
}
71+
return isHttps;
72+
}
73+
74+
module.exports = getHttpsConfig;

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

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -70,32 +70,23 @@ module.exports = function(webpackEnv) {
7070
const isEnvProductionProfile =
7171
isEnvProduction && process.argv.includes('--profile');
7272

73-
// Webpack uses `publicPath` to determine where the app is being served from.
74-
// It requires a trailing slash, or the file assets will get an incorrect path.
75-
// In development, we always serve from the root. This makes config easier.
76-
const publicPath = isEnvProduction
77-
? paths.servedPath
78-
: isEnvDevelopment && '/';
79-
// Some apps do not use client-side routing with pushState.
80-
// For these, "homepage" can be set to "." to enable relative asset paths.
81-
const shouldUseRelativeAssetPaths = publicPath === './';
82-
83-
// `publicUrl` is just like `publicPath`, but we will provide it to our app
73+
// We will provide `paths.publicUrlOrPath` to our app
8474
// as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript.
8575
// Omit trailing slash as %PUBLIC_URL%/xyz looks better than %PUBLIC_URL%xyz.
86-
const publicUrl = isEnvProduction
87-
? publicPath.slice(0, -1)
88-
: isEnvDevelopment && '';
8976
// Get environment variables to inject into our app.
90-
const env = getClientEnvironment(publicUrl);
77+
const env = getClientEnvironment(paths.publicUrlOrPath.slice(0, -1));
9178

9279
// common function to get style loaders
9380
const getStyleLoaders = (cssOptions, preProcessor) => {
9481
const loaders = [
9582
isEnvDevelopment && require.resolve('style-loader'),
9683
isEnvProduction && {
9784
loader: MiniCssExtractPlugin.loader,
98-
options: shouldUseRelativeAssetPaths ? { publicPath: '../../' } : {},
85+
// css is located in `static/css`, use '../../' to locate index.html folder
86+
// in production `paths.publicUrlOrPath` can be a relative path
87+
options: paths.publicUrlOrPath.startsWith('.')
88+
? { publicPath: '../../' }
89+
: {},
9990
},
10091
{
10192
loader: require.resolve('css-loader'),
@@ -192,9 +183,10 @@ module.exports = function(webpackEnv) {
192183
chunkFilename: isEnvProduction
193184
? 'static/js/[name].[contenthash:8].chunk.js'
194185
: isEnvDevelopment && 'static/js/[name].chunk.js',
186+
// webpack uses `publicPath` to determine where the app is being served from.
187+
// It requires a trailing slash, or the file assets will get an incorrect path.
195188
// We inferred the "public path" (such as / or /my-project) from homepage.
196-
// We use "/" in development.
197-
publicPath: publicPath,
189+
publicPath: paths.publicUrlOrPath,
198190
// Point sourcemap entries to original disk location (format as URL on Windows)
199191
devtoolModuleFilenameTemplate: isEnvProduction
200192
? info =>
@@ -203,7 +195,7 @@ module.exports = function(webpackEnv) {
203195
.replace(/\\/g, '/')
204196
: isEnvDevelopment &&
205197
(info => path.resolve(info.absoluteResourcePath).replace(/\\/g, '/')),
206-
// Prevents conflicts when multiple Webpack runtimes (from different apps)
198+
// Prevents conflicts when multiple webpack runtimes (from different apps)
207199
// are used on the same page.
208200
jsonpFunction: `webpackJsonp${appPackageJson.name}`,
209201
// this defaults to 'window', but by setting it to 'this' then
@@ -289,7 +281,7 @@ module.exports = function(webpackEnv) {
289281
},
290282
},
291283
resolve: {
292-
// This allows you to set a fallback for where Webpack should look for modules.
284+
// This allows you to set a fallback for where webpack should look for modules.
293285
// We placed these paths second because we want `node_modules` to "win"
294286
// if there are any conflicts. This matches Node resolution mechanism.
295287
// https://github.com/facebook/create-react-app/issues/253
@@ -330,7 +322,7 @@ module.exports = function(webpackEnv) {
330322
},
331323
resolveLoader: {
332324
plugins: [
333-
// Also related to Plug'n'Play, but this time it tells Webpack to load its loaders
325+
// Also related to Plug'n'Play, but this time it tells webpack to load its loaders
334326
// from the current package.
335327
PnpWebpackPlugin.moduleLoader(module),
336328
],
@@ -615,9 +607,8 @@ module.exports = function(webpackEnv) {
615607
// Makes some environment variables available in index.html.
616608
// The public URL is available as %PUBLIC_URL% in index.html, e.g.:
617609
// <link rel="icon" href="%PUBLIC_URL%/favicon.ico">
618-
// In production, it will be an empty string unless you specify "homepage"
610+
// It will be an empty string unless you specify "homepage"
619611
// in `package.json`, in which case it will be the pathname of that URL.
620-
// In development, this will be an empty string.
621612
new InterpolateHtmlPlugin(HtmlWebpackPlugin, env.raw),
622613
// This gives some necessary context to module not found errors, such as
623614
// the requesting resource.
@@ -635,7 +626,7 @@ module.exports = function(webpackEnv) {
635626
// See https://github.com/facebook/create-react-app/issues/240
636627
isEnvDevelopment && new CaseSensitivePathsPlugin(),
637628
// If you require a missing module and then `npm install` it, you still have
638-
// to restart the development server for Webpack to discover it. This plugin
629+
// to restart the development server for webpack to discover it. This plugin
639630
// makes the discovery automatic so you don't have to restart.
640631
// See https://github.com/facebook/create-react-app/issues/186
641632
isEnvDevelopment &&
@@ -655,7 +646,7 @@ module.exports = function(webpackEnv) {
655646
// can be used to reconstruct the HTML if necessary
656647
new ManifestPlugin({
657648
fileName: 'asset-manifest.json',
658-
publicPath: publicPath,
649+
publicPath: paths.publicUrlOrPath,
659650
generate: (seed, files, entrypoints) => {
660651
const manifestFiles = files.reduce((manifest, file) => {
661652
manifest[file.name] = file.path;
@@ -672,19 +663,19 @@ module.exports = function(webpackEnv) {
672663
},
673664
}),
674665
// Moment.js is an extremely popular library that bundles large locale files
675-
// by default due to how Webpack interprets its code. This is a practical
666+
// by default due to how webpack interprets its code. This is a practical
676667
// solution that requires the user to opt into importing specific locales.
677668
// https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
678669
// You can remove this if you don't use Moment.js:
679670
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
680671
// Generate a service worker script that will precache, and keep up to date,
681-
// the HTML & assets that are part of the Webpack build.
672+
// the HTML & assets that are part of the webpack build.
682673
isEnvProduction &&
683674
new WorkboxWebpackPlugin.GenerateSW({
684675
clientsClaim: true,
685676
exclude: [/\.map$/, /asset-manifest\.json$/],
686677
importWorkboxFrom: 'cdn',
687-
navigateFallback: publicUrl + '/index.html',
678+
navigateFallback: paths.publicUrlOrPath + 'index.html',
688679
navigateFallbackBlacklist: [
689680
// Exclude URLs starting with /_, as they're likely an API call
690681
new RegExp('^/_'),
@@ -724,7 +715,7 @@ module.exports = function(webpackEnv) {
724715
}),
725716
].filter(Boolean),
726717
// Some libraries import Node modules but don't use them in the browser.
727-
// Tell Webpack to provide empty mocks for them so importing them works.
718+
// Tell webpack to provide empty mocks for them so importing them works.
728719
node: {
729720
module: 'empty',
730721
dgram: 'empty',

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

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
'use strict';
1010

1111
const fs = require('fs');
12-
const isWsl = require('is-wsl');
1312
const path = require('path');
1413
const webpack = require('webpack');
1514
const resolve = require('resolve');
@@ -83,40 +82,11 @@ module.exports = function(webpackEnv) {
8382
const isEnvProductionProfile =
8483
isEnvProduction && process.argv.includes('--profile');
8584

86-
// wptheme added - start
87-
const publicPathWithTrailingSlash = isEnvProduction
88-
? wpThemeUserConfig.homepage.endsWith('/')
89-
? wpThemeUserConfig.homepage
90-
: `${wpThemeUserConfig.homepage}/`
91-
: paths.servedPath;
92-
// wptheme added - end
93-
94-
// Webpack uses `publicPath` to determine where the app is being served from.
95-
// It requires a trailing slash, or the file assets will get an incorrect path.
96-
// In development, we always serve from the root. This makes config easier.
97-
// wptheme remarked out - start
98-
// const publicPath = isEnvProduction
99-
// ? paths.servedPath
100-
// : isEnvDevelopment && '/';
101-
// wptheme remarked out - end
102-
const publicPath = publicPathWithTrailingSlash; // wptheme added
103-
104-
// Some apps do not use client-side routing with pushState.
105-
// For these, "homepage" can be set to "." to enable relative asset paths.
106-
const shouldUseRelativeAssetPaths = publicPath === './';
107-
108-
// `publicUrl` is just like `publicPath`, but we will provide it to our app
85+
// We will provide `paths.publicUrlOrPath` to our app
10986
// as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript.
11087
// Omit trailing slash as %PUBLIC_URL%/xyz looks better than %PUBLIC_URL%xyz.
111-
// wptheme remarked out - start
112-
// const publicUrl = isEnvProduction
113-
// ? publicPath.slice(0, -1)
114-
// : isEnvDevelopment && '';
115-
// wptheme remarked out - end
116-
const publicUrl = publicPathWithTrailingSlash.slice(0, -1); // wptheme added
117-
11888
// Get environment variables to inject into our app.
119-
const env = getClientEnvironment(publicUrl);
89+
const env = getClientEnvironment(paths.publicUrlOrPath.slice(0, -1));
12090

12191
// common function to get style loaders
12292
const getStyleLoaders = (cssOptions, preProcessor) => {
@@ -125,7 +95,11 @@ module.exports = function(webpackEnv) {
12595
// isEnvProduction && // wptheme remarked out -- always use MiniCssExtractPlugin to generate css
12696
{
12797
loader: MiniCssExtractPlugin.loader,
128-
options: shouldUseRelativeAssetPaths ? { publicPath: '../../' } : {},
98+
// css is located in `static/css`, use '../../' to locate index.html folder
99+
// in production `paths.publicUrlOrPath` can be a relative path
100+
options: paths.publicUrlOrPath.startsWith('.')
101+
? { publicPath: '../../' }
102+
: {},
129103
},
130104
{
131105
loader: require.resolve('css-loader'),
@@ -219,8 +193,7 @@ module.exports = function(webpackEnv) {
219193
// There are also additional JS chunk files if you use code splitting.
220194
chunkFilename: 'static/js/[name].chunk.js', // wptheme modified... always use cache busting via the HtmlWebpackPlugin config, way below here
221195
// We inferred the "public path" (such as / or /my-project) from homepage.
222-
// We use "/" in development.
223-
publicPath: publicPath,
196+
publicPath: paths.publicUrlOrPath,
224197
// Point sourcemap entries to original disk location (format as URL on Windows)
225198
devtoolModuleFilenameTemplate: isEnvProduction
226199
? info =>
@@ -229,7 +202,7 @@ module.exports = function(webpackEnv) {
229202
.replace(/\\/g, '/')
230203
: isEnvDevelopment &&
231204
(info => path.resolve(info.absoluteResourcePath).replace(/\\/g, '/')),
232-
// Prevents conflicts when multiple Webpack runtimes (from different apps)
205+
// Prevents conflicts when multiple webpack runtimes (from different apps)
233206
// are used on the same page.
234207
jsonpFunction: `webpackJsonp${appPackageJson.name}`,
235208
// this defaults to 'window', but by setting it to 'this' then
@@ -315,7 +288,7 @@ module.exports = function(webpackEnv) {
315288
},
316289
},
317290
resolve: {
318-
// This allows you to set a fallback for where Webpack should look for modules.
291+
// This allows you to set a fallback for where webpack should look for modules.
319292
// We placed these paths second because we want `node_modules` to "win"
320293
// if there are any conflicts. This matches Node resolution mechanism.
321294
// https://github.com/facebook/create-react-app/issues/253
@@ -356,7 +329,7 @@ module.exports = function(webpackEnv) {
356329
},
357330
resolveLoader: {
358331
plugins: [
359-
// Also related to Plug'n'Play, but this time it tells Webpack to load its loaders
332+
// Also related to Plug'n'Play, but this time it tells webpack to load its loaders
360333
// from the current package.
361334
PnpWebpackPlugin.moduleLoader(module),
362335
],
@@ -649,9 +622,8 @@ module.exports = function(webpackEnv) {
649622
// Makes some environment variables available in index.html.
650623
// The public URL is available as %PUBLIC_URL% in index.html, e.g.:
651624
// <link rel="icon" href="%PUBLIC_URL%/favicon.ico">
652-
// In production, it will be an empty string unless you specify "homepage"
625+
// It will be an empty string unless you specify "homepage"
653626
// in `package.json`, in which case it will be the pathname of that URL.
654-
// In development, this will be an empty string.
655627
new InterpolateHtmlPlugin(HtmlWebpackPlugin, env.raw),
656628
// This gives some necessary context to module not found errors, such as
657629
// the requesting resource.
@@ -673,7 +645,7 @@ module.exports = function(webpackEnv) {
673645
// touchFile is used to force WebPack to do a rebuild. It must be a file that WebPack is watching.
674646
new FileWatcherPlugin(fileWatcherPluginConfig),
675647
// If you require a missing module and then `npm install` it, you still have
676-
// to restart the development server for Webpack to discover it. This plugin
648+
// to restart the development server for webpack to discover it. This plugin
677649
// makes the discovery automatic so you don't have to restart.
678650
// See https://github.com/facebook/create-react-app/issues/186
679651
isEnvDevelopment &&
@@ -695,7 +667,7 @@ module.exports = function(webpackEnv) {
695667
// can be used to reconstruct the HTML if necessary
696668
new ManifestPlugin({
697669
fileName: 'asset-manifest.json',
698-
publicPath: publicPath,
670+
publicPath: paths.publicUrlOrPath,
699671
generate: (seed, files, entrypoints) => {
700672
const manifestFiles = files.reduce((manifest, file) => {
701673
manifest[file.name] = file.path;
@@ -712,19 +684,19 @@ module.exports = function(webpackEnv) {
712684
},
713685
}),
714686
// Moment.js is an extremely popular library that bundles large locale files
715-
// by default due to how Webpack interprets its code. This is a practical
687+
// by default due to how webpack interprets its code. This is a practical
716688
// solution that requires the user to opt into importing specific locales.
717689
// https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
718690
// You can remove this if you don't use Moment.js:
719691
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
720692
// Generate a service worker script that will precache, and keep up to date,
721-
// the HTML & assets that are part of the Webpack build.
693+
// the HTML & assets that are part of the webpack build.
722694
isEnvProduction &&
723695
new WorkboxWebpackPlugin.GenerateSW({
724696
clientsClaim: true,
725697
exclude: [/\.map$/, /asset-manifest\.json$/],
726698
importWorkboxFrom: 'cdn',
727-
navigateFallback: publicUrl + '/index.php', // wptheme added php support
699+
navigateFallback: paths.publicUrlOrPath + '/index.php', // wptheme added php support
728700
navigateFallbackBlacklist: [
729701
// Exclude URLs starting with /_, as they're likely an API call
730702
new RegExp('^/_'),
@@ -758,14 +730,13 @@ module.exports = function(webpackEnv) {
758730
'!**/src/setupProxy.*',
759731
'!**/src/setupTests.*',
760732
],
761-
watch: paths.appSrc,
762733
silent: true,
763734
// The formatter is invoked directly in WebpackDevServerUtils during development
764735
formatter: isEnvProduction ? typescriptFormatter : undefined,
765736
}),
766737
].filter(Boolean),
767738
// Some libraries import Node modules but don't use them in the browser.
768-
// Tell Webpack to provide empty mocks for them so importing them works.
739+
// Tell webpack to provide empty mocks for them so importing them works.
769740
node: {
770741
module: 'empty',
771742
dgram: 'empty',

0 commit comments

Comments
 (0)