Skip to content

Fix HMR code not being treeshaken #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified __tests__/frameworks/vite/__snapshots__/external.spec.ts.snap
Binary file not shown.
Binary file modified __tests__/frameworks/vite/__snapshots__/sfc.spec.ts.snap
Binary file not shown.
1 change: 1 addition & 0 deletions __tests__/frameworks/vite/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export async function compile(options: InlineConfig, file: string): Promise<stri
const code = modules
.filter(module => 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
Expand Down
15 changes: 10 additions & 5 deletions src/plugins/external-plugin.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -55,6 +55,7 @@ const isFtl = createFilter(['**/*.ftl'])
interface Dependency {
locale: string
ftlPath: string
relativeFtlPath: string
importVariable: string
}

Expand All @@ -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 = normalizePath(relative(dirname(id), ftlPath))

if (ftlExists) {
dependencies.push({
locale,
ftlPath,
relativeFtlPath,
importVariable: `${makeLegalIdentifier(locale)}_ftl`,
})
}
Expand Down Expand Up @@ -139,14 +142,16 @@ 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`)

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.relativeFtlPath}'`).join(', ')}], () => {
${translations.map(({ locale, importVariable }) => `${target}.fluent['${locale}'] = ${importVariable}`).join('\n')}

delete ${target}._fluent
Expand Down