Skip to content

Commit adca6d0

Browse files
committed
feat(compiler): add import paths to compile results
1 parent 9e12097 commit adca6d0

File tree

7 files changed

+32
-3
lines changed

7 files changed

+32
-3
lines changed

src/compiler/browser/browser-compile.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ export const compile = async (code: string, opts: d.CompileOptions = {}): Promis
1212
map: null,
1313
inputFilePath: (typeof opts.file === 'string' ? opts.file.trim() : 'module.tsx'),
1414
outputFilePath: null,
15-
inputOptions: null
15+
inputOptions: null,
16+
imports: []
1617
};
1718

1819
try {
@@ -39,6 +40,14 @@ export const compile = async (code: string, opts: d.CompileOptions = {}): Promis
3940

4041
results.outputFilePath = transpileResults.moduleFile.jsFilePath;
4142

43+
if (transpileResults.moduleFile) {
44+
transpileResults.moduleFile.originalImports.forEach(originalImport => {
45+
results.imports.push({
46+
path: originalImport
47+
});
48+
});
49+
}
50+
4251
} catch (e) {
4352
catchError(results.diagnostics, e);
4453
}

src/compiler/build/compiler-ctx.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ export const getModule = (config: d.Config, compilerCtx: d.CompilerCtx, sourceFi
110110
isLegacy: false,
111111
localImports: [],
112112
originalCollectionComponentPath: null,
113+
originalImports: [],
113114
potentialCmpRefs: []
114115
};
115116
compilerCtx.moduleMap.set(sourceFilePath, moduleFile);
@@ -128,6 +129,7 @@ export const resetModule = (moduleFile: d.Module) => {
128129
moduleFile.isCollectionDependency = false;
129130
moduleFile.localImports.length = 0;
130131
moduleFile.originalCollectionComponentPath = null;
132+
moduleFile.originalImports.length = 0;
131133

132134
moduleFile.hasVdomAttribute = true;
133135
moduleFile.hasVdomClass = true;

src/compiler/transformers/static-to-meta/call-expression.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,21 @@ const visitCallExpressionArgs = (m: d.Module | d.ComponentCompilerMeta, callExpr
3333

3434
} else if (args.length > 1 && fnName === 'createElementNS') {
3535
visitCallExpressionArg(m, args[1]);
36+
37+
} else if (fnName === 'require' && args.length > 0 && (m as d.Module).originalImports) {
38+
const arg = args[0];
39+
if (ts.isStringLiteral(arg)) {
40+
if (!(m as d.Module).originalImports.includes(arg.text)) {
41+
(m as d.Module).originalImports.push(arg.text);
42+
}
43+
}
3644
}
3745
};
3846

3947

4048
const visitCallExpressionArg = (m: d.Module | d.ComponentCompilerMeta, arg: ts.Expression) => {
41-
if (arg.kind === ts.SyntaxKind.StringLiteral) {
42-
let tag = (arg as ts.StringLiteral).text;
49+
if (ts.isStringLiteral(arg)) {
50+
let tag = arg.text;
4351

4452
if (typeof tag === 'string') {
4553
tag = tag.toLowerCase();

src/compiler/transformers/static-to-meta/import.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ export const parseImport = (config: d.Config, compilerCtx: d.CompilerCtx, buildC
88
if (importNode.moduleSpecifier && ts.isStringLiteral(importNode.moduleSpecifier)) {
99
let importPath = importNode.moduleSpecifier.text;
1010

11+
if (!moduleFile.originalImports.includes(importPath)) {
12+
moduleFile.originalImports.push(importPath);
13+
}
14+
1115
if (config.sys.path.isAbsolute(importPath)) {
1216
// absolute import
1317
importPath = normalizePath(importPath);

src/declarations/browser-compile.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface CompileResults {
1616
inputFilePath: string;
1717
outputFilePath: string;
1818
inputOptions: CompileOptions;
19+
imports: { path: string; }[];
1920
}
2021

2122
export interface CompileScriptMinifyOptions {

src/declarations/module.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export interface Module {
2929
isLegacy: boolean;
3030
jsFilePath: string;
3131
localImports: string[];
32+
originalImports: string[];
3233
originalCollectionComponentPath: string;
3334
potentialCmpRefs: string[];
3435
sourceFilePath: string;

test/browser-compile/src/components/app-root/app-root.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ export class AppRoot {
6464

6565
const results = await stencil.compile(this.sourceCodeInput.value, opts);
6666

67+
results.imports.forEach((imprt: string) => {
68+
console.log('import:', imprt);
69+
});
70+
6771
this.transpiledInput.value = results.code;
6872
this.diagnostics = results.diagnostics;
6973
this.wrap = 'off';

0 commit comments

Comments
 (0)