Skip to content

Commit

Permalink
feat(cli): 优化 npm 安装包资源分析,允许引入 npm 安装包自身依赖资源 close #423
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Oct 1, 2018
1 parent fb10108 commit 14af040
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
28 changes: 28 additions & 0 deletions .project
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>taro</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>com.aptana.ide.core.unifiedBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>com.aptana.projects.webnature</nature>
</natures>
<filteredResources>
<filter>
<id>1538360037677</id>
<name></name>
<type>26</type>
<matcher>
<id>org.eclipse.ui.ide.multiFilter</id>
<arguments>1.0-name-matches-false-false-node_modules</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -36,6 +36,7 @@ Taro 已经投入了我们的生产环境中使用,业界也在广泛地使用

## 文章教程

* [从0到1构建适配不同端(微信小程序、H5、React-Native 等)的taro + dva应用](https://juejin.im/post/5bb1766d5188255c3272cdd0)
* [【小程序taro最佳实践】http请求封装(方便使用,增加token,统一错误日志记录和上报)](https://segmentfault.com/a/1190000016533592)
* [【小程序taro 最佳实践】异步action优雅实践(简化流程)](https://segmentfault.com/a/1190000016534001)
* [使用Taro框架开发小程序](https://juejin.im/post/5ba0a53af265da0ab5037234)
Expand Down
25 changes: 18 additions & 7 deletions packages/taro-cli/src/util/resolve_npm_files.js
Expand Up @@ -35,9 +35,9 @@ const projectConfig = require(configDir)(_.merge)
const pluginsConfig = projectConfig.plugins || {}
const outputDirName = projectConfig.outputRoot || CONFIG.OUTPUT_DIR

function resolveNpmPkgMainPath (pkgName, isProduction, npmConfig) {
function resolveNpmPkgMainPath (pkgName, isProduction, npmConfig, root = basedir) {
try {
return resolvePath.sync(pkgName, { basedir })
return resolvePath.sync(pkgName, { basedir: root })
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') {
console.log(`缺少npm包${pkgName},开始安装...`)
Expand All @@ -46,14 +46,23 @@ function resolveNpmPkgMainPath (pkgName, isProduction, npmConfig) {
installOptions.dev = true
}
npmProcess.installNpmPkg(pkgName, installOptions)
return resolveNpmPkgMainPath(pkgName, isProduction, npmConfig)
return resolveNpmPkgMainPath(pkgName, isProduction, npmConfig, root)
}
}
}

function resolveNpmFilesPath (pkgName, isProduction, npmConfig) {
function recursiveFindNodeModules (filePath) {
const dirname = path.dirname(filePath)
const nodeModules = path.join(dirname, 'node_modules')
if (fs.existsSync(nodeModules)) {
return nodeModules
}
return recursiveFindNodeModules(dirname)
}

function resolveNpmFilesPath (pkgName, isProduction, npmConfig, root = basedir) {
if (!resolvedCache[pkgName]) {
const res = resolveNpmPkgMainPath(pkgName, isProduction, npmConfig)
const res = resolveNpmPkgMainPath(pkgName, isProduction, npmConfig, root)
resolvedCache[pkgName] = {
main: res,
files: []
Expand Down Expand Up @@ -101,8 +110,9 @@ function parseAst (ast, filePath, files, isProduction, npmConfig) {
if (excludeRequire.indexOf(requirePath) < 0) {
if (isNpmPkg(requirePath)) {
if (excludeNpmPkgs.indexOf(requirePath) < 0) {
const res = resolveNpmFilesPath(requirePath, isProduction, npmConfig)
const relativeRequirePath = promoteRelativePath(path.relative(filePath, res.main))
const res = resolveNpmFilesPath(requirePath, isProduction, npmConfig, path.dirname(recursiveFindNodeModules(filePath)))
let relativeRequirePath = promoteRelativePath(path.relative(filePath, res.main))
relativeRequirePath = relativeRequirePath.replace(/node_modules/g, npmConfig.name)
args[0].value = relativeRequirePath
}
} else {
Expand Down Expand Up @@ -136,6 +146,7 @@ function recursiveRequire (filePath, files, isProduction, npmConfig = {}) {
let outputNpmPath
if (!npmConfig.dir) {
outputNpmPath = filePath.replace('node_modules', path.join(outputDirName, npmConfig.name))
outputNpmPath = outputNpmPath.replace(/node_modules/g, npmConfig.name)
} else {
const npmFilePath = filePath.replace(/(.*)node_modules/, '')
outputNpmPath = path.join(path.resolve(configDir, '..', npmConfig.dir), npmConfig.name, npmFilePath)
Expand Down

0 comments on commit 14af040

Please sign in to comment.