forked from rollup/rollup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadConfigFile.ts
167 lines (161 loc) · 4.98 KB
/
loadConfigFile.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { unlink, writeFile } from 'node:fs/promises';
import path from 'node:path';
import process from 'node:process';
import { pathToFileURL } from 'node:url';
import * as rollup from '../../src/node-entry';
import type { MergedRollupOptions } from '../../src/rollup/types';
import { bold } from '../../src/utils/colors';
import {
error,
logCannotBundleConfigAsEsm,
logCannotLoadConfigAsCjs,
logCannotLoadConfigAsEsm,
logMissingConfig
} from '../../src/utils/logs';
import { mergeOptions } from '../../src/utils/options/mergeOptions';
import type { GenericConfigObject } from '../../src/utils/options/options';
import relativeId from '../../src/utils/relativeId';
import { stderr } from '../logging';
import batchWarnings from './batchWarnings';
import { addCommandPluginsToInputOptions, addPluginsFromCommandOption } from './commandPlugins';
import type { LoadConfigFile } from './loadConfigFileType';
export const loadConfigFile: LoadConfigFile = async (
fileName,
commandOptions = {},
watchMode = false
) => {
const configs = await getConfigList(
getDefaultFromCjs(await getConfigFileExport(fileName, commandOptions, watchMode)),
commandOptions
);
const warnings = batchWarnings(commandOptions);
try {
const normalizedConfigs: MergedRollupOptions[] = [];
for (const config of configs) {
const options = await mergeOptions(config, watchMode, commandOptions, warnings.log);
await addCommandPluginsToInputOptions(options, commandOptions);
normalizedConfigs.push(options);
}
return { options: normalizedConfigs, warnings };
} catch (error_) {
warnings.flush();
throw error_;
}
};
async function getConfigFileExport(
fileName: string,
commandOptions: Record<string, unknown>,
watchMode: boolean
) {
if (commandOptions.configPlugin || commandOptions.bundleConfigAsCjs) {
try {
return await loadTranspiledConfigFile(fileName, commandOptions);
} catch (error_: any) {
if (error_.message.includes('not defined in ES module scope')) {
return error(logCannotBundleConfigAsEsm(error_));
}
throw error_;
}
}
let cannotLoadEsm = false;
const handleWarning = (warning: Error): void => {
if (warning.message.includes('To load an ES module')) {
cannotLoadEsm = true;
}
};
process.on('warning', handleWarning);
try {
const fileUrl = pathToFileURL(fileName);
if (watchMode) {
// We are adding the current date to allow reloads in watch mode
fileUrl.search = `?${Date.now()}`;
}
return (await import(fileUrl.href)).default;
} catch (error_: any) {
if (cannotLoadEsm) {
return error(logCannotLoadConfigAsCjs(error_));
}
if (error_.message.includes('not defined in ES module scope')) {
return error(logCannotLoadConfigAsEsm(error_));
}
throw error_;
} finally {
process.off('warning', handleWarning);
}
}
function getDefaultFromCjs(namespace: GenericConfigObject): unknown {
return namespace.default || namespace;
}
async function loadTranspiledConfigFile(
fileName: string,
commandOptions: Record<string, unknown>
): Promise<unknown> {
const { bundleConfigAsCjs, configPlugin, silent } = commandOptions;
const warnings = batchWarnings(commandOptions);
const inputOptions = {
external: (id: string) =>
(id[0] !== '.' && !path.isAbsolute(id)) || id.slice(-5, id.length) === '.json',
input: fileName,
onwarn: warnings.add,
plugins: [],
treeshake: false
};
await addPluginsFromCommandOption(configPlugin, inputOptions);
const bundle = await rollup.rollup(inputOptions);
const {
output: [{ code }]
} = await bundle.generate({
exports: 'named',
format: bundleConfigAsCjs ? 'cjs' : 'es',
plugins: [
{
name: 'transpile-import-meta',
resolveImportMeta(property, { moduleId }) {
if (property === 'url') {
return `'${pathToFileURL(moduleId).href}'`;
}
if (property == 'filename') {
return `'${moduleId}'`;
}
if (property == 'dirname') {
return `'${path.dirname(moduleId)}'`;
}
if (property == null) {
return `{url:'${pathToFileURL(moduleId).href}', filename: '${moduleId}', dirname: '${path.dirname(moduleId)}'}`;
}
}
}
]
});
if (!silent && warnings.count > 0) {
stderr(bold(`loaded ${relativeId(fileName)} with warnings`));
warnings.flush();
}
return loadConfigFromWrittenFile(
path.join(
path.dirname(fileName),
`rollup.config-${Date.now()}.${bundleConfigAsCjs ? 'cjs' : 'mjs'}`
),
code
);
}
async function loadConfigFromWrittenFile(
bundledFileName: string,
bundledCode: string
): Promise<unknown> {
await writeFile(bundledFileName, bundledCode);
try {
return (await import(pathToFileURL(bundledFileName).href)).default;
} finally {
unlink(bundledFileName).catch(error => console.warn(error?.message || error));
}
}
async function getConfigList(configFileExport: any, commandOptions: any): Promise<any[]> {
const config = await (typeof configFileExport === 'function'
? configFileExport(commandOptions)
: configFileExport);
if (Object.keys(config).length === 0) {
return error(logMissingConfig());
}
return Array.isArray(config) ? config : [config];
}