Skip to content

Commit 0bb870b

Browse files
committed
3.4.0 Changes
1 parent 80aefa5 commit 0bb870b

18 files changed

+393
-119
lines changed

packages/react-dev-utils/InterpolateHtmlPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
// This Webpack plugin lets us interpolate custom variables into `index.html`.
8+
// This webpack plugin lets us interpolate custom variables into `index.html`.
99
// Usage: `new InterpolateHtmlPlugin(HtmlWebpackPlugin, { 'MY_VARIABLE': 42 })`
1010
// Then, you can use %MY_VARIABLE% in your `index.html`.
1111

packages/react-dev-utils/README.md

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ There is no single entry point. You can only import individual top-level modules
2020

2121
#### `new InterpolateHtmlPlugin(htmlWebpackPlugin: HtmlWebpackPlugin, replacements: {[key:string]: string})`
2222

23-
This Webpack plugin lets us interpolate custom variables into `index.html`.<br>
23+
This webpack plugin lets us interpolate custom variables into `index.html`.<br>
2424
It works in tandem with [HtmlWebpackPlugin](https://github.com/ampedandwired/html-webpack-plugin) 2.x via its [events](https://github.com/ampedandwired/html-webpack-plugin#events).
2525

2626
```js
2727
var path = require('path');
2828
var HtmlWebpackPlugin = require('html-webpack-plugin');
2929
var InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
3030

31-
// Webpack config
31+
// webpack config
3232
var publicUrl = '/my-custom-url';
3333

3434
module.exports = {
@@ -58,15 +58,15 @@ module.exports = {
5858

5959
#### `new InlineChunkHtmlPlugin(htmlWebpackPlugin: HtmlWebpackPlugin, tests: Regex[])`
6060

61-
This Webpack plugin inlines script chunks into `index.html`.<br>
61+
This webpack plugin inlines script chunks into `index.html`.<br>
6262
It works in tandem with [HtmlWebpackPlugin](https://github.com/ampedandwired/html-webpack-plugin) 4.x.
6363

6464
```js
6565
var path = require('path');
6666
var HtmlWebpackPlugin = require('html-webpack-plugin');
6767
var InlineChunkHtmlPlugin = require('react-dev-utils/InlineChunkHtmlPlugin');
6868

69-
// Webpack config
69+
// webpack config
7070
var publicUrl = '/my-custom-url';
7171

7272
module.exports = {
@@ -91,7 +91,7 @@ module.exports = {
9191

9292
#### `new ModuleScopePlugin(appSrc: string | string[], allowedFiles?: string[])`
9393

94-
This Webpack plugin ensures that relative imports from app's source directories don't reach outside of it.
94+
This webpack plugin ensures that relative imports from app's source directories don't reach outside of it.
9595

9696
```js
9797
var path = require('path');
@@ -113,21 +113,21 @@ module.exports = {
113113

114114
#### `new WatchMissingNodeModulesPlugin(nodeModulesPath: string)`
115115

116-
This Webpack plugin ensures `npm install <library>` forces a project rebuild.<br>
117-
We’re not sure why this isn't Webpack's default behavior.<br>
116+
This webpack plugin ensures `npm install <library>` forces a project rebuild.<br>
117+
We’re not sure why this isn't webpack's default behavior.<br>
118118
See [#186](https://github.com/facebook/create-react-app/issues/186) for details.
119119

120120
```js
121121
var path = require('path');
122122
var WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin');
123123

124-
// Webpack config
124+
// webpack config
125125
module.exports = {
126126
// ...
127127
plugins: [
128128
// ...
129129
// If you require a missing module and then `npm install` it, you still have
130-
// to restart the development server for Webpack to discover it. This plugin
130+
// to restart the development server for webpack to discover it. This plugin
131131
// makes the discovery automatic so you don't have to restart.
132132
// See https://github.com/facebook/create-react-app/issues/186
133133
new WatchMissingNodeModulesPlugin(path.resolve('node_modules')),
@@ -287,9 +287,14 @@ getProcessForPort(3000);
287287

288288
On macOS, tries to find a known running editor process and opens the file in it. It can also be explicitly configured by `REACT_EDITOR`, `VISUAL`, or `EDITOR` environment variables. For example, you can put `REACT_EDITOR=atom` in your `.env.local` file, and Create React App will respect that.
289289

290-
#### `noopServiceWorkerMiddleware(): ExpressMiddleware`
290+
#### `noopServiceWorkerMiddleware(servedPath: string): ExpressMiddleware`
291291

292-
Returns Express middleware that serves a `/service-worker.js` that resets any previously set service worker configuration. Useful for development.
292+
Returns Express middleware that serves a `${servedPath}/service-worker.js` that resets any previously set service worker configuration. Useful for development.
293+
294+
#### `redirectServedPathMiddleware(servedPath: string): ExpressMiddleware`
295+
296+
Returns Express middleware that redirects to `${servedPath}/${req.path}`, if `req.url`
297+
does not start with `servedPath`. Useful for development.
293298

294299
#### `openBrowser(url: string): boolean`
295300

@@ -310,11 +315,11 @@ if (openBrowser('http://localhost:3000')) {
310315

311316
Prints hosting instructions after the project is built.
312317

313-
Pass your parsed `package.json` object as `appPackage`, your the URL where you plan to host the app as `publicUrl`, `output.publicPath` from your Webpack configuration as `publicPath`, the `buildFolder` name, and whether to `useYarn` in instructions.
318+
Pass your parsed `package.json` object as `appPackage`, your the URL where you plan to host the app as `publicUrl`, `output.publicPath` from your webpack configuration as `publicPath`, the `buildFolder` name, and whether to `useYarn` in instructions.
314319

315320
```js
316321
const appPackage = require(paths.appPackageJson);
317-
const publicUrl = paths.publicUrl;
322+
const publicUrl = paths.publicUrlOrPath;
318323
const publicPath = config.output.publicPath;
319324
printHostingInstructions(appPackage, publicUrl, publicPath, 'build', true);
320325
```
@@ -327,7 +332,7 @@ Returns a Promise resolving to either `defaultPort` or next available port if th
327332

328333
##### `createCompiler(args: Object): WebpackCompiler`
329334

330-
Creates a Webpack compiler instance for WebpackDevServer with built-in helpful messages.
335+
Creates a webpack compiler instance for WebpackDevServer with built-in helpful messages.
331336

332337
The `args` object accepts a number of properties:
333338

@@ -340,22 +345,22 @@ The `args` object accepts a number of properties:
340345
- **tscCompileOnError** `boolean`: If `true`, errors in TypeScript type checking will not prevent start script from running app, and will not cause build script to exit unsuccessfully. Also downgrades all TypeScript type checking error messages to warning messages.
341346
- **webpack** `function`: A reference to the webpack constructor.
342347

343-
##### `prepareProxy(proxySetting: string, appPublicFolder: string): Object`
348+
##### `prepareProxy(proxySetting: string, appPublicFolder: string, servedPathname: string): Object`
344349

345350
Creates a WebpackDevServer `proxy` configuration object from the `proxy` setting in `package.json`.
346351

347-
##### `prepareUrls(protocol: string, host: string, port: number): Object`
352+
##### `prepareUrls(protocol: string, host: string, port: number, pathname: string = '/'): Object`
348353

349354
Returns an object with local and remote URLs for the development server. Pass this object to `createCompiler()` described above.
350355

351356
#### `webpackHotDevClient`
352357

353358
This is an alternative client for [WebpackDevServer](https://github.com/webpack/webpack-dev-server) that shows a syntax error overlay.
354359

355-
It currently supports only Webpack 3.x.
360+
It currently supports only webpack 3.x.
356361

357362
```js
358-
// Webpack development config
363+
// webpack development config
359364
module.exports = {
360365
// ...
361366
entry: [

packages/react-dev-utils/WatchMissingNodeModulesPlugin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
// This Webpack plugin ensures `npm install <library>` forces a project rebuild.
9-
// We’re not sure why this isn't Webpack's default behavior.
8+
// This webpack plugin ensures `npm install <library>` forces a project rebuild.
9+
// We’re not sure why this isn't webpack's default behavior.
1010
// See https://github.com/facebook/create-react-app/issues/186.
1111

1212
'use strict';

packages/react-dev-utils/WebpackDevServerUtils.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,20 @@ const forkTsCheckerWebpackPlugin = require('./ForkTsCheckerWebpackPlugin');
2222

2323
const isInteractive = process.stdout.isTTY;
2424

25-
function prepareUrls(protocol, host, port) {
25+
function prepareUrls(protocol, host, port, pathname = '/') {
2626
const formatUrl = hostname =>
2727
url.format({
2828
protocol,
2929
hostname,
3030
port,
31-
pathname: '/',
31+
pathname,
3232
});
3333
const prettyPrintUrl = hostname =>
3434
url.format({
3535
protocol,
3636
hostname,
3737
port: chalk.bold(port),
38-
pathname: '/',
38+
pathname,
3939
});
4040

4141
const isUnspecifiedHost = host === '0.0.0.0' || host === '::';
@@ -111,7 +111,7 @@ function createCompiler({
111111
tscCompileOnError,
112112
webpack,
113113
}) {
114-
// "Compiler" is a low-level interface to Webpack.
114+
// "Compiler" is a low-level interface to webpack.
115115
// It lets us listen to some events and provide our own custom messages.
116116
let compiler;
117117
try {
@@ -124,7 +124,7 @@ function createCompiler({
124124
process.exit(1);
125125
}
126126

127-
// "invalid" event fires when you have changed a file, and Webpack is
127+
// "invalid" event fires when you have changed a file, and webpack is
128128
// recompiling a bundle. WebpackDevServer takes care to pause serving the
129129
// bundle, so if you refresh, it'll wait instead of serving the old one.
130130
// "invalid" is short for "bundle invalidated", it doesn't imply any errors.
@@ -162,14 +162,14 @@ function createCompiler({
162162
});
163163
}
164164

165-
// "done" event fires when Webpack has finished recompiling the bundle.
165+
// "done" event fires when webpack has finished recompiling the bundle.
166166
// Whether or not you have warnings or errors, you will get this event.
167167
compiler.hooks.done.tap('done', async stats => {
168168
if (isInteractive) {
169169
clearConsole();
170170
}
171171

172-
// We have switched off the default Webpack output in WebpackDevServer
172+
// We have switched off the default webpack output in WebpackDevServer
173173
// options so we are going to "massage" the warnings and errors and present
174174
// them in a readable focused way.
175175
// We only construct the warnings and errors for speed:
@@ -356,7 +356,7 @@ function onProxyError(proxy) {
356356
};
357357
}
358358

359-
function prepareProxy(proxy, appPublicFolder) {
359+
function prepareProxy(proxy, appPublicFolder, servedPathname) {
360360
// `proxy` lets you specify alternate servers for specific requests.
361361
if (!proxy) {
362362
return undefined;
@@ -377,10 +377,17 @@ function prepareProxy(proxy, appPublicFolder) {
377377
// If proxy is specified, let it handle any request except for
378378
// files in the public folder and requests to the WebpackDevServer socket endpoint.
379379
// https://github.com/facebook/create-react-app/issues/6720
380+
const sockPath = process.env.WDS_SOCKET_PATH || '/sockjs-node';
381+
const isDefaultSockHost = !process.env.WDS_SOCKET_HOST;
380382
function mayProxy(pathname) {
381-
const maybePublicPath = path.resolve(appPublicFolder, pathname.slice(1));
383+
const maybePublicPath = path.resolve(
384+
appPublicFolder,
385+
pathname.replace(new RegExp('^' + servedPathname), '')
386+
);
382387
const isPublicFileRequest = fs.existsSync(maybePublicPath);
383-
const isWdsEndpointRequest = pathname.startsWith('/sockjs-node'); // used by webpackHotDevClient
388+
// used by webpackHotDevClient
389+
const isWdsEndpointRequest =
390+
isDefaultSockHost && pathname.startsWith(sockPath);
384391
return !(isPublicFileRequest || isWdsEndpointRequest);
385392
}
386393

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
'use strict';
9+
10+
const getPublicUrlOrPath = require('../getPublicUrlOrPath');
11+
12+
const tests = [
13+
// DEVELOPMENT with homepage
14+
{ dev: true, homepage: '/', expect: '/' },
15+
{ dev: true, homepage: '/test', expect: '/test/' },
16+
{ dev: true, homepage: '/test/', expect: '/test/' },
17+
{ dev: true, homepage: './', expect: '/' },
18+
{ dev: true, homepage: '../', expect: '/' },
19+
{ dev: true, homepage: '../test', expect: '/' },
20+
{ dev: true, homepage: './test/path', expect: '/' },
21+
{ dev: true, homepage: 'https://create-react-app.dev/', expect: '/' },
22+
{
23+
dev: true,
24+
homepage: 'https://create-react-app.dev/test',
25+
expect: '/test/',
26+
},
27+
// DEVELOPMENT with publicURL
28+
{ dev: true, publicUrl: '/', expect: '/' },
29+
{ dev: true, publicUrl: '/test', expect: '/test/' },
30+
{ dev: true, publicUrl: '/test/', expect: '/test/' },
31+
{ dev: true, publicUrl: './', expect: '/' },
32+
{ dev: true, publicUrl: '../', expect: '/' },
33+
{ dev: true, publicUrl: '../test', expect: '/' },
34+
{ dev: true, publicUrl: './test/path', expect: '/' },
35+
{ dev: true, publicUrl: 'https://create-react-app.dev/', expect: '/' },
36+
{
37+
dev: true,
38+
publicUrl: 'https://create-react-app.dev/test',
39+
expect: '/test/',
40+
},
41+
// DEVELOPMENT with publicURL and homepage
42+
{ dev: true, publicUrl: '/', homepage: '/test', expect: '/' },
43+
{ dev: true, publicUrl: '/test', homepage: '/path', expect: '/test/' },
44+
{ dev: true, publicUrl: '/test/', homepage: '/test/path', expect: '/test/' },
45+
{ dev: true, publicUrl: './', homepage: '/test', expect: '/' },
46+
{ dev: true, publicUrl: '../', homepage: '/test', expect: '/' },
47+
{ dev: true, publicUrl: '../test', homepage: '/test', expect: '/' },
48+
{ dev: true, publicUrl: './test/path', homepage: '/test', expect: '/' },
49+
{
50+
dev: true,
51+
publicUrl: 'https://create-react-app.dev/',
52+
homepage: '/test',
53+
expect: '/',
54+
},
55+
{
56+
dev: true,
57+
publicUrl: 'https://create-react-app.dev/test',
58+
homepage: '/path',
59+
expect: '/test/',
60+
},
61+
62+
// PRODUCTION with homepage
63+
{ dev: false, homepage: '/', expect: '/' },
64+
{ dev: false, homepage: '/test', expect: '/test/' },
65+
{ dev: false, homepage: '/test/', expect: '/test/' },
66+
{ dev: false, homepage: './', expect: './' },
67+
{ dev: false, homepage: '../', expect: '../' },
68+
{ dev: false, homepage: '../test', expect: '../test/' },
69+
{ dev: false, homepage: './test/path', expect: './test/path/' },
70+
{ dev: false, homepage: 'https://create-react-app.dev/', expect: '/' },
71+
{
72+
dev: false,
73+
homepage: 'https://create-react-app.dev/test',
74+
expect: '/test/',
75+
},
76+
// PRODUCTION with publicUrl
77+
{ dev: false, publicUrl: '/', expect: '/' },
78+
{ dev: false, publicUrl: '/test', expect: '/test/' },
79+
{ dev: false, publicUrl: '/test/', expect: '/test/' },
80+
{ dev: false, publicUrl: './', expect: './' },
81+
{ dev: false, publicUrl: '../', expect: '../' },
82+
{ dev: false, publicUrl: '../test', expect: '../test/' },
83+
{ dev: false, publicUrl: './test/path', expect: './test/path/' },
84+
{
85+
dev: false,
86+
publicUrl: 'https://create-react-app.dev/',
87+
expect: 'https://create-react-app.dev/',
88+
},
89+
{
90+
dev: false,
91+
publicUrl: 'https://create-react-app.dev/test',
92+
expect: 'https://create-react-app.dev/test/',
93+
},
94+
// PRODUCTION with publicUrl and homepage
95+
{ dev: false, publicUrl: '/', homepage: '/test', expect: '/' },
96+
{ dev: false, publicUrl: '/test', homepage: '/path', expect: '/test/' },
97+
{ dev: false, publicUrl: '/test/', homepage: '/test/path', expect: '/test/' },
98+
{ dev: false, publicUrl: './', homepage: '/test', expect: './' },
99+
{ dev: false, publicUrl: '../', homepage: '/test', expect: '../' },
100+
{ dev: false, publicUrl: '../test', homepage: '/test', expect: '../test/' },
101+
{
102+
dev: false,
103+
publicUrl: './test/path',
104+
homepage: '/test',
105+
expect: './test/path/',
106+
},
107+
{
108+
dev: false,
109+
publicUrl: 'https://create-react-app.dev/',
110+
homepage: '/test',
111+
expect: 'https://create-react-app.dev/',
112+
},
113+
{
114+
dev: false,
115+
publicUrl: 'https://create-react-app.dev/test',
116+
homepage: '/path',
117+
expect: 'https://create-react-app.dev/test/',
118+
},
119+
];
120+
121+
describe('getPublicUrlOrPath', () => {
122+
tests.forEach(t =>
123+
it(JSON.stringify(t), () => {
124+
const actual = getPublicUrlOrPath(t.dev, t.homepage, t.publicUrl);
125+
expect(actual).toBe(t.expect);
126+
})
127+
);
128+
});

packages/react-dev-utils/formatWebpackMessages.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function isLikelyASyntaxError(message) {
1818
function formatMessage(message) {
1919
let lines = message.split('\n');
2020

21-
// Strip Webpack-added headers off errors/warnings
21+
// Strip webpack-added headers off errors/warnings
2222
// https://github.com/webpack/webpack/blob/master/lib/ModuleError.js
2323
lines = lines.filter(line => !/Module [A-z ]+\(from/.test(line));
2424

@@ -85,7 +85,7 @@ function formatMessage(message) {
8585
message = lines.join('\n');
8686
// Internal stacks are generally useless so we strip them... with the
8787
// exception of stacks containing `webpack:` because they're normally
88-
// from user code generated by Webpack. For more information see
88+
// from user code generated by webpack. For more information see
8989
// https://github.com/facebook/create-react-app/pull/1050
9090
message = message.replace(
9191
/^\s*at\s((?!webpack:).)*:\d+:\d+[\s)]*(\n|$)/gm,

0 commit comments

Comments
 (0)