Skip to content

Commit

Permalink
feat(v3): mp runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
fxy060608 committed Dec 11, 2019
1 parent 4cd5ee7 commit ae7b600
Show file tree
Hide file tree
Showing 18 changed files with 227 additions and 114 deletions.
7 changes: 7 additions & 0 deletions build/rollup.config.wxs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
input: 'src/core/runtime/mp/wxs.js',
output: {
file: 'packages/uni-mp-weixin/dist/wxs.js',
format: 'es'
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"build:mp-baidu": "cross-env UNI_PLATFORM=mp-baidu rollup -c build/rollup.config.mp.js",
"build:mp-alipay": "cross-env UNI_PLATFORM=mp-alipay rollup -c build/rollup.config.mp.js",
"build:mp-toutiao": "cross-env UNI_PLATFORM=mp-toutiao rollup -c build/rollup.config.mp.js",
"build:mp-weixin:mp": "npm run lint && cross-env UNI_PLATFORM=mp-weixin UNI_MP=true rollup -c build/rollup.config.mp.js",
"build:mp-weixin:mp": "npm run lint && cross-env UNI_PLATFORM=mp-weixin UNI_MP=true rollup -c build/rollup.config.mp.js",
"build:mp-weixin:wxs": "rollup -c build/rollup.config.wxs.js",
"build:runtime": "npm run lint && npm run build:mp-weixin && npm run build:mp-qq && npm run build:mp-alipay && npm run build:mp-baidu && npm run build:mp-toutiao && npm run build:app-plus",
"build:stat": "npm run lint && rollup -c build/rollup.config.stat.js",
"build:web-view": "npm run lint && rollup -c build/rollup.config.web-view.js",
Expand Down
4 changes: 3 additions & 1 deletion packages/uni-app-plus/dist/index.v3.js
Original file line number Diff line number Diff line change
Expand Up @@ -12099,7 +12099,9 @@ var serviceContext = (function () {
Object.keys(dataset).forEach(name => {
try {
ret[name] = JSON.parse(dataset[name]);
} catch (e) {}
} catch (e) { // dataset 存在两种,一种是被JSON.stringify的,一种是原始的
ret[name] = dataset[name];
}
});
return ret
}
Expand Down
27 changes: 27 additions & 0 deletions packages/uni-cli-shared/__tests__/pages.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const {
generateUsingComponentsCode,
generateGlobalUsingComponentsCode
} = require('../lib/pages')
describe('shared:pages', () => {
it('generate usingComponents', () => {
expect(generateUsingComponentsCode()).toBe('')
expect(generateUsingComponentsCode({})).toBe('')
expect(generateUsingComponentsCode({
'van-button': '/wxcomponents/vant/button/index',
'van-card': '../../wxcomponents/vant/card/index'
})).toBe(
`import VanButton from '@/wxcomponents/vant/button/index.vue';import VanCard from '../../wxcomponents/vant/card/index.vue';global['__wxVueOptions'] = {components:{'van-button':VanButton,'van-card':VanCard}};`
)
})
it('generate global usingComponents', () => {
expect(generateGlobalUsingComponentsCode()).toBe('')
expect(generateGlobalUsingComponentsCode({})).toBe('')
expect(generateGlobalUsingComponentsCode({
'van-button': '/wxcomponents/vant/button/index',
'van-cell': 'wxcomponents/vant/cell/index',
'van-cell-group': './wxcomponents/vant/cell-group/index'
})).toBe(
`import VanButton from '@/wxcomponents/vant/button/index.vue';import VanCell from './wxcomponents/vant/cell/index.vue';import VanCellGroup from './wxcomponents/vant/cell-group/index.vue';Vue.component('van-button',VanButton);Vue.component('van-cell',VanCell);Vue.component('van-cell-group',VanCellGroup);`
)
})
})
9 changes: 4 additions & 5 deletions packages/uni-cli-shared/lib/json.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
const fs = require('fs')
const path = require('path')
const stripJsonComments = require('strip-json-comments')
const preprocessor = require('@dcloudio/vue-cli-plugin-uni/packages/webpack-preprocess-loader/preprocess')
const {
jsPreprocessOptions
} = require('./platform')

function parseJson (content, preprocess = false) {
if (typeof content === 'string') {
if (preprocess) {
const preprocessor = require('@dcloudio/vue-cli-plugin-uni/packages/webpack-preprocess-loader/preprocess')
const {
jsPreprocessOptions
} = require('./platform')
content = preprocessor.preprocess(content, jsPreprocessOptions.context, {
type: jsPreprocessOptions.type
})
}

content = JSON.parse(stripJsonComments(content))
}

Expand Down
92 changes: 90 additions & 2 deletions packages/uni-cli-shared/lib/pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ const path = require('path')

const {
removeExt,
normalizePath
normalizePath,
camelize,
capitalize
} = require('./util')

const {
Expand Down Expand Up @@ -244,12 +246,98 @@ function parseEntry (pagesJson) {
}
}

function parseUsingComponents (usingComponents = {}) {
const components = []
Object.keys(usingComponents).forEach(name => {
const identifier = capitalize(camelize(name))
let source = usingComponents[name]
if (source.indexOf('/') === 0) { // 绝对路径
source = '@' + source
} else if (source.indexOf('.') !== 0) { // 相对路径
source = './' + source
}
components.push({
name,
identifier,
source
})
})
return components
}

function generateUsingComponentsCode (usingComponents) {
const components = parseUsingComponents(usingComponents)
const importCode = []
const componentsCode = []
components.forEach(({
name,
identifier,
source
}) => {
importCode.push(`import ${identifier} from '${source}.vue'`)
componentsCode.push(`'${name}':${identifier}`)
})
if (!importCode.length) {
return ''
}
return `${importCode.join(';')};global['__wxVueOptions'] = {components:{${componentsCode.join(',')}}};`
}

function generateGlobalUsingComponentsCode (usingComponents) {
const components = parseUsingComponents(usingComponents)
const importCode = []
const componentsCode = []
components.forEach(({
name,
identifier,
source
}) => {
importCode.push(`import ${identifier} from '${source}.vue'`)
componentsCode.push(`Vue.component('${name}',${identifier})`)
})
if (!importCode.length) {
return ''
}
return `${importCode.join(';')};${componentsCode.join(';')};`
}

function getGlobalUsingComponentsCode () {
const pagesJson = getPagesJson()
const usingComponents = pagesJson.globalStyle && pagesJson.globalStyle.usingComponents
if (!usingComponents) {
return ''
}
return generateGlobalUsingComponentsCode(usingComponents)
}

function getUsingComponentsCode (pagePath) {
const usingComponents = usingComponentsPages[pagePath]
if (!usingComponents) {
return ''
}
return generateUsingComponentsCode(usingComponents)
}

const usingComponentsPages = Object.create(null)

function addPageUsingComponents (pagePath, usingComponents) {
if (usingComponents && Object.keys(usingComponents).length) {
console.log(pagePath + ':' + JSON.stringify(usingComponents))
usingComponentsPages[pagePath] = usingComponents
}
}

module.exports = {
getMainEntry,
getNVueMainEntry,
parsePages,
parseEntry,
getPagesJson,
parsePagesJson,
pagesJsonJsFileName
pagesJsonJsFileName,
addPageUsingComponents,
getUsingComponentsCode,
generateUsingComponentsCode,
getGlobalUsingComponentsCode,
generateGlobalUsingComponentsCode
}
9 changes: 7 additions & 2 deletions packages/uni-cli-shared/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ const camelizeRE = /-(\w)/g

const camelize = cached((str) => {
return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '')
})
})

