Skip to content

Commit 6d02aea

Browse files
fix: logic for eval-only
1 parent 5c444cb commit 6d02aea

3 files changed

Lines changed: 74 additions & 47 deletions

File tree

lib/NodeStuffPlugin.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,19 @@ class NodeStuffPlugin {
267267
break;
268268
}
269269
case "eval-only":
270-
if (filename === "import.meta.filename") {
270+
// Keep `import.meta.filename` in the source code for the ES module output
271+
if (compilation.outputOptions.module) {
272+
const { importMetaName } = compilation.outputOptions;
273+
274+
parser.hooks.expression
275+
.for(filename)
276+
.tap(
277+
PLUGIN_NAME,
278+
toConstantDependency(parser, `${importMetaName}.filename`)
279+
);
280+
}
281+
// Replace `import.meta.filename` with `__filename` for the non-ES module output
282+
else if (filename === "import.meta.filename") {
271283
parser.hooks.expression
272284
.for(filename)
273285
.tap(
@@ -334,7 +346,19 @@ class NodeStuffPlugin {
334346
break;
335347
}
336348
case "eval-only":
337-
if (dirname === "import.meta.dirname") {
349+
// Keep `import.meta.dirname` in the source code for the ES module output and replace `__dirname` on `import.meta.dirname`
350+
if (compilation.outputOptions.module) {
351+
const { importMetaName } = compilation.outputOptions;
352+
353+
parser.hooks.expression
354+
.for(dirname)
355+
.tap(
356+
PLUGIN_NAME,
357+
toConstantDependency(parser, `${importMetaName}.dirname`)
358+
);
359+
}
360+
// Replace `import.meta.dirname` with `__dirname` for the non-ES module output
361+
else if (dirname === "import.meta.dirname") {
338362
parser.hooks.expression
339363
.for(dirname)
340364
.tap(

test/configCases/node/filename-and-dirname/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ it("should bundle", async () => {
3333
case false:
3434
case "eval-only": {
3535
const dirname = __STATS__.children[__STATS_I__].outputPath;
36-
const filename = path.join(__STATS__.children[__STATS_I__].outputPath, `./bundle${__STATS_I__}.js`);
36+
const filename = path.join(
37+
__STATS__.children[__STATS_I__].outputPath,
38+
FORMAT === "esm" ? `./bundle${__STATS_I__}.mjs` : `./bundle${__STATS_I__}.js`
39+
);
3740

3841
expect(dirnameCommonJS).toBe(dirname);
3942
expect(filenameCommonJS).toBe(filename);

test/configCases/node/filename-and-dirname/webpack.config.js

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,61 +20,61 @@ config.push(
2020
},
2121
plugins: [
2222
new webpack.DefinePlugin({
23-
NODE_VALUE: typeof value === "boolean" ? value : JSON.stringify(value)
23+
NODE_VALUE:
24+
typeof value === "boolean" ? value : JSON.stringify(value),
25+
FORMAT: JSON.stringify("cjs")
2426
})
2527
]
2628
}))
2729
);
2830

2931
// ES modules
3032
config.push(
31-
...values
32-
.filter((item) => item !== "eval-only")
33-
.map((value) => ({
34-
target: "node",
35-
node: {
36-
__filename: value,
37-
__dirname: value
38-
},
39-
output: {
40-
module: true
41-
},
42-
experiments: {
43-
outputModule: true
44-
},
45-
plugins: [
46-
new webpack.DefinePlugin({
47-
NODE_VALUE: typeof value === "boolean" ? value : JSON.stringify(value)
48-
})
49-
]
50-
}))
33+
...values.map((value) => ({
34+
target: "node",
35+
node: {
36+
__filename: value,
37+
__dirname: value
38+
},
39+
output: {
40+
module: true
41+
},
42+
experiments: {
43+
outputModule: true
44+
},
45+
plugins: [
46+
new webpack.DefinePlugin({
47+
NODE_VALUE: typeof value === "boolean" ? value : JSON.stringify(value),
48+
FORMAT: JSON.stringify("esm")
49+
})
50+
]
51+
}))
5152
);
5253

5354
// // ES modules with support `import.meta.dirname` and `import.meta.filename`
5455
config.push(
55-
...values
56-
.filter((item) => item !== "eval-only")
57-
.map((value) => ({
58-
target: "node",
59-
node: {
60-
__filename: value,
61-
__dirname: value
62-
},
63-
output: {
64-
module: true,
65-
environment: {
66-
importMetaDirnameAndFilename: true
67-
}
68-
},
69-
experiments: {
70-
outputModule: true
71-
},
72-
plugins: [
73-
new webpack.DefinePlugin({
74-
NODE_VALUE: typeof value === "boolean" ? value : JSON.stringify(value)
75-
})
76-
]
77-
}))
56+
...values.map((value) => ({
57+
target: "node",
58+
node: {
59+
__filename: value,
60+
__dirname: value
61+
},
62+
output: {
63+
module: true,
64+
environment: {
65+
importMetaDirnameAndFilename: true
66+
}
67+
},
68+
experiments: {
69+
outputModule: true
70+
},
71+
plugins: [
72+
new webpack.DefinePlugin({
73+
NODE_VALUE: typeof value === "boolean" ? value : JSON.stringify(value),
74+
FORMAT: JSON.stringify("esm")
75+
})
76+
]
77+
}))
7878
);
7979

8080
config.push({

0 commit comments

Comments
 (0)