diff --git a/crates/mako/src/generate/chunk_pot/util.rs b/crates/mako/src/generate/chunk_pot/util.rs index 8563e8073..f55962d17 100644 --- a/crates/mako/src/generate/chunk_pot/util.rs +++ b/crates/mako/src/generate/chunk_pot/util.rs @@ -105,6 +105,11 @@ pub(crate) fn runtime_code(context: &Arc) -> Result { cjs: context.config.cjs, chunk_loading_global: context.config.output.chunk_loading_global.clone(), pkg_name: get_pkg_name(&context.root), + concatenate_enabled: context + .config + .optimization + .as_ref() + .map_or(false, |o| o.concatenate_modules.unwrap_or(false)), }; let app_runtime = app_runtime.render_once()?; let app_runtime = app_runtime.replace( diff --git a/crates/mako/src/generate/runtime.rs b/crates/mako/src/generate/runtime.rs index 24e54444b..536875e80 100644 --- a/crates/mako/src/generate/runtime.rs +++ b/crates/mako/src/generate/runtime.rs @@ -11,4 +11,5 @@ pub struct AppRuntimeTemplate { pub pkg_name: Option, pub chunk_loading_global: String, pub is_browser: bool, + pub concatenate_enabled: bool, } diff --git a/crates/mako/src/plugins/farm_tree_shake/shake/module_concatenate/root_transformer.rs b/crates/mako/src/plugins/farm_tree_shake/shake/module_concatenate/root_transformer.rs index 75f4fba8e..ea0225afa 100644 --- a/crates/mako/src/plugins/farm_tree_shake/shake/module_concatenate/root_transformer.rs +++ b/crates/mako/src/plugins/farm_tree_shake/shake/module_concatenate/root_transformer.rs @@ -218,7 +218,7 @@ impl<'a> VisitMut for RootTransformer<'a> { } } - let define_exports: Stmt = member_expr!(DUMMY_SP, __mako_require__.e) + let define_exports: Stmt = member_expr!(DUMMY_SP, __mako_require__.es) .as_call( DUMMY_SP, vec![ diff --git a/crates/mako/templates/app_runtime.stpl b/crates/mako/templates/app_runtime.stpl index 12edcc080..c18d9627a 100644 --- a/crates/mako/templates/app_runtime.stpl +++ b/crates/mako/templates/app_runtime.stpl @@ -58,6 +58,19 @@ function createRuntime(makoModules, entryModuleId, global) { get: all[name], }); }; +<% if concatenate_enabled { %> + // Export Star util for concatenated modules + requireModule.es = function(to, from) { + Object.keys(from).forEach(function(k) { + if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) { + Object.defineProperty(to, k, { + enumerable: true, + get: from[k] + }); + } + }); + }; +<% } %> requireModule.d = Object.defineProperty.bind(Object); <% if has_dynamic_chunks || has_hmr { %> diff --git a/e2e/fixtures/mako.scope-hoisting.export_conflict_with_root/expect.js b/e2e/fixtures/mako.scope-hoisting.export_conflict_with_root/expect.js new file mode 100644 index 000000000..c53bb588e --- /dev/null +++ b/e2e/fixtures/mako.scope-hoisting.export_conflict_with_root/expect.js @@ -0,0 +1,11 @@ +const { + injectSimpleJest, + parseBuildResult, + moduleDefinitionOf, +} = require("../../../scripts/test-utils"); +const { files } = parseBuildResult(__dirname); +injectSimpleJest(); + +expect(files["index.js"]).not.toContain(moduleDefinitionOf("inner.js")); + +require("./dist/index.js"); diff --git a/e2e/fixtures/mako.scope-hoisting.export_conflict_with_root/ext.js b/e2e/fixtures/mako.scope-hoisting.export_conflict_with_root/ext.js new file mode 100644 index 000000000..28096318e --- /dev/null +++ b/e2e/fixtures/mako.scope-hoisting.export_conflict_with_root/ext.js @@ -0,0 +1,5 @@ +export const ext = "ext"; + +export const value = "ext"; + +export default "ext-default"; diff --git a/e2e/fixtures/mako.scope-hoisting.export_conflict_with_root/index.js b/e2e/fixtures/mako.scope-hoisting.export_conflict_with_root/index.js new file mode 100644 index 000000000..edf2eb8f7 --- /dev/null +++ b/e2e/fixtures/mako.scope-hoisting.export_conflict_with_root/index.js @@ -0,0 +1,10 @@ +const root = require("./root.js"); +require("./ext"); + +it("skip conflict exports from inner", () => { + expect(root).toStrictEqual({ + ext: "ext", + value: "root", + notConflict: "inner-not-conflict", + }); +}); diff --git a/e2e/fixtures/mako.scope-hoisting.export_conflict_with_root/inner.js b/e2e/fixtures/mako.scope-hoisting.export_conflict_with_root/inner.js new file mode 100644 index 000000000..b8315df61 --- /dev/null +++ b/e2e/fixtures/mako.scope-hoisting.export_conflict_with_root/inner.js @@ -0,0 +1,4 @@ +export const value = "inner"; +export const notConflict = "inner-not-conflict"; + +export default "inner-default"; diff --git a/e2e/fixtures/mako.scope-hoisting.export_conflict_with_root/mako.config.json b/e2e/fixtures/mako.scope-hoisting.export_conflict_with_root/mako.config.json new file mode 100644 index 000000000..be2f82ff9 --- /dev/null +++ b/e2e/fixtures/mako.scope-hoisting.export_conflict_with_root/mako.config.json @@ -0,0 +1,9 @@ +{ + "entry": { + "index": "index.js" + }, + "optimization": { + "skipModules": true, + "concatenateModules": true + } +} diff --git a/e2e/fixtures/mako.scope-hoisting.export_conflict_with_root/root.js b/e2e/fixtures/mako.scope-hoisting.export_conflict_with_root/root.js new file mode 100644 index 000000000..3cbc5cc1b --- /dev/null +++ b/e2e/fixtures/mako.scope-hoisting.export_conflict_with_root/root.js @@ -0,0 +1,3 @@ +export * from "./inner"; +export * from "./ext"; +export const value = "root";