/
load-config.ts
184 lines (152 loc) · 6.41 KB
/
load-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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import type { CompilerSystem, Config, Diagnostic, LoadConfigInit, LoadConfigResults } from '../../declarations';
import { buildError, catchError, isString, normalizePath, hasError, IS_NODE_ENV } from '@utils';
import { createLogger } from '../sys/logger/console-logger';
import { createSystem } from '../sys/stencil-sys';
import { dirname, resolve } from 'path';
import { loadTypescript } from '../sys/typescript/typescript-load';
import { validateConfig } from './validate-config';
import { validateTsConfig } from '../sys/typescript/typescript-config';
import type TypeScript from 'typescript';
export const loadConfig = async (init: LoadConfigInit = {}) => {
const results: LoadConfigResults = {
config: null,
diagnostics: [],
tsconfig: {
compilerOptions: null,
},
};
try {
const sys = init.sys || createSystem();
const config = init.config || {};
let configPath = init.configPath || config.configPath;
const loadedTs = await loadTypescript(sys, init.typescriptPath, false);
const loadedConfigFile = await loadConfigFile(loadedTs, sys, results.diagnostics, configPath);
if (hasError(results.diagnostics)) {
return results;
}
if (loadedConfigFile != null) {
// merge the user's config object into their loaded config file
configPath = loadedConfigFile.configPath;
results.config = { ...loadedConfigFile, ...config };
results.config.configPath = configPath;
results.config.rootDir = normalizePath(dirname(configPath));
} else {
// no stencil.config.ts or .js file, which is fine
// #0CJS ¯\_(ツ)_/¯
results.config = { ...config };
results.config.configPath = null;
results.config.rootDir = normalizePath(sys.getCurrentDirectory());
}
results.config.sys = sys;
const validated = validateConfig(results.config);
results.diagnostics.push(...validated.diagnostics);
if (hasError(results.diagnostics)) {
return results;
}
results.config = validated.config;
if (results.config.flags.debug || results.config.flags.verbose) {
results.config.logLevel = 'debug';
} else if (results.config.flags.logLevel) {
results.config.logLevel = results.config.flags.logLevel;
} else if (typeof results.config.logLevel !== 'string') {
results.config.logLevel = 'info';
}
results.config.logger = init.logger || results.config.logger || createLogger();
results.config.logger.setLevel(results.config.logLevel);
if (!hasError(results.diagnostics)) {
const tsConfigResults = await validateTsConfig(loadedTs, results.config, sys, init);
results.diagnostics.push(...tsConfigResults.diagnostics);
results.config.tsconfig = tsConfigResults.path;
results.config.tsCompilerOptions = tsConfigResults.compilerOptions;
results.tsconfig.compilerOptions = tsConfigResults.compilerOptions;
}
if (isString(init.typescriptPath)) {
results.config.typescriptPath = init.typescriptPath;
}
} catch (e) {
catchError(results.diagnostics, e);
}
return results;
};
const loadConfigFile = async (loadedTs: typeof TypeScript, sys: CompilerSystem, diagnostics: Diagnostic[], configPath: string) => {
let config: Config = null;
if (isString(configPath)) {
// the passed in config was a string, so it's probably a path to the config we need to load
const configFileData = await evaluateConfigFile(loadedTs, sys, diagnostics, configPath);
if (hasError(diagnostics)) {
return config;
}
if (!configFileData.config) {
const err = buildError(diagnostics);
err.messageText = `Invalid Stencil configuration file "${configPath}". Missing "config" property.`;
err.absFilePath = configPath;
return config;
}
config = configFileData.config;
config.configPath = normalizePath(configPath);
}
return config;
};
const evaluateConfigFile = async (loadedTs: typeof TypeScript, sys: CompilerSystem, diagnostics: Diagnostic[], configFilePath: string) => {
let configFileData: { config?: Config } = null;
try {
if (IS_NODE_ENV) {
// ensure we cleared out node's internal require() cache for this file
delete require.cache[resolve(configFilePath)];
// let's override node's require for a second
// don't worry, we'll revert this when we're done
require.extensions['.ts'] = (module: NodeModuleWithCompile, filename: string) => {
let sourceText = sys.readFileSync(filename, 'utf8');
if (configFilePath.endsWith('.ts')) {
// looks like we've got a typed config file
// let's transpile it to .js quick
sourceText = transpileTypedConfig(loadedTs, diagnostics, sourceText, configFilePath);
} else {
// quick hack to turn a modern es module
// into and old school commonjs module
sourceText = sourceText.replace(/export\s+\w+\s+(\w+)/gm, 'exports.$1');
}
module._compile(sourceText, filename);
};
// let's do this!
configFileData = require(configFilePath);
// all set, let's go ahead and reset the require back to the default
require.extensions['.ts'] = undefined;
} else {
// browser environment, can't use node's require() to evaluate
let sourceText = await sys.readFile(configFilePath);
sourceText = transpileTypedConfig(loadedTs, diagnostics, sourceText, configFilePath);
if (hasError(diagnostics)) {
return configFileData;
}
const evalConfig = new Function(`const exports = {}; ${sourceText}; return exports;`);
configFileData = evalConfig();
}
} catch (e) {
catchError(diagnostics, e);
}
return configFileData;
};
const transpileTypedConfig = (loadedTs: typeof TypeScript, diagnostics: Diagnostic[], sourceText: string, filePath: string) => {
// let's transpile an awesome stencil.config.ts file into
// a boring stencil.config.js file
if (hasError(diagnostics)) {
return sourceText;
}
const opts: TypeScript.TranspileOptions = {
fileName: filePath,
compilerOptions: {
module: loadedTs.ModuleKind.CommonJS,
moduleResolution: loadedTs.ModuleResolutionKind.NodeJs,
esModuleInterop: true,
target: loadedTs.ScriptTarget.ES2015,
allowJs: true,
},
reportDiagnostics: false,
};
const output = loadedTs.transpileModule(sourceText, opts);
return output.outputText;
};
interface NodeModuleWithCompile extends NodeModule {
_compile(code: string, filename: string): any;
}