ConnectForce/webpack.config.ts

156 lines
5.6 KiB
TypeScript
Raw Normal View History

2022-12-03 14:20:07 +00:00
import { writeFile } from "fs/promises";
import { dirname, isAbsolute, relative, resolve } from "path";
2022-12-01 23:58:56 +00:00
import { fileURLToPath } from "url";
import exports, { Configuration } from "webpack";
2022-12-03 14:20:07 +00:00
const { WatchIgnorePlugin, SourceMapDevToolPlugin } = exports;
2022-12-01 23:58:56 +00:00
2022-12-03 14:20:07 +00:00
let dirName = fileURLToPath(new URL(".", import.meta.url));
2022-12-01 23:58:56 +00:00
let generator = (env: any, argv: any): Configuration[] =>
{
return [
{
target: "web",
mode: env.production ? "production" : "development",
entry: {
2022-12-02 00:11:30 +00:00
main: "./src/index.ts",
...(
env.production ?
{} :
{
"tests/main.test": "./src/tests/main.test.ts"
})
2022-12-01 23:58:56 +00:00
},
output: {
filename: "[name].js",
2022-12-03 14:20:07 +00:00
path: resolve(dirName, "lib"),
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;
}
}
},
2022-12-01 23:58:56 +00:00
libraryTarget: "module",
chunkFormat: "module",
environment: {
dynamicImport: true
}
},
devtool: "source-map",
resolve: {
extensions: [
".ts",
".js"
],
extensionAlias: {
".js": [
".js",
".ts"
],
".mjs": [
".mjs",
".mts"
],
".cjs": [
".cjs",
".cts"
]
}
},
plugins: [
new WatchIgnorePlugin(
{
paths: [
/\.d\.ts$/
]
2022-12-03 14:20:07 +00:00
}),
{
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 {}
}
});
}
}
2022-12-01 23:58:56 +00:00
],
module: {
rules: [
{
test: /\.([cm]?ts|tsx)$/,
exclude: /node_modules/,
use: [
{
loader: "ts-loader",
options: {
2022-12-03 14:20:07 +00:00
configFile: resolve(dirName, "tsconfig.build.json"),
2022-12-01 23:58:56 +00:00
projectReferences: true,
compilerOptions: {
2022-12-03 14:20:07 +00:00
outDir: resolve(dirName, "lib", "temp")
2022-12-01 23:58:56 +00:00
}
}
}
]
}
]
},
experiments: {
outputModule: true
}
}
];
};
// eslint-disable-next-line import/no-default-export
export default generator;