Fix webpack source maps
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Manuel Thalmann 2022-12-03 15:20:07 +01:00
parent a409ebb160
commit 40b069b632
No known key found for this signature in database
GPG key ID: 5FD9AD3CCDDBD27B

View file

@ -1,10 +1,11 @@
import { resolve } from "path"; import { writeFile } from "fs/promises";
import { dirname, isAbsolute, relative, resolve } from "path";
import { fileURLToPath } from "url"; import { fileURLToPath } from "url";
import exports, { Configuration } from "webpack"; import exports, { Configuration } from "webpack";
const { WatchIgnorePlugin } = exports; const { WatchIgnorePlugin, SourceMapDevToolPlugin } = exports;
let dirname = fileURLToPath(new URL(".", import.meta.url)); let dirName = fileURLToPath(new URL(".", import.meta.url));
let generator = (env: any, argv: any): Configuration[] => let generator = (env: any, argv: any): Configuration[] =>
{ {
@ -23,8 +24,30 @@ let generator = (env: any, argv: any): Configuration[] =>
}, },
output: { output: {
filename: "[name].js", filename: "[name].js",
path: resolve(dirname, "lib"), path: resolve(dirName, "lib"),
devtoolFallbackModuleFilenameTemplate: "../[resource-path]", devtoolModuleFilenameTemplate: (context: any) =>
{
let path = context.absoluteResourcePath;
// For regular files, this statement is true.
if (isAbsolute(path))
{
return path;
}
else
{
let fallback = new SourceMapDevToolPlugin().moduleFilenameTemplate;
if (typeof fallback === "function")
{
return fallback(context);
}
else
{
return fallback;
}
}
},
libraryTarget: "module", libraryTarget: "module",
chunkFormat: "module", chunkFormat: "module",
environment: { environment: {
@ -58,7 +81,48 @@ let generator = (env: any, argv: any): Configuration[] =>
paths: [ paths: [
/\.d\.ts$/ /\.d\.ts$/
] ]
}) }),
{
apply(compiler)
{
compiler.hooks.assetEmitted.tap(
{
name: "AdjustSourceMap"
},
async (file, { content, source, outputPath, compilation, targetPath }) =>
{
if (file.endsWith(".map"))
{
try
{
let sourceMap = JSON.parse(content.toString());
if (Array.isArray(sourceMap.sources))
{
sourceMap.sources = sourceMap.sources.map(
(source: string) =>
{
// Prevent `webpack://` sources from being changed
if (isAbsolute(source))
{
// Change regular file paths to relative ones
return relative(dirname(targetPath), source);
}
else
{
return source;
}
});
// Overwrite old source map
await writeFile(targetPath, JSON.stringify(sourceMap));
}
}
catch {}
}
});
}
}
], ],
module: { module: {
rules: [ rules: [
@ -69,10 +133,10 @@ let generator = (env: any, argv: any): Configuration[] =>
{ {
loader: "ts-loader", loader: "ts-loader",
options: { options: {
configFile: resolve(dirname, "tsconfig.build.json"), configFile: resolve(dirName, "tsconfig.build.json"),
projectReferences: true, projectReferences: true,
compilerOptions: { compilerOptions: {
outDir: resolve(dirname, "lib", "temp") outDir: resolve(dirName, "lib", "temp")
} }
} }
} }