提交 b381a51d 编写于 作者: Q qiang

fix: support copy-webpack-plugin version 6+

上级 eb824626
......@@ -19,12 +19,13 @@ function transform(content) {
}
function getIndexCssPath(assetsDir, template) {
const CopyWebpackPluginVersion = Number(require('copy-webpack-plugin/package.json').version.split('.')[0])
const VUE_APP_INDEX_CSS_HASH = process.env.VUE_APP_INDEX_CSS_HASH
if (VUE_APP_INDEX_CSS_HASH) {
try {
const templateContent = fs.readFileSync(getTemplatePath(template))
if (/\bVUE_APP_INDEX_CSS_HASH\b/.test(templateContent)) {
return path.join(assetsDir, `[name].${VUE_APP_INDEX_CSS_HASH}.[ext]`)
return path.join(assetsDir, `[name].${VUE_APP_INDEX_CSS_HASH}${CopyWebpackPluginVersion > 5 ? '' : '.'}[ext]`)
}
} catch (e) {}
}
......
......@@ -6,12 +6,17 @@ const COMPONENTS_DIR_NAME = 'wxcomponents'
function getComponentsCopyOption () {
if (process.env.UNI_OUTPUT_TMP_DIR) { // TODO v3不需要,即将废弃
const componentsDir = path.resolve(process.env.UNI_INPUT_DIR, COMPONENTS_DIR_NAME)
const CopyWebpackPluginVersion = Number(require('copy-webpack-plugin/package.json').version.split('.')[0])
if (fs.existsSync(componentsDir)) {
return {
const ignore = ['**/*.vue', '**/*.css']
return Object.assign({
from: componentsDir,
to: COMPONENTS_DIR_NAME,
ignore: ['**/*.vue', '**/*.css']
}
to: COMPONENTS_DIR_NAME
}, CopyWebpackPluginVersion > 5 ? {
globOptions: { ignore }
} : {
ignore
})
}
}
}
......
......@@ -22,6 +22,7 @@ module.exports = {
subPackages: true
},
copyWebpackOptions (platformOptions, vueOptions) {
const CopyWebpackPluginVersion = Number(require('copy-webpack-plugin/package.json').version.split('.')[0])
const copyOptions = [
// 'sitemap.json',
// 'ext.json',
......@@ -31,21 +32,29 @@ module.exports = {
workers && copyOptions.push(workers)
const wxcomponentsDir = path.resolve(process.env.UNI_INPUT_DIR, COMPONENTS_DIR_NAME)
const ignore = ['**/*.vue', '**/*.css'] // v3 会自动转换生成vue,css文件,需要过滤
if (fs.existsSync(wxcomponentsDir)) {
copyOptions.push({
copyOptions.push(Object.assign({
from: wxcomponentsDir,
to: COMPONENTS_DIR_NAME,
ignore: ['**/*.vue', '**/*.css'] // v3 会自动转换生成vue,css文件,需要过滤
})
to: COMPONENTS_DIR_NAME
}, CopyWebpackPluginVersion > 5 ? {
globOptions: { ignore }
} : {
ignore
}))
}
global.uniModules.forEach(module => {
const wxcomponentsDir = path.resolve(process.env.UNI_INPUT_DIR, 'uni_modules', module, COMPONENTS_DIR_NAME)
if (fs.existsSync(wxcomponentsDir)) {
copyOptions.push({
copyOptions.push(Object.assign({
from: wxcomponentsDir,
to: 'uni_modules/' + module + '/' + COMPONENTS_DIR_NAME,
ignore: ['**/*.vue', '**/*.css'] // v3 会自动转换生成vue,css文件,需要过滤
})
to: 'uni_modules/' + module + '/' + COMPONENTS_DIR_NAME
}, CopyWebpackPluginVersion > 5 ? {
globOptions: { ignore }
} : {
ignore
}))
}
})
return copyOptions
......
......@@ -24,12 +24,13 @@ module.exports = {
darkmode: true
},
copyWebpackOptions (platformOptions, vueOptions) {
const CopyWebpackPluginVersion = Number(require('copy-webpack-plugin/package.json').version.split('.')[0])
const copyOptions = [
'theme.json',
'sitemap.json',
'ext.json',
'custom-tab-bar',
'functional-pages',
'functional-pages',
'project.private.config.json'
]
......@@ -46,25 +47,32 @@ module.exports = {
const manifestConfig = process.UNI_MANIFEST
const weixinConfig = manifestConfig['mp-weixin'] || {}
const copyWxComponentsOnDemandSwitch = !!weixinConfig.copyWxComponentsOnDemand // 默认值false
const ignore = ['**/*.vue', '**/*.css'] // v3 会自动转换生成vue,css文件,需要过滤
if (!copyWxComponentsOnDemandSwitch) {
const wxcomponentsDir = path.resolve(process.env.UNI_INPUT_DIR, COMPONENTS_DIR_NAME)
if (fs.existsSync(wxcomponentsDir)) {
copyOptions.push({
copyOptions.push(Object.assign({
from: wxcomponentsDir,
to: COMPONENTS_DIR_NAME,
ignore: ['**/*.vue', '**/*.css'] // v3 会自动转换生成vue,css文件,需要过滤
})
to: COMPONENTS_DIR_NAME
}, CopyWebpackPluginVersion > 5 ? {
globOptions: { ignore }
} : {
ignore
}))
}
}
global.uniModules.forEach(module => {
const wxcomponentsDir = path.resolve(process.env.UNI_INPUT_DIR, 'uni_modules', module, COMPONENTS_DIR_NAME)
if (fs.existsSync(wxcomponentsDir)) {
copyOptions.push({
copyOptions.push(Object.assign({
from: wxcomponentsDir,
to: 'uni_modules/' + module + '/' + COMPONENTS_DIR_NAME,
ignore: ['**/*.vue', '**/*.css'] // v3 会自动转换生成vue,css文件,需要过滤
})
to: 'uni_modules/' + module + '/' + COMPONENTS_DIR_NAME
}, CopyWebpackPluginVersion > 5 ? {
globOptions: { ignore }
} : {
ignore
}))
}
})
return copyOptions
......
......@@ -2,6 +2,7 @@ const path = require('path')
const webpack = require('webpack')
const CopyPlugin = require('copy-webpack-plugin')
const CopyPluginVersion = Number(require('copy-webpack-plugin/package.json').version.split('.')[0])
const HandlerPlugin = require('@hap-toolkit/packager/lib/plugin/handler-plugin')
const ZipPlugin = require('@hap-toolkit/packager/lib/plugin/zip-plugin')
......@@ -39,6 +40,11 @@ function genPriorities (entryPagePath) {
const uniCloudPath = require.resolve('@dcloudio/vue-cli-plugin-uni/packages/uni-cloud/dist/index.js')
const patterns = [{
from: path.resolve(__dirname, '../dist/' + dslFilename),
to: 'dsl.js'
}]
module.exports = {
devtool: false,
entry () {
......@@ -77,10 +83,7 @@ module.exports = {
new webpack.ProvidePlugin({
uniCloud: [uniCloudPath, 'default']
}),
new CopyPlugin([{
from: path.resolve(__dirname, '../dist/' + dslFilename),
to: 'dsl.js'
}]),
new CopyPlugin(CopyPluginVersion > 5 ? { patterns } : patterns),
new HandlerPlugin({}),
new Css2jsonPlugin(),
new InstVuePlugin(),
......@@ -101,4 +104,4 @@ module.exports = {
}),
new NotifyPlugin()
]
}
}
......@@ -3,6 +3,7 @@ const path = require('path')
const webpack = require('webpack')
const VueLoaderPlugin = require('@dcloudio/vue-cli-plugin-uni/packages/vue-loader/lib/plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const CopyWebpackPluginVersion = Number(require('copy-webpack-plugin/package.json').version.split('.')[0])
const TerserPlugin = require('terser-webpack-plugin')
const {
......@@ -194,7 +195,7 @@ rules.unshift({
if (process.env.UNI_USING_V3_NATIVE) {
try {
const automatorJson = require.resolve('@dcloudio/uni-automator/dist/automator.json')
plugins.push(new CopyWebpackPlugin([{
const patterns = [{
from: automatorJson,
to: '../.automator/' + (process.env.UNI_SUB_PLATFORM || process.env.UNI_PLATFORM) +
'/.automator.json',
......@@ -207,7 +208,8 @@ if (process.env.UNI_USING_V3_NATIVE) {
}
return ''
}
}]))
}]
plugins.push(new CopyWebpackPlugin(CopyWebpackPluginVersion > 5 ? { patterns } : patterns))
} catch (e) { }
}
......@@ -215,7 +217,7 @@ if (process.env.UNI_USING_NATIVE || process.env.UNI_USING_V3_NATIVE) {
plugins.push(new WebpackUniMPPlugin())
const assetsDir = 'static'
const hybridDir = 'hybrid/html'
const array = [{
const patterns = [{
from: path.resolve(process.env.UNI_INPUT_DIR, assetsDir),
to: assetsDir
}]
......@@ -223,7 +225,7 @@ if (process.env.UNI_USING_NATIVE || process.env.UNI_USING_V3_NATIVE) {
if (!process.env.UNI_AUTOMATOR_WS_ENDPOINT) {
const androidPrivacyPath = path.resolve(process.env.UNI_INPUT_DIR, 'androidPrivacy.json')
if (fs.existsSync(androidPrivacyPath)) {
array.push({
patterns.push({
from: androidPrivacyPath,
to: 'androidPrivacy.json'
})
......@@ -231,7 +233,7 @@ if (process.env.UNI_USING_NATIVE || process.env.UNI_USING_V3_NATIVE) {
}
const hybridHtmlPath = path.resolve(process.env.UNI_INPUT_DIR, hybridDir)
if (fs.existsSync(hybridHtmlPath)) {
array.push({
patterns.push({
from: hybridHtmlPath,
to: hybridDir
})
......@@ -242,7 +244,7 @@ if (process.env.UNI_USING_NATIVE || process.env.UNI_USING_V3_NATIVE) {
const assets = modules + module + '/' + assetsDir
const assetsPath = path.resolve(process.env.UNI_INPUT_DIR, assets)
if (fs.existsSync(assetsPath)) {
array.push({
patterns.push({
from: assetsPath,
to: assets
})
......@@ -250,7 +252,7 @@ if (process.env.UNI_USING_NATIVE || process.env.UNI_USING_V3_NATIVE) {
const hybridHtml = modules + module + '/' + hybridDir
const hybridHtmlPath = path.resolve(process.env.UNI_INPUT_DIR, hybridHtml)
if (fs.existsSync(hybridHtmlPath)) {
array.push({
patterns.push({
from: hybridHtmlPath,
to: hybridHtml
})
......@@ -258,12 +260,12 @@ if (process.env.UNI_USING_NATIVE || process.env.UNI_USING_V3_NATIVE) {
})
if (process.env.UNI_USING_NVUE_COMPILER) {
array.push({
patterns.push({
from: path.resolve(getTemplatePath(), 'common'),
to: process.env.UNI_OUTPUT_DIR
})
} else if (process.env.UNI_USING_V3_NATIVE) {
array.push({
patterns.push({
from: path.resolve(getTemplatePath(), 'weex'),
to: process.env.UNI_OUTPUT_DIR
})
......@@ -275,7 +277,7 @@ if (process.env.UNI_USING_NATIVE || process.env.UNI_USING_V3_NATIVE) {
'weapp-tools/template/v8'
)
}
array.push({
patterns.push({
from: nativeTemplatePath,
to: process.env.UNI_OUTPUT_DIR
}, {
......@@ -284,15 +286,17 @@ if (process.env.UNI_USING_NATIVE || process.env.UNI_USING_V3_NATIVE) {
'weapp-tools/template/common'
),
to: process.env.UNI_OUTPUT_DIR,
ignore: [
'*.js',
'*.json',
'__uniapppicker.html',
'__uniappview.html'
]
globOptions: {
ignore: [
'*.js',
'*.json',
'__uniapppicker.html',
'__uniappview.html'
]
}
})
}
plugins.push(new CopyWebpackPlugin(array))
plugins.push(new CopyWebpackPlugin(CopyWebpackPluginVersion > 5 ? { patterns } : patterns))
}
try {
......
......@@ -14,13 +14,10 @@ class WebpackAppPlusNVuePlugin {
const changedFiles = []
compiler.hooks.emit.tapAsync('webpack-uni-nvue', (compilation, callback) => {
changedFiles.length = 0
const changedChunks = compilation.chunks.filter(chunk => {
compilation.chunks.forEach(chunk => {
const oldVersion = chunkVersions[chunk.name]
chunkVersions[chunk.name] = chunk.hash
return chunk.hash !== oldVersion
})
changedChunks.map(chunk => {
if (Array.isArray(chunk.files)) {
if (chunk.hash !== oldVersion && Array.isArray(chunk.files)) {
chunk.files.forEach(file => {
!changedFiles.includes(file) && (changedFiles.push(file))
})
......
......@@ -44,13 +44,10 @@ class WebpackAppPlusPlugin {
})
compiler.hooks.emit.tapAsync('WebpackAppPlusPlugin', (compilation, callback) => {
const changedChunks = compilation.chunks.filter(chunk => {
compilation.chunks.forEach(chunk => {
const oldVersion = chunkVersions[chunk.name]
chunkVersions[chunk.name] = chunk.hash
return chunk.hash !== oldVersion
})
changedChunks.map(chunk => {
if (Array.isArray(chunk.files)) {
if (chunk.hash !== oldVersion && Array.isArray(chunk.files)) {
chunk.files.forEach(file => {
if (isAppService) {
!serviceChangedFiles.includes(file) && (serviceChangedFiles.push(file))
......
......@@ -2,6 +2,7 @@ const fs = require('fs')
const path = require('path')
const webpack = require('webpack')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const CopyWebpackPluginVersion = Number(require('copy-webpack-plugin/package.json').version.split('.')[0])
const merge = require('webpack-merge')
......@@ -231,12 +232,13 @@ module.exports = function configureWebpack (platformOptions, manifestPlatformOpt
vueOptions.pluginOptions['uni-app-plus'].view
if (!isAppView) { // app-plus view不需要copy
plugins.push(new CopyWebpackPlugin(getCopyWebpackPluginOptions(manifestPlatformOptions, vueOptions)))
const patterns = getCopyWebpackPluginOptions(manifestPlatformOptions, vueOptions)
plugins.push(new CopyWebpackPlugin(CopyWebpackPluginVersion > 5 ? { patterns } : patterns))
}
if (!process.env.UNI_SUBPACKGE || !process.env.UNI_MP_PLUGIN) {
try {
const automatorJson = require.resolve('@dcloudio/uni-automator/dist/automator.json')
plugins.push(new CopyWebpackPlugin([{
const patterns = [{
from: automatorJson,
to: '../.automator/' + (process.env.UNI_SUB_PLATFORM || process.env.UNI_PLATFORM) +
'/.automator.json',
......@@ -249,8 +251,9 @@ module.exports = function configureWebpack (platformOptions, manifestPlatformOpt
}
return ''
}
}]))
} catch (e) {}
}]
plugins.push(new CopyWebpackPlugin(CopyWebpackPluginVersion > 5 ? { patterns } : patterns))
} catch (e) { }
}
if (process.UNI_SCRIPT_ENV && Object.keys(process.UNI_SCRIPT_ENV).length) {
......
......@@ -7,6 +7,7 @@ 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)) {
......@@ -42,7 +43,9 @@ function getAssetsCopyOptions (assetsDir) {
const copyOptions = []
// 主包静态资源
const mainAssetsCopyOption = getAssetsCopyOption(assetsDir, {
const mainAssetsCopyOption = getAssetsCopyOption(assetsDir, CopyWebpackPluginVersion > 5 ? {
globOptions: { ignore }
} : {
ignore
})
if (mainAssetsCopyOption) {
......@@ -51,11 +54,11 @@ function getAssetsCopyOptions (assetsDir) {
// 分包静态资源
process.UNI_SUBPACKAGES &&
Object.keys(process.UNI_SUBPACKAGES).forEach(root => {
const subAssetsCopyOption = getAssetsCopyOption(
path.join(root, assetsDir), {
ignore
}
)
const subAssetsCopyOption = getAssetsCopyOption(path.join(root, assetsDir), CopyWebpackPluginVersion > 5 ? {
globOptions: { ignore }
} : {
ignore
})
if (subAssetsCopyOption) {
copyOptions.push(subAssetsCopyOption)
}
......@@ -91,7 +94,8 @@ function getCopyWebpackPluginOptions (platformOptions, vueOptions) {
if (process.env.UNI_PLATFORM === 'app-plus' && !process.env.UNI_AUTOMATOR_WS_ENDPOINT) {
copyOptions.push({
from: path.resolve(process.env.UNI_INPUT_DIR, 'android*.json'),
to: '[name].[ext]',
to: `[name]${CopyWebpackPluginVersion > 5 ? '' : '.'}[ext]`,
noErrorOnMissing: true,
globOptions: {
ignored: require('./util').getWatchOptions().ignored
},
......
......@@ -20,7 +20,6 @@
"@dcloudio/uni-stat": "^2.0.0-32920211029001",
"buffer-json": "^2.0.0",
"clone-deep": "^4.0.1",
"copy-webpack-plugin": "^5.1.1",
"cross-env": "^5.2.0",
"envinfo": "^6.0.1",
"hash-sum": "^1.0.2",
......@@ -39,6 +38,7 @@
"xregexp": "4.0.0"
},
"peerDependencies": {
"copy-webpack-plugin": ">=5",
"postcss": ">=7"
},
"gitHead": "9e2d0f8e244724fcd64880316c57d837d1778cf8"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册