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
6 changes: 6 additions & 0 deletions .changeset/brave-snails-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@rspack/binding": patch
"@rspack/core": patch
---

fix: amd should return with iife
2 changes: 2 additions & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,8 @@ export interface RawOutputOptions {
enabledLibraryTypes?: Array<string>
globalObject: string
importFunctionName: string
iife: boolean
module: boolean
}
export interface RawResolveOptions {
preferRelative?: boolean
Expand Down
4 changes: 4 additions & 0 deletions crates/rspack_binding_options/src/options/raw_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ pub struct RawOutputOptions {
pub enabled_library_types: Option<Vec<String>>,
pub global_object: String,
pub import_function_name: String,
pub iife: bool,
pub module: bool,
/* pub entry_filename: Option<String>,
* pub source_map: Option<String>, */
}
Expand All @@ -109,6 +111,8 @@ impl RawOptionsApply for RawOutputOptions {
enabled_library_types: self.enabled_library_types,
global_object: self.global_object,
import_function_name: self.import_function_name,
iife: self.iife,
module: self.module,
})
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/rspack_core/src/options/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub struct OutputOptions {
pub strict_module_error_handling: bool,
pub global_object: String,
pub import_function_name: String,
pub iife: bool,
pub module: bool,
}

pub const NAME_PLACEHOLDER: &str = "[name]";
Expand Down
2 changes: 2 additions & 0 deletions crates/rspack_loader_sass/tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ async fn loader_test(actual: impl AsRef<Path>, expected: impl AsRef<Path>) {
strict_module_error_handling: false,
global_object: "self".to_string(),
import_function_name: "import".to_string(),
iife: true,
module: false,
},
target: rspack_core::Target::new(&vec![String::from("web")]).expect("TODO:"),
resolve: rspack_core::Resolve::default(),
Expand Down
6 changes: 5 additions & 1 deletion crates/rspack_plugin_javascript/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,11 @@ impl JsPlugin {
sources.add(source);
}
}
let final_source = self.render_iife(sources.boxed());
let final_source = if compilation.options.output.iife {
self.render_iife(sources.boxed())
} else {
sources.boxed()
};
if let Some(source) = compilation.plugin_driver.read().await.render(RenderArgs {
compilation,
chunk: &args.chunk_ukey,
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_library/src/amd_library_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl Plugin for AmdLibraryPlugin {
let external_deps_array = external_dep_array(&modules);
let external_arguments = external_arguments(&modules, compilation);
let mut fn_start = format!("function({external_arguments}){{\n");
if !chunk.has_runtime(&compilation.chunk_group_by_ukey) {
if compilation.options.output.iife && !chunk.has_runtime(&compilation.chunk_group_by_ukey) {
fn_start.push_str(" return ");
}
let name = self.normalize_name(&compilation.options.output.library)?;
Expand Down
2 changes: 2 additions & 0 deletions crates/rspack_testing/src/test_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@ impl TestConfig {
strict_module_error_handling: false,
global_object: "self".to_string(),
import_function_name: "import".to_string(),
iife: true,
module: false,
},
mode: c::Mode::from(self.mode),
target: c::Target::new(&self.target).expect("Can't construct target"),
Expand Down
8 changes: 6 additions & 2 deletions packages/rspack/src/config/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ function getRawOutput(output: OutputNormalized): RawOptions["output"] {
!isNil(output.enabledLibraryTypes) &&
!isNil(output.strictModuleErrorHandling) &&
!isNil(output.globalObject) &&
!isNil(output.importFunctionName),
!isNil(output.importFunctionName) &&
!isNil(output.module) &&
!isNil(output.iife),
"fields should not be nil after defaults"
);
return {
Expand All @@ -161,7 +163,9 @@ function getRawOutput(output: OutputNormalized): RawOptions["output"] {
library: output.library && getRawLibrary(output.library),
strictModuleErrorHandling: output.strictModuleErrorHandling,
globalObject: output.globalObject,
importFunctionName: output.importFunctionName
importFunctionName: output.importFunctionName,
iife: output.iife,
module: output.module
};
}

Expand Down
2 changes: 2 additions & 0 deletions packages/rspack/src/config/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ const applyOutputDefaults = (
return "self";
});
D(output, "importFunctionName", "import");
F(output, "iife", () => !output.module);
F(output, "module", () => false); // TODO experiments.outputModule
};

const applyExternalsPresetsDefaults = (
Expand Down
2 changes: 2 additions & 0 deletions packages/rspack/src/config/normalization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export const getNormalizedRspackOptions = (
: ["..."],
globalObject: output.globalObject,
importFunctionName: output.importFunctionName,
iife: output.iife,
module: output.module,
library: libraryBase && {
type:
output.libraryTarget !== undefined
Expand Down
8 changes: 8 additions & 0 deletions packages/rspack/src/config/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -911,12 +911,20 @@ module.exports = {
}
]
},
Iife: {
description:
"Wrap javascript code into IIFE's to avoid leaking into global scope.",
type: "boolean"
},
Output: {
description:
"Options affecting the output of the compilation. `output` options tell rspack how to write the compiled files to disk.",
type: "object",
additionalProperties: false,
properties: {
iife: {
$ref: "#/definitions/Iife"
},
assetModuleFilename: {
$ref: "#/definitions/AssetModuleFilename"
},
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 @@ -128,6 +128,7 @@ export interface Output {
strictModuleErrorHandling?: StrictModuleErrorHandling;
globalObject?: GlobalObject;
importFunctionName?: ImportFunctionName;
iife?: Iife;
}
export type Path = string;
export type PublicPath = "auto" | RawPublicPath;
Expand All @@ -142,6 +143,7 @@ export type UniqueName = string;
export type Library = LibraryName | LibraryOptions;
export type StrictModuleErrorHandling = boolean;
export type OutputModule = boolean;
export type Iife = boolean;
export interface LibraryCustomUmdCommentObject {
amd?: string;
commonjs?: string;
Expand Down Expand Up @@ -204,6 +206,7 @@ export interface OutputNormalized {
strictModuleErrorHandling?: StrictModuleErrorHandling;
globalObject?: GlobalObject;
importFunctionName?: ImportFunctionName;
iife?: Iife;
}

///// Resolve /////
Expand Down
2 changes: 2 additions & 0 deletions packages/rspack/tests/Defaults.unittest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,10 @@ describe("snapshots", () => {
"enabledLibraryTypes": [],
"filename": "[name].js",
"globalObject": "self",
"iife": true,
"importFunctionName": "import",
"library": undefined,
"module": false,
"path": "<cwd>/dist",
"publicPath": "auto",
"strictModuleErrorHandling": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ it("should name define", function () {
var source = fs.readFileSync(__filename, "utf-8");

expect(source).toMatch('define("NamedLibrary",');
expect(source.includes("return __webpack_exports__")).toBe(true);
});