-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.js
More file actions
140 lines (123 loc) · 3.79 KB
/
vite.config.js
File metadata and controls
140 lines (123 loc) · 3.79 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { resolve } from 'node:path';
import process from 'node:process';
import vuePlugin from '@vitejs/plugin-vue';
import replaceImportsWithVarsPlugin from 'rollup-plugin-replace-imports-with-vars';
import { defineConfig } from 'vite';
const target = process.env.TARGET ?? '.';
const workingDir = resolve(target);
const manifestPath = resolve(workingDir, 'manifest.json');
export default defineConfig(async ({ mode }) => {
const isDev = mode === 'development';
const manifest = await import(manifestPath, { with: { type: "json" } }).then((m) => m.default);
return {
envDir: workingDir,
build: {
target: 'chrome58',
outDir: isDev ? resolve('../yskra/plugins') : resolve('./dist'),
emptyOutDir: false,
watch: isDev ? ({}) : null,
minify: isDev ? false : 'esbuild',
lib: {
entry: resolve(workingDir, 'src/main.js'),
fileName: `${manifest.name.toLowerCase().replace(/\s/g, '_')}`,
formats: ['es', 'system'],
},
rollupOptions: {
external: ['vue', 'pinia', 'vue-router', 'utils', 'vue-i18n', 'arrow-navigation', 'vue-vnode-utils'],
},
},
plugins: [
vuePlugin(),
styleInjectPlugin(),
replaceImportsWithVarsPlugin({ // anyway it's use for es target
varType: 'const',
replacementLookup: {
'vue': `Yskra.import('vue')`,
'vue-router': `Yskra.import('router')`,
'vue-i18n': `Yskra.import('i18n')`,
'pinia': `Yskra.import('pinia')`,
'utils': `Yskra.import('utils')`,
},
}),
manifestInjectPlugin(manifest),
],
resolve: {
alias: {
'@': resolve(workingDir, 'src'),
},
},
};
});
// build manifest.json to JSDoc
function manifestInjectPlugin(manifest) {
return {
name: 'manifest-inject',
generateBundle(options, chunks) {
Object.values(chunks).forEach((chunk) => {
const jsdoc = generateJsdoc({ ...manifest, runtime: options.format });
chunk.code = `${jsdoc}\n\n${chunk.code}`;
});
},
};
function generateJsdoc(manifest) {
const pad = Object.keys(manifest).reduce((acc, key) => acc > key.length ? acc : key.length, 0);
return `/**\n${
Object.entries(manifest)
.reduce(reduceValues, [])
.map(([k, v]) => ` * @${k.padEnd(pad)} ${v}`)
.join('\n')
}\n*/`;
}
function reduceValues(acc, [key, value]) {
let result;
if (typeof value === 'object' && value !== null) {
if (Array.isArray(value)) {
result = value.map((v) => [key, v]);
} else {
result = Object.entries(value).map(([k, v]) => [key, `${k}:${v}`]);
}
result = result.flat();
} else {
result = [key, value];
}
return [...acc, result];
}
}
// inject stylesheets to JS bundle as virtual module
function styleInjectPlugin() {
const VIRTUAL_MODULE_NAME = 'virtual:styles';
const MODULE_ID = `\0${VIRTUAL_MODULE_NAME}`;
const REPLACE_PATTERN = '__virtual_styles__';
return {
apply: 'build',
enforce: 'post',
name: 'style-inject',
resolveId(id) {
if (id === VIRTUAL_MODULE_NAME) {
return MODULE_ID;
}
},
load(id) {
if (id === MODULE_ID) {
return `export default ${REPLACE_PATTERN}`;
}
},
generateBundle(opts, bundle) {
let styleCode = '';
for (const key in bundle) {
const chunk = bundle[key];
if (chunk?.type === 'asset' && chunk.fileName.includes('.css')) {
styleCode += chunk.source;
delete bundle[key];
}
}
for (const key in bundle) {
const chunk = bundle[key];
if (chunk?.type === 'chunk' && chunk.fileName.includes('.js')) {
chunk.code = chunk.code.replace(REPLACE_PATTERN, JSON.stringify(styleCode));
break;
}
}
},
};
}