-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.shared.js
More file actions
100 lines (86 loc) · 2.94 KB
/
Copy pathvite.shared.js
File metadata and controls
100 lines (86 loc) · 2.94 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
import { readdir, readFile } from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath, URL } from 'node:url';
export function cmsProjectRoot(importMetaUrl) {
return fileURLToPath(new URL('../../..', importMetaUrl));
}
export function cmsModuleRoot(importMetaUrl) {
return fileURLToPath(new URL('.', importMetaUrl));
}
export function vueforgeAliases(importMetaUrl) {
const vendorRoot = fileURLToPath(
new URL('../../../node_modules/@codemonster-ru/', new URL('.', importMetaUrl)),
);
return [
{
find: /^@codemonster-ru\/vueforge-core$/,
replacement: `${vendorRoot}/vueforge-core/dist/vueforge-core.js`,
},
{
find: /^@codemonster-ru\/vueforge-core\/foundation$/,
replacement: `${vendorRoot}/vueforge-core/dist/foundation-api.js`,
},
{
find: /^@codemonster-ru\/vueforge-core\/(.+)$/,
replacement: `${vendorRoot}/vueforge-core/dist/auto/$1.js`,
},
{
find: /^@codemonster-ru\/vueforge-layouts$/,
replacement: `${vendorRoot}/vueforge-layouts/dist/index.js`,
},
{
find: /^@codemonster-ru\/vueforge-layouts\/(.+)$/,
replacement: `${vendorRoot}/vueforge-layouts/dist/auto/$1.js`,
},
{
find: /^@codemonster-ru\/vueforge-theme$/,
replacement: `${vendorRoot}/vueforge-theme/dist/index.js`,
},
{
find: /^@codemonster-ru\/vueforge-icons$/,
replacement: `${vendorRoot}/vueforge-icons/dist/index.ts.mjs`,
},
{
find: /^@codemonster-ru\/floater\.js$/,
replacement: `${vendorRoot}/floater.js/dist/index.mjs`,
},
];
}
export function verifyViteBundlePlugin({ name, assetsRoot, entrypoint }) {
return {
name: `verify-${name}-bundle`,
apply: 'build',
async writeBundle() {
const manifestPath = path.join(assetsRoot, '.vite/manifest.json');
const manifest = JSON.parse(await readFile(manifestPath, 'utf8'));
const entry = manifest[entrypoint];
if (!entry || typeof entry.file !== 'string') {
throw new Error(`${name} Vite manifest is missing ${entrypoint}.`);
}
for (const bundlePath of await listJavaScriptBundles(assetsRoot)) {
const bundleContents = await readFile(bundlePath, 'utf8');
if (bundleContents.includes('@codemonster-ru/')) {
throw new Error(
`${name} bundle contains unresolved bare imports: ${path.relative(assetsRoot, bundlePath)}`,
);
}
}
console.log(`Verified ${name} bundle: ${entry.file}`);
},
};
}
async function listJavaScriptBundles(directory) {
const entries = await readdir(directory, { withFileTypes: true });
const bundles = [];
for (const entry of entries) {
const resolvedPath = path.join(directory, entry.name);
if (entry.isDirectory()) {
bundles.push(...await listJavaScriptBundles(resolvedPath));
continue;
}
if (entry.isFile() && entry.name.endsWith('.js')) {
bundles.push(resolvedPath);
}
}
return bundles;
}