| [9af201e] | 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 = Array.from(server._stats.compilation.modules).find(
|
|---|
| 18 | (m) => server._stats.compilation.chunkGraph.getModuleId(m) == id,
|
|---|
| 19 | );
|
|---|
| 20 | return module.originalSource();
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | /*
|
|---|
| 24 | * Middleware responsible for retrieving a generated source
|
|---|
| 25 | * Receives a webpack internal url: "webpack-internal:///<module-id>"
|
|---|
| 26 | * Returns a generated source: "<source-text><sourceMappingURL><sourceURL>"
|
|---|
| 27 | *
|
|---|
| 28 | * Based on EvalSourceMapDevToolModuleTemplatePlugin.js
|
|---|
| 29 | */
|
|---|
| 30 | module.exports = function createEvalSourceMapMiddleware(server) {
|
|---|
| 31 | return function handleWebpackInternalMiddleware(req, res, next) {
|
|---|
| 32 | if (req.url.startsWith('/__get-internal-source')) {
|
|---|
| 33 | const fileName = req.query.fileName;
|
|---|
| 34 | const id = fileName.match(/webpack-internal:\/\/\/(.+)/)[1];
|
|---|
| 35 | if (!id || !server._stats) {
|
|---|
| 36 | next();
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | const source = getSourceById(server, id);
|
|---|
| 40 | const sourceMapURL = `//# sourceMappingURL=${base64SourceMap(source)}`;
|
|---|
| 41 | const sourceURL = `//# sourceURL=webpack-internal:///${module.id}`;
|
|---|
| 42 | res.end(`${source.source()}\n${sourceMapURL}\n${sourceURL}`);
|
|---|
| 43 | } else {
|
|---|
| 44 | next();
|
|---|
| 45 | }
|
|---|
| 46 | };
|
|---|
| 47 | };
|
|---|