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(exernals): add hash to distinguish conflict id #8834

Merged
merged 1 commit into from
Dec 25, 2024
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
17 changes: 15 additions & 2 deletions crates/rspack_core/src/external_module.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::{borrow::Cow, iter};
use std::{borrow::Cow, hash::Hash, iter};

use rspack_cacheable::{cacheable, cacheable_dyn};
use rspack_collections::{Identifiable, Identifier};
use rspack_error::{error, impl_empty_diagnosable_trait, Diagnostic, Result};
use rspack_hash::RspackHash;
use rspack_macros::impl_source_map_config;
use rspack_util::{ext::DynHash, json_stringify, source_map::SourceMapKind};
use rustc_hash::{FxHashMap as HashMap, FxHashSet};
Expand Down Expand Up @@ -334,7 +335,19 @@ impl ExternalModule {
),
"module" if let Some(request) = request => {
if compilation.options.output.module {
let id = to_identifier(&request.primary);
let id: Cow<'_, str> = if to_identifier(&request.primary) != request.primary {
let mut hasher = RspackHash::from(&compilation.options.output);
request.primary.hash(&mut hasher);
let hash_suffix = hasher.digest(&compilation.options.output.hash_digest);
Cow::Owned(format!(
"{}_{}",
to_identifier(&request.primary),
hash_suffix.rendered(8)
))
} else {
to_identifier(&request.primary)
};

chunk_init_fragments.push(
NormalInitFragment::new(
format!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ export * from "external2-alias";
`;

exports[`config config/externals/reexport-star exported tests reexport star from external module 2`] = `
import * as __WEBPACK_EXTERNAL_MODULE_external1_alias__ from "external1-alias";
import * as __WEBPACK_EXTERNAL_MODULE_external1_alias_bc4b12e4__ from "external1-alias";
export * from "external1-alias";

;// CONCATENATED MODULE: external "external1-alias"
Expand All @@ -340,13 +340,13 @@ export * from "external1-alias";



var __webpack_exports__a = __WEBPACK_EXTERNAL_MODULE_external1_alias__.a;
var __webpack_exports__b = __WEBPACK_EXTERNAL_MODULE_external1_alias__.b;
export { __WEBPACK_EXTERNAL_MODULE_external1_alias__ as n1, __webpack_exports__a as a, __webpack_exports__b as b };
var __webpack_exports__a = __WEBPACK_EXTERNAL_MODULE_external1_alias_bc4b12e4__.a;
var __webpack_exports__b = __WEBPACK_EXTERNAL_MODULE_external1_alias_bc4b12e4__.b;
export { __WEBPACK_EXTERNAL_MODULE_external1_alias_bc4b12e4__ as n1, __webpack_exports__a as a, __webpack_exports__b as b };
`;

exports[`config config/externals/reexport-star exported tests reexport star from external module 3`] = `
import * as __WEBPACK_EXTERNAL_MODULE_external1_alias__ from "external1-alias";
import * as __WEBPACK_EXTERNAL_MODULE_external1_alias_bc4b12e4__ from "external1-alias";

;// CONCATENATED MODULE: external "external1-alias"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'foo2'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'foo1'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { foo as foo1 } from './a/foo.mjs'
import { foo as foo2 } from './_a/foo.mjs'

it("foo1 and foo2 should from different namespace import identifier", () => {
expect(foo1).toBe("foo1");
expect(foo2).toBe("foo2");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const rspack = require("@rspack/core");

/** @type {function(any, any): import("@rspack/core").Configuration[]} */
module.exports = (env, { testPath }) => {
return {
externals: [/.*foo.*/],
externalsType: "module",
output: {
module: true,
chunkFormat: "module",
filename: "[name].mjs"
},
experiments: {
outputModule: true
},
optimization: {
minimize: true,
concatenateModules: true,
},
plugins: [
new rspack.CopyRspackPlugin({
patterns: ["./a/**/*", "./_a/**/*"],
}),
]
};
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import("../../../../dist").TConfigCaseConfig} */
module.exports = {
findBundle: (i, options) => {
return ["main.mjs"];
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ 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('import * as __WEBPACK_EXTERNAL_MODULE_lit_alias_9f8ad874__ from "lit-alias"');
expect(initialChunk).toContain('import * as __WEBPACK_EXTERNAL_MODULE_svelte_alias_b2b3c54d__ from "svelte-alias"');
expect(initialChunk).toContain('import * as __WEBPACK_EXTERNAL_MODULE_react_alias_fd8b3826__ from "react-alias"');
expect(initialChunk).toContain('import * as __WEBPACK_EXTERNAL_MODULE_angular_alias_7d25287d__ from "angular-alias"');
expect(initialChunk).toContain('const reactNs = await import("react-alias")');
expect(initialChunk).toContain('const vueNs = await import("vue-alias")');
expect(initialChunk).toContain('const jqueryNs = await import("jquery-alias", { with: {"type":"url"} })');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ module.exports = {
compilation.hooks.afterProcessAssets.tap("testcase", assets => {
const bundle = Object.values(assets)[0]._value
expect(bundle).toContain(`var __webpack_exports__cjsInterop = (foo_default());
var __webpack_exports__defaultImport = __WEBPACK_EXTERNAL_MODULE_external_module__["default"];
var __webpack_exports__namedImport = __WEBPACK_EXTERNAL_MODULE_external_module__.namedImport;`)
var __webpack_exports__defaultImport = __WEBPACK_EXTERNAL_MODULE_external_module_43054e33__["default"];
var __webpack_exports__namedImport = __WEBPACK_EXTERNAL_MODULE_external_module_43054e33__.namedImport;`)
});
};
this.hooks.compilation.tap("testcase", handler);
Expand Down
Loading