-
-
Notifications
You must be signed in to change notification settings - Fork 603
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: preserve star export from external module
- Loading branch information
Showing
13 changed files
with
307 additions
and
9 deletions.
There are no files selected for viewing
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
File renamed without changes.
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
mod dependency; | ||
mod import_dependency; | ||
mod reexport_star_external_dependency; | ||
|
||
pub use dependency::*; | ||
pub use import_dependency::*; | ||
pub use reexport_star_external_dependency::*; |
111 changes: 111 additions & 0 deletions
111
crates/rspack_plugin_library/src/modern_module/reexport_star_external_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,111 @@ | ||
use rspack_core::{ | ||
AsContextDependency, Dependency, InitFragmentExt, InitFragmentKey, InitFragmentStage, | ||
NormalInitFragment, | ||
}; | ||
use rspack_core::{Compilation, DependencyType, ExternalRequest, ExternalType, 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 ModernModuleReexportStarExternalDependency { | ||
id: DependencyId, | ||
request: Atom, | ||
target_request: ExternalRequest, | ||
external_type: ExternalType, | ||
resource_identifier: String, | ||
} | ||
|
||
impl ModernModuleReexportStarExternalDependency { | ||
pub fn new(request: Atom, target_request: ExternalRequest, external_type: ExternalType) -> Self { | ||
let resource_identifier = create_resource_identifier_for_esm_dependency(request.as_str(), None); | ||
Self { | ||
request, | ||
target_request, | ||
external_type, | ||
id: DependencyId::new(), | ||
resource_identifier, | ||
} | ||
} | ||
} | ||
|
||
impl Dependency for ModernModuleReexportStarExternalDependency { | ||
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 could_affect_referencing_module(&self) -> rspack_core::AffectType { | ||
rspack_core::AffectType::True | ||
} | ||
} | ||
|
||
impl ModuleDependency for ModernModuleReexportStarExternalDependency { | ||
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 ModernModuleReexportStarExternalDependency { | ||
fn apply( | ||
&self, | ||
_source: &mut TemplateReplaceSource, | ||
code_generatable_context: &mut TemplateContext, | ||
) { | ||
let request = match &self.target_request { | ||
ExternalRequest::Single(request) => Some(request), | ||
ExternalRequest::Map(map) => map.get(&self.external_type), | ||
}; | ||
|
||
if let Some(request) = request { | ||
let chunk_init_fragments = code_generatable_context.chunk_init_fragments(); | ||
chunk_init_fragments.push( | ||
NormalInitFragment::new( | ||
format!( | ||
"export * from {};\n", | ||
serde_json::to_string(request.primary()).expect("invalid json to_string") | ||
), | ||
InitFragmentStage::StageESMImports, | ||
0, | ||
InitFragmentKey::Const(format!("modern_module_reexport_star_{}", self.request)), | ||
None, | ||
) | ||
.boxed(), | ||
); | ||
} | ||
} | ||
|
||
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 ModernModuleReexportStarExternalDependency {} |
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
2 changes: 2 additions & 0 deletions
2
packages/rspack-test-tools/tests/configCases/externals/reexport-star/case1.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,2 @@ | ||
export * from 'external1' | ||
export * from 'external2' |
5 changes: 5 additions & 0 deletions
5
packages/rspack-test-tools/tests/configCases/externals/reexport-star/case2.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,5 @@ | ||
import { b } from 'external1' | ||
export * from 'external1' | ||
export { a } from 'external1' | ||
export { b } | ||
export * as n1 from 'external1' |
2 changes: 2 additions & 0 deletions
2
packages/rspack-test-tools/tests/configCases/externals/reexport-star/case3/foo.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,2 @@ | ||
export * from 'external1' | ||
export const foo = 2 |
2 changes: 2 additions & 0 deletions
2
packages/rspack-test-tools/tests/configCases/externals/reexport-star/case3/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,2 @@ | ||
export { foo } from './foo' | ||
export const bar = 1 |
9 changes: 9 additions & 0 deletions
9
packages/rspack-test-tools/tests/configCases/externals/reexport-star/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,9 @@ | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const readCase = (name)=> fs.readFileSync(path.resolve(__dirname, `${name}.mjs`), "utf-8"); | ||
|
||
it("reexport star from external module", function () { | ||
expect(readCase("case1")).toMatchSnapshot(); | ||
expect(readCase("case2")).toMatchSnapshot(); | ||
expect(readCase("case3")).toMatchSnapshot(); | ||
}); |
26 changes: 26 additions & 0 deletions
26
packages/rspack-test-tools/tests/configCases/externals/reexport-star/rspack.config.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,26 @@ | ||
/** @type {import("@rspack/core").Configuration} */ | ||
module.exports = [ | ||
{ | ||
entry: { | ||
"case1": "./case1.js", | ||
"case2": "./case2.js", | ||
"case3": "./case3/index.js", | ||
"index": "./index.js", | ||
}, | ||
output: { | ||
module: true, | ||
filename: "[name].mjs", | ||
chunkFormat: "module", | ||
library: { | ||
type: "modern-module" | ||
}, | ||
}, | ||
externals: { | ||
external1: "module external1-alias", | ||
external2: "module-import external2-alias", | ||
}, | ||
experiments: { | ||
outputModule: true | ||
}, | ||
}, | ||
]; |
Oops, something went wrong.