Skip to content

Commit

Permalink
fix(taro-cli): 小程序解析 npm 中的 json 引用需直接替换为 json 值,close #4164
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Aug 14, 2019
1 parent a6af470 commit 4443ee0
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion packages/taro-cli/src/util/resolve_npm_files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import defaultUglifyConfig from '../config/uglify'

import * as npmProcess from './npm'
import { IInstallOptions, INpmConfig, IResolvedCache, TogglableOptions, ITaroManifestConfig } from './types'
import { convertArrayToAstExpression, convertObjectToAstExpression } from './astConvert'

const excludeNpmPkgs = ['ReactPropTypes']

Expand Down Expand Up @@ -284,6 +285,29 @@ function parseAst ({
const node = astPath.node
const source = node.source
const value = source.value
if (REG_JSON.test(value)) {
const realRequirePath = path.resolve(path.dirname(filePath), value)
if (fs.existsSync(realRequirePath)) {
const obj = JSON.parse(fs.readFileSync(realRequirePath).toString())
const specifiers = node.specifiers
let defaultSpecifier
specifiers.forEach(item => {
if (item.type === 'ImportDefaultSpecifier') {
defaultSpecifier = item.local.name
}
})
if (defaultSpecifier) {
let objArr: t.NullLiteral | t.Expression = t.nullLiteral()
if (Array.isArray(obj)) {
objArr = t.arrayExpression(convertArrayToAstExpression(obj))
} else {
objArr = t.objectExpression(convertObjectToAstExpression(obj))
}
astPath.replaceWith(t.variableDeclaration('const', [t.variableDeclarator(t.identifier(defaultSpecifier), objArr)]))
}
}
return
}
analyzeImportUrl({
requirePath: value,
excludeRequire,
Expand All @@ -308,6 +332,20 @@ function parseAst ({
if (callee.name === 'require') {
const args = node.arguments as Array<t.StringLiteral>
const requirePath = args[0].value
if (REG_JSON.test(requirePath)) {
const realRequirePath = path.resolve(path.dirname(filePath), requirePath)
if (fs.existsSync(realRequirePath)) {
const obj = JSON.parse(fs.readFileSync(realRequirePath).toString())
let objArr: t.NullLiteral | t.Expression | t.ObjectProperty[] = t.nullLiteral()
if (Array.isArray(obj)) {
objArr = t.arrayExpression(convertArrayToAstExpression(obj))
} else {
objArr = convertObjectToAstExpression(obj)
}
astPath.replaceWith(t.objectExpression(objArr as any))
}
return
}
analyzeImportUrl({
requirePath,
excludeRequire,
Expand Down Expand Up @@ -370,7 +408,7 @@ async function recursiveRequire ({
if (REG_STYLE.test(path.basename(filePath))) {
return
}
if (REG_FONT.test(filePath) || REG_IMAGE.test(filePath) || REG_MEDIA.test(filePath) || REG_JSON.test(filePath)) {
if (REG_FONT.test(filePath) || REG_IMAGE.test(filePath) || REG_MEDIA.test(filePath)) {
fs.ensureDirSync(path.dirname(outputNpmPath))
fs.writeFileSync(outputNpmPath, fileContent)
let modifyOutput = outputNpmPath.replace(path.dirname(rootNpm) + path.sep, '')
Expand Down

0 comments on commit 4443ee0

Please sign in to comment.