-
-
Notifications
You must be signed in to change notification settings - Fork 595
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(modern-module): make dynamic import runtime-less (#7759)
- Loading branch information
Showing
15 changed files
with
330 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
crates/rspack_plugin_library/src/modern_module/dependency.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
use rspack_core::{AsContextDependency, Dependency}; | ||
use rspack_core::{ | ||
Compilation, DependencyType, ErrorSpan, ExternalRequest, ExternalType, ImportAttributes, | ||
RealDependencyLocation, RuntimeSpec, | ||
}; | ||
use rspack_core::{DependencyCategory, DependencyId, DependencyTemplate}; | ||
use rspack_core::{ModuleDependency, TemplateContext, TemplateReplaceSource}; | ||
use rspack_plugin_javascript::dependency::create_resource_identifier_for_esm_dependency; | ||
use swc_core::ecma::atoms::Atom; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct ModernModuleImportDependency { | ||
id: DependencyId, | ||
request: Atom, | ||
target_request: ExternalRequest, | ||
external_type: ExternalType, | ||
range: RealDependencyLocation, | ||
attributes: Option<ImportAttributes>, | ||
resource_identifier: String, | ||
} | ||
|
||
impl ModernModuleImportDependency { | ||
pub fn new( | ||
request: Atom, | ||
target_request: ExternalRequest, | ||
external_type: ExternalType, | ||
range: RealDependencyLocation, | ||
attributes: Option<ImportAttributes>, | ||
) -> Self { | ||
let resource_identifier = | ||
create_resource_identifier_for_esm_dependency(request.as_str(), attributes.as_ref()); | ||
Self { | ||
request, | ||
target_request, | ||
external_type, | ||
range, | ||
id: DependencyId::new(), | ||
attributes, | ||
resource_identifier, | ||
} | ||
} | ||
} | ||
|
||
impl Dependency for ModernModuleImportDependency { | ||
fn id(&self) -> &DependencyId { | ||
&self.id | ||
} | ||
|
||
fn resource_identifier(&self) -> Option<&str> { | ||
Some(&self.resource_identifier) | ||
} | ||
|
||
fn category(&self) -> &DependencyCategory { | ||
&DependencyCategory::Esm | ||
} | ||
|
||
fn dependency_type(&self) -> &DependencyType { | ||
&DependencyType::DynamicImport | ||
} | ||
|
||
fn get_attributes(&self) -> Option<&ImportAttributes> { | ||
self.attributes.as_ref() | ||
} | ||
|
||
fn span(&self) -> Option<ErrorSpan> { | ||
Some(ErrorSpan::new(self.range.start, self.range.end)) | ||
} | ||
|
||
fn could_affect_referencing_module(&self) -> rspack_core::AffectType { | ||
rspack_core::AffectType::True | ||
} | ||
} | ||
|
||
impl ModuleDependency for ModernModuleImportDependency { | ||
fn request(&self) -> &str { | ||
&self.request | ||
} | ||
|
||
fn user_request(&self) -> &str { | ||
&self.request | ||
} | ||
|
||
fn set_request(&mut self, request: String) { | ||
self.request = request.into(); | ||
} | ||
} | ||
|
||
impl DependencyTemplate for ModernModuleImportDependency { | ||
fn apply( | ||
&self, | ||
source: &mut TemplateReplaceSource, | ||
_code_generatable_context: &mut TemplateContext, | ||
) { | ||
let request_and_external_type = match &self.target_request { | ||
ExternalRequest::Single(request) => (Some(request), &self.external_type), | ||
ExternalRequest::Map(map) => (map.get(&self.external_type), &self.external_type), | ||
}; | ||
|
||
if let Some(request_and_external_type) = request_and_external_type.0 { | ||
source.replace( | ||
self.range.start, | ||
self.range.end, | ||
format!( | ||
"import({})", | ||
serde_json::to_string(request_and_external_type.primary()) | ||
.expect("invalid json to_string") | ||
) | ||
.as_str(), | ||
None, | ||
); | ||
} | ||
} | ||
|
||
fn dependency_id(&self) -> Option<DependencyId> { | ||
Some(self.id) | ||
} | ||
|
||
fn update_hash( | ||
&self, | ||
_hasher: &mut dyn std::hash::Hasher, | ||
_compilation: &Compilation, | ||
_runtime: Option<&RuntimeSpec>, | ||
) { | ||
} | ||
} | ||
|
||
impl AsContextDependency for ModernModuleImportDependency {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod dependency; | ||
|
||
pub use dependency::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...s/rspack-test-tools/tests/configCases/library/modern-module-dynamic-import-runtime/dyn.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { lit } from 'lit' // 'module' + 'import' externalized | ||
import { svelte } from 'svelte' // 'module' externalized | ||
|
||
export default dynamic = async () => { | ||
const litNs = await import('lit') // 'module' + 'import' externalized | ||
const solidNs = await import('solid') // 'import' externalized | ||
console.log(svelte, lit, litNs, solidNs) | ||
} | ||
|
17 changes: 17 additions & 0 deletions
17
...rspack-test-tools/tests/configCases/library/modern-module-dynamic-import-runtime/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
|
||
it("modern-module-dynamic-import-runtime", () => { | ||
const initialChunk = fs.readFileSync(path.resolve(__dirname, "main.js"), "utf-8"); | ||
const asyncChunk = fs.readFileSync(path.resolve(__dirname, "async.js"), "utf-8"); | ||
|
||
expect(initialChunk).toContain('import * as __WEBPACK_EXTERNAL_MODULE_lit_alias__ from "lit-alias"'); | ||
expect(initialChunk).toContain('import * as __WEBPACK_EXTERNAL_MODULE_svelte_alias__ from "svelte-alias"'); | ||
expect(initialChunk).toContain('import * as __WEBPACK_EXTERNAL_MODULE_react_alias__ from "react-alias"'); | ||
expect(initialChunk).toContain('import * as __WEBPACK_EXTERNAL_MODULE_angular_alias__ from "angular-alias"'); | ||
expect(initialChunk).toContain('const reactNs = await import("react-alias")'); | ||
expect(initialChunk).toContain('const vueNs = await import("vue-alias")'); | ||
|
||
expect(asyncChunk).toContain('const litNs = await import("lit-alias")'); | ||
expect(asyncChunk).toContain('const solidNs = await import("solid-alias")'); | ||
}); |
9 changes: 9 additions & 0 deletions
9
.../rspack-test-tools/tests/configCases/library/modern-module-dynamic-import-runtime/main.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { react } from 'react' // 'module' + 'import' externalized | ||
import { angular } from 'angular' // 'module' externalized | ||
|
||
export const main = async () => { | ||
const dyn = await import('./dyn.js') // lazy dynamic import | ||
const reactNs = await import('react') // 'module' + 'import' externalized | ||
const vueNs = await import('vue') // 'import' externalized | ||
console.log(angular, react, reactNs, vueNs, dyn) | ||
} |
Oops, something went wrong.
d9a6d59
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Benchmark detail: Open
d9a6d59
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Ran ecosystem CI: Open