Skip to content

Commit

Permalink
feat(cli): 支持编译 node_modules 中的包,close #1358
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Dec 5, 2018
1 parent 5c77f2d commit 8514833
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
30 changes: 23 additions & 7 deletions packages/taro-cli/src/util/resolve_npm_files.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const {
} = require('./index')

const CONFIG = require('../config')
const defaultBabelConfig = require('../config/babel')

const npmProcess = require('./npm')

Expand All @@ -37,6 +38,12 @@ const projectConfig = require(configDir)(_.merge)
const pluginsConfig = projectConfig.plugins || {}
const outputDirName = projectConfig.outputRoot || CONFIG.OUTPUT_DIR

const babelConfig = _.mergeWith(defaultBabelConfig, pluginsConfig.babel, (objValue, srcValue) => {
if (Array.isArray(objValue)) {
return Array.from(new Set(srcValue.concat(objValue)))
}
})

function resolveNpmPkgMainPath (pkgName, isProduction, npmConfig, buildAdapter = BUILD_TYPES.WEAPP, root = basedir) {
try {
return resolvePath.sync(pkgName, { basedir: root })
Expand All @@ -62,20 +69,20 @@ function recursiveFindNodeModules (filePath) {
return recursiveFindNodeModules(dirname)
}

function resolveNpmFilesPath (pkgName, isProduction, npmConfig, buildAdapter = BUILD_TYPES.WEAPP, root = basedir) {
function resolveNpmFilesPath (pkgName, isProduction, npmConfig, buildAdapter = BUILD_TYPES.WEAPP, root = basedir, compileInclude = []) {
if (!resolvedCache[pkgName]) {
const res = resolveNpmPkgMainPath(pkgName, isProduction, npmConfig, buildAdapter, root)
resolvedCache[pkgName] = {
main: res,
files: []
}
resolvedCache[pkgName].files.push(res)
recursiveRequire(res, resolvedCache[pkgName].files, isProduction, npmConfig, buildAdapter)
recursiveRequire(res, resolvedCache[pkgName].files, isProduction, npmConfig, buildAdapter, compileInclude)
}
return resolvedCache[pkgName]
}

function parseAst (ast, filePath, files, isProduction, npmConfig, buildAdapter = BUILD_TYPES.WEAPP) {
function parseAst (ast, filePath, files, isProduction, npmConfig, buildAdapter = BUILD_TYPES.WEAPP, compileInclude) {
const excludeRequire = []
traverse(ast, {
IfStatement (astPath) {
Expand Down Expand Up @@ -112,7 +119,7 @@ function parseAst (ast, filePath, files, isProduction, npmConfig, buildAdapter =
if (excludeRequire.indexOf(requirePath) < 0) {
if (isNpmPkg(requirePath)) {
if (excludeNpmPkgs.indexOf(requirePath) < 0) {
const res = resolveNpmFilesPath(requirePath, isProduction, npmConfig, buildAdapter, path.dirname(recursiveFindNodeModules(filePath)))
const res = resolveNpmFilesPath(requirePath, isProduction, npmConfig, buildAdapter, path.dirname(recursiveFindNodeModules(filePath)), compileInclude)
let relativeRequirePath = promoteRelativePath(path.relative(filePath, res.main))
relativeRequirePath = relativeRequirePath.replace(/node_modules/g, npmConfig.name)
if (buildAdapter === BUILD_TYPES.ALIPAY) {
Expand All @@ -133,7 +140,7 @@ function parseAst (ast, filePath, files, isProduction, npmConfig, buildAdapter =
}
if (files.indexOf(realRequirePath) < 0) {
files.push(realRequirePath)
recursiveRequire(realRequirePath, files, isProduction, npmConfig, buildAdapter)
recursiveRequire(realRequirePath, files, isProduction, npmConfig, buildAdapter, compileInclude)
}
args[0].value = requirePath
}
Expand All @@ -147,7 +154,7 @@ function parseAst (ast, filePath, files, isProduction, npmConfig, buildAdapter =
return generate(ast).code
}

function recursiveRequire (filePath, files, isProduction, npmConfig = {}, buildAdapter) {
async function recursiveRequire (filePath, files, isProduction, npmConfig = {}, buildAdapter, compileInclude = []) {
let fileContent = fs.readFileSync(filePath).toString()
let outputNpmPath
if (!npmConfig.dir) {
Expand Down Expand Up @@ -183,11 +190,20 @@ function recursiveRequire (filePath, files, isProduction, npmConfig = {}, buildA
[require('babel-plugin-transform-define').default, constantsReplaceList]
]
}).ast
fileContent = parseAst(ast, filePath, files, isProduction, npmConfig, buildAdapter)
fileContent = parseAst(ast, filePath, files, isProduction, npmConfig, buildAdapter, compileInclude)
} catch (err) {
console.log(err)
}
if (!copyedFiles[outputNpmPath]) {
if (compileInclude && compileInclude.length) {
const filePathArr = filePath.split(path.sep)
const nodeModulesIndex = filePathArr.indexOf('node_modules')
const npmPkgName = filePathArr[nodeModulesIndex + 1]
if (compileInclude.indexOf(npmPkgName) >= 0) {
const compileScriptRes = await npmProcess.callPlugin('babel', fileContent, filePath, babelConfig)
fileContent = compileScriptRes.code
}
}
if (isProduction) {
const uglifyPluginConfig = pluginsConfig.uglify || { enable: true }
if (uglifyPluginConfig.enable) {
Expand Down
4 changes: 3 additions & 1 deletion packages/taro-cli/src/weapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ let constantsReplaceList = Object.assign({}, Util.generateEnvList(projectConfig.

function getExactedNpmFilePath (npmName, filePath) {
try {
const npmInfo = resolveNpmFilesPath(npmName, isProduction, weappNpmConfig, buildAdapter)
const useCompileConf = Object.assign({}, weappConf.compile)
const compileInclude = useCompileConf.include || []
const npmInfo = resolveNpmFilesPath(npmName, isProduction, weappNpmConfig, buildAdapter, appPath, compileInclude)
const npmInfoMainPath = npmInfo.main
let outputNpmPath
if (Util.REG_STYLE.test(npmInfoMainPath)) {
Expand Down

0 comments on commit 8514833

Please sign in to comment.