forked from stenciljs/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler.ts
More file actions
65 lines (53 loc) · 2.03 KB
/
Copy pathcompiler.ts
File metadata and controls
65 lines (53 loc) · 2.03 KB
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
import { isFunction } from '@utils';
import ts from 'typescript';
import type { Compiler, Config, Diagnostic, ValidatedConfig } from '../declarations';
import { CompilerContext } from './build/compiler-ctx';
import { createFullBuild } from './build/full-build';
import { createWatchBuild } from './build/watch-build';
import { Cache } from './cache';
import { getConfig } from './sys/config';
import { createInMemoryFs } from './sys/in-memory-fs';
import { resolveModuleIdAsync } from './sys/resolve/resolve-module-async';
import { patchTypescript } from './sys/typescript/typescript-sys';
import { createSysWorker } from './sys/worker/sys-worker';
/**
* Generate a Stencil compiler instance
* @param userConfig a user-provided Stencil configuration to apply to the compiler instance
* @returns a new instance of a Stencil compiler
* @public
*/
export const createCompiler = async (userConfig: Config): Promise<Compiler> => {
// actual compiler code
const config: ValidatedConfig = getConfig(userConfig);
const diagnostics: Diagnostic[] = [];
const sys = config.sys;
const compilerCtx = new CompilerContext();
if (isFunction(config.sys.setupCompiler)) {
config.sys.setupCompiler({ ts });
}
compilerCtx.fs = createInMemoryFs(sys);
compilerCtx.cache = new Cache(config, createInMemoryFs(sys));
await compilerCtx.cache.initCacheDir();
sys.resolveModuleId = (opts) => resolveModuleIdAsync(sys, compilerCtx.fs, opts);
compilerCtx.worker = createSysWorker(config);
if (sys.events) {
// Pipe events from sys.events to compilerCtx
sys.events.on(compilerCtx.events.emit);
}
patchTypescript(config, compilerCtx.fs);
const build = () => createFullBuild(config, compilerCtx);
const createWatcher = () => createWatchBuild(config, compilerCtx);
const destroy = async () => {
compilerCtx.reset();
compilerCtx.events.unsubscribeAll();
await sys.destroy();
};
const compiler: Compiler = {
build,
createWatcher,
destroy,
sys,
};
config.logger.printDiagnostics(diagnostics);
return compiler;
};