| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,5 @@ | |||
| 1 | 1 | import { describe, expect, it } from 'vitest'; | |
| 2 | - import { transformDynamicImports } from './dynamic-import-plugin.js'; | ||
| 2 | + import { dynamicImportPlugin, transformDynamicImports } from './dynamic-import-plugin.js'; | ||
| 3 | 3 | ||
| 4 | 4 | const vitePreloadHelper = `const scriptRel = /* @__PURE__ */ (function detectScriptRel() { | |
| 5 | 5 | const relList = typeof document !== "undefined" && document.createElement("link").relList; | |
@@ -37,4 +37,23 @@ describe('dynamic import plugin', () => { | |||
| 37 | 37 | const source = 'export const value = 42;'; | |
| 38 | 38 | expect(transformDynamicImports(source)).toBe(source); | |
| 39 | 39 | }); | |
| 40 | + | ||
| 41 | + // Minified builds rename __vitePreload before generateBundle; the browser | ||
| 42 | + // helper then survived and its window.dispatchEvent masked every dynamic | ||
| 43 | + // import failure as "window is not defined". | ||
| 44 | + it('replaces the helper in renderChunk as a post plugin, ahead of esbuild minification', () => { | ||
| 45 | + const plugin = dynamicImportPlugin() as any; | ||
| 46 | + expect(plugin.enforce).toBe('post'); | ||
| 47 | + expect(plugin.generateBundle).toBeUndefined(); | ||
| 48 | + const result = plugin.renderChunk(`${vitePreloadHelper}\nconst page = () => __vitePreload(() => import('./page.js'), []);`); | ||
| 49 | + expect(result.code).not.toContain('document'); | ||
| 50 | + expect(result.code).toContain("import('~/page.js')"); | ||
| 51 | + }); | ||
| 52 | + | ||
| 53 | + it('returns null from renderChunk when nothing changes, including on already-transformed code', () => { | ||
| 54 | + const plugin = dynamicImportPlugin() as any; | ||
| 55 | + expect(plugin.renderChunk('export const value = 42;')).toBeNull(); | ||
| 56 | + const once = transformDynamicImports(`${vitePreloadHelper}\nexport const page = () => __vitePreload(() => import('./page.js'), []);`); | ||
| 57 | + expect(plugin.renderChunk(once)).toBeNull(); | ||
| 58 | + }); | ||
| 40 | 59 | }); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -62,15 +62,18 @@ export function transformDynamicImports(code: string) { | |||
| 62 | 62 | // Fix NativeScript dynamic imports by transforming paths and simplifying __vitePreload. | |
| 63 | 63 | // Vite still emits its browser preload helper when modulePreload is disabled: | |
| 64 | 64 | // https://github.com/vitejs/vite/issues/13952 | |
| 65 | + // Runs in renderChunk as a post plugin: user post plugins precede Vite's | ||
| 66 | + // esbuild minifier, so `__vitePreload` is still named when we look for it. | ||
| 65 | 67 | export function dynamicImportPlugin() { | |
| 66 | 68 | return { | |
| 67 | 69 | name: 'nativescript-dynamic-import-fix', | |
| 68 | - generateBundle(_options, bundle) { | ||
| 69 | - for (const chunk of Object.values(bundle) as any) { | ||
| 70 | - if (chunk.type === 'chunk') { | ||
| 71 | - chunk.code = transformDynamicImports(chunk.code); | ||
| 72 | - } | ||
| 70 | + enforce: 'post' as const, | ||
| 71 | + renderChunk(code: string) { | ||
| 72 | + const transformed = transformDynamicImports(code); | ||
| 73 | + if (transformed === code) { | ||
| 74 | + return null; | ||
| 73 | 75 | } | |
| 76 | + return { code: transformed, map: null }; | ||
| 74 | 77 | }, | |
| 75 | 78 | }; | |
| 76 | 79 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments