Skip to content

Commit

Permalink
fix(transform): add legacy h
Browse files Browse the repository at this point in the history
fixes #1761
  • Loading branch information
manucorporat committed Jul 25, 2019
1 parent 41c4f75 commit 93cb82e
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 38 deletions.
48 changes: 10 additions & 38 deletions src/compiler/transformers/add-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const addEsmImports = (tsSourceFile: ts.SourceFile, importFnNames: string[], imp
// ESM Imports
// import { importNames } from 'importPath';

let importSpecifiers = importFnNames.map(importKey => {
const importSpecifiers = importFnNames.map(importKey => {
const splt = importKey.split(' as ');
let importAs = importKey;
let importFnName = importKey;
Expand All @@ -38,44 +38,16 @@ const addEsmImports = (tsSourceFile: ts.SourceFile, importFnNames: string[], imp
});

const statements = tsSourceFile.statements.slice();

const existingImportIndex = statements.findIndex(s => {
return ts.isImportDeclaration(s) &&
s.moduleSpecifier &&
(s.moduleSpecifier as ts.StringLiteral).text === importPath;
});

if (existingImportIndex > -1) {
const existingImport = statements[existingImportIndex] as ts.ImportDeclaration;

importSpecifiers = [
...(existingImport.importClause.namedBindings as ts.NamedImports).elements.map(elm => elm),
...importSpecifiers
];

statements[existingImportIndex] = ts.updateImportDeclaration(
existingImport,
undefined,
undefined,
ts.createImportClause(
undefined,
ts.createNamedImports(importSpecifiers)
),
ts.createLiteral(importPath)
);

} else {
const newImport = ts.createImportDeclaration(
undefined,
const newImport = ts.createImportDeclaration(
undefined,
undefined,
ts.createImportClause(
undefined,
ts.createImportClause(
undefined,
ts.createNamedImports(importSpecifiers)
),
ts.createLiteral(importPath)
);
statements.unshift(newImport);
}
ts.createNamedImports(importSpecifiers)
),
ts.createLiteral(importPath)
);
statements.unshift(newImport);

return ts.updateSourceFileNode(tsSourceFile, statements);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { catchError, loadTypeScriptDiagnostics } from '@utils';
import { getComponentMeta, getModuleFromSourceFile, getScriptTarget } from '../transform-utils';
import { updateHydrateComponentClass } from './hydrate-component';
import ts from 'typescript';
import { addLegacyImports } from '../core-runtime-apis';


export const transformToHydrateComponentText = (compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx, cmp: d.ComponentCompilerMeta, inputJsText: string) => {
Expand Down Expand Up @@ -69,6 +70,9 @@ const hydrateComponentTransform = (compilerCtx: d.CompilerCtx, transformOpts: d.

tsSourceFile = ts.visitEachChild(tsSourceFile, visitNode, transformCtx);

if (moduleFile.isLegacy) {
addLegacyImports(moduleFile);
}
tsSourceFile = addImports(transformOpts, tsSourceFile, moduleFile.coreRuntimeApis, transformOpts.coreImportPath);

return tsSourceFile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { catchError, loadTypeScriptDiagnostics } from '@utils';
import { getComponentMeta, getModuleFromSourceFile, getScriptTarget } from '../transform-utils';
import { updateLazyComponentClass } from './lazy-component';
import ts from 'typescript';
import { addLegacyImports } from '../core-runtime-apis';


export const transformToLazyComponentText = (compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx, transformOpts: d.TransformOptions, cmp: d.ComponentCompilerMeta, inputText: string) => {
Expand Down Expand Up @@ -62,6 +63,9 @@ export const lazyComponentTransform = (compilerCtx: d.CompilerCtx, transformOpts

tsSourceFile = ts.visitEachChild(tsSourceFile, visitNode, transformCtx);

if (moduleFile.isLegacy) {
addLegacyImports(moduleFile);
}
tsSourceFile = addImports(transformOpts, tsSourceFile, moduleFile.coreRuntimeApis, transformOpts.coreImportPath);

return tsSourceFile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { catchError, loadTypeScriptDiagnostics } from '@utils';
import { defineCustomElement } from '../define-custom-element';
import { updateNativeComponentClass } from './native-component';
import ts from 'typescript';
import { addLegacyImports } from '../core-runtime-apis';


export const transformToNativeComponentText = (compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx, cmp: d.ComponentCompilerMeta, inputJsText: string) => {
Expand Down Expand Up @@ -86,6 +87,9 @@ export const nativeComponentTransform = (compilerCtx: d.CompilerCtx, transformOp
}
}

if (moduleFile.isLegacy) {
addLegacyImports(moduleFile);
}
tsSourceFile = addImports(transformOpts, tsSourceFile, moduleFile.coreRuntimeApis, transformOpts.coreImportPath);

return tsSourceFile;
Expand Down
5 changes: 5 additions & 0 deletions src/compiler/transformers/core-runtime-apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const RUNTIME_APIS = {
getContext: `getContext as ${GET_CONTEXT}`,
getElement: `getElement as ${GET_ELEMENT}`,
h: `h as ${H}`,
legacyH: `h`,
Host: `Host as ${HOST}`,
HTMLElement: HTML_ELEMENT,
proxyCustomElement: `proxyCustomElement as ${PROXY_CUSTOM_ELEMENT}`,
Expand All @@ -36,3 +37,7 @@ export const addCoreRuntimeApi = (moduleFile: d.Module, coreRuntimeApi: string)
moduleFile.coreRuntimeApis.push(coreRuntimeApi);
}
};

export const addLegacyImports = (moduleFile: d.Module) => {
addCoreRuntimeApi(moduleFile, RUNTIME_APIS.legacyH);
};

0 comments on commit 93cb82e

Please sign in to comment.