Skip to content

Commit 5781822

Browse files
committed
feat: improves robustness of rollup plugin
1 parent fa073a2 commit 5781822

File tree

1 file changed

+52
-31
lines changed

1 file changed

+52
-31
lines changed

app-config-rollup/src/index.ts

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,47 +14,68 @@ export interface Options {
1414
}
1515

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

19-
export default function appConfigRollup({
20-
useGlobalNamespace,
21-
loadingOptions,
22-
schemaLoadingOptions,
23-
injectValidationFunction,
24-
25-
readGlobal,
26-
}: Options = {}): Plugin & { currentFilePaths?: string[] } {
20+
export default function appConfigRollup(
21+
options: Options = {},
22+
): Plugin & { currentFilePaths?: string[] } {
2723
const currentFilePaths: string[] = [];
2824

2925
return {
3026
name: '@app-config/rollup',
3127
currentFilePaths,
32-
resolveId(source) {
33-
if (packageNameRegex.exec(source) || appConfigImportRegex.exec(source)) {
34-
return '.config-placeholder';
28+
resolveId(id) {
29+
if (shouldTransform(id)) {
30+
return { id, moduleSideEffects: false, external: false };
3531
}
36-
37-
return null;
3832
},
3933
async load(id) {
40-
if (packageNameRegex.exec(id) || appConfigImportRegex.exec(id)) {
41-
const { fullConfig, environment, validationFunctionCode, filePaths } =
42-
await loadValidatedConfig(loadingOptions, schemaLoadingOptions);
43-
44-
if (filePaths) {
45-
currentFilePaths.length = 0;
46-
currentFilePaths.push(...filePaths);
47-
}
48-
49-
return generateModuleText(fullConfig, {
50-
environment,
51-
useGlobalNamespace: useGlobalNamespace ?? readGlobal ?? true,
52-
validationFunctionCode: injectValidationFunction ? validationFunctionCode : undefined,
53-
esmValidationCode: true,
54-
});
34+
if (shouldTransform(id)) {
35+
return loadConfig(options, currentFilePaths);
5536
}
56-
57-
return null;
5837
},
38+
async transform(_, id) {
39+
if (shouldTransform(id)) {
40+
return loadConfig(options, currentFilePaths);
41+
}
42+
},
43+
};
44+
}
45+
46+
function shouldTransform(id: string) {
47+
return !!packageNameRegex.exec(id) || !!appConfigImportRegex.exec(id);
48+
}
49+
50+
async function loadConfig(
51+
{
52+
useGlobalNamespace,
53+
loadingOptions,
54+
schemaLoadingOptions,
55+
injectValidationFunction,
56+
readGlobal,
57+
}: Options,
58+
currentFilePaths: string[],
59+
) {
60+
const { fullConfig, environment, validationFunctionCode, filePaths } = await loadValidatedConfig(
61+
loadingOptions,
62+
schemaLoadingOptions,
63+
);
64+
65+
if (filePaths) {
66+
currentFilePaths.splice(0);
67+
currentFilePaths.push(...filePaths);
68+
}
69+
70+
const code = generateModuleText(fullConfig, {
71+
environment,
72+
useGlobalNamespace: useGlobalNamespace ?? readGlobal ?? true,
73+
validationFunctionCode: injectValidationFunction ? validationFunctionCode : undefined,
74+
esmValidationCode: true,
75+
});
76+
77+
return {
78+
code,
79+
moduleSideEffects: false,
5980
};
6081
}

0 commit comments

Comments
 (0)