Skip to content

Commit

Permalink
feat: support defineTemplateComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
meixg committed May 19, 2022
1 parent a547f88 commit 15c17ec
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 53 deletions.
48 changes: 28 additions & 20 deletions bin/debug-local.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,37 @@ const { markExternalComponent, SanProject, cancelMarkExternalComponent } = requi
const fs = require('fs')
const path = require('path')

function compile (componentPath, externalPath, outputPath) {
markExternalComponent({
isExternalComponent (id) {
if (id === externalPath) {
return true
}
}
})

// offline
const MyComponent = require(componentPath)
// component to source
// function compile (componentPath, externalPath, outputPath) {
// markExternalComponent({
// isExternalComponent (id) {
// if (id === externalPath) {
// return true
// }
// }
// })

// // offline
// const MyComponent = require(componentPath)
// const sanProject = new SanProject()
// const res = sanProject.compileToSource(MyComponent, 'js', {
// useProvidedComponentClass: true
// })

// cancelMarkExternalComponent()

// fs.writeFileSync(path.resolve(__dirname, outputPath), res)
// }

// js to source
function compile (componentPath, _, outputPath) {
const sanProject = new SanProject()
const res = sanProject.compileToSource(MyComponent, 'js', {
useProvidedComponentClass: true
})

cancelMarkExternalComponent()

fs.writeFileSync(path.resolve(__dirname, outputPath), res)
const res = sanProject.compileToSource(path.resolve(__dirname, componentPath), 'js')
fs.writeFileSync(path.resolve(__dirname, outputPath), res);
}

compile('./sample/component', './component2', './dist/component.js')
compile('./sample/component2', './component', './dist/component2.js')
compile('./sample/component.js', './component2', './dist/component.js')
compile('./sample/component2.js', './component', './dist/component2.js')

// // online
const Component = require('./sample/component')
Expand Down
53 changes: 27 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"jest": "^27.0.6",
"mkdirp": "^1.0.4",
"mustache": "^4.0.1",
"san": "^3.10.0",
"san": "^3.12.0",
"san-html-cases": "^3.10.29",
"san-ssr-target-fake-cmd": "^1.0.0",
"san-ssr-target-fake-esm": "^1.0.0",
Expand Down
24 changes: 18 additions & 6 deletions src/parsers/javascript-san-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ export class JavaScriptSanParser {
entryComponentInfo?: JSComponentInfo

private sanComponentIdentifier?: string
private defineComponentIdentifier?: string
private defineComponentIdentifier: string
private defineTemplateComponentIdentifier: string
private defaultExport?: string
private imports: Map<LocalName, [ImportSpecifier, ImportName]> = new Map()
private exports: Map<LocalName, ExportName> = new Map()
Expand All @@ -75,6 +76,8 @@ export class JavaScriptSanParser {
sourceType: 'module' | 'script' = 'script',
options?: parseSanSourceFileOptions
) {
this.defineComponentIdentifier = 'defineComponent'
this.defineTemplateComponentIdentifier = 'defineTemplateComponent'
this.root = parse(
fileContent === undefined ? readFileSync(filePath, 'utf8') : fileContent,
{ ecmaVersion: 2020, sourceType }
Expand Down Expand Up @@ -214,7 +217,15 @@ export class JavaScriptSanParser {
if (imported === 'defineComponent' && specifier === 'san') {
this.defineComponentIdentifier = local
}
if (imported === 'defineTemplateComponent' && specifier === 'san') {
this.defineTemplateComponentIdentifier = local
}
}
if (this.sanReferenceInfo) {
this.sanComponentIdentifier = this.sanReferenceInfo.moduleName
this.defineComponentIdentifier = this.sanReferenceInfo.methodName || this.defineComponentIdentifier
}

for (const [local, exported] of findExportNames(this.root)) {
if (exported === 'default') this.defaultExport = local
this.exports.set(local, exported)
Expand Down Expand Up @@ -278,23 +289,24 @@ export class JavaScriptSanParser {

private isDefineComponentCall (node: Node): node is CallExpression {
return isCallExpression(node) &&
this.isImportedFromSan(node.callee, this.sanReferenceInfo?.methodName || 'defineComponent')
(this.isImportedFromSanWithName(node.callee, this.defineComponentIdentifier) ||
this.isImportedFromSanWithName(node.callee, this.defineTemplateComponentIdentifier))
}

private isCreateComponentLoaderCall (node: Node): node is CallExpression {
return isCallExpression(node) && this.isImportedFromSan(node.callee, 'createComponentLoader')
return isCallExpression(node) && this.isImportedFromSanWithName(node.callee, 'createComponentLoader')
}

private isComponentClass (node: Node): node is Class {
return isClass(node) && !!node.superClass && this.isImportedFromSan(node.superClass, 'Component')
return isClass(node) && !!node.superClass && this.isImportedFromSanWithName(node.superClass, 'Component')
}

private isImportedFromSan (expr: Node, sanExport: string): boolean {
private isImportedFromSanWithName (expr: Node, sanExport: string): boolean {
if (isIdentifier(expr)) {
return this.isImportedFrom(expr.name, this.sanReferenceInfo?.moduleName || 'san', sanExport)
}
if (isMemberExpression(expr)) {
return this.isImportedFromSan(expr.object, 'default') && getStringValue(expr.property) === sanExport
return this.isImportedFromSanWithName(expr.object, 'default') && getStringValue(expr.property) === sanExport
}
if (isCallExpression(expr)) {
return isRequireSpecifier(expr, this.sanReferenceInfo?.moduleName || 'san') && sanExport === 'default'
Expand Down

0 comments on commit 15c17ec

Please sign in to comment.