-
Notifications
You must be signed in to change notification settings - Fork 4
/
webpack.config.cjs.js
94 lines (85 loc) · 3.37 KB
/
webpack.config.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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 {
devtool: 'source-map',
entry: {
main: './dist/src/index.js'
},
output: {
path: `${__dirname}/dist`, // Use __dirname here
filename: 'bundle-common-js.cjs', // Use the '.cjs' extension for 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;
}
},
module: {
rules: [
{
test: /\.js$/,
exclude: [/node_modules/, `${__dirname}/dist/src/FancyLogger.js`, `${__dirname}/dist/src/CliCore.js`, `${__dirname}/dist/src/StatedREPL.js`],
use: {
loader: 'babel-loader', // You can add Babel or other loaders here
},
},
],
},
resolve: {
extensions: ['.ts', '.tsx', '.js'],
fallback: {
util: false,
path: false,
fs: false,
winston: false,
os:false,
buffer:false,
zlib: false,
http: false,
https:false,
}
},
plugins: [
// Define global constants
new webpack.DefinePlugin({
BUILD_TARGET: JSON.stringify('web'),
}),
//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,
},
],
})
],
node: false
};