|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { parse } from '@babel/parser'; |
| 3 | +import { vueServerStrategy } from '../frameworks/vue/server/strategy.js'; |
| 4 | +import { astNormalizeModuleImportsAndHelpers } from '../helpers/ast-normalizer.js'; |
| 5 | +import { cleanCode, processCodeForDevice, rewriteImports } from './websocket-device-transform.js'; |
| 6 | + |
| 7 | +describe('Vue package imports in HTTP modules', () => { |
| 8 | + it('keeps native navigation helpers on the runtime bridge through the full transform', () => { |
| 9 | + const source = `import { $navigateTo, ref } from 'nativescript-vue';\nexport const current = ref(null);\nexport function go(page) { $navigateTo(page); }`; |
| 10 | + const cleaned = cleanCode(source, vueServerStrategy); |
| 11 | + const processed = processCodeForDevice(cleaned, false, true, true, '/node_modules/native-router/dist/frame.js'); |
| 12 | + const output = rewriteImports(processed, '/node_modules/native-router/dist/frame.js', new Map(), new Map(), '/', false, undefined, 'http://localhost:5173'); |
| 13 | + expect(output).not.toContain('/@nativescript/vendor.mjs'); |
| 14 | + expect(output).toContain('/ns/rt'); |
| 15 | + expect(() => parse(output, { sourceType: 'module' })).not.toThrow(); |
| 16 | + }); |
| 17 | + |
| 18 | + it.each(['vue-router-ns', 'vue-router-ns_file-routing', 'vue-demi', 'nativescript-vue-router'])('does not redirect the %s prebundle to the Vue runtime', (name) => { |
| 19 | + const url = `/node_modules/.vite/deps/${name}.js?v=abc`; |
| 20 | + const output = astNormalizeModuleImportsAndHelpers(`import { createRouter } from '${url}'; export const router = createRouter();`); |
| 21 | + expect(output).toContain(url); |
| 22 | + expect(output).not.toContain('/ns/rt'); |
| 23 | + }); |
| 24 | + |
| 25 | + it.each(['vue', 'nativescript-vue', 'vue_dist_vue_runtime_esm-bundler'])('still redirects the %s runtime prebundle', (name) => { |
| 26 | + const output = astNormalizeModuleImportsAndHelpers(`import { ref } from '/node_modules/.vite/deps/${name}.js?v=abc'; export const value = ref(0);`); |
| 27 | + expect(output).toContain('/ns/rt'); |
| 28 | + expect(output).not.toContain('/node_modules/.vite/deps/'); |
| 29 | + }); |
| 30 | + |
| 31 | + it('preserves a similarly named bare plugin import', () => { |
| 32 | + const output = astNormalizeModuleImportsAndHelpers(`import { createRouter } from 'nativescript-vue-router'; export const router = createRouter();`); |
| 33 | + expect(output).toContain('nativescript-vue-router'); |
| 34 | + expect(output).not.toContain('/ns/rt'); |
| 35 | + }); |
| 36 | +}); |
0 commit comments