Skip to content

Commit

Permalink
feat: platform node support code splitting (#887)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc authored Jan 22, 2024
1 parent add11f1 commit f8fa3a9
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/mako/src/chunk_pot/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ pub(crate) fn runtime_code(context: &Arc<Context>) -> Result<String> {
has_dynamic_chunks,
has_hmr,
umd,
is_browser: matches!(context.config.platform, crate::config::Platform::Browser),
cjs: context.config.cjs,
chunk_loading_global: context.config.output.chunk_loading_global.clone(),
};
Expand Down
1 change: 1 addition & 0 deletions crates/mako/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ pub struct AppRuntimeTemplate {
pub umd: Option<String>,
pub cjs: bool,
pub chunk_loading_global: String,
pub is_browser: bool,
}
9 changes: 9 additions & 0 deletions crates/mako/templates/app_runtime.stpl
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ function createRuntime(makoModules, entryModuleId) {
requireModule.jsonpInstalled = {};
var installedChunks = requireModule.jsonpInstalled;

<% if is_browser { %>
requireModule.chunkEnsures.jsonp = function (chunkId, promises) {
var data = installedChunks[chunkId];
if (data === 0) return;
Expand Down Expand Up @@ -125,6 +126,14 @@ function createRuntime(makoModules, entryModuleId) {
return promise;
}
};
<% } else { %>
requireModule.chunkEnsures.require = (chunkId, promises) => {
if(!installedChunks[chunkId]) {
require("./" + chunksIdToUrlMap[chunkId]);
installedChunks[chunkId] = true;
}
};
<% } %>
})();
// chunk and async load

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const assert = require("assert");
const { parseBuildResult, trim, moduleReg } = require("../../../scripts/test-utils");
const { files } = parseBuildResult(__dirname);

module.exports = async () => {
const result = await require('./dist').bar();
assert(result === 'foo_bar', `should support code splitting`);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"platform": "node",
"cjs": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'foo';
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

export async function bar() {
const foo = await import('./foo');
return foo.foo + '_bar';
}

0 comments on commit f8fa3a9

Please sign in to comment.