forked from symfony/reprise
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.test.ts
More file actions
114 lines (102 loc) · 4.41 KB
/
Copy pathformat.test.ts
File metadata and controls
114 lines (102 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import type { BuildContext, NormalizedGraph } from '../../src/types';
import { describe, expect, it } from 'vitest';
import { buildEntrypoints, buildManifest } from '../../src/core/format';
const ctx: BuildContext = {
isProd: true,
devServer: null,
publicPath: '/build/',
urlPrefix: '/build/',
manifestKeyPrefix: 'build/',
};
const graph: NormalizedGraph = {
entryPoints: {
app: { js: ['app-a1b2.js'], css: ['app-c3d4.css'], preload: ['vendor-e5f6.js'], dynamic: ['lazy-x.js'] },
admin: { js: ['admin-99.js'], css: [], preload: [], dynamic: [] },
},
assets: [],
};
describe('buildEntrypoints', () => {
it('prefixes every asset list with publicPath', () => {
const out = buildEntrypoints(graph, ctx);
expect(out.entryPoints.app).toEqual({
js: ['/build/app-a1b2.js'],
css: ['/build/app-c3d4.css'],
preload: ['/build/vendor-e5f6.js'],
dynamic: ['/build/lazy-x.js'],
});
});
it('carries the mode/devServer/publicPath fields', () => {
const out = buildEntrypoints(graph, ctx);
expect(out.isProd).toBe(true);
expect(out.devServer).toBeNull();
expect(out.publicPath).toBe('/build/');
});
it('keeps empty arrays for entries without css/preload/dynamic', () => {
const out = buildEntrypoints(graph, ctx);
expect(out.entryPoints.admin).toEqual({ js: ['/build/admin-99.js'], css: [], preload: [], dynamic: [] });
});
it('inserts a slash when urlPrefix has no trailing slash', () => {
const out = buildEntrypoints(graph, { ...ctx, urlPrefix: '/build' });
expect(out.entryPoints.app.js).toEqual(['/build/app-a1b2.js']);
});
it('emits a top-level integrity map keyed by URL when the graph carries hashes', () => {
const out = buildEntrypoints(
{ ...graph, integrity: { 'app-a1b2.js': 'sha384-JS', 'app-c3d4.css': 'sha384-CSS' } },
ctx
);
expect(out.integrity).toEqual({
'/build/app-a1b2.js': 'sha384-JS',
'/build/app-c3d4.css': 'sha384-CSS',
});
});
it('omits integrity when the graph carries no hashes', () => {
expect(buildEntrypoints(graph, ctx).integrity).toBeUndefined();
});
it('builds URLs from urlPrefix but emits the original publicPath field', () => {
const devCtx: BuildContext = {
isProd: false,
devServer: { origin: 'http://127.0.0.1:5173', client: 'vite' },
publicPath: '/build/',
urlPrefix: 'http://127.0.0.1:5173/build/',
manifestKeyPrefix: 'build/',
};
const out = buildEntrypoints(
{ entryPoints: { app: { js: ['assets/app.js'], css: [], preload: [], dynamic: [] } }, assets: [] },
devCtx
);
expect(out.publicPath).toBe('/build/');
expect(out.devServer).toEqual({ origin: 'http://127.0.0.1:5173', client: 'vite' });
expect(out.entryPoints.app.js).toEqual(['http://127.0.0.1:5173/build/assets/app.js']);
});
});
describe('buildManifest', () => {
it('maps logical keys (prefixed) to public URLs, sorted', () => {
const g: NormalizedGraph = {
entryPoints: {},
assets: [
{ logicalName: 'app.js', fileName: 'app-a1b2.js' },
{ logicalName: 'app.css', fileName: 'app-c3d4.css' },
{ logicalName: 'images/logo.png', fileName: 'logo-77.png' },
],
};
expect(buildManifest(g, ctx)).toEqual({
'build/app.css': '/build/app-c3d4.css',
'build/app.js': '/build/app-a1b2.js',
'build/images/logo.png': '/build/logo-77.png',
});
});
it('returns an empty object for no assets', () => {
expect(buildManifest({ entryPoints: {}, assets: [] }, ctx)).toEqual({});
});
it('builds manifest values from urlPrefix, keys from manifestKeyPrefix', () => {
const g: NormalizedGraph = { entryPoints: {}, assets: [{ logicalName: 'app.js', fileName: 'app-a1b2.js' }] };
const devCtx: BuildContext = {
isProd: false,
devServer: { origin: 'http://127.0.0.1:5173', client: 'vite' },
publicPath: '/build/',
urlPrefix: 'http://127.0.0.1:5173/build/',
manifestKeyPrefix: 'build/',
};
expect(buildManifest(g, devCtx)).toEqual({ 'build/app.js': 'http://127.0.0.1:5173/build/app-a1b2.js' });
});
});