Skip to content

Commit

Permalink
chore(cli): add build:plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
fxy060608 committed Mar 30, 2020
1 parent 398690a commit a13204d
Show file tree
Hide file tree
Showing 13 changed files with 179 additions and 61 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ src/platforms/app-plus-nvue/runtime
build/rollup-plugin-require-context
packages/*/packages
packages/*/template/**/*
qh-api.js
packages/uni-h5/src
packages/uni-stat
75 changes: 75 additions & 0 deletions build/build.plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const path = require('path')
const del = require('del')

const {
error
} = require('@vue/cli-shared-utils')

const Service = require('@vue/cli-service')

const vueConfig = require('./vue.config.js')

const extendsApiPath = path.resolve(__dirname, '../lib/h5/extends-api')

vueConfig.configureWebpack.resolve.alias['uni-invoke-api'] = extendsApiPath

const service = new Service(process.env.VUE_CLI_CONTEXT || process.cwd(), {
inlineOptions: vueConfig
})
// 删除 cache 目录
del.sync(['node_modules/.cache'])

let pluginDir = process.argv[2]
if (!pluginDir) {
console.error(`缺少参数`)
process.exit(0)
}

if(pluginDir.indexOf('/') === -1){
pluginDir = path.resolve(__dirname,'../packages/uni-'+pluginDir)
}

const pkg = require(path.join(pluginDir, 'package.json'))
if (!pkg['uni-app']) {
console.error(`缺少 uni-app 配置`)
process.exit(0)
}

service.webpackRawConfigFns.push(function () {
return {
resolve: {
alias: {
'uni-platform/service/api.js': extendsApiPath,
'uni-sub-platform': path.resolve(pluginDir, 'src'),
'uni-platform-api': path.resolve(__dirname, '../src/platforms/h5/service/api'),
'uni-sub-platform-api': path.resolve(pluginDir, 'src/service/api')
}
},
module: {
rules: [{
test: path.resolve(__dirname, '../src/platforms/h5/service/api/index.js'),
use: [{
loader: path.resolve(__dirname, '../lib/extends-loader'),
options: {
'extends': path.resolve(pluginDir, 'src/service/api'),
'base': path.resolve(__dirname, '../src/platforms/h5/service/api')
}
}]
}]
}
}
})

service.run('build', {
name: 'index',
watch: process.env.UNI_WATCH === 'true',
target: 'lib',
formats: process.env.UNI_WATCH === 'true' ? 'umd' : 'umd-min',
entry: './lib/h5/main.js',
dest: path.join(pluginDir, 'dist'),
clean: true,
mode: process.env.NODE_ENV
}).then(function () {}).catch(err => {
error(err)
process.exit(1)
})
43 changes: 21 additions & 22 deletions build/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,27 @@ const provides = {
}
if (process.env.UNI_VIEW) { // 方便调试
delete provides['console']
}