function capitalize (str) {
return str.charAt(0).toUpperCase() + str.slice(1)
}

const hyphenateRE = /\B([A-Z])/g

Expand Down Expand Up @@ -106,7 +110,8 @@ module.exports = {
},
hashify,
removeExt,
camelize,
camelize,
capitalize,
hyphenate,
normalizePath,
convertStaticStyle,
Expand Down
16 changes: 8 additions & 8 deletions packages/uni-migration/__tests__/wxml.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,37 @@ describe('wxml:compiler', () => {
it('generate event', () => {
assertCodegen(
`<view bindtouchstart="startDrag" catchtouchmove="{{ catchMove ? 'noop' : '' }}"/>`,
`<view @touchstart="startDrag" @touchmove.stop.prevent="catchMove ? 'noop' : ''"></view>`
`<uni-shadow-root><view @touchstart="startDrag" @touchmove.stop.prevent="catchMove ? 'noop' : ''"></view></uni-shadow-root>`
)
assertCodegen(
`<uni-transition bind:click="click" bindtouchstart="startDrag" catchtouchmove="{{ catchMove ? 'noop' : '' }}"/>`,
`<uni-transition @click="click" @touchstart.native="startDrag" @touchmove.native.stop.prevent="catchMove ? 'noop' : ''"></uni-transition>`
`<uni-shadow-root><uni-transition @click="click" @touchstart.native="startDrag" @touchmove.native.stop.prevent="catchMove ? 'noop' : ''"></uni-transition></uni-shadow-root>`
)
})
it('generate class', () => {
assertCodegen(
`<view class="van-notice-bar__content {{ !scrollable && !wrapable ? 'van-ellipsis' : '' }}"></view>`,
`<view :class="'van-notice-bar__content '+(!scrollable && !wrapable ? 'van-ellipsis' : '')"></view>`
`<uni-shadow-root><view :class="'van-notice-bar__content '+(!scrollable && !wrapable ? 'van-ellipsis' : '')"></view></uni-shadow-root>`
)
})
it('generate v-if', () => {
assertCodegen(
'<block wx:if="{{ !loading }}" loading loading-text="">{{ item.name }}</block>',
`<block v-if="(!loading)" loading loading-text>{{ item.name }}</block>`
`<uni-shadow-root><block v-if="(!loading)" loading loading-text>{{ item.name }}</block></uni-shadow-root>`
)
})
it('generate v-for', () => {
assertCodegen(
'<view wx:for="{{ isSimple(columns) ? [columns] : columns }}" wx:for-item="item1" wx:key="{{ index }}"/>',
`<view v-for="(item1,index) in (isSimple(columns) ? [columns] : columns)" :key="item1.index"></view>`
`<uni-shadow-root><view v-for="(item1,index) in (isSimple(columns) ? [columns] : columns)" :key="item1.index"></view></uni-shadow-root>`
)
assertCodegen(
'<view wx:for="{{ columns }}" wx:for-item="item1" wx:key="item1"/>',
`<view v-for="(item1,index) in (columns)" :key="item1"></view>`
`<uni-shadow-root><view v-for="(item1,index) in (columns)" :key="item1"></view></uni-shadow-root>`
)
assertCodegen(
'<view wx:for="{{ columns }}" wx:for-item="index" wx:key="*this"/>',
`<view v-for="(index,___i___) in (columns)" :key="index"></view>`
`<uni-shadow-root><view v-for="(index,___i___) in (columns)" :key="index"></view></uni-shadow-root>`
)
})
it('generate root element', () => {
Expand All @@ -51,7 +51,7 @@ describe('wxml:compiler', () => {
assertCodegen(
`<view></view>
<wxs></wxs>`,
`<view></view>`
`<uni-shadow-root><view></view></uni-shadow-root>`
)


Expand Down
4 changes: 0 additions & 4 deletions packages/uni-migration/lib/mp-weixin/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
const patch = require('./patch')
module.exports = {
options: {
extname: {
template: '.wxml',
style: '.wxss'
},
shouldWrapper(filepath) {
return patch.wrapper(filepath)
}
},
transform: require('./transform'),
Expand Down
21 changes: 1 addition & 20 deletions packages/uni-migration/lib/mp-weixin/patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,7 @@ function patchVue(file) {
}
}

const VANT_WRAPPERS = [
function test(filepath) {
return filepath.indexOf('/cell/index') !== -1
},
function test(filepath) {
return filepath.indexOf('/sticky/index') !== -1
}
]

const PATCH_WRAPPERS = [
...VANT_WRAPPERS
]

function patchWrapper(filepath) {
filepath = normalizePath(filepath)
return !!PATCH_WRAPPERS.find(test => test(filepath))
}

module.exports = {
vue: patchVue,
asset: patchAsset,
wrapper: patchWrapper
asset: patchAsset
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,30 +57,6 @@ function genWxs(wxs, state) {
return [wxsCode.join('').trim(), wxsFiles]
}

function shouldWrapper(node, state) {
if (state.shouldWrapper(state.filepath)) {
return true
}
node.children = node.children.filter(child => { // remove \n
if (child.type === 'text' && !child.data.trim()) {
return false
}
return true
})
if (node.children.length > 1) { // multi root
return true
}
const rootNode = node.children[0]
if (rootNode && rootNode.name === 'slot') { // slot root
return true
}
return false
}

module.exports = function generate(node, state) {
// [`<uni-shadow-root>${genChildren(node).trim()}</uni-shadow-root>`, ...genWxs(state.wxs, state)]
// if (shouldWrapper(node, state)) {
return [`<uni-shadow-root>${genChildren(node).trim()}</uni-shadow-root>`, ...genWxs(state.wxs, state)]
// }
// return [genChildren(node).trim(), ...genWxs(state.wxs, state)]
return [`<uni-shadow-root>${genChildren(node).trim()}</uni-shadow-root>`, ...genWxs(state.wxs, state)]
}
20 changes: 1 addition & 19 deletions packages/uni-mp-weixin/dist/mp.js
Original file line number Diff line number Diff line change
Expand Up @@ -769,24 +769,6 @@ var polyfill = {
}
};

/**
* wxs getRegExp
*/
function getRegExp () {
const args = Array.prototype.slice.call(arguments);
args.unshift(RegExp);
return new (Function.prototype.bind.apply(RegExp, args))()
}

/**
* wxs getDate
*/
function getDate () {
const args = Array.prototype.slice.call(arguments);
args.unshift(Date);
return new (Function.prototype.bind.apply(Date, args))()
}

global['__wxRoute'] = '';
global['__wxComponents'] = Object.create(null);
global['__wxVueOptions'] = Object.create(null);
Expand Down Expand Up @@ -823,4 +805,4 @@ function Behavior (options) {
const nextTick = Vue.nextTick;

export default uni;
export { Behavior, Component, Page, getDate, getRegExp, nextTick };
export { Behavior, Component, Page, nextTick };
Loading

0 comments on commit ae7b600

Please sign in to comment.