Skip to content

Commit

Permalink
add import.meta.hot.events
Browse files Browse the repository at this point in the history
  • Loading branch information
dmail committed Nov 26, 2024
1 parent e34dae6 commit 6784321
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 11 deletions.
6 changes: 5 additions & 1 deletion dist/js/autoreload.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { parseSrcSet, stringifySrcSet } from "@jsenv/ast/src/html/html_src_set.js";
import { urlHotMetas } from "./import_meta_hot.js";
import { urlHotMetas, dispatchBeforeFullReload, dispatchBeforePartialReload, dispatchAfterPartialReload, dispatchBeforePrune } from "./import_meta_hot.js";

const initAutoreload = ({ mainFilePath }) => {
const reloader = {
Expand Down Expand Up @@ -55,9 +55,11 @@ const initAutoreload = ({ mainFilePath }) => {
(reloadMessage) => reloadMessage.type === "full",
);
if (someEffectIsFullReload) {
dispatchBeforeFullReload();
reloadHtmlPage();
return;
}
dispatchBeforePartialReload();
reloader.status.goTo("reloading");
const onApplied = (reloadMessage) => {
reloader.changes.remove(reloadMessage);
Expand All @@ -67,6 +69,7 @@ const initAutoreload = ({ mainFilePath }) => {
() => {
onApplied(reloadMessage);
reloader.currentExecution = null;
dispatchAfterPartialReload();
},
(e) => {
reloader.status.goTo("failed");
Expand Down Expand Up @@ -134,6 +137,7 @@ This could be due to syntax errors or importing non-existent modules (see errors
// - import.meta.hot.accept() is not called (happens for HTML and CSS)
if (type === "prune") {
if (urlHotMeta) {
dispatchBeforePrune();
delete urlHotMetas[urlToFetch];
if (urlHotMeta.disposeCallback) {
console.groupCollapsed(
Expand Down
30 changes: 29 additions & 1 deletion dist/js/import_meta_hot.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,40 @@

const urlHotMetas = {};

const createEvent = () => {
const callbackSet = new Set();
const addCallback = (callback) => {
callbackSet.add(callback);
return () => {
callbackSet.delete(callback);
};
};
const dispatch = () => {
for (const callback of callbackSet) {
callback();
}
};
return [{ addCallback }, dispatch];
};
const [beforePartialReload, dispatchBeforePartialReload] = createEvent();
const [afterPartialReload, dispatchAfterPartialReload] = createEvent();
const [beforeFullReload, dispatchBeforeFullReload] = createEvent();
const [beforePrune, dispatchBeforePrune] = createEvent();

const hotEvents = {
beforePartialReload,
afterPartialReload,
beforeFullReload,
beforePrune,
};

const createImportMetaHot = (importMetaUrl) => {
const data = {};
const url = asUrlWithoutHotSearchParam(importMetaUrl);

return {
data,
events: hotEvents,
accept: (firstArg, secondArg) => {
if (!firstArg) {
addUrlMeta(url, {
Expand Down Expand Up @@ -80,4 +108,4 @@ const asUrlWithoutHotSearchParam = (url) => {
return url;
};

export { createImportMetaHot, urlHotMetas };
export { createImportMetaHot, dispatchAfterPartialReload, dispatchBeforeFullReload, dispatchBeforePartialReload, dispatchBeforePrune, urlHotMetas };
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jsenv/core",
"version": "39.5.26",
"version": "39.6.0",
"description": "Tool to develop, test and build js projects",
"license": "MIT",
"author": {
Expand Down
2 changes: 1 addition & 1 deletion packages/related/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jsenv/cli",
"version": "0.2.14",
"version": "0.2.15",
"description": "Command Line Interface for jsenv",
"license": "MIT",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/related/cli/template-node-package/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"devDependencies": {
"@jsenv/assert": "4.4.2",
"@jsenv/core": "39.5.26",
"@jsenv/core": "39.6.0",
"@jsenv/eslint-config-relax": "1.3.4",
"@jsenv/test": "3.5.28",
"eslint": "9.15.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/related/cli/template-web-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"@babel/plugin-syntax-import-attributes": "7.26.0",
"@jsenv/custom-elements-redefine": "0.0.1",
"@jsenv/assert": "4.4.2",
"@jsenv/core": "39.5.26",
"@jsenv/core": "39.6.0",
"@jsenv/plugin-bundling": "2.7.19",
"@jsenv/plugin-minification": "1.5.11",
"@jsenv/eslint-config-relax": "1.3.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/related/cli/template-web-preact/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"@babel/plugin-syntax-import-attributes": "7.26.0",
"@babel/plugin-transform-react-jsx": "7.25.9",
"@jsenv/assert": "4.4.2",
"@jsenv/core": "39.5.26",
"@jsenv/core": "39.6.0",
"@jsenv/plugin-preact": "1.6.28",
"@jsenv/plugin-bundling": "2.7.19",
"@jsenv/plugin-minification": "1.5.11",
Expand Down
2 changes: 1 addition & 1 deletion packages/related/cli/template-web-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@babel/plugin-syntax-import-attributes": "7.26.0",
"@babel/plugin-transform-react-jsx": "7.25.9",
"@jsenv/assert": "4.4.2",
"@jsenv/core": "39.5.26",
"@jsenv/core": "39.6.0",
"@jsenv/plugin-react": "1.5.47",
"@jsenv/plugin-bundling": "2.7.19",
"@jsenv/plugin-minification": "1.5.11",
Expand Down
2 changes: 1 addition & 1 deletion packages/related/cli/template-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"devDependencies": {
"@babel/plugin-syntax-import-attributes": "7.26.0",
"@jsenv/assert": "4.4.2",
"@jsenv/core": "39.5.26",
"@jsenv/core": "39.6.0",
"@jsenv/eslint-config-relax": "1.3.4",
"@jsenv/plugin-bundling": "2.7.19",
"@jsenv/plugin-minification": "1.5.11",
Expand Down
12 changes: 11 additions & 1 deletion src/plugins/autoreload/client/autoreload.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import {
parseSrcSet,
stringifySrcSet,
} from "@jsenv/ast/src/html/html_src_set.js";
import { urlHotMetas } from "../../import_meta_hot/client/import_meta_hot.js";
import {
dispatchAfterPartialReload,
dispatchBeforeFullReload,
dispatchBeforePartialReload,
dispatchBeforePrune,
urlHotMetas,
} from "../../import_meta_hot/client/import_meta_hot.js";

export const initAutoreload = ({ mainFilePath }) => {
let debug = false;
Expand Down Expand Up @@ -62,9 +68,11 @@ export const initAutoreload = ({ mainFilePath }) => {
(reloadMessage) => reloadMessage.type === "full",
);
if (someEffectIsFullReload) {
dispatchBeforeFullReload();
reloadHtmlPage();
return;
}
dispatchBeforePartialReload();
reloader.status.goTo("reloading");
const onApplied = (reloadMessage) => {
reloader.changes.remove(reloadMessage);
Expand All @@ -74,6 +82,7 @@ export const initAutoreload = ({ mainFilePath }) => {
() => {
onApplied(reloadMessage);
reloader.currentExecution = null;
dispatchAfterPartialReload();
},
(e) => {
reloader.status.goTo("failed");
Expand Down Expand Up @@ -141,6 +150,7 @@ This could be due to syntax errors or importing non-existent modules (see errors
// - import.meta.hot.accept() is not called (happens for HTML and CSS)
if (type === "prune") {
if (urlHotMeta) {
dispatchBeforePrune();
delete urlHotMetas[urlToFetch];
if (urlHotMeta.disposeCallback) {
console.groupCollapsed(
Expand Down
34 changes: 34 additions & 0 deletions src/plugins/import_meta_hot/client/import_meta_hot.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,46 @@

export const urlHotMetas = {};

const createEvent = () => {
const callbackSet = new Set();
const addCallback = (callback) => {
callbackSet.add(callback);
return () => {
callbackSet.delete(callback);
};
};
const dispatch = () => {
for (const callback of callbackSet) {
callback();
}
};
return [{ addCallback }, dispatch];
};
const [beforePartialReload, dispatchBeforePartialReload] = createEvent();
const [afterPartialReload, dispatchAfterPartialReload] = createEvent();
const [beforeFullReload, dispatchBeforeFullReload] = createEvent();
const [beforePrune, dispatchBeforePrune] = createEvent();
export {
dispatchAfterPartialReload,
dispatchBeforeFullReload,
dispatchBeforePartialReload,
dispatchBeforePrune,
};

const hotEvents = {
beforePartialReload,
afterPartialReload,
beforeFullReload,
beforePrune,
};

export const createImportMetaHot = (importMetaUrl) => {
const data = {};
const url = asUrlWithoutHotSearchParam(importMetaUrl);

return {
data,
events: hotEvents,
accept: (firstArg, secondArg) => {
if (!firstArg) {
addUrlMeta(url, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ see [./.jsenv/[email protected]/](./.jsenv/[email protected]/)
"meta": {
"url": "window.origin/main.js",
"hot": {
"data": {}
"data": {},
"events": {
"beforePartialReload": {},
"afterPartialReload": {},
"beforeFullReload": {},
"beforePrune": {}
}
}
},
"url": "window.origin/main.js",
Expand Down

0 comments on commit 6784321

Please sign in to comment.