From 4443ee0aa7d69792b70067fed6224ffd39df0479 Mon Sep 17 00:00:00 2001 From: luckyadam Date: Wed, 14 Aug 2019 21:26:16 +0800 Subject: [PATCH] =?UTF-8?q?fix(taro-cli):=20=E5=B0=8F=E7=A8=8B=E5=BA=8F?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=20npm=20=E4=B8=AD=E7=9A=84=20json=20?= =?UTF-8?q?=E5=BC=95=E7=94=A8=E9=9C=80=E7=9B=B4=E6=8E=A5=E6=9B=BF=E6=8D=A2?= =?UTF-8?q?=E4=B8=BA=20json=20=E5=80=BC=EF=BC=8Cclose=20#4164?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../taro-cli/src/util/resolve_npm_files.ts | 40 ++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/packages/taro-cli/src/util/resolve_npm_files.ts b/packages/taro-cli/src/util/resolve_npm_files.ts index 928924db646c..a589098bd51d 100644 --- a/packages/taro-cli/src/util/resolve_npm_files.ts +++ b/packages/taro-cli/src/util/resolve_npm_files.ts @@ -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'] @@ -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, @@ -308,6 +332,20 @@ function parseAst ({ if (callee.name === 'require') { const args = node.arguments as Array 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, @@ -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, '')