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