-
Notifications
You must be signed in to change notification settings - Fork 775
/
update-to-lazy-component.ts
47 lines (38 loc) · 1.91 KB
/
update-to-lazy-component.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
import * as d from '../../declarations';
import { COMPILER_BUILD } from '../build/compiler-build-id';
import { normalizePath } from '@utils';
import { transformToLazyComponentText } from '../transformers/component-lazy/transform-lazy-component';
export const updateToLazyComponent = async (config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx, cmp: d.ComponentCompilerMeta): Promise<d.ComponentCompilerData> => {
const inputFilePath = cmp.jsFilePath;
const inputFileDir = config.sys.path.dirname(inputFilePath);
const inputFileName = config.sys.path.basename(inputFilePath);
const inputText = await compilerCtx.fs.readFile(inputFilePath);
const cacheKey = await compilerCtx.cache.createKey('lazy', COMPILER_BUILD.id, COMPILER_BUILD.transpiler, inputText);
const outputFileName = `${cacheKey}-${inputFileName}`;
const outputFilePath = config.sys.path.join(inputFileDir, outputFileName);
let outputJsText = await compilerCtx.cache.get(cacheKey);
if (outputJsText == null) {
const transformOpts: d.TransformOptions = {
coreImportPath: '@stencil/core',
componentExport: null,
componentMetadata: null,
proxy: null,
scopeCss: false,
style: 'inline'
};
outputJsText = transformToLazyComponentText(compilerCtx, buildCtx, transformOpts, cmp, inputText);
await compilerCtx.cache.put(cacheKey, outputJsText);
}
await compilerCtx.fs.writeFile(outputFilePath, outputJsText, { inMemoryOnly: true });
return {
filePath: outputFilePath,
exportLine: createComponentExport(cmp, outputFilePath),
cmp
};
};
const createComponentExport = (cmp: d.ComponentCompilerMeta, lazyModuleFilePath: string) => {
const originalClassName = cmp.componentClassName;
const underscoredClassName = cmp.tagName.replace(/-/g, '_');
const filePath = normalizePath(lazyModuleFilePath);
return `export { ${originalClassName} as ${underscoredClassName} } from '${filePath}';`;
};