Skip to content

Commit 7a5edcb

Browse files
authored
Fix HMR code not being treeshaken (#43)
1 parent 7d19621 commit 7a5edcb

File tree

4 files changed

+11
-5
lines changed

4 files changed

+11
-5
lines changed
Binary file not shown.
Binary file not shown.

__tests__/frameworks/vite/util.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export async function compile(options: InlineConfig, file: string): Promise<stri
5555
const code = modules
5656
.filter(module => module.transform)
5757
.filter(module => !module.module.url.includes('node_modules'))
58+
.filter(module => !module.module.url.includes('virtual:empty:'))
5859
.map(module => `=== ${module.module.url} ===\n${module.transform.code}`).join('\n\n')
5960

6061
// normalize paths

src/plugins/external-plugin.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { join, relative } from 'node:path'
1+
import { dirname, join, relative } from 'node:path'
22
import { stat as fsStat } from 'node:fs/promises'
33
import { createUnplugin } from 'unplugin'
44

@@ -55,6 +55,7 @@ const isFtl = createFilter(['**/*.ftl'])
5555
interface Dependency {
5656
locale: string
5757
ftlPath: string
58+
relativeFtlPath: string
5859
importVariable: string
5960
}
6061

@@ -80,11 +81,13 @@ export const unplugin = createUnplugin((options: ExternalPluginOptions, meta) =>
8081
for (const locale of options.locales) {
8182
const ftlPath = normalizePath(resolvedOptions.getFtlPath(locale, id))
8283
const ftlExists = await fileExists(ftlPath)
84+
const relativeFtlPath = normalizePath(relative(dirname(id), ftlPath))
8385

8486
if (ftlExists) {
8587
dependencies.push({
8688
locale,
8789
ftlPath,
90+
relativeFtlPath,
8891
importVariable: `${makeLegalIdentifier(locale)}_ftl`,
8992
})
9093
}
@@ -139,14 +142,16 @@ export const unplugin = createUnplugin((options: ExternalPluginOptions, meta) =>
139142
this.addWatchFile(ftlPath)
140143

141144
for (const dep of translations)
142-
magic.prepend(`import ${dep.importVariable} from '${dep.ftlPath}';\n`)
145+
magic.prepend(`import ${dep.importVariable} from '${dep.relativeFtlPath}';\n`)
143146
magic.appendLeft(insertPos, `${target}.fluent = ${target}.fluent || {};\n`)
144147
for (const dep of translations)
145148
magic.appendLeft(insertPos, `${target}.fluent['${dep.locale}'] = ${dep.importVariable}\n`)
149+
150+
const __HOT_API__ = meta.framework === 'webpack' ? 'import.meta.webpackHot' : 'import.meta.hot'
151+
146152
magic.appendLeft(insertPos, `
147-
const __HOT_API__ = import.meta.hot || import.meta.webpackHot
148-
if (__HOT_API__) {
149-
__HOT_API__.accept([${translations.map(dep => `'${dep.ftlPath}'`).join(', ')}], () => {
153+
if (${__HOT_API__}) {
154+
${__HOT_API__}.accept([${translations.map(dep => `'${dep.relativeFtlPath}'`).join(', ')}], () => {
150155
${translations.map(({ locale, importVariable }) => `${target}.fluent['${locale}'] = ${importVariable}`).join('\n')}
151156
152157
delete ${target}._fluent

0 commit comments

Comments
 (0)