Skip to content

Commit

Permalink
feat(plugin): support custom component
Browse files Browse the repository at this point in the history
  • Loading branch information
fxy060608 committed Jun 10, 2020
1 parent 5b11af7 commit aecbbd7
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 30 deletions.
22 changes: 16 additions & 6 deletions build/build.plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ let pluginDir = process.argv[2]
if (!pluginDir) {
console.error('缺少参数')
process.exit(0)
}

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

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

const pkg = require(path.join(pluginDir, 'package.json'))
if (!pkg['uni-app']) {
Expand All @@ -47,9 +47,19 @@ service.webpackRawConfigFns.push(function () {
},
module: {
rules: [{
test: path.resolve(__dirname, '../src/core/view/components/index.js'),
use: [{
loader: path.resolve(__dirname, '../lib/extends-component-loader'),
options: {
extends: path.resolve(pluginDir, 'src/view/components'),
base: path.resolve(__dirname, '../src/core/view/components'),
platform: path.resolve(__dirname, '../src/platforms/h5/view/components')
}
}]
}, {
test: path.resolve(__dirname, '../src/platforms/h5/service/api/index.js'),
use: [{
loader: path.resolve(__dirname, '../lib/extends-loader'),
loader: path.resolve(__dirname, '../lib/extends-api-loader'),
options: {
extends: path.resolve(pluginDir, 'src/service/api'),
base: path.resolve(__dirname, '../src/platforms/h5/service/api')
Expand Down
File renamed without changes.
58 changes: 58 additions & 0 deletions lib/extends-component-loader/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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 platformDir = options['platform']
const extendsDir = options['extends']

const components = []
const extendsFiles = []
// extends目录均导出
glob.sync('**/index.vue', {
cwd: extendsDir
}).forEach(file => {
extendsFiles.push(file)
components.push(normalizePath(path.join(extendsDir, file)))
})

//base目录中有,extends无的导出
glob.sync('**/index.vue', {
cwd: baseDir
}).forEach(file => {
if (!extendsFiles.includes(file)) {
components.push(normalizePath(path.join(baseDir, file)))
}
})
//platform目录中有,extends无的导出
glob.sync('**/index.vue', {
cwd: platformDir
}).forEach(file => {
if (!extendsFiles.includes(file)) {
components.push(normalizePath(path.join(platformDir, file)))
}
})

const componentsCode = components.map(component => {
return `require('${component}').default`
}).join(',')
return `
import Vue from 'vue'
import baseMixin from 'uni-mixins/base'
import animation from 'uni-mixins/animation'
[${componentsCode}].forEach(component=>{
component.mixins = component.mixins ? [].concat(baseMixin, component.mixins) : [baseMixin]
component.mixins.push(animation)
component.name = 'VUni' + component.name
component.isReserved = true
Vue.component(component.name, component)
})
`
}
14 changes: 6 additions & 8 deletions packages/uni-app-plus/dist/index.v3.js
Original file line number Diff line number Diff line change
Expand Up @@ -7526,9 +7526,8 @@ var serviceContext = (function () {
const SUB_FILENAME = 'app-sub-service.js';

function evaluateScriptFile (file, callback) {
setTimeout(() => {
callback();
}, 2000);
// TODO 有可能当前 instance 是非 app-service
weex.requireModule('plus').evalJSFiles([file], callback);
}

function loadSubPackage$1 ({
Expand Down Expand Up @@ -8496,9 +8495,8 @@ var serviceContext = (function () {
const SUB_FILENAME$1 = 'app-sub-service.js';

function evaluateScriptFiles (files, callback) {
setTimeout(() => {
callback();
}, 2000);
// TODO 有可能当前 instance 是非 app-service
weex.requireModule('plus').evalJSFiles(files, callback);
}

function loadSubPackages (packages, callback) {
Expand Down Expand Up @@ -8606,7 +8604,7 @@ var serviceContext = (function () {
openType === 'reLaunch' ||
(
!__uniConfig.realEntryPagePath &&
pages.length === 0
getCurrentPages$1().length === 0 // redirectTo
)
) {
routeOptions.meta.isQuit = true;
Expand Down Expand Up @@ -13386,7 +13384,7 @@ var serviceContext = (function () {
if (~conflictTags.indexOf(tag)) { // svg 部分标签名称与 uni 标签冲突
return false
}
return oldGetTagNamespace(tag) || false
return oldGetTagNamespace(tag)
};
}

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

Large diffs are not rendered by default.

30 changes: 15 additions & 15 deletions packages/vue-cli-plugin-uni/lib/split-chunks.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ const {
normalizePath
} = require('@dcloudio/uni-cli-shared')

function createCacheGroups() {
function createCacheGroups () {
const cacheGroups = {}
if (process.UNI_CONFUSION) { // 加密
cacheGroups.confusion = {
enforce: true,
test: function(module) {
test: function (module) {
if (!module.resource) {
return false
}
Expand All @@ -26,7 +26,7 @@ function createCacheGroups() {
process.env.UNI_OPT_SUBPACKAGES && Object.keys(process.UNI_SUBPACKAGES).forEach(root => {
cacheGroups[root] = {
enforce: true,
test: function(module) {
test: function (module) {
if (!module.resource) {
return false
}
Expand All @@ -42,7 +42,7 @@ function createCacheGroups() {
return cacheGroups
}

module.exports = function getSplitChunks() {
module.exports = function getSplitChunks () {
const {
normalizePath
} = require('@dcloudio/uni-cli-shared')
Expand All @@ -69,22 +69,22 @@ module.exports = function getSplitChunks() {

if (!process.env.UNI_OPT_SUBPACKAGES) {
return {
chunks(chunk) { // 防止 node_modules 内 vue 组件被 split
chunks (chunk) { // 防止 node_modules 内 vue 组件被 split
return chunk.name.indexOf('node-modules') !== 0
},
cacheGroups: {
default: false,
vendors: false,
commons: {
test(module) {
test (module) {
if (module.type === 'css/mini-extract') {
return false
}
if (module.resource && (
module.resource.indexOf('.vue') !== -1 ||
module.resource.indexOf('.vue') !== -1 ||
module.resource.indexOf('.nvue') !== -1 ||
normalizePath(module.resource).indexOf(mainPath) === 0 // main.js
)) {
)) {
return false
}
return true
Expand All @@ -97,7 +97,7 @@ module.exports = function getSplitChunks() {
}
}

function baseTest(module) {
function baseTest (module) {
if (module.type === 'css/mini-extract') {
return false
}
Expand All @@ -119,7 +119,7 @@ module.exports = function getSplitChunks() {
default: false,
vendors: false,
commons: {
test(module, chunks) {
test (module, chunks) {
if (!baseTest(module)) {
return false
}
Expand All @@ -144,7 +144,7 @@ module.exports = function getSplitChunks() {
}
}

const findSubPackages = function(chunks) {
const findSubPackages = function (chunks) {
return chunks.reduce((pkgs, item) => {
const name = normalizePath(item.name)
const pkgRoot = subPackageRoots.find(root => name.indexOf(root) === 0)
Expand All @@ -153,16 +153,16 @@ module.exports = function getSplitChunks() {
}, new Set())
}

const hasMainPackage = function(chunks) {
const hasMainPackage = function (chunks) {
return chunks.find(item => !subPackageRoots.find(root => item.name.indexOf(root) === 0))
}

const subPackageRoots = Object.keys(process.UNI_SUBPACKAGES).map(root => root + '/')

Object.keys(process.UNI_SUBPACKAGES).forEach(root => {
(function(root) {
(function (root) {
cacheGroups[root + '/commons'] = {
test(module, chunks) {
test (module, chunks) {
if (!baseTest(module)) {
return false
}
Expand All @@ -187,7 +187,7 @@ module.exports = function getSplitChunks() {
})

return {
chunks(chunk) { // 防止 node_modules 内 vue 组件被 split
chunks (chunk) { // 防止 node_modules 内 vue 组件被 split
return chunk.name.indexOf('node-modules') !== 0
},
cacheGroups
Expand Down

0 comments on commit aecbbd7

Please sign in to comment.