Skip to content

Commit

Permalink
feat(cli): 小程序端 npm 目录支持配置
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyadam committed Aug 6, 2018
1 parent 38c87da commit 9a816a5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
25 changes: 16 additions & 9 deletions packages/taro-cli/src/util/resolve_npm_files.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const {
} = require('./index')

const npmProcess = require('./npm')
const { NPM_DIR, OUTPUT_DIR } = require('../config')

const requireRegex = /require\(['"]([\w\d_\-./@]+)['"]\)/ig
const excludeNpmPkgs = ['ReactPropTypes']
Expand All @@ -24,10 +23,12 @@ const resolvedCache = {}
const copyedFiles = {}

const basedir = process.cwd()
const projectConfig = require(path.join(basedir, PROJECT_CONFIG))(_.merge)
const configDir = path.join(basedir, PROJECT_CONFIG)
const projectConfig = require(configDir)(_.merge)
const pluginsConfig = projectConfig.plugins || {}
const outputDirName = projectConfig.outputRoot || CONFIG.OUTPUT_DIR

function resolveNpmFilesPath (pkgName, isProduction) {
function resolveNpmFilesPath (pkgName, isProduction, npmConfig) {
if (!resolvedCache[pkgName]) {
try {
const res = resolvePath.sync(pkgName, { basedir })
Expand All @@ -36,7 +37,7 @@ function resolveNpmFilesPath (pkgName, isProduction) {
files: []
}
resolvedCache[pkgName].files.push(res)
recursiveRequire(res, resolvedCache[pkgName].files, isProduction)
recursiveRequire(res, resolvedCache[pkgName].files, isProduction, npmConfig)
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') {
console.log(`缺少npm包${pkgName},开始安装...`)
Expand All @@ -45,14 +46,14 @@ function resolveNpmFilesPath (pkgName, isProduction) {
installOptions.dev = true
}
npmProcess.installNpmPkg(pkgName, installOptions)
return resolveNpmFilesPath(pkgName, isProduction)
return resolveNpmFilesPath(pkgName, isProduction, npmConfig)
}
}
}
return resolvedCache[pkgName]
}

function recursiveRequire (filePath, files, isProduction) {
function recursiveRequire (filePath, files, isProduction, npmConfig = {}) {
let fileContent = fs.readFileSync(filePath).toString()
fileContent = replaceContentEnv(fileContent, projectConfig.env || {})
fileContent = npmCodeHack(filePath, fileContent)
Expand All @@ -61,7 +62,7 @@ function recursiveRequire (filePath, files, isProduction) {
if (excludeNpmPkgs.indexOf(requirePath) >= 0) {
return `require('${requirePath}')`
}
const res = resolveNpmFilesPath(requirePath, isProduction)
const res = resolveNpmFilesPath(requirePath, isProduction, npmConfig)
const relativeRequirePath = promoteRelativePath(path.relative(filePath, res.main))
return `require('${relativeRequirePath}')`
}
Expand All @@ -78,11 +79,17 @@ function recursiveRequire (filePath, files, isProduction) {
}
if (files.indexOf(realRequirePath) < 0) {
files.push(realRequirePath)
recursiveRequire(realRequirePath, files, isProduction)
recursiveRequire(realRequirePath, files, isProduction, npmConfig)
}
return `require('${requirePath}')`
})
const outputNpmPath = filePath.replace('node_modules', path.join(OUTPUT_DIR, NPM_DIR))
let outputNpmPath
if (!npmConfig.dir) {
outputNpmPath = filePath.replace('node_modules', path.join(outputDirName, npmConfig.name))
} else {
const npmFilePath = filePath.replace(/(.*)node_modules/, '')
outputNpmPath = path.join(path.resolve(configDir, '..', npmConfig.dir), npmConfig.name, npmFilePath)
}
if (!copyedFiles[outputNpmPath]) {
if (isProduction) {
const uglifyPluginConfig = pluginsConfig.uglify || { enable: true }
Expand Down
20 changes: 15 additions & 5 deletions packages/taro-cli/src/weapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ const defaultBabelConfig = require('./config/babel')
const defaultTSConfig = require('./config/tsconfig.json')

const appPath = process.cwd()
const projectConfig = require(path.join(appPath, Util.PROJECT_CONFIG))(_.merge)
const configDir = path.join(appPath, Util.PROJECT_CONFIG)
const projectConfig = require(configDir)(_.merge)
const sourceDirName = projectConfig.sourceRoot || CONFIG.SOURCE_DIR
const outputDirName = projectConfig.outputRoot || CONFIG.OUTPUT_DIR
const sourceDir = path.join(appPath, sourceDirName)
Expand All @@ -36,6 +37,11 @@ const entryFileName = path.basename(entryFilePath)
const outputEntryFilePath = path.join(outputDir, entryFileName)

const pluginsConfig = projectConfig.plugins || {}
const weappConf = projectConfig.weapp || {}
const weappNpmConfig = Object.assign({
name: CONFIG.NPM_DIR,
dir: null
}, weappConf.npm)

const notExistNpmList = []
const taroJsFramework = '@tarojs/taro'
Expand Down Expand Up @@ -64,9 +70,15 @@ const isWindows = os.platform() === 'win32'

function getExactedNpmFilePath (npmName, filePath) {
try {
const npmInfo = resolveNpmFilesPath(npmName, isProduction)
const npmInfo = resolveNpmFilesPath(npmName, isProduction, weappNpmConfig)
const npmInfoMainPath = npmInfo.main
const outputNpmPath = npmInfoMainPath.replace('node_modules', path.join(outputDirName, CONFIG.NPM_DIR))
let outputNpmPath
if (!weappNpmConfig.dir) {
outputNpmPath = npmInfoMainPath.replace('node_modules', path.join(outputDirName, weappNpmConfig.name))
} else {
const npmFilePath = npmInfoMainPath.replace(/(.*)node_modules/, '')
outputNpmPath = path.join(path.resolve(configDir, '..', weappNpmConfig.dir), weappNpmConfig.name, npmFilePath)
}
const relativePath = path.relative(filePath, outputNpmPath)
return Util.promoteRelativePath(relativePath)
} catch (err) {
Expand Down Expand Up @@ -950,7 +962,6 @@ async function buildSinglePage (page) {
}

async function processStyleWithPostCSS (styleObj) {
const weappConf = projectConfig.weapp || {}
const useModuleConf = weappConf.module || {}
const customPostcssConf = useModuleConf.postcss || {}
const customPxtransformConf = customPostcssConf.pxtransform || {}
Expand Down Expand Up @@ -1182,7 +1193,6 @@ function compileDepScripts (scriptFiles) {
scriptFiles.forEach(async item => {
if (path.isAbsolute(item)) {
const outputItem = item.replace(path.join(sourceDir), path.join(outputDir)).replace(path.extname(item), '.js')
const weappConf = projectConfig.weapp || {}
const useCompileConf = Object.assign({}, weappConf.compile)
const compileExclude = useCompileConf.exclude || []
let isInCompileExclude = false
Expand Down

0 comments on commit 9a816a5

Please sign in to comment.