Skip to content

Commit f619fdc

Browse files
committed
feat: track currentFilePaths in rollup plugin and do hot updates in vite
1 parent 1e0e4dd commit f619fdc

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

app-config-rollup/src/index.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,46 @@ import { ConfigLoadingOptions, loadValidatedConfig } from '@app-config/config';
44
import { asEnvOptions, currentEnvironment } from '@app-config/node';
55
import type { SchemaLoadingOptions } from '@app-config/schema';
66

7-
interface Options {
7+
export interface Options {
88
readGlobal?: boolean;
99
injectValidationFunction?: boolean;
1010
loadingOptions?: ConfigLoadingOptions;
1111
schemaLoadingOptions?: SchemaLoadingOptions;
1212
}
1313

1414
// vite resolves first before passing to the rollup plugin
15-
const manualImport = /(app-config|app-config-main)\/dist(\/es)?\/index\.js/;
15+
export const appConfigImportRegex = /(app-config|app-config-main)\/dist(\/es)?\/index\.js/;
1616

1717
export default function appConfigRollup({
1818
readGlobal,
1919
injectValidationFunction,
2020
loadingOptions,
2121
schemaLoadingOptions,
22-
}: Options = {}): Plugin {
22+
}: Options = {}): Plugin & { currentFilePaths?: string[] } {
23+
const currentFilePaths: string[] = [];
24+
2325
return {
2426
name: '@app-config/rollup',
27+
currentFilePaths,
2528
resolveId(source) {
26-
if (packageNameRegex.exec(source) || manualImport.exec(source)) {
29+
if (packageNameRegex.exec(source) || appConfigImportRegex.exec(source)) {
2730
return '.config-placeholder';
2831
}
2932

3033
return null;
3134
},
3235
async load(id) {
33-
if (packageNameRegex.exec(id) || manualImport.exec(id)) {
34-
const { parsed: config, validationFunctionCode } = await loadValidatedConfig(
35-
loadingOptions,
36-
schemaLoadingOptions,
37-
);
38-
39-
// TODO: alternative for addDependecy with filePaths
36+
if (packageNameRegex.exec(id) || appConfigImportRegex.exec(id)) {
37+
const {
38+
parsed: config,
39+
validationFunctionCode,
40+
filePaths,
41+
} = await loadValidatedConfig(loadingOptions, schemaLoadingOptions);
42+
43+
if (filePaths) {
44+
currentFilePaths.length = 0;
45+
currentFilePaths.push(...filePaths);
46+
}
4047

4148
let generatedText: string;
4249

app-config-vite/src/index.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,24 @@
1+
import rollupPlugin, { Options } from '@app-config/rollup';
2+
import type { Plugin } from 'vite';
3+
14
export * from '@app-config/rollup';
2-
export { default } from '@app-config/rollup';
5+
6+
export default function appConfigVite(options: Options = {}): Plugin {
7+
const plugin = rollupPlugin(options);
8+
9+
return {
10+
...plugin,
11+
name: '@app-config/vite',
12+
13+
async handleHotUpdate({ server, file }) {
14+
if (plugin.currentFilePaths?.includes(file)) {
15+
server.ws.send({
16+
type: 'full-reload',
17+
path: '@app-config/main',
18+
});
19+
20+
return [];
21+
}
22+
},
23+
};
24+
}

0 commit comments

Comments
 (0)