Skip to content

Commit

Permalink
feat: switch the way to get dependecies's paths to be watched
Browse files Browse the repository at this point in the history
  • Loading branch information
zp365238 committed Jan 14, 2024
1 parent bba1061 commit f5ea2d1
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 60 deletions.
1 change: 1 addition & 0 deletions crates/mako/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![feature(box_patterns)]
#![feature(hasher_prefixfree_extras)]
#![feature(let_chains)]
#![feature(result_option_inspect)]

mod analyze_deps;
mod ast;
Expand Down
1 change: 1 addition & 0 deletions crates/mako/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![feature(box_patterns)]
#![feature(let_chains)]
#![feature(result_option_inspect)]

use std::sync::Arc;

Expand Down
15 changes: 11 additions & 4 deletions crates/mako/src/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use mako_core::notify::{self, EventKind, Watcher};
use mako_core::notify_debouncer_full::DebouncedEvent;

use crate::compiler::Compiler;
use crate::resolve::ResolverResource;

pub struct Watch {
pub root: PathBuf,
Expand Down Expand Up @@ -43,11 +44,17 @@ impl Watch {
let module_graph = compiler.context.module_graph.read().unwrap();
let (dependencies, _) = module_graph.toposort();
dependencies
.into_iter()
.iter()
.try_for_each(|module_id| -> anyhow::Result<()> {
let path = Path::new(&module_id.id);
if path.is_file() {
watcher.watch(Path::new(&path), notify::RecursiveMode::NonRecursive)?;
if let Some(module) = module_graph.get_module(module_id) {
if let Some(ResolverResource::Resolved(resource)) = module
.info
.as_ref()
.and_then(|info| info.resolved_resource.as_ref())
{
let path = &resource.0.path;
watcher.watch(Path::new(&path), notify::RecursiveMode::NonRecursive)?;
}
}
Ok(())
})?;
Expand Down
148 changes: 92 additions & 56 deletions scripts/test-hmr.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1070,65 +1070,101 @@ runTest('issue: 861', async () => {
});

runTest('link npm packages', async () => {
await commonTest(
() => {
write(
normalizeFiles({
'/src/index.tsx': `
import React from 'react';
import ReactDOM from "react-dom/client";
import { foo } from "mako-test-package-link";
function App() {
return <div>{foo}<section>{Math.random()}</section></div>;
}
ReactDOM.createRoot(document.getElementById("root")!).render(<App />);
`,
}),
);
writePackage(
'mako-test-package-link',
normalizeFiles({
'package.json': `
{
"name": "mako-test-package-link",
"version": "1.0.0",
"main": "index.js"
}
`,
'index.js': `
export * from './src/index';
`,
'src/index.js': `
const foo = 'foo';
export { foo };
`,
}),
);
execSync(
'cd ./tmp/packages/mako-test-package-link && pnpm link --global',
);
execSync('pnpm link --global mako-test-package-link');
},
(lastResult) => {
assert.equal(lastResult.html, '<div>foo</div>', 'Initial render');
},
() => {
writePackage(
'mako-test-package-link',
normalizeFiles({
'src/index.js': `
const foo = 'bar';
write(
normalizeFiles({
'/src/index.tsx': `
import React from 'react';
import ReactDOM from "react-dom/client";
import { foo } from "mako-test-package-link";
function App() {
return <div>{foo}<section>{Math.random()}</section></div>;
}
ReactDOM.createRoot(document.getElementById("root")!).render(<App />);
`,
}),
);
writePackage(
'mako-test-package-link',
normalizeFiles({
'package.json': `
{
"name": "mako-test-package-link",
"version": "1.0.0",
"main": "index.js"
}
`,
'index.js': `
export * from './src/index';
`,
'src/index.js': `
const foo = 'foo';
export { foo };
`,
}),
);
execSync('cd ./tmp/packages/mako-test-package-link && pnpm link --global');
execSync('pnpm link --global mako-test-package-link');
await startMakoDevServer();
await delay(DELAY_TIME);
const { browser, page } = await startBrowser();
let lastResult;
let thisResult;
let isReload;
lastResult = normalizeHtml(await getRootHtml(page));
assert.equal(lastResult.html, '<div>foo</div>', 'Initial render');

// modify package file content
writePackage(
'mako-test-package-link',
normalizeFiles({
'src/index.js': `
const foo = 'foo2';
export { foo };
`,
}),
);
},
(thisResult) => {
assert.equal(thisResult.html, '<div>bar</div>', 'Second render');
fs.unlinkSync('./node_modules/mako-test-package-link');
},
true,
}),
);
await delay(DELAY_TIME);
thisResult = normalizeHtml(await getRootHtml(page));
assert.equal(thisResult.html, '<div>foo2</div>', 'Second render');
isReload = lastResult.random !== thisResult.random;
assert.equal(isReload, true, 'should reload');
lastResult = thisResult;

// // add files
// writePackage(
// 'mako-test-package-link',
// normalizeFiles({
// 'src/index2.js': `
// const bar = 'bar';
// export { bar };
// `,
// 'index.js': `
// export * from './src/index';
// export * from './src/index2';
// `,
// }),
// );
// write(
// normalizeFiles({
// '/src/index.tsx': `
// import React from 'react';
// import ReactDOM from "react-dom/client";
// import { foo, bar } from "mako-test-package-link";
// function App() {
// return <div>{foo}{bar}<section>{Math.random()}</section></div>;
// }
// ReactDOM.createRoot(document.getElementById("root")!).render(<App />);
// `,
// }),
// );
// await delay(DELAY_TIME);
// thisResult = normalizeHtml(await getRootHtml(page));
// assert.equal(thisResult.html, '<div>foo2bar</div>', 'Third render');
// isReload = lastResult.random !== thisResult.random;
// assert.equal(isReload, true, 'should reload');

await cleanup({ process, browser });
fs.unlinkSync('./node_modules/mako-test-package-link');
});

function normalizeFiles(files, makoConfig = {}) {
Expand Down

0 comments on commit f5ea2d1

Please sign in to comment.