-
Notifications
You must be signed in to change notification settings - Fork 85
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
feat: support to install chunks before entry loaded #783
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ pub struct OutputConfig { | |
pub es_version: EsVersion, | ||
pub ascii_only: bool, | ||
pub meta: bool, | ||
pub chunk_loading_global: String, | ||
|
||
pub preserve_modules: bool, | ||
pub preserve_modules_root: PathBuf, | ||
|
@@ -314,6 +315,7 @@ const DEFAULT_CONFIG: &str = r#" | |
"esVersion": "es2022", | ||
"meta": false, | ||
"asciiOnly": true, | ||
"chunkLoadingGlobal": "", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 我们是不是也叫 namespace 好一点 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. webpack 的默认值也是 "" 吗? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
webpack 也是叫这个名字:https://webpack.js.org/configuration/output/#outputchunkloadingglobal
不是,Mako 和 webpack 一样默认值是根据配置动态计算的 |
||
"preserveModules": false, | ||
"preserveModulesRoot": "" | ||
}, | ||
|
@@ -403,10 +405,16 @@ impl Config { | |
let mut ret = c.try_deserialize::<Config>(); | ||
// normalize & check | ||
if let Ok(config) = &mut ret { | ||
// normalize output | ||
if config.output.path.is_relative() { | ||
config.output.path = root.join(config.output.path.to_string_lossy().to_string()); | ||
} | ||
|
||
if config.output.chunk_loading_global.is_empty() { | ||
config.output.chunk_loading_global = | ||
get_default_chunk_loading_global(config.umd.clone(), root); | ||
} | ||
|
||
let node_env_config_opt = config.define.get("NODE_ENV"); | ||
if let Some(node_env_config) = node_env_config_opt { | ||
if node_env_config.as_str() != Some(config.mode.to_string().as_str()) { | ||
|
@@ -530,6 +538,28 @@ impl Default for Config { | |
} | ||
} | ||
|
||
fn get_default_chunk_loading_global(umd: String, root: &Path) -> String { | ||
let unique_name = if umd != "none" { | ||
umd | ||
} else { | ||
let pkg_json_path = root.join("package.json"); | ||
let mut pkg_name = "global".to_string(); | ||
|
||
if pkg_json_path.exists() { | ||
let pkg_json = std::fs::read_to_string(pkg_json_path).unwrap(); | ||
let pkg_json: serde_json::Value = serde_json::from_str(&pkg_json).unwrap(); | ||
|
||
if let Some(name) = pkg_json.get("name") { | ||
pkg_name = name.as_str().unwrap().to_string(); | ||
} | ||
} | ||
|
||
pkg_name | ||
}; | ||
|
||
format!("makoChunk_{}", unique_name) | ||
} | ||
|
||
#[derive(Error, Debug)] | ||
pub enum ConfigError { | ||
#[error("define value '{0}' is not an Expression")] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
function createRuntime(makoModules, entryModuleId) { | ||
var global = typeof globalThis !== 'undefined' ? globalThis : self; | ||
var modulesRegistry = {}; | ||
|
||
function requireModule(moduleId) { | ||
|
@@ -287,6 +288,12 @@ function createRuntime(makoModules, entryModuleId) { | |
installedChunks[id] = 0; | ||
} | ||
}; | ||
var chunkLoadingGlobal = global['<%= chunk_loading_global.clone() %>'] = global['<%= chunk_loading_global.clone() %>'] || []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这段感觉也是可以按需载入的,如果没有打包出前置加载的 chunk,应该不需要。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已线下沟通,先不做处理 |
||
chunkLoadingGlobal.forEach(jsonpCallback.bind(null)); | ||
chunkLoadingGlobal.push = (function(push, data) { | ||
push(data); | ||
jsonpCallback(data); | ||
}).bind(null, chunkLoadingGlobal.push.bind(chunkLoadingGlobal)); | ||
<% } %> | ||
|
||
var registerModules = function(modules) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
globalThis['makoChunk_global'] =
赋值是需要前置加载的 chunk 需要的吧,async chunk 应该不需要。这个优化比较小,如果先不做,可以注释里记个 TODO。There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里是打算 async chunk 和 shared chunk 用同一套 wrap,不然还得区分两种 chunk 的 runtime wrap