Fix webpack source maps
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
a409ebb160
commit
40b069b632
1 changed files with 72 additions and 8 deletions
|
@ -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")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue