fix(webpack): es module source map resolution (#10860)

Including source file remapping for newer runtimes.
This commit is contained in:
Nathan Walker
2025-09-27 11:43:46 -07:00
committed by GitHub
parent 54c069f519
commit ce589a8fc9
2 changed files with 108 additions and 49 deletions

View File

@ -171,7 +171,40 @@ export default function (config: Config, env: IWebpackEnv = _env): Config {
return map as Config.DevTool;
};
config.devtool(getSourceMapType(env.sourceMap));
const sourceMapType = getSourceMapType(env.sourceMap);
// Use devtool for both CommonJS and ESM - let webpack handle source mapping properly
config.devtool(sourceMapType);
// For ESM builds, fix the sourceMappingURL to use correct paths
if (!env.commonjs && sourceMapType && sourceMapType !== 'hidden-source-map') {
class FixSourceMapUrlPlugin {
apply(compiler) {
compiler.hooks.emit.tap('FixSourceMapUrlPlugin', (compilation) => {
const leadingCharacter = process.platform === "win32" ? "/":"";
Object.keys(compilation.assets).forEach((filename) => {
if (filename.endsWith('.mjs') || filename.endsWith('.js')) {
const asset = compilation.assets[filename];
let source = asset.source();
// Replace sourceMappingURL to use file:// protocol pointing to actual location
source = source.replace(
/\/\/# sourceMappingURL=(.+\.map)/g,
`//# sourceMappingURL=file://${leadingCharacter}${outputPath}/$1`,
);
compilation.assets[filename] = {
source: () => source,
size: () => source.length,
};
}
});
});
}
}
config.plugin('FixSourceMapUrlPlugin').use(FixSourceMapUrlPlugin);
}
// when using hidden-source-map, output source maps to the `platforms/{platformName}-sourceMaps` folder
if (env.sourceMap === 'hidden-source-map') {