刮刮前端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

127 lines
3.5 KiB

const fs = require('fs')
const path = require('path')
const {
compileI18nJsonStr
} = require('@dcloudio/uni-i18n')
const {
initI18nOptions
} = require('@dcloudio/uni-cli-shared/lib/i18n')
const assetsDir = 'static'
const CopyWebpackPluginVersion = Number(require('copy-webpack-plugin/package.json').version.split('.')[0])
function getAssetsCopyOption (from, options = {}) {
if (path.isAbsolute(from)) {
if (fs.existsSync(from)) {
return Object.assign({
from,
to: path.resolve(process.env.UNI_OUTPUT_DIR)
},
options
)
}
}
const to = from
from = path.resolve(process.env.UNI_INPUT_DIR, from)
if (fs.existsSync(from)) {
return Object.assign({
from,
to: path.resolve(process.env.UNI_OUTPUT_DIR, to)
},
options
)
}
}
// 暂未考虑动态添加static目录
function getAssetsCopyOptions (assetsDir) {
const ignore = []
global.uniPlugin.platforms.forEach(platform => {
if (global.uniPlugin.name !== platform) {
if (CopyWebpackPluginVersion > 5) {
ignore.push(`${process.env.UNI_INPUT_DIR.replace(/\\/g, '/')}/static/${platform}/**/*`)
} else {
ignore.push(platform + '/**/*')
}
}
})
const copyOptions = []
// 主包静态资源
const mainAssetsCopyOption = getAssetsCopyOption(assetsDir, CopyWebpackPluginVersion > 5 ? {
globOptions: { ignore }
} : {
ignore
})
if (mainAssetsCopyOption) {
copyOptions.push(mainAssetsCopyOption)
}
// 分包静态资源
process.UNI_SUBPACKAGES &&
Object.keys(process.UNI_SUBPACKAGES).forEach(root => {
const subAssetsCopyOption = getAssetsCopyOption(path.join(root, assetsDir), CopyWebpackPluginVersion > 5 ? {
globOptions: { ignore }
} : {
ignore
})
if (subAssetsCopyOption) {
copyOptions.push(subAssetsCopyOption)
}
})
return copyOptions
}
function getUniModulesAssetsCopyOptions (assetsDir) {
const copyOptions = []
global.uniModules.forEach(module => {
copyOptions.push(
...getAssetsCopyOptions('uni_modules/' + module + '/' + assetsDir)
)
})
return copyOptions
}
function getCopyWebpackPluginOptions (platformOptions, vueOptions) {
const copyOptions = getAssetsCopyOptions(assetsDir).concat(
getUniModulesAssetsCopyOptions(assetsDir)
)
global.uniPlugin.copyWebpackOptions.forEach(copyWebpackOptions => {
const platformCopyOptions =
copyWebpackOptions(platformOptions, vueOptions, copyOptions) || []
platformCopyOptions.forEach(copyOption => {
if (typeof copyOption === 'string') {
copyOption = getAssetsCopyOption(copyOption)
}
copyOption && copyOptions.push(copyOption)
})
})
// 自动化测试时,不启用androidPrivacy.json
if (process.env.UNI_PLATFORM === 'app-plus' && !process.env.UNI_AUTOMATOR_WS_ENDPOINT) {
const fileName = 'androidPrivacy.json'
const context = path.resolve(process.env.UNI_INPUT_DIR)
if (fs.existsSync(path.join(context, fileName))) {
copyOptions.push({
from: fileName,
context,
to: fileName,
transform (content) {
const options = initI18nOptions(
process.env.UNI_PLATFORM,
process.env.UNI_INPUT_DIR,
false,
true
)
if (!options) {
return content
}
return compileI18nJsonStr(content.toString(), options)
}
})
}
}
return copyOptions
}
module.exports = {
assetsDir,
getCopyWebpackPluginOptions
}