Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ impl NodeStuffPlugin {
}

/// Get the member replacement value for import.meta.filename/dirname
/// Returns None if the property should be preserved as-is, Some(replacement) otherwise
fn get_import_meta_member_replacement(
parser: &mut JavascriptParser,
property: NodeMetaProperty,
Expand Down Expand Up @@ -331,7 +330,7 @@ impl NodeStuffPlugin {
.unwrap_or(false)
{
// Keep as import.meta.filename/dirname - runtime supports it
return None;
return Some(property.import_meta_name().to_string());
}
Self::add_node_module_dependencies(parser, property);
return Some(property.node_module_runtime_expr().to_string());
Expand Down
1 change: 1 addition & 0 deletions packages/rspack/etc/core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2262,6 +2262,7 @@ export type Environment = {
document?: boolean;
dynamicImport?: boolean;
dynamicImportInWorker?: boolean;
importMetaDirnameAndFilename?: boolean;
forOf?: boolean;
globalThis?: boolean;
methodShorthand?: boolean;
Expand Down
4 changes: 4 additions & 0 deletions packages/rspack/src/config/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,15 @@ function getRawOutputEnvironment(
destructuring: Boolean(environment.destructuring),
document: Boolean(environment.document),
dynamicImport: Boolean(environment.dynamicImport),
dynamicImportInWorker: Boolean(environment.dynamicImportInWorker),
forOf: Boolean(environment.forOf),
globalThis: Boolean(environment.globalThis),
module: Boolean(environment.module),
optionalChaining: Boolean(environment.optionalChaining),
templateLiteral: Boolean(environment.templateLiteral),
importMetaDirnameAndFilename: Boolean(
environment.importMetaDirnameAndFilename,
),
};
}

Expand Down
5 changes: 5 additions & 0 deletions packages/rspack/src/config/browserslistTargetHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@ export const resolve = (
rawChecker({
node: [14, 18],
}),
importMetaDirnameAndFilename:
nodeProperty &&
rawChecker({
node: [22, 16],
}),
require: nodeProperty,
};
};
7 changes: 7 additions & 0 deletions packages/rspack/src/config/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,13 @@ const applyOutputDefaults = (
'nodePrefixForCoreModules',
() => tp && optimistic(tp.nodePrefixForCoreModules),
);
F(
environment,
'importMetaDirnameAndFilename',
() =>
// No optimistic, because it is new
tp?.importMetaDirnameAndFilename,
);
F(environment, 'templateLiteral', () => tp && optimistic(tp.templateLiteral));
F(environment, 'dynamicImport', () =>
conditionallyOptimistic(tp?.dynamicImport, output.module),
Expand Down
7 changes: 7 additions & 0 deletions packages/rspack/src/config/target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export type ApiTargetProperties = {
importScripts: boolean | null;
/** has importScripts available when creating a worker */
importScriptsInWorker: boolean | null;
/** node.js allows to use `import.meta.dirname` and `import.meta.filename` */
importMetaDirnameAndFilename: boolean | null;
/** has fetch function available for WebAssembly */
fetchWasm: boolean | null;
/** has global variable available */
Expand Down Expand Up @@ -235,6 +237,9 @@ You can also more options via the 'target' option: 'browserslist' / 'browserslis
nodeBuiltins: true,
// v16.0.0, v14.18.0
nodePrefixForCoreModules: +major < 15 ? v(14, 18) : v(16),
// Added in: v21.2.0, v20.11.0, but Node.js will output experimental warning, we don't want it
// v24.0.0, v22.16.0 - This property is no longer experimental.
importMetaDirnameAndFilename: v(22, 16),
global: true,
document: false,
fetchWasm: false,
Expand Down Expand Up @@ -281,6 +286,8 @@ You can also more options via the 'target' option: 'browserslist' / 'browserslis
// 15.0.0 - Node.js v16.5
// 14.0.0 - Mode.js v14.17, but prefixes only since v14.18
nodePrefixForCoreModules: v(15),
// 37.0.0 - Node.js v22.16
importMetaDirnameAndFilename: v(37),

require: true,
document: context === 'renderer',
Expand Down
3 changes: 3 additions & 0 deletions packages/rspack/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,9 @@ export type Environment = {
/** The environment supports an async import() when creating a worker, only for web targets at the moment. */
dynamicImportInWorker?: boolean;

/** The environment supports `import.meta.dirname` and `import.meta.filename`. */
importMetaDirnameAndFilename?: boolean;

/** The environment supports 'for of' iteration ('for (const x of array) { ... }'). */
forOf?: boolean;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = {
dynamicImportInWorker: false,
forOf: false,
globalThis: false,
importMetaDirnameAndFilename: false,
methodShorthand: false,
module: false,
nodePrefixForCoreModules: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = {
dynamicImportInWorker: false,
forOf: false,
globalThis: false,
importMetaDirnameAndFilename: false,
methodShorthand: false,
module: false,
nodePrefixForCoreModules: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = {
dynamicImportInWorker: false,
forOf: false,
globalThis: false,
importMetaDirnameAndFilename: false,
methodShorthand: false,
module: false,
nodePrefixForCoreModules: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = {
dynamicImportInWorker: false,
forOf: false,
globalThis: false,
importMetaDirnameAndFilename: false,
methodShorthand: false,
module: false,
nodePrefixForCoreModules: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = {
dynamicImportInWorker: false,
forOf: false,
globalThis: false,
importMetaDirnameAndFilename: false,
methodShorthand: false,
module: false,
nodePrefixForCoreModules: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = {
dynamicImportInWorker: false,
forOf: false,
globalThis: false,
importMetaDirnameAndFilename: false,
methodShorthand: false,
module: false,
nodePrefixForCoreModules: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = {
dynamicImportInWorker: false,
forOf: true,
globalThis: true,
importMetaDirnameAndFilename: false,
methodShorthand: true,
module: true,
nodePrefixForCoreModules: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = {
dynamicImportInWorker: false,
forOf: false,
globalThis: false,
importMetaDirnameAndFilename: false,
methodShorthand: false,
module: false,
nodePrefixForCoreModules: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = {
dynamicImportInWorker: false,
forOf: true,
globalThis: true,
importMetaDirnameAndFilename: false,
methodShorthand: true,
module: true,
nodePrefixForCoreModules: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import path from "path";
import fs from "fs";

it("should use custom name", () => {
expect(__dirname).toBe(__STATS__.outputPath);

const bundleFile = path.join(__STATS__.outputPath, "./bundle0.mjs");

expect(__filename).toBe(bundleFile);

const content = fs.readFileSync(bundleFile, "utf8");

expect(content.includes("custom.dirname")).toBe(true);
expect(content.includes("custom.filename")).toBe(true);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use strict";

/** @type {import("@rspack/core").Configuration} */
module.exports = {
target: "node",
experiments: {
outputModule: true
},
output: {
module: true,
importMetaName: "custom",
environment: {
importMetaDirnameAndFilename: true
}
},
node: {
__filename: "node-module",
__dirname: "node-module"
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use strict";

const path = require("path");
const { pathToFileURL } = require("url");

let counter = 0;

module.exports = {
moduleScope(scope, _stats, options) {
const bundleFilename = path.join(
options.output.path,
`bundle${counter++}.mjs`
);
scope.custom = {
url: pathToFileURL(bundleFilename).toString(),
dirname: path.dirname(bundleFilename),
filename: bundleFilename
};
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ config.push(
}))
);

// // ES modules
// ES modules
config.push(
...values.map((value) => ({
target: "node",
Expand Down
2 changes: 2 additions & 0 deletions tests/rspack-test/defaultsCases/default/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ module.exports = {
dynamicImportInWorker: undefined,
forOf: true,
globalThis: undefined,
importMetaDirnameAndFilename: undefined,
methodShorthand: true,
module: undefined,
nodePrefixForCoreModules: true,
Expand Down Expand Up @@ -337,6 +338,7 @@ module.exports = {
dynamicImportInWorker: undefined,
forOf: true,
globalThis: undefined,
importMetaDirnameAndFilename: undefined,
methodShorthand: true,
module: undefined,
nodePrefixForCoreModules: true,
Expand Down
2 changes: 2 additions & 0 deletions website/docs/en/config/output.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,8 @@ type Environment = {
dynamicImport?: boolean;
/** The environment supports an async import() when creating a worker, only for web targets at the moment. */
dynamicImportInWorker?: boolean;
/** The environment supports `import.meta.dirname` and `import.meta.filename`. */
importMetaDirnameAndFilename?: boolean;
/** The environment supports 'for of' iteration ('for (const x of array) { ... }'). */
forOf?: boolean;
/** The environment supports 'globalThis'. */
Expand Down
2 changes: 2 additions & 0 deletions website/docs/zh/config/output.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,8 @@ type Environment = {
dynamicImport?: boolean;
/** The environment supports an async import() when creating a worker, only for web targets at the moment. */
dynamicImportInWorker?: boolean;
/** The environment supports `import.meta.dirname` and `import.meta.filename`. */
importMetaDirnameAndFilename?: boolean;
/** The environment supports 'for of' iteration ('for (const x of array) { ... }'). */
forOf?: boolean;
/** The environment supports 'globalThis'. */
Expand Down
Loading