Repository navigation
Expand file tree
/
Copy pathwebpack.config.js
More file actions
93 lines (82 loc) · 2.39 KB
/
Copy pathwebpack.config.js
File metadata and controls
93 lines (82 loc) · 2.39 KB
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
import { resolve } from 'node:path';
import { pathToFileURL } from 'node:url';
import { BUILD_DIR, BUNDLER_CONFIG_PATH, LIB_DIR, testDir } from './config.js';
let config = {};
try {
if (BUNDLER_CONFIG_PATH) {
config = await import(pathToFileURL(resolve(BUNDLER_CONFIG_PATH)).href);
} else if (testDir) {
config = await import(pathToFileURL(resolve(testDir, 'webpack.config.js')).href);
} else {
config = await import(pathToFileURL(resolve('webpack.config.js')).href);
}
if ('default' in config) {
config = config.default;
}
if (!config) {
throw new Error('Bundler configuration is not defined');
}
} catch (error) {
console.error('Error loading user bundler config:', error);
}
export default async (...args) => {
let userConfig = config;
if (typeof config === 'function') {
userConfig = config(...args);
}
// If user config is an array (multi-compiler), use the first config
if (Array.isArray(userConfig)) {
userConfig = userConfig[0] || {};
}
// Override module rules to ensure test files are not processed by user loaders
const userModule = userConfig.module || {};
const userRules = userModule.rules || [];
// Filter out rules that might interfere with test files
const filteredRules = userRules.map(rule => {
// Skip babel-loader and other JS processors for test files
if (rule.test && rule.test.toString().includes('\\.js')) {
return {
...rule,
exclude: [
...(Array.isArray(rule.exclude) ? rule.exclude : rule.exclude ? [rule.exclude] : []),
/\.test\.js$/,
/node_modules[\/\\]ektest/,
]
};
}
return rule;
});
// Add rule for test files - keep them as ES modules with top-level await support
filteredRules.unshift({
test: /\.test\.js$/,
type: 'javascript/esm',
parser: {
javascript: {
dynamicImportMode: 'lazy',
importMeta: true,
}
},
resolve: {
fullySpecified: false,
},
});
return {
...userConfig,
mode: userConfig.mode || 'development',
entry: {
main: resolve(LIB_DIR, 'tests.js'),
},
output: {
path: BUILD_DIR,
filename: 'main.cjs',
assetModuleFilename: '[name][ext]',
publicPath: '/',
clean: true,
},
// Enable top-level await and other modern features
experiments: {
...userConfig.experiments,
topLevelAwait: true,
},
};
};