@virmator/frontend 13.4.1 → 13.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/configs/vite.config.base.ts +7 -0
- package/package.json +8 -8
- package/src/always-reload.vite.ts +114 -0
- package/src/frontend.ts +199 -0
|
@@ -72,6 +72,13 @@ export function createBaseConfig({forGitHubPages, packageDirPath}: BaseConfigOpt
|
|
|
72
72
|
build: {
|
|
73
73
|
outDir,
|
|
74
74
|
emptyOutDir: true,
|
|
75
|
+
target: 'es2022', // This should be kept in sync with `@virmator/compile/configs/tsconfig.base.json`.
|
|
76
|
+
rollupOptions: {
|
|
77
|
+
external: [
|
|
78
|
+
// This is specified, but not used, in `@augment-vir/common`'s `log` export. It is only used inside a Node.js environment.
|
|
79
|
+
'node:util',
|
|
80
|
+
],
|
|
81
|
+
},
|
|
75
82
|
},
|
|
76
83
|
},
|
|
77
84
|
basePaths: {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@virmator/frontend",
|
|
3
|
-
"version": "13.
|
|
3
|
+
"version": "13.5.1",
|
|
4
4
|
"description": "Default frontend plugin for virmator.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"virmator",
|
|
@@ -34,17 +34,17 @@
|
|
|
34
34
|
"test:update": "npm test update"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@augment-vir/assert": "^30.0
|
|
38
|
-
"@augment-vir/common": "^30.0
|
|
39
|
-
"@augment-vir/node": "^30.0
|
|
40
|
-
"@virmator/core": "^13.
|
|
37
|
+
"@augment-vir/assert": "^30.2.0",
|
|
38
|
+
"@augment-vir/common": "^30.2.0",
|
|
39
|
+
"@augment-vir/node": "^30.2.0",
|
|
40
|
+
"@virmator/core": "^13.5.1",
|
|
41
41
|
"mri": "^1.2.0"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
|
-
"@augment-vir/test": "^30.0
|
|
45
|
-
"@types/node": "^22.
|
|
44
|
+
"@augment-vir/test": "^30.2.0",
|
|
45
|
+
"@types/node": "^22.7.4",
|
|
46
46
|
"typescript": "^5.6.2",
|
|
47
|
-
"vite": "^5.4.
|
|
47
|
+
"vite": "^5.4.8",
|
|
48
48
|
"vite-tsconfig-paths": "^5.0.1"
|
|
49
49
|
},
|
|
50
50
|
"engines": {
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import {logColors} from '@augment-vir/common';
|
|
2
|
+
import {existsSync, lstatSync, readlinkSync} from 'node:fs';
|
|
3
|
+
import {relative} from 'node:path';
|
|
4
|
+
import type {LogOptions, PluginOption} from 'vite';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Include actual paths and symlinked target paths if they exist.
|
|
8
|
+
*
|
|
9
|
+
* This is needed because when removing files from the watcher, sym links have be removed with the
|
|
10
|
+
* path to the symlink itself AND the path to the symlink target or the path will still be watched.
|
|
11
|
+
*/
|
|
12
|
+
function mapToActualPaths(paths: Readonly<string[]>): Readonly<string[]> {
|
|
13
|
+
return paths.reduce((accum: string[], path) => {
|
|
14
|
+
if (existsSync(path)) {
|
|
15
|
+
if (lstatSync(path).isSymbolicLink()) {
|
|
16
|
+
console.info('reading symlink from', path);
|
|
17
|
+
// sym links AND the original path both need to be included
|
|
18
|
+
return accum.concat(readlinkSync(path), path);
|
|
19
|
+
} else {
|
|
20
|
+
return accum.concat(path);
|
|
21
|
+
}
|
|
22
|
+
} else {
|
|
23
|
+
return accum;
|
|
24
|
+
}
|
|
25
|
+
}, []);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* There are similar plugins out there that try to do this but they aren't aggressive enough. This
|
|
30
|
+
* plugin literally always reloads on save, no questions asked.
|
|
31
|
+
*/
|
|
32
|
+
export function alwaysReloadPlugin(
|
|
33
|
+
config: Partial<{
|
|
34
|
+
exclusions: string[];
|
|
35
|
+
/** Inclusions apply after exclusions so they will override exclusions. */
|
|
36
|
+
inclusions: string[];
|
|
37
|
+
}> = {},
|
|
38
|
+
): PluginOption {
|
|
39
|
+
return {
|
|
40
|
+
name: 'alwaysReloadPlugin',
|
|
41
|
+
apply: 'serve',
|
|
42
|
+
config: () => ({server: {watch: {disableGlobbing: false}}}),
|
|
43
|
+
handleHotUpdate() {
|
|
44
|
+
return [];
|
|
45
|
+
},
|
|
46
|
+
configureServer({watcher, ws, config: {logger, publicDir = '', root = process.cwd()}}) {
|
|
47
|
+
const {inclusions = [], exclusions = []} = config;
|
|
48
|
+
let callingAlready = false;
|
|
49
|
+
|
|
50
|
+
// eslint-disable-next-line @typescript-eslint/unbound-method
|
|
51
|
+
const oldInfoLog = logger.info;
|
|
52
|
+
|
|
53
|
+
function customInfoLog(message: string, options: LogOptions) {
|
|
54
|
+
/**
|
|
55
|
+
* Ignore vite's built-in extra page reload logging as this will duplicate our
|
|
56
|
+
* messages.
|
|
57
|
+
*/
|
|
58
|
+
if (message.startsWith('page reload')) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
oldInfoLog(message, options);
|
|
62
|
+
}
|
|
63
|
+
logger.info = customInfoLog;
|
|
64
|
+
|
|
65
|
+
function reloadCallback(path: string) {
|
|
66
|
+
// prevent duplicate calls cause the watcher is very eager to call callbacks multiple times in a row
|
|
67
|
+
if (!callingAlready) {
|
|
68
|
+
callingAlready = true;
|
|
69
|
+
ws.send({
|
|
70
|
+
type: 'full-reload',
|
|
71
|
+
path: '*',
|
|
72
|
+
});
|
|
73
|
+
logger.info(
|
|
74
|
+
`${logColors.success}page reload ${logColors.faint}${relative(
|
|
75
|
+
process.cwd(),
|
|
76
|
+
path,
|
|
77
|
+
)}${logColors.reset}`,
|
|
78
|
+
{
|
|
79
|
+
clear: true,
|
|
80
|
+
timestamp: true,
|
|
81
|
+
},
|
|
82
|
+
);
|
|
83
|
+
/**
|
|
84
|
+
* Debounce reloads calls so that they don't get spammed. If you're actually
|
|
85
|
+
* intentionally saving faster than this, then what the heck are you doing?
|
|
86
|
+
*/
|
|
87
|
+
setTimeout(() => {
|
|
88
|
+
callingAlready = false;
|
|
89
|
+
}, 100);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
watcher.add(root);
|
|
94
|
+
|
|
95
|
+
if (exclusions.length) {
|
|
96
|
+
watcher.unwatch(mapToActualPaths(exclusions));
|
|
97
|
+
}
|
|
98
|
+
// ignore macOS file system metadata stuff
|
|
99
|
+
watcher.unwatch('./**/.DS_Store');
|
|
100
|
+
if (inclusions.length) {
|
|
101
|
+
watcher.add(mapToActualPaths(inclusions));
|
|
102
|
+
}
|
|
103
|
+
if (publicDir) {
|
|
104
|
+
watcher.add(publicDir);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (!watcher.listeners('change').includes(reloadCallback)) {
|
|
108
|
+
watcher.on('change', reloadCallback);
|
|
109
|
+
watcher.on('add', reloadCallback);
|
|
110
|
+
watcher.on('unlink', reloadCallback);
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
}
|
package/src/frontend.ts
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import {check} from '@augment-vir/assert';
|
|
2
|
+
import {type MaybePromise, RuntimeEnv} from '@augment-vir/common';
|
|
3
|
+
import {toPosixPath} from '@augment-vir/node';
|
|
4
|
+
import {defineVirmatorPlugin, NpmDepType, PackageType} from '@virmator/core';
|
|
5
|
+
import mri from 'mri';
|
|
6
|
+
import {cp, rm} from 'node:fs/promises';
|
|
7
|
+
import {join, relative, resolve} from 'node:path';
|
|
8
|
+
import {pathToFileURL} from 'node:url';
|
|
9
|
+
import type {UserConfig} from 'vite';
|
|
10
|
+
|
|
11
|
+
/** A virmator plugin for running and building frontend packages. */
|
|
12
|
+
export const virmatorFrontendPlugin = defineVirmatorPlugin(
|
|
13
|
+
import.meta.dirname,
|
|
14
|
+
{
|
|
15
|
+
name: 'Frontend',
|
|
16
|
+
cliCommands: {
|
|
17
|
+
frontend: {
|
|
18
|
+
doc: {
|
|
19
|
+
sections: [
|
|
20
|
+
`
|
|
21
|
+
Runs a frontend dev server with Vite.
|
|
22
|
+
`,
|
|
23
|
+
],
|
|
24
|
+
examples: [
|
|
25
|
+
{
|
|
26
|
+
content: 'virmator frontend',
|
|
27
|
+
},
|
|
28
|
+
],
|
|
29
|
+
},
|
|
30
|
+
subCommands: {
|
|
31
|
+
build: {
|
|
32
|
+
doc: {
|
|
33
|
+
sections: [
|
|
34
|
+
`
|
|
35
|
+
Builds a frontend for deployment using Vite (and Rollup).
|
|
36
|
+
`,
|
|
37
|
+
],
|
|
38
|
+
examples: [
|
|
39
|
+
{
|
|
40
|
+
content: 'virmator frontend build',
|
|
41
|
+
},
|
|
42
|
+
],
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
preview: {
|
|
46
|
+
doc: {
|
|
47
|
+
sections: [
|
|
48
|
+
`
|
|
49
|
+
Builds a frontend and previews that build in a local dev server.
|
|
50
|
+
`,
|
|
51
|
+
],
|
|
52
|
+
examples: [
|
|
53
|
+
{
|
|
54
|
+
content: 'virmator frontend preview',
|
|
55
|
+
},
|
|
56
|
+
],
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
configFiles: {
|
|
61
|
+
vite: {
|
|
62
|
+
copyFromPath: join('configs', 'vite.config.ts'),
|
|
63
|
+
copyToPath: join('configs', 'vite.config.ts'),
|
|
64
|
+
env: [
|
|
65
|
+
RuntimeEnv.Web,
|
|
66
|
+
],
|
|
67
|
+
packageType: [
|
|
68
|
+
PackageType.TopPackage,
|
|
69
|
+
PackageType.MonoPackage,
|
|
70
|
+
],
|
|
71
|
+
required: true,
|
|
72
|
+
configFlags: ['--config'],
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
npmDeps: {
|
|
76
|
+
typescript: {
|
|
77
|
+
type: NpmDepType.Dev,
|
|
78
|
+
env: [
|
|
79
|
+
RuntimeEnv.Web,
|
|
80
|
+
],
|
|
81
|
+
packageType: [
|
|
82
|
+
PackageType.MonoPackage,
|
|
83
|
+
PackageType.TopPackage,
|
|
84
|
+
],
|
|
85
|
+
},
|
|
86
|
+
vite: {
|
|
87
|
+
type: NpmDepType.Dev,
|
|
88
|
+
env: [
|
|
89
|
+
RuntimeEnv.Web,
|
|
90
|
+
],
|
|
91
|
+
packageType: [
|
|
92
|
+
PackageType.MonoPackage,
|
|
93
|
+
PackageType.TopPackage,
|
|
94
|
+
],
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
async ({
|
|
101
|
+
cliInputs: {filteredArgs, usedCommands},
|
|
102
|
+
package: {cwdPackagePath},
|
|
103
|
+
runShellCommand,
|
|
104
|
+
cwd,
|
|
105
|
+
configs,
|
|
106
|
+
log,
|
|
107
|
+
}) => {
|
|
108
|
+
const args = mri(filteredArgs);
|
|
109
|
+
|
|
110
|
+
const needsBuild =
|
|
111
|
+
usedCommands.frontend?.subCommands.build || usedCommands.frontend?.subCommands.preview;
|
|
112
|
+
const configPath = args.config || configs.frontend.configs.vite.fullCopyToPath;
|
|
113
|
+
|
|
114
|
+
const viteConfig = await ((
|
|
115
|
+
await import(pathToFileURL(configPath).toString())
|
|
116
|
+
).default as MaybePromise<UserConfig>);
|
|
117
|
+
|
|
118
|
+
const rootDir: string = viteConfig.root ? resolve(cwd, viteConfig.root) : cwd;
|
|
119
|
+
const outDir: string = resolve(rootDir, viteConfig.build?.outDir || 'dist');
|
|
120
|
+
|
|
121
|
+
const configArgs = args.config
|
|
122
|
+
? []
|
|
123
|
+
: [
|
|
124
|
+
'--config',
|
|
125
|
+
toPosixPath(relative(cwd, configPath)),
|
|
126
|
+
];
|
|
127
|
+
|
|
128
|
+
const baseViteCommands = [
|
|
129
|
+
/** Required so that .ts vite configs can import other .ts files. */
|
|
130
|
+
'NODE_OPTIONS="--import tsx"',
|
|
131
|
+
'npx',
|
|
132
|
+
'vite',
|
|
133
|
+
];
|
|
134
|
+
|
|
135
|
+
if (needsBuild) {
|
|
136
|
+
await rm(join(cwdPackagePath, 'node_modules', '.vite'), {force: true, recursive: true});
|
|
137
|
+
await rm(outDir, {recursive: true, force: true});
|
|
138
|
+
|
|
139
|
+
const buildCommand = [
|
|
140
|
+
...baseViteCommands,
|
|
141
|
+
'build',
|
|
142
|
+
...filteredArgs,
|
|
143
|
+
...configArgs,
|
|
144
|
+
]
|
|
145
|
+
.filter(check.isTruthy)
|
|
146
|
+
.join(' ');
|
|
147
|
+
|
|
148
|
+
log.faint('Building...');
|
|
149
|
+
await runShellCommand(buildCommand, {
|
|
150
|
+
env: {
|
|
151
|
+
...process.env,
|
|
152
|
+
VITE_CJS_IGNORE_WARNING: 'true',
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
log.faint('Creating 404 page...');
|
|
157
|
+
const indexPath = join(outDir, 'index.html');
|
|
158
|
+
const dist404Path = join(outDir, '404.html');
|
|
159
|
+
|
|
160
|
+
await cp(indexPath, dist404Path);
|
|
161
|
+
|
|
162
|
+
/** Run preview. */
|
|
163
|
+
if (usedCommands.frontend.subCommands.preview) {
|
|
164
|
+
const previewCommand = [
|
|
165
|
+
...baseViteCommands,
|
|
166
|
+
'preview',
|
|
167
|
+
'--force',
|
|
168
|
+
...filteredArgs,
|
|
169
|
+
...configArgs,
|
|
170
|
+
]
|
|
171
|
+
.filter(check.isTruthy)
|
|
172
|
+
.join(' ');
|
|
173
|
+
log.faint('Previewing...');
|
|
174
|
+
await runShellCommand(previewCommand, {
|
|
175
|
+
env: {
|
|
176
|
+
...process.env,
|
|
177
|
+
VITE_CJS_IGNORE_WARNING: 'true',
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
} else {
|
|
182
|
+
const devCommand = [
|
|
183
|
+
...baseViteCommands,
|
|
184
|
+
'--force',
|
|
185
|
+
...filteredArgs,
|
|
186
|
+
...configArgs,
|
|
187
|
+
]
|
|
188
|
+
.filter(check.isTruthy)
|
|
189
|
+
.join(' ');
|
|
190
|
+
|
|
191
|
+
await runShellCommand(devCommand, {
|
|
192
|
+
env: {
|
|
193
|
+
...process.env,
|
|
194
|
+
VITE_CJS_IGNORE_WARNING: 'true',
|
|
195
|
+
},
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
},
|
|
199
|
+
);
|