Skip to content

Commit

Permalink
feat: fully support HMR
Browse files Browse the repository at this point in the history
  • Loading branch information
DreamOfIce committed Apr 28, 2023
1 parent 2d785e8 commit 752a2ed
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ parserOptions:
- .vue
plugins:
- "@typescript-eslint"
rules:
"@typescript-eslint/no-non-null-assertion": off
1 change: 0 additions & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
auto-install-peers=true
git-checks=false
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@vuepress/client": "2.0.0-beta.61",
"@vuepress/core": "2.0.0-beta.61",
"@vuepress/utils": "2.0.0-beta.61",
"chokidar": "^3.5.3",
"vue": "^3.2.47",
"vuepress": "2.0.0-beta.61",
"vuepress-shared": "2.0.0-beta.205"
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/client/components/I18nTip.vue
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ const pageData = usePageData<PageData>();
const locale = computed(
() =>
locales[pageData.value.i18n?.pathLocale ?? baseLocalePath] ??
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
locales[baseLocalePath]!
);
const showTips = computed(
Expand Down
39 changes: 38 additions & 1 deletion src/node/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { getDirname, path } from "@vuepress/utils";
import type { App, Plugin } from "@vuepress/core";
import { watch } from "chokidar";
import type { Page } from "../shared/types.js";
import { type I18nPluginOptions, getOptions } from "./options.js";
import { addComponent, getLocales, PLUGIN_NAME } from "./utils.js";
import {
addComponent,
getLocales,
getPageFromDataFilePath,
PLUGIN_NAME,
} from "./utils.js";
import {
addPageData,
fillUntranslatedPages,
Expand Down Expand Up @@ -47,5 +53,36 @@ export const i18nPlugin =
},
onPrepared: async (app) =>
await writeLocales(app, getLocales(app.siteData, locales), options),
onWatched: (app, watcher) => {
const pageWatcher = watch("pages/**/*.js", {
cwd: app.dir.temp(),
ignoreInitial: true,
});
pageWatcher.on("change", (filePath) => {
const page: Page | undefined = getPageFromDataFilePath(app, filePath);
if (page && page.data.i18n?.updatedTime) {
app.pages.forEach((p: Page) => {
if (p.data.i18n?.sourceLink === page.path && p.data.i18n)
p.data.i18n.sourceUpdatedTime = page.data.i18n!.updatedTime!;
});
}
});
// Remove filled page if source has been removed
pageWatcher.on("unlink", (filePath) => {
const { path, pathLocale } =
getPageFromDataFilePath(app, filePath) ?? {};
if (pathLocale === options.baseLocalePath && path) {
app.pages = app.pages.filter(
(page: Page) =>
!(
page.path ===
path.replace(options.baseLocalePath, page.pathLocale) &&
page.data.i18n?.untranslated
)
);
}
});
watcher.push(pageWatcher);
},
};
};
5 changes: 4 additions & 1 deletion src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ const getLocales = (
{},
pluginLocaleData[lang] ??
pluginLocaleData[siteData.lang] ??
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
pluginLocaleData["en-US"]!,
customLocales[lang]
),
])
) as Record<string, I18nPluginLocaleData>;

const getPageFromDataFilePath = (app: App, path: string) =>
app.pages.find((page) => page.dataFilePath === path);

const insertAfterFrontmatter = (content: string, data: string) => {
const regexp = /^---$/gm;
regexp.exec(content);
Expand All @@ -79,6 +81,7 @@ export {
PLUGIN_NAME,
addComponent,
getLocales,
getPageFromDataFilePath,
insertAfterFrontmatter,
logger,
};
8 changes: 7 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ export default defineConfig({
formats: ["es"],
},
rollupOptions: {
external: ["vue", /vuepress-share(\/.+)?/, /@vuepress\/.+/, /@temp\/.+/],
external: [
"chokidar",
"vue",
/vuepress-share(\/.+)?/,
/@vuepress\/.+/,
/@temp\/.+/,
],
},
},
plugins: [
Expand Down

0 comments on commit 752a2ed

Please sign in to comment.