[READ-ONLY] Mirror of https://github.com/danielroe/cross-origin-storage. Load shared dependencies from Cross-Origin Storage (COS).
cross-origin-storage
experimental
nuxt
vite
vite-plugin
1import { execSync } from 'node:child_process'
2import { readFileSync, readdirSync, rmSync } from 'node:fs'
3import { fileURLToPath } from 'node:url'
4import { join } from 'node:path'
5import { beforeAll, describe, expect, it } from 'vitest'
6
7const fixtureDir = fileURLToPath(new URL('./fixtures/basic', import.meta.url))
8const publicNuxt = join(fixtureDir, '.output/public/_nuxt')
9
10function importsOf(file: string): string[] {
11 const code = readFileSync(join(publicNuxt, file), 'utf8')
12 const specifiers = [...code.matchAll(/(?:from|import)\s*["']([^"']+)["']/g)].map(m => m[1]!)
13 return [...new Set(specifiers)]
14}
15
16describe('cos build output', () => {
17 beforeAll(() => {
18 rmSync(join(fixtureDir, '.output'), { recursive: true, force: true })
19 rmSync(join(fixtureDir, '.nuxt'), { recursive: true, force: true })
20 execSync('npx nuxi build', { cwd: fixtureDir, stdio: 'inherit' })
21 }, 240_000)
22
23 it('emits a standalone chunk for every managed vue package', () => {
24 const files = readdirSync(publicNuxt)
25 for (const name of ['vue', 'vue-runtime-dom', 'vue-runtime-core', 'vue-reactivity', 'vue-shared']) {
26 expect(files, `missing ${name}.js`).toContain(`${name}.js`)
27 }
28 })
29
30 it('externalises managed packages instead of inlining them (no duplication)', () => {
31 // If vue were self-contained it would be ~300KB; externalised it is tiny.
32 const vue = readFileSync(join(publicNuxt, 'vue.js'), 'utf8')
33 expect(vue.length).toBeLessThan(5_000)
34 expect(importsOf('vue.js')).toEqual(['coschunk-vue-runtime-dom'])
35 })
36
37 it('keeps the reactivity singleton as a single shared leaf chunk', () => {
38 // @vue/shared is imported by every other vue chunk and imports nothing.
39 expect(importsOf('vue-shared.js')).toEqual([])
40 for (const dependant of ['vue-runtime-dom', 'vue-runtime-core', 'vue-reactivity']) {
41 expect(importsOf(`${dependant}.js`)).toContain('coschunk-vue-shared')
42 }
43 })
44
45 it('leaves no dangling bare vue specifiers in any chunk', () => {
46 for (const file of readdirSync(publicNuxt).filter(f => f.endsWith('.js'))) {
47 const bare = importsOf(file).filter(s => /^(?:vue|@vue\/)/.test(s))
48 expect(bare, `${file} still imports ${bare.join(', ')}`).toEqual([])
49 }
50 })
51})