-
Notifications
You must be signed in to change notification settings - Fork 4
/
webpack.nodeSingleBin.cjs.js
69 lines (63 loc) · 2.71 KB
/
webpack.nodeSingleBin.cjs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { fileURLToPath } from 'url';
import path, { dirname } from 'path';
import webpack from 'webpack';
import nodeExternals from 'webpack-node-externals';
import CopyPlugin from "copy-webpack-plugin";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
export default {
target: "node",
devtool: 'source-map',
entry: {
main: './dist/stated.js'
},
output: {
path: `${__dirname}/dist`, // Use __dirname here
filename: 'bundle-node-single-bin.cjs',
libraryTarget: 'commonjs', // Use 'module' as the library target
publicPath: '/', // Important for correct source map paths
// In sourcemap files, webpack generates sources with URLs like this: webpack://stated-js/dist/src/JsonPointer.ts
// Strangely, webpack's sourcemap loader itself cannot handle these 'webpack://' URLS that it generates for TS files.
// 3rd party lib trying to preserver Stated-js sourcemaps will fail with "Failed to parse source map: 'webpack://stated-js/dist/src/JsonPointer.ts' URL is not supported"
// However, i have this workaround - we just drop the webpack:// crap at the begining of the URL
devtoolModuleFilenameTemplate: info => {
let resourcePath = info.absoluteResourcePath;
const srcIndex = resourcePath.indexOf('/src/');
if (srcIndex !== -1) {
// Extract the part of the path from '/src/' onward
return resourcePath.substring(srcIndex);//path is now relative to, and inside 'dist' with no 'webpack:// cruft'
}
const node_modules_index = resourcePath.indexOf('/node_modules/');
if(node_modules_index !== -1){
return ".."+resourcePath.substring(node_modules_index);
}
return resourcePath;
}
},
resolve: {
extensions: ['.ts', '.tsx', '.js'],
},
plugins: [
// Define global constants
new webpack.DefinePlugin({
BUILD_TARGET: JSON.stringify('node'),
}),
//make sure the .ts files go into dist for sourcemaps
new CopyPlugin({
patterns: [
{
from: 'src/**/*.ts', // Match .ts files in src and subdirectories
to: ({ context, absoluteFilename }) => {
return path.relative(context, absoluteFilename);
},
globOptions: {
ignore: [
'**/test/**', // Exclude the src/test directory and its subdirectories
],
},
noErrorOnMissing: true,
},
],
})
]
};