if (process.env.UNI_VIEW === 'true') {
alias['vue$'] = resolve('packages/vue-cli-plugin-uni/packages/h5-vue/dist/vue.runtime.esm.js')
}
module.exports = function configureWebpack (config) {
if (process.env.UNI_VIEW === 'true') {
alias['vue$'] = resolve('packages/vue-cli-plugin-uni/packages/h5-vue/dist/vue.runtime.esm.js')
}

return {
mode: 'production',
devtool: false,
externals,
resolve: {
alias
},
module: {
rules: []
},
plugins: [
new webpack.DefinePlugin({
__VERSION__: JSON.stringify(pkg.version),
__PLATFORM__: JSON.stringify(process.env.UNI_PLATFORM)
}),
new webpack.ProvidePlugin(provides)
]
}
module.exports = {
mode: 'production',
devtool: false,
externals,
resolve: {
alias
},
module: {
rules: []
},
plugins: [
new webpack.DefinePlugin({
__VERSION__: JSON.stringify(pkg.version),
__PLATFORM__: JSON.stringify(process.env.UNI_PLATFORM)
}),
new webpack.ProvidePlugin(provides)
]
}
40 changes: 40 additions & 0 deletions lib/extends-loader/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const fs = require('fs')
const path = require('path')

const glob = require('glob')
const loaderUtils = require('loader-utils')

const isWin = /^win/.test(process.platform)
const normalizePath = path => (isWin ? path.replace(/\\/g, '/') : path)

module.exports = function loader(source) {
const options = loaderUtils.getOptions(this)
const baseDir = options['base']
const extendsDir = options['extends']

const exportCode = []
const extendsFiles = []
// extends目录均导出
glob.sync('**/*.js', {
cwd: extendsDir
}).forEach(file => {
if (file === 'index.js') {
return
}
extendsFiles.push(file)
exportCode.push(`export * from 'uni-sub-platform-api/${normalizePath(file)}'`)
})
//base目录中有,extends无的导出
glob.sync('**/*.js', {
cwd: baseDir
}).forEach(file => {
if (file === 'index.js') {
return
}
if (!extendsFiles.includes(file)) {
exportCode.push(`export * from 'uni-platform-api/${normalizePath(file)}'`)
}
})
console.log(exportCode.join('\n'))
return exportCode.join('\n')
}
3 changes: 3 additions & 0 deletions lib/h5/extends-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import 'uni-sub-platform/service/index'
import * as api from 'uni-platform/service/api/index'
export default api
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"dev:h5": "npm run lint && cross-env NODE_ENV=production UNI_WATCH=true UNI_PLATFORM=h5 node build/build.js",
"build:h5": "npm run lint && cross-env NODE_ENV=production UNI_WATCH=false UNI_PLATFORM=h5 node build/build.js",
"build:h5:ui": "cross-env NODE_ENV=production UNI_WATCH=false UNI_PLATFORM=h5 UNI_UI=true node build/build.js",
"dev:plugin": "cross-env NODE_ENV=production UNI_WATCH=true UNI_PLATFORM=h5 node build/build.plugin.js",
"build:plugin": "cross-env NODE_ENV=production UNI_WATCH=false UNI_PLATFORM=h5 node build/build.plugin.js",
"build:app-plus": "cross-env UNI_PLATFORM=app-plus rollup -c build/rollup.config.mp.js",
"build:app:all": "npm run lint && npm run build:app:nvue && npm run build:app:legacy && npm run build:app:service && npm run build:app:view",
"build:app:v3": "npm run lint && npm run build:app:service && npm run dev:app:view",
Expand Down Expand Up @@ -109,6 +111,7 @@
"swan": true,
"tt": true,
"qh": true,
"HWH5": true,
"weex": true,
"__id__": true,
"__uniConfig": true,
Expand Down Expand Up @@ -142,4 +145,4 @@
"main": "index.js",
"description": "",
"author": ""
}
}
4 changes: 2 additions & 2 deletions packages/uni-app-plus/dist/view.umd.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/uni-h5/dist/index.umd.min.js

Large diffs are not rendered by default.

52 changes: 26 additions & 26 deletions packages/uni-h5/package.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
{
"name": "@dcloudio/uni-h5",
"version": "2.0.0-alpha-26420200309002",
"description": "uni-app h5",
"main": "dist/index.umd.min.js",
"repository": {
"type": "git",
"url": "git+https://github.com/dcloudio/uni-app.git",
"directory": "packages/uni-h5"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "fxy060608",
"license": "Apache-2.0",
"dependencies": {
"base64-arraybuffer": "^0.2.0",
"intersection-observer": "^0.7.0",
"safe-area-insets": "^1.4.1"
},
"uni-app": {
"name": "h5",
"title": "H5",
"main": "lib/h5/uni.config.js"
},
"gitHead": "84e9cb1ca1898054d161f1514efadd1ab24fd804"
{
"name": "@dcloudio/uni-h5",
"version": "2.0.0-alpha-26420200309002",
"description": "uni-app h5",
"main": "dist/index.umd.min.js",
"repository": {
"type": "git",
"url": "git+https://github.com/dcloudio/uni-app.git",
"directory": "packages/uni-h5"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "fxy060608",
"license": "Apache-2.0",
"dependencies": {
"base64-arraybuffer": "^0.2.0",
"intersection-observer": "^0.7.0",
"safe-area-insets": "^1.4.1"
},
"uni-app": {
"name": "h5",
"title": "H5",
"main": "lib/h5/uni.config.js"
},
"gitHead": "84e9cb1ca1898054d161f1514efadd1ab24fd804"
}
1 change: 0 additions & 1 deletion packages/vue-cli-plugin-uni/lib/h5/qh-api.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/core/service/platform-api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import baseApi from 'uni-core/service/api'
import platformApi from 'uni-platform/service/api'
import platformApi from 'uni-invoke-api'

export default Object.assign(Object.create(null), baseApi, platformApi)
Empty file.
10 changes: 4 additions & 6 deletions src/platforms/h5/service/api/ui/navigation-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ function setNavigationBar (type, args) {

if (frontColor) {
page.navigationBar.textColor = frontColor === '#000000' ? 'black' : 'white'
UniServiceJSBridge.emit('onNavigationBarChange', {
textColor: frontColor === '#000000' ? '#000' : '#fff'
})
}
if (backgroundColor) {
page.navigationBar.backgroundColor = backgroundColor
UniServiceJSBridge.emit('onNavigationBarChange', {
backgroundColor
})
}
UniServiceJSBridge.emit('onNavigationBarChange', {
textColor: frontColor === '#000000' ? '#000' : '#fff',
backgroundColor: page.navigationBar.backgroundColor
})
page.navigationBar.duration = duration + 'ms'
page.navigationBar.timingFunc = timingFunc
break
Expand Down

0 comments on commit a13204d

Please sign in to comment.