提交 bc6d5ef8 编写于 作者: fxy060608's avatar fxy060608

refactor(v3): improve watch

上级 1c7f4755
......@@ -27,9 +27,11 @@ if (process.env.UNI_PLATFORM === 'h5' && process.env.UNI_UI === 'true') {
entry = './lib/' + process.env.UNI_PLATFORM + '/ui.js'
}
let formats = process.env.UNI_WATCH === 'true' ? 'umd' : 'umd-min'
if (process.env.UNI_PLATFORM === 'app-plus' && process.env.UNI_VIEW === 'true') {
name = 'uni'
filename = 'view'
formats = 'umd'
entry = './lib/' + process.env.UNI_PLATFORM + '/view.js'
}
service.run('build', {
......@@ -37,11 +39,11 @@ service.run('build', {
filename,
watch: process.env.UNI_WATCH === 'true',
target: 'lib',
formats: process.env.UNI_WATCH === 'true' ? 'umd' : 'umd-min',
formats,
entry,
clean: !process.env.UNI_VIEW,
mode: process.env.NODE_ENV
}).then(function () {
}).then(function() {
if (
process.env.UNI_WATCH !== 'true' &&
process.env.UNI_UI !== 'true' &&
......@@ -62,7 +64,7 @@ if (process.env.UNI_PLATFORM === 'h5' && process.env.UNI_WATCH === 'false') {
const packageJsonPath = path.join(packagePath, 'package.json')
del(path.join(packagePath, '{lib,src}'))
.then(() => {
copy([path.join(__dirname, '../{lib,src}/**/*')], packagePath, function (err, file) {
copy([path.join(__dirname, '../{lib,src}/**/*')], packagePath, function(err, file) {
if (err) {
throw err
}
......
......@@ -129,8 +129,16 @@ const PLATFORMS = {
'--window-bottom': '0px'
},
copyWebpackOptions ({
assetsDir
}) {
assetsDir,
vueOptions
}) {
if (
vueOptions.pluginOptions['uni-app-plus'] &&
vueOptions.pluginOptions['uni-app-plus']['view']
) { // app-view 无需拷贝资源(app-service 已经做了)
return []
}
const files = ['hybrid/html']
let wxcomponents = []
if (!process.env.UNI_USING_NATIVE && !process.env.UNI_USING_V3) {
......@@ -143,7 +151,6 @@ const PLATFORMS = {
if (process.env.UNI_USING_V3) {
view = getCopyOptions([
require.resolve('@dcloudio/uni-app-plus/dist/view.css'),
// TODO view.umd.min.js
require.resolve('@dcloudio/uni-app-plus/dist/view.umd.js')
])
template = getCopyOptions([path.resolve(__dirname, '../template')])
......
......@@ -5,28 +5,68 @@ const {
log,
done
} = require('@vue/cli-shared-utils')
let serviceCompiled = true
let viewCompiled = true
let viewCompiled = true
const serviceChangedFiles = []
const viewChangedFiles = []
let isFirst = true
class WebpackAppPlusPlugin {
apply(compiler) {
if (process.env.UNI_USING_V3) {
const chunkVersions = {}
const isAppService = compiler.options.entry['app-service']
const isAppView = compiler.options.entry['app-view']
compiler.hooks.beforeCompile.tapAsync('WebpackAppPlusPlugin', (params, callback) => {
isAppService && (serviceCompiled = false)
isAppView && (viewCompiled = false)
callback()
})
compiler.hooks.emit.tapAsync('WebpackAppPlusPlugin', (compilation, callback) => {
isAppService && (serviceChangedFiles.length = 0)
isAppView && (viewChangedFiles.length = 0)
const changedChunks = compilation.chunks.filter(chunk => {
const oldVersion = chunkVersions[chunk.name]
chunkVersions[chunk.name] = chunk.hash
return chunk.hash !== oldVersion
})
changedChunks.map(chunk => {
if (Array.isArray(chunk.files)) {
chunk.files.forEach(file => {
if (isAppService) {
!serviceChangedFiles.includes(file) && (serviceChangedFiles.push(file))
} else if (isAppView) {
!viewChangedFiles.includes(file) && (viewChangedFiles.push(file))
}
})
}
})
callback()
})
compiler.hooks.done.tapPromise('WebpackAppPlusPlugin', compilation => {
return new Promise((resolve, reject) => {
isAppService && (serviceCompiled = true)
isAppView && (viewCompiled = true)
if (serviceCompiled && viewCompiled) {
const changedFiles = [...new Set([...serviceChangedFiles, ...viewChangedFiles])]
if (process.env.NODE_ENV === 'development') {
done(`Build complete. Watching for changes...`)
if (!isFirst && changedFiles.length > 0) {
done(`Build complete. FILES:` + JSON.stringify(changedFiles))
} else {
done(`Build complete. Watching for changes...`)
}
isFirst = false
} else {
done(`Build complete. `)
}
......
......@@ -75,7 +75,7 @@ const v3 = {
return {
devtool,
mode: isAppView ? 'production' : process.env.NODE_ENV,
mode: process.env.NODE_ENV,
externals: {
vue: 'Vue'
},
......
......@@ -174,7 +174,7 @@ module.exports = function configureWebpack (platformOptions, manifestPlatformOpt
'node_modules')
const plugins = [
new CopyWebpackPlugin(getCopyWebpackPluginOptions(manifestPlatformOptions))
new CopyWebpackPlugin(getCopyWebpackPluginOptions(manifestPlatformOptions, vueOptions))
]
if (process.UNI_SCRIPT_ENV && Object.keys(process.UNI_SCRIPT_ENV).length) {
......
const assetsDir = 'static'
function getCopyWebpackPluginOptions (manifestPlatformOptions) {
function getCopyWebpackPluginOptions (manifestPlatformOptions, vueOptions) {
const {
getPlatformCopy
} = require('@dcloudio/uni-cli-shared/lib/platform')
return getPlatformCopy()({
assetsDir,
manifestPlatformOptions
manifestPlatformOptions,
vueOptions
})
}
......
......@@ -93,7 +93,7 @@ function getStylesCode(loaderContext) {
module.exports = function(source, map) {
return `
import 'uni-pages'
import 'uni-pages?${JSON.stringify({type:'view'})}'
function initView(){
${getStylesCode(this)}
injectStyles()
......
const fs = require('fs')
const path = require('path')
const loaderUtils = require('loader-utils')
const {
parsePages,
normalizePath,
......@@ -32,6 +34,12 @@ function renameUsingComponents (jsonObj) {
module.exports = function (content) {
this.cacheable && this.cacheable()
let isAppView = false
if (this.resourceQuery) {
const params = loaderUtils.parseQuery(this.resourceQuery)
isAppView = params.type === 'view'
}
const pagesJsonJsPath = path.resolve(process.env.UNI_INPUT_DIR, pagesJsonJsFileName)
const manifestJsonPath = path.resolve(process.env.UNI_INPUT_DIR, 'manifest.json')
const manifestJson = parseManifestJson(fs.readFileSync(manifestJsonPath, 'utf8'))
......@@ -68,32 +76,32 @@ module.exports = function (content) {
const jsonFiles = require('./platforms/' + process.env.UNI_PLATFORM)(pagesJson, manifestJson)
if (jsonFiles && jsonFiles.length) {
if (process.env.UNI_USING_V3) {
let appConfigContent = ''
jsonFiles.forEach(jsonFile => {
if (jsonFile) {
if (jsonFile.name === 'define-pages.js') {
appConfigContent = jsonFile.content
} else {
this.emitFile(jsonFile.name, jsonFile.content)
}
}
})
return appConfigContent
}
if (process.env.UNI_USING_NATIVE) {
let appConfigContent = ''
jsonFiles.forEach(jsonFile => {
if (jsonFile) {
if (jsonFile.name === 'app-config.js') {
appConfigContent = jsonFile.content
} else {
this.emitFile(jsonFile.name, jsonFile.content)
}
}
})
return appConfigContent
if (jsonFiles && jsonFiles.length) {
if (process.env.UNI_USING_V3 && !isAppView) { // app-view 不需要生成 app-config-service.js,manifest.json
let appConfigContent = ''
jsonFiles.forEach(jsonFile => {
if (jsonFile) {
if (jsonFile.name === 'define-pages.js') {
appConfigContent = jsonFile.content
} else {
this.emitFile(jsonFile.name, jsonFile.content)
}
}
})
return appConfigContent
}
if (process.env.UNI_USING_NATIVE) {
let appConfigContent = ''
jsonFiles.forEach(jsonFile => {
if (jsonFile) {
if (jsonFile.name === 'app-config.js') {
appConfigContent = jsonFile.content
} else {
this.emitFile(jsonFile.name, jsonFile.content)
}
}
})
return appConfigContent
}
jsonFiles.forEach(jsonFile => {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册