From f4eb3d9890ddc46d133ec63addbdf30f521c5063 Mon Sep 17 00:00:00 2001 From: Ivan Demchuk Date: Fri, 17 Mar 2023 12:40:55 +0200 Subject: [PATCH 1/5] Fix HMR code not being treeshaken --- src/plugins/external-plugin.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/plugins/external-plugin.ts b/src/plugins/external-plugin.ts index 224231f..d4e51ef 100644 --- a/src/plugins/external-plugin.ts +++ b/src/plugins/external-plugin.ts @@ -143,10 +143,12 @@ export const unplugin = createUnplugin((options: ExternalPluginOptions, meta) => magic.appendLeft(insertPos, `${target}.fluent = ${target}.fluent || {};\n`) for (const dep of translations) magic.appendLeft(insertPos, `${target}.fluent['${dep.locale}'] = ${dep.importVariable}\n`) + + const __HOT_API__ = meta.framework === 'webpack' ? 'import.meta.webpackHot' : 'import.meta.hot' + magic.appendLeft(insertPos, ` -const __HOT_API__ = import.meta.hot || import.meta.webpackHot -if (__HOT_API__) { - __HOT_API__.accept([${translations.map(dep => `'${dep.ftlPath}'`).join(', ')}], () => { +if (${__HOT_API__}) { + ${__HOT_API__}.accept([${translations.map(dep => `'${dep.ftlPath}'`).join(', ')}], () => { ${translations.map(({ locale, importVariable }) => `${target}.fluent['${locale}'] = ${importVariable}`).join('\n')} delete ${target}._fluent From 1afa91c86d54eb213c51253ea9613aaf8ae838a7 Mon Sep 17 00:00:00 2001 From: Ivan Demchuk Date: Wed, 22 Mar 2023 14:48:06 +0200 Subject: [PATCH 2/5] Update snapshots --- .../vite/__snapshots__/external.spec.ts.snap | Bin 10160 -> 10018 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/__tests__/frameworks/vite/__snapshots__/external.spec.ts.snap b/__tests__/frameworks/vite/__snapshots__/external.spec.ts.snap index a7c4cd28778c886632d767abf8c9e8c24bf041cd..682425c48364e3536bcd5863040551b1429a2131 100644 GIT binary patch delta 257 zcmdnszsPUHL^hMmGzE>!+=Bd~6208il0?0X{1Q!tYAyu@G(o+@AX7^TS$ zN~)6=aI$j%**Xd!0e?mvW+1&ejJ=YF>PC9AZN4w{lWeCNfm}8@j9q{8QugDFllf#g KHy5ecF#`a+?o);U delta 381 zcmZ4Fx50nIM7E6N{Ji24h4^?6|B!gc0MGb%1zUy8+=Bd~6208il0?0X{1Szl8Wf@O z)TDyMIzga|yj}-tk;Cxd6 From dcbb41828dcbcea3277bd890582581aa0c65b4b9 Mon Sep 17 00:00:00 2001 From: Ivan Demchuk Date: Wed, 22 Mar 2023 15:05:30 +0200 Subject: [PATCH 3/5] Do not snapshot empty virtual modules --- .../vite/__snapshots__/external.spec.ts.snap | Bin 10018 -> 9608 bytes .../vite/__snapshots__/sfc.spec.ts.snap | Bin 4332 -> 4242 bytes __tests__/frameworks/vite/util.ts | 1 + 3 files changed, 1 insertion(+) diff --git a/__tests__/frameworks/vite/__snapshots__/external.spec.ts.snap b/__tests__/frameworks/vite/__snapshots__/external.spec.ts.snap index 682425c48364e3536bcd5863040551b1429a2131..1432cacb6cf9cfa9de0349eb4fd545dd8b6eb1b5 100644 GIT binary patch delta 61 zcmZ4F*Wtb4Bj@A|imH?MF|tptL delta 305 zcmeD1UgWpoBj@D3V$xy?wzjrhsTBqJMI{O;scDI&IVB3!wVM;f_?bZR&e-L-xPaOe s$})>eN)vOeQgaJRDy_;&Q?V>k6QzDUvimpEb0F>%#ssI20 diff --git a/__tests__/frameworks/vite/__snapshots__/sfc.spec.ts.snap b/__tests__/frameworks/vite/__snapshots__/sfc.spec.ts.snap index 146341d1925846fd3f751d50d8bdb648b0e57f58..54c7a909b96ee1af7452caf40677bb4a3b9d2c26 100644 GIT binary patch delta 21 dcmaE(I7x9sGu!43Y`@tj2XcvTe$V%p5dddf2^atX delta 107 zcmbQF_(pLHCLVvbd6Zb3< module.transform) .filter(module => !module.module.url.includes('node_modules')) + .filter(module => !module.module.url.includes('virtual:empty:')) .map(module => `=== ${module.module.url} ===\n${module.transform.code}`).join('\n\n') // normalize paths From 09a05986995722b532e585fb784f448ae3f31322 Mon Sep 17 00:00:00 2001 From: Ivan Demchuk Date: Wed, 22 Mar 2023 15:09:32 +0200 Subject: [PATCH 4/5] Use relative paths for imports --- src/plugins/external-plugin.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/plugins/external-plugin.ts b/src/plugins/external-plugin.ts index d4e51ef..dbb5f5b 100644 --- a/src/plugins/external-plugin.ts +++ b/src/plugins/external-plugin.ts @@ -1,4 +1,4 @@ -import { join, relative } from 'node:path' +import { dirname, join, relative } from 'node:path' import { stat as fsStat } from 'node:fs/promises' import { createUnplugin } from 'unplugin' @@ -55,6 +55,7 @@ const isFtl = createFilter(['**/*.ftl']) interface Dependency { locale: string ftlPath: string + relativeFtlPath: string importVariable: string } @@ -80,11 +81,13 @@ export const unplugin = createUnplugin((options: ExternalPluginOptions, meta) => for (const locale of options.locales) { const ftlPath = normalizePath(resolvedOptions.getFtlPath(locale, id)) const ftlExists = await fileExists(ftlPath) + const relativeFtlPath = relative(dirname(id), ftlPath) if (ftlExists) { dependencies.push({ locale, ftlPath, + relativeFtlPath, importVariable: `${makeLegalIdentifier(locale)}_ftl`, }) } @@ -139,7 +142,7 @@ export const unplugin = createUnplugin((options: ExternalPluginOptions, meta) => this.addWatchFile(ftlPath) for (const dep of translations) - magic.prepend(`import ${dep.importVariable} from '${dep.ftlPath}';\n`) + magic.prepend(`import ${dep.importVariable} from '${dep.relativeFtlPath}';\n`) magic.appendLeft(insertPos, `${target}.fluent = ${target}.fluent || {};\n`) for (const dep of translations) magic.appendLeft(insertPos, `${target}.fluent['${dep.locale}'] = ${dep.importVariable}\n`) @@ -148,7 +151,7 @@ export const unplugin = createUnplugin((options: ExternalPluginOptions, meta) => magic.appendLeft(insertPos, ` if (${__HOT_API__}) { - ${__HOT_API__}.accept([${translations.map(dep => `'${dep.ftlPath}'`).join(', ')}], () => { + ${__HOT_API__}.accept([${translations.map(dep => `'${dep.relativeFtlPath}'`).join(', ')}], () => { ${translations.map(({ locale, importVariable }) => `${target}.fluent['${locale}'] = ${importVariable}`).join('\n')} delete ${target}._fluent From be66f5f63704d6072831a446fc4b2cb6eaea84dd Mon Sep 17 00:00:00 2001 From: Ivan Demchuk Date: Wed, 22 Mar 2023 15:14:18 +0200 Subject: [PATCH 5/5] Normalize path for Windows --- src/plugins/external-plugin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/external-plugin.ts b/src/plugins/external-plugin.ts index dbb5f5b..4c2cf5a 100644 --- a/src/plugins/external-plugin.ts +++ b/src/plugins/external-plugin.ts @@ -81,7 +81,7 @@ export const unplugin = createUnplugin((options: ExternalPluginOptions, meta) => for (const locale of options.locales) { const ftlPath = normalizePath(resolvedOptions.getFtlPath(locale, id)) const ftlExists = await fileExists(ftlPath) - const relativeFtlPath = relative(dirname(id), ftlPath) + const relativeFtlPath = normalizePath(relative(dirname(id), ftlPath)) if (ftlExists) { dependencies.push({