-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.ts
129 lines (117 loc) · 3.27 KB
/
vite.config.ts
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
// replace 'world-builder' below with package name
import copy from 'rollup-plugin-copy';
import scss from 'rollup-plugin-scss';
import path from 'path';
import envCompatible from 'vite-plugin-env-compatible';
import * as fsPromises from 'fs/promises';
import { createHtmlPlugin } from 'vite-plugin-html';
import { viteCommonjs } from '@originjs/vite-plugin-commonjs';
import { defineConfig, Plugin } from 'vite';
// to get the verison number
import npmPackage from './package.json';
export default defineConfig({
resolve: {
alias: [
{
find: '@',
replacement: path.resolve(__dirname,'src')
},
{
find: '@module',
replacement: path.resolve(__dirname,'static/module.json')
},
],
extensions: [
'.mjs',
'.js',
'.ts',
'.jsx',
'.tsx',
'.json',
'.vue'
]
},
plugins: [
// copy the static module file
copy({
targets: [
{ src: 'static/lang', dest: 'dist' },
{ src: 'static/templates', dest: 'dist' }
],
hook: 'writeBundle',
}),
scss({
output: 'styles/style.css',
sourceMap: true,
watch: ['src/styles/*.scss'],
}), viteCommonjs(),
envCompatible(),
createHtmlPlugin({
inject: {
data: {
title: 'world-builder'
}
}
}),
updateModuleManifestPlugin(),
],
build: {
sourcemap: true,
outDir: 'dist',
rollupOptions: {
input: 'src/main.ts',
output: {
assetFileNames: (assetInfo): string => {
if (assetInfo.name === 'output.css')
return 'styles/world-builder.css';
else if (assetInfo.name ==='output.css.map')
return 'styles/world-builder.css.map';
else if (assetInfo.name)
return assetInfo.name;
else
throw 'Asset missing name';
},
entryFileNames: (assetInfo): string => 'scripts/index.js',
format: 'es',
},
},
}
});
// a plugin to save the manifest, setting the version # from the npm package.json
function updateModuleManifestPlugin(): Plugin {
return {
name: 'update-module-manifest',
async writeBundle(): Promise<void> {
// get github info
const githubProject = process.env.GH_PROJECT;
const githubTag = process.env.GH_TAG;
// get the version number
const moduleVersion = npmPackage.version;
// read the static file
const manifestContents: string = await fsPromises.readFile(
'static/module.json',
'utf-8'
);
// convert to JSON
const manifestJson = JSON.parse(manifestContents) as Record<string,unknown>;
// set the version #
if (moduleVersion) {
delete manifestJson['## comment:version'];
manifestJson.version = moduleVersion;
}
// set the release info
if (githubProject) {
const baseUrl = `https://github.com/${githubProject}/releases`;
manifestJson.manifest = `${baseUrl}/latest/download/module.json`;
if (githubTag) {
manifestJson.download = `${baseUrl}/download/${githubTag}/module.zip`;
}
}
// write the updated file
await fsPromises.writeFile(
'dist/module.json',
JSON.stringify(manifestJson, null, 4)
);
},
};
}