diff --git a/packages/uni-cli-shared/lib/platform.js b/packages/uni-cli-shared/lib/platform.js index 888f10b9e8fcb2d9baaecd974bb3a38a1aebd50a..0b514c9ba1ee21249a25e99952d7678e41fdba87 100644 --- a/packages/uni-cli-shared/lib/platform.js +++ b/packages/uni-cli-shared/lib/platform.js @@ -79,7 +79,8 @@ const PLATFORMS = { exts: false, vue: '@dcloudio/vue-cli-plugin-uni/packages/h5-vue', compiler: false, - megalo: false, + megalo: false, + filterTag: 'wxs', subPackages: false, cssVars: { '--status-bar-height': '0px' @@ -398,8 +399,20 @@ function devtoolModuleFilenameTemplate (info) { ) { return `uni-app:///${filePath}` } -} -module.exports = { +} + +const NODE_MODULES_REGEX = /(\.\.\/)?node_modules/g + +function normalizeNodeModules (str) { + str = str.replace(NODE_MODULES_REGEX, 'node-modules') + if (process.env.UNI_PLATFORM === 'mp-alipay') { + str = str.replace('node-modules/@', 'node-modules/npm-scope-') + } + return str +} + +module.exports = { + normalizeNodeModules, isInHBuilderX, isInHBuilderXAlpha, runByHBuilderX: isInHBuilderX || !!process.env.UNI_HBUILDERX_PLUGINS, diff --git a/packages/uni-cli-shared/package.json b/packages/uni-cli-shared/package.json index a904039430ded58f2c102898572286769a646271..8f69c062b9325682774cf0f31434212ffa1211ef 100644 --- a/packages/uni-cli-shared/package.json +++ b/packages/uni-cli-shared/package.json @@ -1,6 +1,6 @@ { "name": "@dcloudio/uni-cli-shared", - "version": "0.2.989", + "version": "0.2.990", "description": "uni-cli-shared", "main": "lib/index.js", "files": [ diff --git a/packages/uni-template-compiler/lib/index.js b/packages/uni-template-compiler/lib/index.js index 6eccbba99a41b1e453536786ef38d2ece01bf47b..9e6b3cd5e442dc08dbf808c53679e7fcb90a01c0 100644 --- a/packages/uni-template-compiler/lib/index.js +++ b/packages/uni-template-compiler/lib/index.js @@ -100,15 +100,18 @@ at ${resourcePath}.vue:1`) } const filterTemplate = [] options.mp.filterModules.forEach(name => { - const filterTag = options.filterTagName - const filterModule = options.filterModules[name] - if (filterModule.content) { - filterTemplate.push(`<${filterTag} module="${name}"> -${filterModule.content} -`) - } + filterTemplate.push( + options.mp.platform.createFilterTag( + options.filterTagName, + options.filterModules[name] + ) + ) }) + if (filterTemplate.length) { + template = filterTemplate.join('\n') + '\n' + template + } + if ( process.UNI_ENTRY[resourcePath] && process.env.UNI_PLATFORM !== 'app-plus' && diff --git a/packages/uni-template-compiler/lib/platforms/mp-alipay.js b/packages/uni-template-compiler/lib/platforms/mp-alipay.js index 7ef7d8c6d85d882ae5de934c25e02140c4cd8f8c..f9abfc174f078e097c3cb77cd0084a84df9ebc22 100644 --- a/packages/uni-template-compiler/lib/platforms/mp-alipay.js +++ b/packages/uni-template-compiler/lib/platforms/mp-alipay.js @@ -42,6 +42,11 @@ module.exports = { 'regionchange': 'onRegionChange' } }, + createFilterTag (filterTag, { + attrs + }) { + return `<${filterTag} name="${attrs.module}" from="${attrs.src}">` + }, getEventType (eventType) { return EVENTS[eventType] || eventType }, diff --git a/packages/uni-template-compiler/lib/platforms/mp-base.js b/packages/uni-template-compiler/lib/platforms/mp-base.js index a17835913bf5bfcddde416fb4465595f4238075f..b7af2fa38d6e049e585aed71ddb9a7683ffa03e0 100644 --- a/packages/uni-template-compiler/lib/platforms/mp-base.js +++ b/packages/uni-template-compiler/lib/platforms/mp-base.js @@ -46,7 +46,7 @@ const tags = [ 'textarea', 'video', 'view', - 'web-view', + 'web-view', 'editor' ] @@ -55,13 +55,26 @@ module.exports = { refInFor: 'data-ref-in-for', specialEvents: {}, /** - * TODO 暂时先简单判断是不是自定义组件, - * 如果要依赖真实导入的组件识别,需要 template-loader 与 script-loader 结合, - * 目前 template 在前,script 在后,要做的话,就需要把 wxml 的生成机制放到 plugin 中才可以拿到真实的组件列表 - */ + * TODO 暂时先简单判断是不是自定义组件, + * 如果要依赖真实导入的组件识别,需要 template-loader 与 script-loader 结合, + * 目前 template 在前,script 在后,要做的话,就需要把 wxml 的生成机制放到 plugin 中才可以拿到真实的组件列表 + */ isComponent (tagName) { return !tags.includes(tagName) }, + createFilterTag (filterTag, { + content, + attrs + }) { + content = content.trim() + if (content) { + return `<${filterTag} module="${attrs.module}"> +${content} +` + } else if (attrs.src) { + return `<${filterTag} src="${attrs.src}" module="${attrs.module}">` + } + }, getEventType (eventType) { return EVENTS[eventType] || eventType }, diff --git a/packages/uni-template-compiler/package.json b/packages/uni-template-compiler/package.json index f8e07225eb88015aa50a34c4cd49b4c813bc33bd..8d44782eacbd79c4088f83d608719982cbcca77d 100644 --- a/packages/uni-template-compiler/package.json +++ b/packages/uni-template-compiler/package.json @@ -1,6 +1,6 @@ { "name": "@dcloudio/uni-template-compiler", - "version": "1.0.0-alpha-22120190814002", + "version": "0.9.182", "description": "uni-template-compiler", "main": "lib/index.js", "files": [ diff --git a/packages/vue-cli-plugin-uni/lib/h5/index.js b/packages/vue-cli-plugin-uni/lib/h5/index.js index ed136daae17fd2227bbd2df5451d3435e900f780..28683ab11eff186ff133e2ff222c75775f810eb6 100644 --- a/packages/vue-cli-plugin-uni/lib/h5/index.js +++ b/packages/vue-cli-plugin-uni/lib/h5/index.js @@ -80,6 +80,11 @@ module.exports = { before: [``] } } + }, { + resourceQuery: /blockType=wxs/, + use: [{ + loader: resolve('packages/webpack-uni-filter-loader') + }] }] }, resolveLoader: { @@ -90,7 +95,7 @@ module.exports = { plugins } }, - chainWebpack (webpackConfig) { + chainWebpack (webpackConfig) { webpackConfig.plugins.delete('copy') if (!process.env.UNI_OPT_PREFETCH) { @@ -106,16 +111,22 @@ module.exports = { .use('vue-loader') .tap(options => Object.assign(options, { compiler: getPlatformCompiler(), - compilerOptions: require('./compiler-options'), - cacheDirectory: false, + compilerOptions: require('./compiler-options'), + cacheDirectory: false, cacheIdentifier: false })) .end() + .use('uniapp-custom-block-loader') + .loader(require.resolve('@dcloudio/vue-cli-plugin-uni/packages/webpack-custom-block-loader')) + .options({ + compiler: getPlatformCompiler() + }) + .end() .use('uniapp-scoped') .loader(resolve('packages/webpack-scoped-loader')) .end() .uses - .delete('cache-loader') + .delete('cache-loader') if (process.env.NODE_ENV === 'production') { const module = webpackConfig.module diff --git a/packages/vue-cli-plugin-uni/lib/mp.js b/packages/vue-cli-plugin-uni/lib/mp.js index a2d2bd2b0dcb32d323bdc4680e9947a4dc618f9e..1742c1ac99d31a9e2f37a9ff09fef2c673c65aa6 100644 --- a/packages/vue-cli-plugin-uni/lib/mp.js +++ b/packages/vue-cli-plugin-uni/lib/mp.js @@ -119,6 +119,12 @@ module.exports = { use: [{ loader: '@dcloudio/webpack-uni-mp-loader/lib/template' }] + }, { + resourceQuery: [/blockType=wxs/, /blockType=filter/, /blockType=import-sjs/], + use: [{ + loader: require.resolve( + '@dcloudio/vue-cli-plugin-uni/packages/webpack-uni-filter-loader') + }] }] }, plugins: [ @@ -128,6 +134,13 @@ module.exports = { } }, chainWebpack (webpackConfig) { + if (process.env.UNI_PLATFORM === 'mp-baidu') { + webpackConfig.module + .rule('js') + .exclude + .add(/\.filter\.js$/) + } + // disable vue cache-loader webpackConfig.module .rule('vue') diff --git a/packages/vue-cli-plugin-uni/package.json b/packages/vue-cli-plugin-uni/package.json index bce7a76733d54f87b5e9b238fcd42cfe6b258d5b..b4e067a96e2d2293585184e891008f0140f32fd3 100644 --- a/packages/vue-cli-plugin-uni/package.json +++ b/packages/vue-cli-plugin-uni/package.json @@ -1,6 +1,6 @@ { "name": "@dcloudio/vue-cli-plugin-uni", - "version": "0.9.525", + "version": "0.9.527", "description": "uni-app plugin for vue-cli 3", "main": "index.js", "scripts": { diff --git a/packages/vue-cli-plugin-uni/packages/webpack-custom-block-loader/index.js b/packages/vue-cli-plugin-uni/packages/webpack-custom-block-loader/index.js index a1758b593799e6461f10ae17bf2fbcb68b6a5f7b..6a0206825d5da2f1ecaa5d41abf8431d0a458619 100644 --- a/packages/vue-cli-plugin-uni/packages/webpack-custom-block-loader/index.js +++ b/packages/vue-cli-plugin-uni/packages/webpack-custom-block-loader/index.js @@ -8,15 +8,13 @@ const { })) // 确保使用的与 vue-loader 一致 const { - getPlatformFilterTag + getPlatformFilterTag, + normalizeNodeModules } = require('@dcloudio/uni-cli-shared/lib/platform') const FILTER_TAG = getPlatformFilterTag() module.exports = function(source) { - if (!FILTER_TAG) { - return source - } const loaderContext = this @@ -40,23 +38,31 @@ module.exports = function(source) { needMap: sourceMap }) - if (!descriptor.template) { + if (!descriptor.template || !FILTER_TAG) { + // delete customBlocks + descriptor.customBlocks.length = 0 return source } const modules = Object.create(null) - descriptor.customBlocks.filter(block => { + descriptor.customBlocks = descriptor.customBlocks.filter(block => { if (block.type === FILTER_TAG && block.attrs.module) { modules[block.attrs.module] = block + return true } }) if (Object.keys(modules)) { - descriptor.template.attrs['filter-modules'] = JSON.stringify(modules) + const filterModules = JSON.parse(JSON.stringify(modules)) + Object.keys(filterModules).forEach(name => { + const filterModule = filterModules[name] + if (filterModule.attrs.src) { + filterModule.attrs.src = normalizeNodeModules(filterModule.attrs.src) + } + }) + descriptor.template.attrs['filter-modules'] = JSON.stringify(filterModules) } - // delete customBlocks - descriptor.customBlocks.length = 0 return source } diff --git a/packages/vue-cli-plugin-uni/packages/webpack-uni-filter-loader/index.js b/packages/vue-cli-plugin-uni/packages/webpack-uni-filter-loader/index.js new file mode 100644 index 0000000000000000000000000000000000000000..e9dec94102a40be249e9adf2b2c6183550c659e8 --- /dev/null +++ b/packages/vue-cli-plugin-uni/packages/webpack-uni-filter-loader/index.js @@ -0,0 +1,27 @@ +const path = require('path') +const loaderUtils = require('loader-utils') +const { + normalizeNodeModules +} = require('@dcloudio/uni-cli-shared/lib/platform') + +module.exports = function(source, map) { + const params = loaderUtils.parseQuery(this.resourceQuery) + if (process.env.UNI_PLATFORM === 'h5') { // h5 + this.callback( + null, + `export default function (Component) { + (Component.options.wxs||(Component.options.wxs={}))['${params.module}'] = (function(module){ + ${source.trim()} + return module.exports + })({exports:{}}); + }`, + map + ) + } else { // mp + if (params.issuerPath) { + const resourcePath = normalizeNodeModules(path.relative(process.env.UNI_INPUT_DIR, this.resourcePath)) + this.emitFile(resourcePath, source) + } + this.callback(null, `export default {}`, map) + } +} diff --git a/packages/webpack-uni-mp-loader/lib/shared.js b/packages/webpack-uni-mp-loader/lib/shared.js index 9ba79bef74de756deb6904ae4c8eae501ee63e38..06d01d187ea4668bae6027adfb86ca84b0e85a6b 100644 --- a/packages/webpack-uni-mp-loader/lib/shared.js +++ b/packages/webpack-uni-mp-loader/lib/shared.js @@ -3,8 +3,10 @@ const { getPlatformExts, getPlatformTarget } = require('@dcloudio/uni-cli-shared') - -const NODE_MODULES_REGEX = /(\.\.\/)?node_modules/g + +const { + normalizeNodeModules +} = require('@dcloudio/uni-cli-shared/lib/platform') const templates = {} const compilerOptions = {} @@ -29,14 +31,6 @@ function resolve (source) { }) } -function normalizeNodeModules (str) { - str = str.replace(NODE_MODULES_REGEX, 'node-modules') - if (process.env.UNI_PLATFORM === 'mp-alipay') { - str = str.replace('node-modules/@', 'node-modules/npm-scope-') - } - return str -} - function restoreNodeModules (str) { if (process.env.UNI_PLATFORM === 'mp-alipay') { str = str.replace('node-modules/npm-scope-', 'node-modules/@') diff --git a/packages/webpack-uni-mp-loader/package.json b/packages/webpack-uni-mp-loader/package.json index dee0eecca83e731bbe46cb2b4b6b01714d6b24b2..51488b3ea3929394f439d4688b2bc4f49efd4c48 100644 --- a/packages/webpack-uni-mp-loader/package.json +++ b/packages/webpack-uni-mp-loader/package.json @@ -1,6 +1,6 @@ { "name": "@dcloudio/webpack-uni-mp-loader", - "version": "1.0.0-alpha-22120190814002", + "version": "0.3.644", "description": "webpack-uni-mp-loader", "main": "index.js", "files": [ diff --git a/packages/webpack-uni-pages-loader/package.json b/packages/webpack-uni-pages-loader/package.json index 5a1abd49d4c1655f9569dbf80adf0cf85d1b5789..0675408df3aeef16db8422accd6237769b91b65f 100644 --- a/packages/webpack-uni-pages-loader/package.json +++ b/packages/webpack-uni-pages-loader/package.json @@ -1,6 +1,6 @@ { "name": "@dcloudio/webpack-uni-pages-loader", - "version": "0.2.873", + "version": "0.2.874", "description": "uni-app pages.json loader", "main": "lib/index.js", "files": [ @@ -16,7 +16,7 @@ "strip-json-comments": "^2.0.1" }, "uni-app": { - "compilerVersion": "2.2.1" + "compilerVersion": "2.2.3" }, "gitHead": "08ea04b669e93f0db3acb2dfa38138298edd5789" }