Skip to content

Commit

Permalink
fix(rn): 使用 require 写法引入样式不编译
Browse files Browse the repository at this point in the history
  • Loading branch information
Pines-Cheng committed Aug 8, 2019
1 parent b31fdc0 commit 39334c6
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions packages/taro-cli/src/rn/transformJS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,19 @@ export function parseJSCode ({code, filePath, isEntryFile, projectConfig}) {
traverse(ast, {
ClassExpression: ClassDeclarationOrExpression,
ClassDeclaration: ClassDeclarationOrExpression,
ExpressionStatement (astPath) {
const node = astPath.node as t.ExpressionStatement
const expression = node.expression as t.CallExpression
const callee = expression.callee as t.Identifier
if (callee && callee.name === 'require') {
const argument = expression.arguments[0] as t.StringLiteral
const value = argument.value
const valueExtname = path.extname(value)
if (REG_STYLE.test(valueExtname)) {
astPath.replaceWith(t.importDeclaration([], t.stringLiteral(value)))
}
}
},
ImportDeclaration (astPath) {
const node = astPath.node as t.ImportDeclaration
const source = node.source
Expand All @@ -262,13 +275,13 @@ export function parseJSCode ({code, filePath, isEntryFile, projectConfig}) {
// if (pathArr.indexOf('pages') >= 0) {
// astPath.remove()
// } else
if (REG_SCRIPTS.test(value) || path.extname(value) === '') {
const absolutePath = path.resolve(filePath, '..', value)
const dirname = path.dirname(absolutePath)
const extname = path.extname(absolutePath)
const realFilePath = Util.resolveScriptPath(path.join(dirname, path.basename(absolutePath, extname)))
const removeExtPath = realFilePath.replace(path.extname(realFilePath), '')
node.source = t.stringLiteral(Util.promoteRelativePath(path.relative(filePath, removeExtPath)).replace(/\\/g, '/'))
if (REG_SCRIPTS.test(value) || path.extname(value) === '') {
const absolutePath = path.resolve(filePath, '..', value)
const dirname = path.dirname(absolutePath)
const extname = path.extname(absolutePath)
const realFilePath = Util.resolveScriptPath(path.join(dirname, path.basename(absolutePath, extname)))
const removeExtPath = realFilePath.replace(path.extname(realFilePath), '')
node.source = t.stringLiteral(Util.promoteRelativePath(path.relative(filePath, removeExtPath)).replace(/\\/g, '/'))
}
}
return
Expand Down

0 comments on commit 39334c6

Please sign in to comment.