Skip to content

Commit

Permalink
fix: commonjs-static libraryType
Browse files Browse the repository at this point in the history
  • Loading branch information
hai-x authored and hai-x committed Feb 25, 2025
1 parent ba7556d commit 22a212a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 19 deletions.
21 changes: 20 additions & 1 deletion crates/rspack_plugin_library/src/assign_library_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::sync::LazyLock;
use regex::Regex;
use rspack_collections::DatabaseItem;
use rspack_core::rspack_sources::SourceExt;
use rspack_core::ExportInfoProvided;
use rspack_core::{
get_entry_runtime, property_access, ApplyContext, BoxModule, ChunkUkey,
CodeGenerationDataTopLevelDeclarations, CompilationAdditionalChunkRuntimeRequirements,
Expand Down Expand Up @@ -242,7 +243,7 @@ fn render_startup(
&self,
compilation: &Compilation,
chunk_ukey: &ChunkUkey,
_module: &ModuleIdentifier,
module: &ModuleIdentifier,
render_source: &mut RenderSource,
) -> Result<()> {
let Some(options) = self.get_options_for_chunk(compilation, chunk_ukey)? else {
Expand All @@ -258,6 +259,24 @@ fn render_startup(
.unwrap_or_default();
if matches!(self.options.unnamed, Unnamed::Static) {
let export_target = access_with_init(&full_name_resolved, self.options.prefix.len(), true);
let module_graph = compilation.get_module_graph();
let exports_info = module_graph.get_exports_info(module);
for export_info in exports_info.ordered_exports(&module_graph) {
if matches!(
export_info.provided(&module_graph),
Some(ExportInfoProvided::False)
) {
continue;
}
let export_info_name = export_info
.name(&module_graph)
.expect("should have name")
.to_string();
let name_access = property_access([export_info_name], 0);
source.add(RawStringSource::from(format!(
"{export_target}{name_access} = __webpack_exports__{export_access}{name_access};\n"
)));
}
source.add(RawStringSource::from(format!(
"Object.defineProperty({export_target}, '__esModule', {{ value: true }});\n",
)));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const fs = require("fs")
export const foo1 = () => {}
export const foo2 = () => {}
const bar = "bar";
export default bar

it("should success compile and work",()=>{
const output = fs.readFileSync(__filename).toString();
expect(output.match(/exports(\[|\.)/g).length).toBe(3)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** @type {import("@rspack/core").Configuration} */
module.exports = {
target: "node",
output: {
library: { type: "commonjs-static" }
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -314,24 +314,23 @@ module.exports = (env, { testPath }) => [
})
]
},
// TODO: amd esm import exports presence check
// {
// resolve: {
// alias: {
// library: path.resolve(
// testPath,
// "../0-create-library/commonjs-static-external.js"
// ),
// external: path.resolve(__dirname, "node_modules/external.js")
// }
// },
// plugins: [
// new webpack.DefinePlugin({
// NAME: JSON.stringify("commonjs-static with external"),
// TEST_EXTERNAL: true
// })
// ]
// },
{
resolve: {
alias: {
library: path.resolve(
testPath,
"../0-create-library/commonjs-static-external.js"
),
external: path.resolve(__dirname, "node_modules/external.js")
}
},
plugins: [
new webpack.DefinePlugin({
NAME: JSON.stringify("commonjs-static with external"),
TEST_EXTERNAL: true
})
]
},
{
resolve: {
alias: {
Expand Down

0 comments on commit 22a212a

Please sign in to comment.