Skip to content

Commit

Permalink
refactor(taroize): 把不影响运行的错误也导出结果
Browse files Browse the repository at this point in the history
  • Loading branch information
yuche authored and luckyadam committed Nov 19, 2018
1 parent 01c5f7c commit ad9340c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
2 changes: 2 additions & 0 deletions packages/taroize/src/global.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export const usedComponents = new Set<string>()

export const errors: string[] = []
4 changes: 3 additions & 1 deletion packages/taroize/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as t from 'babel-types'
import { parseWXML } from './wxml'
import { parseScript } from './script'
import { parseJSON } from './json'
import { errors } from './global'

interface Option {
json?: string,
Expand All @@ -16,6 +17,7 @@ export function parse (option: Option) {
const ast = parseScript(option.script, wxml as t.Expression, json, wxses)
return {
ast,
imports
imports,
errors
}
}
6 changes: 3 additions & 3 deletions packages/taroize/src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { buildRender, buildBlockElement } from './utils'
import { relative, dirname, resolve } from 'path'
import * as fs from 'fs'
import { parseWXML } from './wxml'
import { errors } from './global'

export function parseTemplate (path: NodePath<t.JSXElement>) {
const openingElement = path.get('openingElement')
Expand Down Expand Up @@ -130,8 +131,7 @@ function getWXMLsource (dirPath: string, src: string, type: string) {
try {
return fs.readFileSync(resolve(dirPath, src), 'utf-8')
} catch (e) {
// tslint:disable-next-line
console.error(`找不到这个路径的 wxml: <${type} src="${src}" />,该标签将会被忽略掉`)
errors.push(`找不到这个路径的 wxml: <${type} src="${src}" />,该标签将会被忽略掉`)
return ''
}
}
Expand All @@ -150,7 +150,7 @@ export function parseModule (jsx: NodePath<t.JSXElement>, dirPath: string, type:
const srcValue = value.node.value
if (type === 'import') {
const wxml = getWXMLsource(dirPath, srcValue, type)
const { imports } = parseWXML(dirname(relative(dirPath, srcValue)), wxml)
const { imports } = parseWXML(dirname(relative(dirPath, srcValue)), wxml, true)
jsx.remove()
return imports
} else {
Expand Down
7 changes: 5 additions & 2 deletions packages/taroize/src/wxml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import traverse, { NodePath } from 'babel-traverse'
import { buildTemplate, DEFAULT_Component_SET, buildImportStatement, buildBlockElement } from './utils'
import { specialEvents } from './events'
import { parseTemplate, parseModule } from './template'
import { usedComponents } from './global'
import { usedComponents, errors } from './global'
// const generate = require('babel-generator').default

const allCamelCase = (str: string) =>
Expand Down Expand Up @@ -83,11 +83,14 @@ function buildElement (
}
}

export function parseWXML (dirPath: string, wxml?: string): {
export function parseWXML (dirPath: string, wxml?: string, parseImport?: boolean): {
wxses: WXS[]
wxml?: t.Node
imports: Imports[]
} {
if (!parseImport) {
errors.length = 0
}
usedComponents.clear()
let wxses: WXS[] = []
let imports: Imports[] = []
Expand Down

0 comments on commit ad9340c

Please sign in to comment.