Skip to content

Commit

Permalink
feat: support nodePrefixForCoreModules
Browse files Browse the repository at this point in the history
  • Loading branch information
inottn committed Nov 23, 2024
1 parent 5d8f982 commit a0cce8b
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/get-runner-labels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ on:
jobs:
main:
name: Get Runner Labels
runs-on: [self-hosted, Linux, ci]
runs-on: ubuntu-latest
outputs:
LINUX_RUNNER_LABELS: ${{ steps.run.outputs.LINUX_RUNNER_LABELS }}
MACOS_RUNNER_LABELS: ${{ steps.run.outputs.MACOS_RUNNER_LABELS }}
Expand Down
1 change: 1 addition & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1335,6 +1335,7 @@ export interface RawEntryDynamicResult {
export interface RawEnvironment {
const?: boolean
arrowFunction?: boolean
nodePrefixForCoreModules?: boolean
}

export interface RawEvalDevToolModulePluginOptions {
Expand Down
2 changes: 2 additions & 0 deletions crates/rspack_binding_options/src/options/raw_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ impl From<RawCrossOriginLoading> for CrossOriginLoading {
pub struct RawEnvironment {
pub r#const: Option<bool>,
pub arrow_function: Option<bool>,
pub node_prefix_for_core_modules: Option<bool>,
}

impl From<RawEnvironment> for Environment {
fn from(value: RawEnvironment) -> Self {
Self {
r#const: value.r#const,
arrow_function: value.arrow_function,
node_prefix_for_core_modules: value.node_prefix_for_core_modules,
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions crates/rspack_core/src/external_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,19 @@ impl ExternalModule {
)
}
"node-commonjs" if let Some(request) = request => {
let need_prefix = compilation
.options
.output
.environment
.supports_node_prefix_for_core_modules();

if compilation.options.output.module {
chunk_init_fragments.push(
NormalInitFragment::new(
"import { createRequire as __WEBPACK_EXTERNAL_createRequire } from \"module\";\n"
.to_string(),
format!(
"import {{ createRequire as __WEBPACK_EXTERNAL_createRequire }} from \"{}\";\n",
if need_prefix { "node:module" } else { "module" }
),
InitFragmentStage::StageESMImports,
0,
InitFragmentKey::ModuleExternal("node-commonjs".to_string()),
Expand Down
5 changes: 5 additions & 0 deletions crates/rspack_core/src/options/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ pub struct LibraryCustomUmdObject {
pub struct Environment {
pub r#const: Option<bool>,
pub arrow_function: Option<bool>,
pub node_prefix_for_core_modules: Option<bool>,
}

impl Environment {
Expand All @@ -471,4 +472,8 @@ impl Environment {
pub fn supports_arrow_function(&self) -> bool {
self.arrow_function.unwrap_or_default()
}

pub fn supports_node_prefix_for_core_modules(&self) -> bool {
self.node_prefix_for_core_modules.unwrap_or_default()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,15 @@ impl DependencyTemplate for ExternalModuleDependency {
_source: &mut TemplateReplaceSource,
code_generatable_context: &mut TemplateContext,
) {
let need_prefix = code_generatable_context
.compilation
.options
.output
.environment
.supports_node_prefix_for_core_modules();
let chunk_init_fragments = code_generatable_context.chunk_init_fragments();
let fragment = ExternalModuleInitFragment::new(
self.module.clone(),
format!("{}{}", if need_prefix { "node:" } else { "" }, self.module),
self.import_specifier.clone(),
self.default_import.clone(),
InitFragmentStage::StageConstants,
Expand Down
13 changes: 11 additions & 2 deletions crates/rspack_plugin_javascript/src/plugin/api_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,26 @@ async fn compilation(
#[plugin_hook(JavascriptModulesRenderModuleContent for APIPlugin)]
fn render_module_content(
&self,
_compilation: &Compilation,
compilation: &Compilation,
module: &BoxModule,
_source: &mut RenderSource,
init_fragments: &mut ChunkInitFragments,
) -> Result<()> {
if let Some(build_info) = module.build_info()
&& build_info.need_create_require
{
let need_prefix = compilation
.options
.output
.environment
.supports_node_prefix_for_core_modules();

init_fragments.push(
NormalInitFragment::new(
"import { createRequire as __WEBPACK_EXTERNAL_createRequire } from 'module';\n".to_string(),
format!(
"import {{ createRequire as __WEBPACK_EXTERNAL_createRequire }} from \"{}\";\n",
if need_prefix { "node:module" } else { "module" }
),
InitFragmentStage::StageESMImports,
0,
InitFragmentKey::ModuleExternal("node-commonjs".to_string()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ export { a };
`;
exports[`config config/library/modern-module-force-concaten step should pass: external module should bail out when bundling 1`] = `
import { createRequire as __WEBPACK_EXTERNAL_createRequire } from "module";
import { createRequire as __WEBPACK_EXTERNAL_createRequire } from "node:module";
var __webpack_modules__ = ({
"17": (function (module) {
module.exports = __WEBPACK_EXTERNAL_createRequire(import.meta.url)("path");
Expand Down
16 changes: 16 additions & 0 deletions tests/webpack-test/configCases/node/prefix-in-runtime/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import fs from "fs";

it(`should have/have not 'node:' prefix ${__filename}`, () => {
const content = fs.readFileSync(__filename, "utf-8");

if (/bundle7\.js$/.test(__filename)) {
expect(content).toContain("require(\"fs\");");
} else if (/(bundle1\.mjs|bundle3\.mjs|bundle6\.mjs)$/.test(__filename)) {
expect(content).toContain("from \"url\"");
expect(content).toContain("from \"module\"");
} else {
expect(content).toContain("from \"node:url\"");
expect(content).toContain("from \"node:module\"");
}
});

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function () {
return !process.version.startsWith("v10.");
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/** @type {import("../../../../").Configuration} */
module.exports = [
{
target: "node",
experiments: {
outputModule: true
},
output: {
module: true,
chunkFormat: "module"
}
},
{
target: "node14.17",
experiments: {
outputModule: true
},
output: {
module: true,
chunkFormat: "module"
}
},
{
target: "node14.18",
experiments: {
outputModule: true
},
output: {
module: true,
chunkFormat: "module"
}
},
{
target: "node15",
experiments: {
outputModule: true
},
output: {
module: true,
chunkFormat: "module"
}
},
{
target: "node16",
experiments: {
outputModule: true
},
output: {
module: true,
chunkFormat: "module"
}
},
{
target: "browserslist:node 14.18.0, node 16.0.0",
experiments: {
outputModule: true
},
output: {
module: true,
chunkFormat: "module"
}
},
{
target: "browserslist:node 14.18.0, node 15.0.0, node 16.0.0",
experiments: {
outputModule: true
},
output: {
module: true,
chunkFormat: "module"
}
},
{
target: "node"
}
];

0 comments on commit a0cce8b

Please sign in to comment.