Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: commonjs static library mode #9390

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
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
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
10 changes: 10 additions & 0 deletions tests/webpack-test/configCases/library/cjs-static/index.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this test to packages/rspack-test-tools/tests/configCases

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, webpack/webpack#19242 is trying to add some test into webpack. We could wait for a while to let it merged into webpack first. Or we could move it to rspack test if there's no response for a while.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think both are fine, what do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's see if it's merged tomorrow.

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("../../../../").Configuration} */
module.exports = {
target: "node",
output: {
library: { type: "commonjs-static" }
}
};
Loading