Skip to content

Commit

Permalink
feat(compiler): add import paths to compile results
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Jul 25, 2019
1 parent 9e12097 commit adca6d0
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/compiler/browser/browser-compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export const compile = async (code: string, opts: d.CompileOptions = {}): Promis
map: null,
inputFilePath: (typeof opts.file === 'string' ? opts.file.trim() : 'module.tsx'),
outputFilePath: null,
inputOptions: null
inputOptions: null,
imports: []
};

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

results.outputFilePath = transpileResults.moduleFile.jsFilePath;

if (transpileResults.moduleFile) {
transpileResults.moduleFile.originalImports.forEach(originalImport => {
results.imports.push({
path: originalImport
});
});
}

} catch (e) {
catchError(results.diagnostics, e);
}
Expand Down
2 changes: 2 additions & 0 deletions src/compiler/build/compiler-ctx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export const getModule = (config: d.Config, compilerCtx: d.CompilerCtx, sourceFi
isLegacy: false,
localImports: [],
originalCollectionComponentPath: null,
originalImports: [],
potentialCmpRefs: []
};
compilerCtx.moduleMap.set(sourceFilePath, moduleFile);
Expand All @@ -128,6 +129,7 @@ export const resetModule = (moduleFile: d.Module) => {
moduleFile.isCollectionDependency = false;
moduleFile.localImports.length = 0;
moduleFile.originalCollectionComponentPath = null;
moduleFile.originalImports.length = 0;

moduleFile.hasVdomAttribute = true;
moduleFile.hasVdomClass = true;
Expand Down
12 changes: 10 additions & 2 deletions src/compiler/transformers/static-to-meta/call-expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,21 @@ const visitCallExpressionArgs = (m: d.Module | d.ComponentCompilerMeta, callExpr

} else if (args.length > 1 && fnName === 'createElementNS') {
visitCallExpressionArg(m, args[1]);

} else if (fnName === 'require' && args.length > 0 && (m as d.Module).originalImports) {
const arg = args[0];
if (ts.isStringLiteral(arg)) {
if (!(m as d.Module).originalImports.includes(arg.text)) {
(m as d.Module).originalImports.push(arg.text);
}
}
}
};


const visitCallExpressionArg = (m: d.Module | d.ComponentCompilerMeta, arg: ts.Expression) => {
if (arg.kind === ts.SyntaxKind.StringLiteral) {
let tag = (arg as ts.StringLiteral).text;
if (ts.isStringLiteral(arg)) {
let tag = arg.text;

if (typeof tag === 'string') {
tag = tag.toLowerCase();
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/transformers/static-to-meta/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export const parseImport = (config: d.Config, compilerCtx: d.CompilerCtx, buildC
if (importNode.moduleSpecifier && ts.isStringLiteral(importNode.moduleSpecifier)) {
let importPath = importNode.moduleSpecifier.text;

if (!moduleFile.originalImports.includes(importPath)) {
moduleFile.originalImports.push(importPath);
}

if (config.sys.path.isAbsolute(importPath)) {
// absolute import
importPath = normalizePath(importPath);
Expand Down
1 change: 1 addition & 0 deletions src/declarations/browser-compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface CompileResults {
inputFilePath: string;
outputFilePath: string;
inputOptions: CompileOptions;
imports: { path: string; }[];
}

export interface CompileScriptMinifyOptions {
Expand Down
1 change: 1 addition & 0 deletions src/declarations/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface Module {
isLegacy: boolean;
jsFilePath: string;
localImports: string[];
originalImports: string[];
originalCollectionComponentPath: string;
potentialCmpRefs: string[];
sourceFilePath: string;
Expand Down
4 changes: 4 additions & 0 deletions test/browser-compile/src/components/app-root/app-root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ export class AppRoot {

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

results.imports.forEach((imprt: string) => {
console.log('import:', imprt);
});

this.transpiledInput.value = results.code;
this.diagnostics = results.diagnostics;
this.wrap = 'off';
Expand Down

0 comments on commit adca6d0

Please sign in to comment.