diff --git a/packages/taro-cli/src/h5/index.ts b/packages/taro-cli/src/h5/index.ts index dc695135b52a..a7ba5d32e098 100644 --- a/packages/taro-cli/src/h5/index.ts +++ b/packages/taro-cli/src/h5/index.ts @@ -232,8 +232,9 @@ class Compiler { .on('unlink', filePath => { const relativePath = path.relative(appPath, filePath) const extname = path.extname(relativePath) + const distDirname = this.getTempDir(filePath) const isScriptFile = REG_SCRIPTS.test(extname) - const dist = this.getDist(filePath, isScriptFile) + const dist = this.getDist(distDirname, filePath, isScriptFile) printLog(processTypeEnum.UNLINK, '删除文件', relativePath) fs.unlinkSync(dist) }) @@ -267,7 +268,6 @@ class Compiler { let hasComponentDidShow = false let hasComponentDidHide = false let hasComponentWillUnmount = false - let hasJSX = false let hasNerv = false let stateNode: t.ClassProperty @@ -542,6 +542,7 @@ class Compiler { } } + // require('fs').writeFileSync('./ast.json', JSON.stringify(ast, null, 2)) traverse(ast, { ClassExpression: ClassDeclarationOrExpression, ClassDeclaration: ClassDeclarationOrExpression, @@ -669,11 +670,6 @@ class Compiler { } } }, - JSXElement: { - enter (astPath: NodePath) { - hasJSX = true - } - }, JSXOpeningElement: { enter (astPath: NodePath) { const node = astPath.node @@ -709,10 +705,10 @@ class Compiler { createHistoryNode, mountApisNode ] - astPath.traverse(programExitVisitor) - if (hasJSX && !hasNerv) { + /* Taro.render() 会被移除,导致hasJSX判断错误 */ + if (!hasNerv) { extraNodes.unshift( t.importDeclaration( [t.importDefaultSpecifier(t.identifier(nervJsImportDefaultName))], @@ -887,9 +883,13 @@ class Compiler { } } }, + JSXElement: { + exit (astPath: NodePath) { + hasJSX = true + } + }, JSXOpeningElement: { exit (astPath: NodePath) { - hasJSX = true const node = astPath.node const componentName = componentnameMap.get(toVar(node.name)) const componentId = getComponentId(componentName, node) @@ -1143,16 +1143,23 @@ class Compiler { } } - processFiles (filePath) { - const sourceRoot = this.sourceRoot + getTempDir (filePath) { + const appPath = this.appPath + const sourcePath = this.sourcePath const tempDir = this.tempDir - const file = fs.readFileSync(filePath) const dirname = path.dirname(filePath) + const relPath = path.relative(sourcePath, dirname) + + return path.resolve(appPath, tempDir, relPath) + } + + processFiles (filePath) { + const file = fs.readFileSync(filePath) const extname = path.extname(filePath) - const distDirname = dirname.replace(sourceRoot, tempDir) + const distDirname = this.getTempDir(filePath) const isScriptFile = REG_SCRIPTS.test(extname) - const distPath = this.getDist(filePath, isScriptFile) + const distPath = this.getDist(distDirname, filePath, isScriptFile) try { if (isScriptFile) { @@ -1179,12 +1186,7 @@ class Compiler { } } - getDist (filename, isScriptFile) { - const sourceRoot = this.sourceRoot - const tempDir = this.tempDir - - const dirname = path.dirname(filename) - const distDirname = dirname.replace(sourceRoot, tempDir) + getDist (distDirname, filename, isScriptFile) { return isScriptFile ? path.format({ dir: distDirname, diff --git a/packages/taro-components/src/components/video/index.js b/packages/taro-components/src/components/video/index.js index 77e114984f7b..d39bd1ea7cce 100644 --- a/packages/taro-components/src/components/video/index.js +++ b/packages/taro-components/src/components/video/index.js @@ -210,6 +210,7 @@ class Video extends Component { } onLoadedMetadata = e => { + if (this.props.poster) return this.setState({ duration: this.videoRef.duration }) diff --git a/packages/taro-transformer-wx/src/class.ts b/packages/taro-transformer-wx/src/class.ts index e735ea34f486..c2680d826f52 100644 --- a/packages/taro-transformer-wx/src/class.ts +++ b/packages/taro-transformer-wx/src/class.ts @@ -741,7 +741,7 @@ class Transformer { } if (id.name.endsWith(CONTEXT_PROVIDER)) { - const valueAttr = path.node.openingElement.attributes.find(a => a.name.name === 'value') + const valueAttr = path.node.openingElement.attributes.find(a => t.isJSXIdentifier(a.name) && a.name.name === 'value') const contextName = id.name.slice(0, id.name.length - CONTEXT_PROVIDER.length) if (valueAttr) { if (t.isJSXElement(valueAttr.value)) { diff --git a/packages/taro-transformer-wx/src/render.ts b/packages/taro-transformer-wx/src/render.ts index 68d38432ed20..a3ea45be558c 100644 --- a/packages/taro-transformer-wx/src/render.ts +++ b/packages/taro-transformer-wx/src/render.ts @@ -502,7 +502,7 @@ export class RenderParser { } } const block = buildBlockElement() - const hasIfAttr = jsxElementPath.node.openingElement.attributes.find(a => a.name.name === Adapter.if) + const hasIfAttr = jsxElementPath.node.openingElement.attributes.find(a => t.isJSXIdentifier(a.name) && a.name.name === Adapter.if) const needWrapper = Adapters.swan === Adapter.type && hasIfAttr if (needWrapper) { block.children = [jsxElementPath.node] @@ -872,7 +872,7 @@ export class RenderParser { newJSXIfAttr(jsxElementPath.node, test, jsxElementPath) } } - const ifAttr = block.openingElement.attributes.find(a => a.name.name === Adapter.if) + const ifAttr = block.openingElement.attributes.find(a => t.isJSXIdentifier(a.name) && a.name.name === Adapter.if) if (ifAttr && t.isJSXExpressionContainer(ifAttr.value, { expression: test })) { const newBlock = buildBlockElement() newBlock.children = [block, jsxElementPath.node] @@ -883,7 +883,7 @@ export class RenderParser { let hasNest = false this.handleNestedIfStatement(block, jsxElementPath.node, parentIfStatement.node.test, hasNest, isElse || !!ifStatement.findParent(p => p.node === parentIfStatement.node.alternate)) if (!hasNest && parentIfStatement.get('alternate') !== ifStatement) { - const ifAttr = block.openingElement.attributes.find(a => a.name.name === Adapter.if) + const ifAttr = block.openingElement.attributes.find(a => t.isJSXIdentifier(a.name) && a.name.name === Adapter.if) if (ifAttr && t.isJSXExpressionContainer(ifAttr.value, { expression: parentIfStatement.node.test })) { const newBlock = buildBlockElement() block.children.push(jsxElementPath.node) @@ -922,8 +922,8 @@ export class RenderParser { if (!t.isJSXElement(child)) { continue } - const ifAttr = child.openingElement.attributes.find(a => a.name.name === Adapter.if) - const ifElseAttr = child.openingElement.attributes.find(a => a.name.name === Adapter.elseif) + const ifAttr = child.openingElement.attributes.find(a => t.isJSXIdentifier(a.name) && a.name.name === Adapter.if) + const ifElseAttr = child.openingElement.attributes.find(a => t.isJSXIdentifier(a.name) && a.name.name === Adapter.elseif) if ( (ifAttr && t.isJSXExpressionContainer(ifAttr.value, { expression: test })) || @@ -946,8 +946,8 @@ export class RenderParser { if (!t.isJSXElement(child)) { continue } - const ifAttr = child.openingElement.attributes.find(a => a.name.name === Adapter.if) - const ifElseAttr = child.openingElement.attributes.find(a => a.name.name === Adapter.elseif) + const ifAttr = child.openingElement.attributes.find(a => t.isJSXIdentifier(a.name) && a.name.name === Adapter.if) + const ifElseAttr = child.openingElement.attributes.find(a => t.isJSXIdentifier(a.name) && a.name.name === Adapter.elseif) if ( (ifAttr && t.isJSXExpressionContainer(ifAttr.value, { expression: test })) ||