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

feat: hot-update 文件生成放到 node_modules 下 #424

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
27 changes: 20 additions & 7 deletions crates/mako/src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,13 +320,13 @@ impl Compiler {
let (code, sourcemap) =
self.generate_hmr_chunk(chunk, &filename, &modified_ids, current_full_hash)?;
// TODO the final format should be {name}.{full_hash}.hot-update.{ext}
self.write_to_dist(&filename, code);
self.write_to_dist(format!("{}.map", &filename), sourcemap);
self.write_to_hot_update_dir(&filename, code);
self.write_to_hot_update_dir(format!("{}.map", &filename), sourcemap);
}
}
let t_generate_hmr_chunk = t_generate_hmr_chunk.elapsed();

self.write_to_dist(
self.write_to_hot_update_dir(
format!("{}.hot-update.json", last_full_hash),
serde_json::to_string(&HotUpdateManifest {
removed_chunks,
Expand Down Expand Up @@ -355,15 +355,28 @@ impl Compiler {
Ok(current_full_hash)
}

pub fn write_to_dist<P: AsRef<std::path::Path>, C: AsRef<[u8]>>(
// pub fn write_to_dist<P: AsRef<std::path::Path>, C: AsRef<[u8]>>(
// &self,
// filename: P,
// content: C,
// ) {
// let to = self.context.config.output.path.join(filename);

// std::fs::write(to, content).unwrap();
// }

pub fn write_to_hot_update_dir<P: AsRef<std::path::Path>, C: AsRef<[u8]>>(
&self,
filename: P,
content: C,
) {
let to = self.context.config.output.path.join(filename);

std::fs::write(to, content).unwrap();
let hmr_dir = self.context.root.join("node_modules/.mako/hot_update");
Copy link
Member

Choose a reason for hiding this comment

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

这里用 node_modules/.mako,是不是 runtime 就不需要调整了,改动可以小一点。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

好像是可以的,我再调整下

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  • 如果用 node_modules/.mako,runtime 还是需要改的:不放到 output 目录下了,得把 requireModule.publicPath 去掉。
  • 我这里还是保留 /hot_update 呢,感觉语义更清晰些

if !hmr_dir.exists() {
fs::create_dir_all(&hmr_dir).unwrap();
}
std::fs::write(hmr_dir.join(filename), content).unwrap();
}

// 写入产物前记录 content 大小, 并加上 hash 值
pub fn write_to_dist_with_stats(&self, file: EmitFile) {
let to: PathBuf = self.context.config.output.path.join(file.hashname.clone());
Expand Down
7 changes: 2 additions & 5 deletions crates/mako/src/runtime/runtime_entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,7 @@ function createRuntime(makoModules, entryModuleId) {
invalidate() {},
check() {
const current_hash = requireModule.currentHash();

return fetch(
`${requireModule.publicPath}${current_hash}.hot-update.json`,
)
return fetch(`/hot_update/${current_hash}.hot-update.json`)
.then((res) => {
return res.json();
})
Expand All @@ -196,7 +193,7 @@ function createRuntime(makoModules, entryModuleId) {
].join('.');

return new Promise((done) => {
const url = `${requireModule.publicPath}${hotChunkName}`;
const url = `hot_update/${hotChunkName}`;
requireModule.loadScript(url, done);
});
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function createRuntime(makoModules, entryModuleId) {
invalidate () {},
check () {
const current_hash = requireModule.currentHash();
return fetch(`${requireModule.publicPath}${current_hash}.hot-update.json`).then((res)=>{
return fetch(`/hot_update/${current_hash}.hot-update.json`).then((res)=>{
Copy link
Member

Choose a reason for hiding this comment

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

这里写死 会不会和用户的路由有冲突。业务有一个 hot_update/:id 的路由。这里可以参考下 umi 在 配置了 publicPath 这些资源是怎么 serve 的

原来 public_path 下 也可能定义 /:id

return res.json();
}).then((update)=>{
return Promise.all(update.c.map((chunk)=>{
Expand All @@ -165,7 +165,7 @@ function createRuntime(makoModules, entryModuleId) {
ext
].join('.');
return new Promise((done)=>{
const url = `${requireModule.publicPath}${hotChunkName}`;
const url = `hot_update/${hotChunkName}`;
requireModule.loadScript(url, done);
});
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function createRuntime(makoModules, entryModuleId) {
invalidate () {},
check () {
const current_hash = requireModule.currentHash();
return fetch(`${requireModule.publicPath}${current_hash}.hot-update.json`).then((res)=>{
return fetch(`/hot_update/${current_hash}.hot-update.json`).then((res)=>{
return res.json();
}).then((update)=>{
return Promise.all(update.c.map((chunk)=>{
Expand All @@ -165,7 +165,7 @@ function createRuntime(makoModules, entryModuleId) {
ext
].join('.');
return new Promise((done)=>{
const url = `${requireModule.publicPath}${hotChunkName}`;
const url = `hot_update/${hotChunkName}`;
requireModule.loadScript(url, done);
});
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ function createRuntime(makoModules, entryModuleId) {
invalidate () {},
check () {
const current_hash = requireModule.currentHash();
return fetch(`${requireModule.publicPath}${current_hash}.hot-update.json`).then((res)=>{
return fetch(`/hot_update/${current_hash}.hot-update.json`).then((res)=>{
return res.json();
}).then((update)=>{
return Promise.all(update.c.map((chunk)=>{
Expand All @@ -166,7 +166,7 @@ function createRuntime(makoModules, entryModuleId) {
ext
].join('.');
return new Promise((done)=>{
const url = `${requireModule.publicPath}${hotChunkName}`;
const url = `hot_update/${hotChunkName}`;
requireModule.loadScript(url, done);
});
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function createRuntime(makoModules, entryModuleId) {
invalidate () {},
check () {
const current_hash = requireModule.currentHash();
return fetch(`${requireModule.publicPath}${current_hash}.hot-update.json`).then((res)=>{
return fetch(`/hot_update/${current_hash}.hot-update.json`).then((res)=>{
return res.json();
}).then((update)=>{
return Promise.all(update.c.map((chunk)=>{
Expand All @@ -165,7 +165,7 @@ function createRuntime(makoModules, entryModuleId) {
ext
].join('.');
return new Promise((done)=>{
const url = `${requireModule.publicPath}${hotChunkName}`;
const url = `hot_update/${hotChunkName}`;
requireModule.loadScript(url, done);
});
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function createRuntime(makoModules, entryModuleId) {
invalidate () {},
check () {
const current_hash = requireModule.currentHash();
return fetch(`${requireModule.publicPath}${current_hash}.hot-update.json`).then((res)=>{
return fetch(`/hot_update/${current_hash}.hot-update.json`).then((res)=>{
return res.json();
}).then((update)=>{
return Promise.all(update.c.map((chunk)=>{
Expand All @@ -165,7 +165,7 @@ function createRuntime(makoModules, entryModuleId) {
ext
].join('.');
return new Promise((done)=>{
const url = `${requireModule.publicPath}${hotChunkName}`;
const url = `hot_update/${hotChunkName}`;
requireModule.loadScript(url, done);
});
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function createRuntime(makoModules, entryModuleId) {
invalidate () {},
check () {
const current_hash = requireModule.currentHash();
return fetch(`${requireModule.publicPath}${current_hash}.hot-update.json`).then((res)=>{
return fetch(`/hot_update/${current_hash}.hot-update.json`).then((res)=>{
return res.json();
}).then((update)=>{
return Promise.all(update.c.map((chunk)=>{
Expand All @@ -165,7 +165,7 @@ function createRuntime(makoModules, entryModuleId) {
ext
].join('.');
return new Promise((done)=>{
const url = `${requireModule.publicPath}${hotChunkName}`;
const url = `hot_update/${hotChunkName}`;
requireModule.loadScript(url, done);
});
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function createRuntime(makoModules, entryModuleId) {
invalidate () {},
check () {
const current_hash = requireModule.currentHash();
return fetch(`${requireModule.publicPath}${current_hash}.hot-update.json`).then((res)=>{
return fetch(`/hot_update/${current_hash}.hot-update.json`).then((res)=>{
return res.json();
}).then((update)=>{
return Promise.all(update.c.map((chunk)=>{
Expand All @@ -165,7 +165,7 @@ function createRuntime(makoModules, entryModuleId) {
ext
].join('.');
return new Promise((done)=>{
const url = `${requireModule.publicPath}${hotChunkName}`;
const url = `hot_update/${hotChunkName}`;
requireModule.loadScript(url, done);
});
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function createRuntime(makoModules, entryModuleId) {
invalidate () {},
check () {
const current_hash = requireModule.currentHash();
return fetch(`${requireModule.publicPath}${current_hash}.hot-update.json`).then((res)=>{
return fetch(`/hot_update/${current_hash}.hot-update.json`).then((res)=>{
return res.json();
}).then((update)=>{
return Promise.all(update.c.map((chunk)=>{
Expand All @@ -165,7 +165,7 @@ function createRuntime(makoModules, entryModuleId) {
ext
].join('.');
return new Promise((done)=>{
const url = `${requireModule.publicPath}${hotChunkName}`;
const url = `hot_update/${hotChunkName}`;
requireModule.loadScript(url, done);
});
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function createRuntime(makoModules, entryModuleId) {
invalidate () {},
check () {
const current_hash = requireModule.currentHash();
return fetch(`${requireModule.publicPath}${current_hash}.hot-update.json`).then((res)=>{
return fetch(`/hot_update/${current_hash}.hot-update.json`).then((res)=>{
return res.json();
}).then((update)=>{
return Promise.all(update.c.map((chunk)=>{
Expand All @@ -165,7 +165,7 @@ function createRuntime(makoModules, entryModuleId) {
ext
].join('.');
return new Promise((done)=>{
const url = `${requireModule.publicPath}${hotChunkName}`;
const url = `hot_update/${hotChunkName}`;
requireModule.loadScript(url, done);
});
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function createRuntime(makoModules, entryModuleId) {
invalidate () {},
check () {
const current_hash = requireModule.currentHash();
return fetch(`${requireModule.publicPath}${current_hash}.hot-update.json`).then((res)=>{
return fetch(`/hot_update/${current_hash}.hot-update.json`).then((res)=>{
return res.json();
}).then((update)=>{
return Promise.all(update.c.map((chunk)=>{
Expand All @@ -165,7 +165,7 @@ function createRuntime(makoModules, entryModuleId) {
ext
].join('.');
return new Promise((done)=>{
const url = `${requireModule.publicPath}${hotChunkName}`;
const url = `hot_update/${hotChunkName}`;
requireModule.loadScript(url, done);
});
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function createRuntime(makoModules, entryModuleId) {
invalidate () {},
check () {
const current_hash = requireModule.currentHash();
return fetch(`${requireModule.publicPath}${current_hash}.hot-update.json`).then((res)=>{
return fetch(`/hot_update/${current_hash}.hot-update.json`).then((res)=>{
return res.json();
}).then((update)=>{
return Promise.all(update.c.map((chunk)=>{
Expand All @@ -165,7 +165,7 @@ function createRuntime(makoModules, entryModuleId) {
ext
].join('.');
return new Promise((done)=>{
const url = `${requireModule.publicPath}${hotChunkName}`;
const url = `hot_update/${hotChunkName}`;
requireModule.loadScript(url, done);
});
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function createRuntime(makoModules, entryModuleId) {
invalidate () {},
check () {
const current_hash = requireModule.currentHash();
return fetch(`${requireModule.publicPath}${current_hash}.hot-update.json`).then((res)=>{
return fetch(`/hot_update/${current_hash}.hot-update.json`).then((res)=>{
return res.json();
}).then((update)=>{
return Promise.all(update.c.map((chunk)=>{
Expand All @@ -165,7 +165,7 @@ function createRuntime(makoModules, entryModuleId) {
ext
].join('.');
return new Promise((done)=>{
const url = `${requireModule.publicPath}${hotChunkName}`;
const url = `hot_update/${hotChunkName}`;
requireModule.loadScript(url, done);
});
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function createRuntime(makoModules, entryModuleId) {
invalidate () {},
check () {
const current_hash = requireModule.currentHash();
return fetch(`${requireModule.publicPath}${current_hash}.hot-update.json`).then((res)=>{
return fetch(`/hot_update/${current_hash}.hot-update.json`).then((res)=>{
return res.json();
}).then((update)=>{
return Promise.all(update.c.map((chunk)=>{
Expand All @@ -165,7 +165,7 @@ function createRuntime(makoModules, entryModuleId) {
ext
].join('.');
return new Promise((done)=>{
const url = `${requireModule.publicPath}${hotChunkName}`;
const url = `hot_update/${hotChunkName}`;
requireModule.loadScript(url, done);
});
}));
Expand Down
6 changes: 5 additions & 1 deletion crates/node/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ alias?: Record<string, string>;
extensions?: string[];
};
manifest?: boolean;
manifest_config?: {file_name: string; base_path: string;};
manifest_config?: {
file_name: string;
base_path: string;
};
mode?: "development" | "production";
define?: Record<string, string>;
devtool?: "source-map" | "inline-source-map" | "none";
externals?: Record<string, string>;
copy?: string[];
code_splitting: "bigVendors" | "depPerChunk" | "none";
providers?: Record<string, string[]>;
public_path?: string;
inline_limit?: number;
Expand Down
1 change: 1 addition & 0 deletions packages/bundler-okam/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ exports.dev = async function (opts) {
// before middlewares
(opts.beforeMiddlewares || []).forEach((m) => app.use(m));
// serve dist files
app.use(express.static(path.join(opts.cwd, 'node_modules/.mako')));
app.use(express.static(path.join(opts.cwd, 'dist')));
// TODO: proxy
// opts.config.proxy
Expand Down
Loading