diff --git a/.project b/.project new file mode 100644 index 000000000000..e05f98ded655 --- /dev/null +++ b/.project @@ -0,0 +1,28 @@ + + + taro + + + + + + com.aptana.ide.core.unifiedBuilder + + + + + + com.aptana.projects.webnature + + + + 1538360037677 + + 26 + + org.eclipse.ui.ide.multiFilter + 1.0-name-matches-false-false-node_modules + + + + diff --git a/README.md b/README.md index 9897d1d76e4a..2c98ed7c2745 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/packages/taro-cli/src/util/resolve_npm_files.js b/packages/taro-cli/src/util/resolve_npm_files.js index a1db7c93c008..66a19db8c186 100644 --- a/packages/taro-cli/src/util/resolve_npm_files.js +++ b/packages/taro-cli/src/util/resolve_npm_files.js @@ -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},开始安装...`) @@ -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: [] @@ -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 { @@ -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)