Skip to content

Commit 2ed8ecc

Browse files
jasonLasterTimer
authored andcommittedSep 20, 2018
Switch to eval-source-map (#4930)
1 parent e8b0ee8 commit 2ed8ecc

File tree

5 files changed

+57
-3
lines changed

5 files changed

+57
-3
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
'use strict';
8+
9+
function base64SourceMap(source) {
10+
const base64 = Buffer.from(JSON.stringify(source.map()), 'utf8').toString(
11+
'base64'
12+
);
13+
return `data:application/json;charset=utf-8;base64,${base64}`;
14+
}
15+
16+
function getSourceById(server, id) {
17+
const module = server._stats.compilation.modules.find(m => m.id == id);
18+
return module.originalSource();
19+
}
20+
21+
/*
22+
* Middleware responsible for retrieving a generated source
23+
* Receives a webpack internal url: "webpack-internal:///<module-id>"
24+
* Returns a generated source: "<source-text><sourceMappingURL><sourceURL>"
25+
*
26+
* Based on EvalSourceMapDevToolModuleTemplatePlugin.js
27+
*/
28+
module.exports = function createEvalSourceMapMiddleware(server) {
29+
return function handleWebpackInternalMiddleware(req, res, next) {
30+
if (req.url.startsWith('/__get-internal-source')) {
31+
const fileName = req.query.fileName;
32+
const id = fileName.match(/webpack-internal:\/\/\/(.+)/)[1];
33+
if (!id || !server._stats) {
34+
next();
35+
}
36+
37+
const source = getSourceById(server, id);
38+
const sourceMapURL = `//# sourceMappingURL=${base64SourceMap(source)}`;
39+
const sourceURL = `//# sourceURL=webpack-internal:///${module.id}`;
40+
res.end(`${source.source()}\n${sourceMapURL}\n${sourceURL}`);
41+
} else {
42+
next();
43+
}
44+
};
45+
};

‎packages/react-dev-utils/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"crossSpawn.js",
1818
"errorOverlayMiddleware.js",
1919
"eslintFormatter.js",
20+
"evalSourceMapMiddleware.js",
2021
"FileSizeReporter.js",
2122
"formatWebpackMessages.js",
2223
"getCSSModuleLocalIdent.js",

‎packages/react-error-overlay/src/utils/mapper.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ async function map(
3434
});
3535
await settle(
3636
files.map(async fileName => {
37-
const fileSource = await fetch(fileName).then(r => r.text());
37+
const fetchUrl = fileName.startsWith('webpack-internal:')
38+
? `/__get-internal-source?fileName=${encodeURIComponent(fileName)}`
39+
: fileName;
40+
41+
const fileSource = await fetch(fetchUrl).then(r => r.text());
3842
const map = await getSourceMap(fileName, fileSource);
3943
cache[fileName] = { fileSource, map };
4044
})

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ module.exports = {
7676
mode: 'development',
7777
// You may want 'eval' instead if you prefer to see the compiled output in DevTools.
7878
// See the discussion in https://github.com/facebook/create-react-app/issues/343
79-
devtool: 'cheap-module-source-map',
79+
devtool: 'eval-source-map',
8080
// These are the "entry points" to our application.
8181
// This means they will be the "root" imports that are included in JS bundle.
8282
// The first two entry points enable "hot" CSS and auto-refreshes for JS.

‎packages/react-scripts/config/webpackDevServer.config.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
'use strict';
1010

1111
const errorOverlayMiddleware = require('react-dev-utils/errorOverlayMiddleware');
12+
const evalSourceMapMiddleware = require('react-dev-utils/evalSourceMapMiddleware');
1213
const noopServiceWorkerMiddleware = require('react-dev-utils/noopServiceWorkerMiddleware');
1314
const ignoredFiles = require('react-dev-utils/ignoredFiles');
1415
const config = require('./webpack.config.dev');
@@ -89,7 +90,10 @@ module.exports = function(proxy, allowedHost) {
8990
},
9091
public: allowedHost,
9192
proxy,
92-
before(app) {
93+
before(app, server) {
94+
// This lets us fetch source contents from webpack for the error overlay
95+
app.use(evalSourceMapMiddleware(server));
96+
9397
// This lets us open files from the runtime error overlay.
9498
app.use(errorOverlayMiddleware());
9599
// This service worker file is effectively a 'no-op' that will reset any

0 commit comments

Comments
 (0)
Please sign in to comment.