123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- 'use strict';
- function base64SourceMap(source) {
- const base64 = Buffer.from(JSON.stringify(source.map()), 'utf8').toString(
- 'base64'
- );
- return `data:application/json;charset=utf-8;base64,${base64}`;
- }
- function getSourceById(server, id) {
- const module = server._stats.compilation.modules.find(m => m.id == id);
- return module.originalSource();
- }
- module.exports = function createEvalSourceMapMiddleware(server) {
- return function handleWebpackInternalMiddleware(req, res, next) {
- if (req.url.startsWith('/__get-internal-source')) {
- const fileName = req.query.fileName;
- const id = fileName.match(/webpack-internal:\/\/\/(.+)/)[1];
- if (!id || !server._stats) {
- next();
- }
- const source = getSourceById(server, id);
- const sourceMapURL = `//# sourceMappingURL=${base64SourceMap(source)}`;
- const sourceURL = `//# sourceURL=webpack-internal:///${module.id}`;
- res.end(`${source.source()}\n${sourceMapURL}\n${sourceURL}`);
- } else {
- next();
- }
- };
- };
|