diff --git a/packages/uni-cli-shared/src/mp/template.ts b/packages/uni-cli-shared/src/mp/template.ts index 1e125fe890cdccec8a0a879c5ecdf5cae5c60178..c9c0b9a959980fa0d824ac0007c74bdae8804a03 100644 --- a/packages/uni-cli-shared/src/mp/template.ts +++ b/packages/uni-cli-shared/src/mp/template.ts @@ -1,3 +1,4 @@ +import path from 'path' import { EmittedAsset } from 'rollup' import { isComponentTag, LINEFEED } from '@dcloudio/uni-shared' import { @@ -10,6 +11,7 @@ import { TemplateChildNode, TransformContext, } from '@vue/compiler-core' +import { normalizeMiniProgramFilename } from '../utils' export interface MiniProgramCompilerOptions { slot: { @@ -29,11 +31,29 @@ export interface MiniProgramFilterOptions { code: string } -type GenFilterFn = (filter: MiniProgramFilterOptions) => string | void +type GenFilterFn = ( + filter: MiniProgramFilterOptions, + filename: string +) => string | void const templateFilesCache = new Map() const templateFiltersCache = new Map() +function relativeFilterFilename( + filename: string, + filter: MiniProgramFilterOptions +) { + if (!filter.src) { + return '' + } + return ( + './' + + normalizeMiniProgramFilename( + path.relative(path.dirname(filename), filter.src) + ) + ) +} + export function findMiniProgramTemplateFiles(genFilter?: GenFilterFn) { const files: Record = Object.create(null) templateFilesCache.forEach((code, filename) => { @@ -43,7 +63,11 @@ export function findMiniProgramTemplateFiles(genFilter?: GenFilterFn) { const filters = getMiniProgramTemplateFilters(filename) if (filters && filters.length) { files[filename] = - filters.map((filter) => genFilter(filter)).join(LINEFEED) + + filters + .map((filter) => + genFilter(filter, relativeFilterFilename(filename, filter)) + ) + .join(LINEFEED) + LINEFEED + code } else { diff --git a/packages/uni-mp-qq/build.json b/packages/uni-mp-qq/build.json index 954a5a4701feb3a4d21bfb7b43960ec11d2053f8..7c5fdf36463e2543a7d170a123e6f94d73dce92e 100644 --- a/packages/uni-mp-qq/build.json +++ b/packages/uni-mp-qq/build.json @@ -6,7 +6,11 @@ "output": { "format": "cjs" }, - "external": ["@dcloudio/uni-cli-shared", "@dcloudio/uni-mp-vite"] + "external": [ + "fs-extra", + "@dcloudio/uni-cli-shared", + "@dcloudio/uni-mp-vite" + ] }, { "input": { diff --git a/packages/uni-mp-qq/dist/uni.compiler.js b/packages/uni-mp-qq/dist/uni.compiler.js new file mode 100644 index 0000000000000000000000000000000000000000..432eb76991bd8e486ca4e4aaa080dde0e5291bb8 --- /dev/null +++ b/packages/uni-mp-qq/dist/uni.compiler.js @@ -0,0 +1,158 @@ +'use strict'; + +var uniCliShared = require('@dcloudio/uni-cli-shared'); +var initMiniProgramPlugin = require('@dcloudio/uni-mp-vite'); +var path = require('path'); +var fs = require('fs-extra'); + +function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } + +var initMiniProgramPlugin__default = /*#__PURE__*/_interopDefaultLegacy(initMiniProgramPlugin); +var path__default = /*#__PURE__*/_interopDefaultLegacy(path); +var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs); + +var description = "项目配置文件。"; +var packOptions = { + ignore: [ + ] +}; +var setting = { + urlCheck: true, + es6: true, + postcss: false, + minified: false, + newFeature: true, + nodeModules: false +}; +var compileType = "miniprogram"; +var libVersion = ""; +var appid = "touristappid"; +var projectname = ""; +var condition = { + search: { + current: -1, + list: [ + ] + }, + conversation: { + current: -1, + list: [ + ] + }, + game: { + current: -1, + list: [ + ] + }, + miniprogram: { + current: -1, + list: [ + ] + } +}; +var source = { + description: description, + packOptions: packOptions, + setting: setting, + compileType: compileType, + libVersion: libVersion, + appid: appid, + projectname: projectname, + condition: condition +}; + +let isFixed = false; +function fix2648(bundle) { + if (isFixed) { + return; + } + const appJsonAsset = bundle['app.json']; + if (!appJsonAsset) { + return; + } + try { + const { usingComponents } = JSON.parse(appJsonAsset.source.toString()); + if (usingComponents && !Object.keys(usingComponents).length) { + fs__default["default"].outputFileSync(path__default["default"].resolve(process.env.UNI_OUTPUT_DIR, 'fix-2648.json'), `{"component":true}`); + fs__default["default"].outputFileSync(path__default["default"].resolve(process.env.UNI_OUTPUT_DIR, 'fix-2648.qml'), ``); + fs__default["default"].outputFileSync(path__default["default"].resolve(process.env.UNI_OUTPUT_DIR, 'fix-2648.js'), `Component({})`); + } + isFixed = true; + } + catch (_a) { } +} + +const uniMiniProgramWeixinPlugin = { + name: 'vite:uni-mp-qq', + config() { + return { + define: { + __VUE_CREATED_DEFERRED__: false, + }, + }; + }, + writeBundle(_, bundle) { + fix2648(bundle); + }, +}; +const options = { + vite: { + inject: { + uni: [ + uniCliShared.resolveBuiltIn('@dcloudio/uni-mp-qq/dist/uni.api.esm.js'), + 'default', + ], + }, + alias: { + 'uni-mp-runtime': uniCliShared.resolveBuiltIn('@dcloudio/uni-mp-qq/dist/uni.mp.esm.js'), + }, + copyOptions: { + assets: ['wxcomponents'], + targets: [ + { + src: ['custom-tab-bar'], + get dest() { + return process.env.UNI_OUTPUT_DIR; + }, + }, + ], + }, + }, + global: 'qq', + app: { + darkmode: true, + subpackages: true, + }, + project: { + filename: 'project.config.json', + source, + }, + template: { + filter: { + extname: '.qs', + lang: 'wxs', + generate(filter, filename) { + if (filename) { + return ``; + } + return ` +${filter.code} +`; + }, + }, + slot: { + fallback: false, + }, + extname: '.qml', + directive: 'qq:', + compilerOptions: { + nodeTransforms: [uniCliShared.addComponentBindLink], + }, + }, + style: { + extname: '.qss', + }, +}; +var index = [uniMiniProgramWeixinPlugin, ...initMiniProgramPlugin__default["default"](options)]; + +module.exports = index; diff --git a/packages/uni-mp-qq/package.json b/packages/uni-mp-qq/package.json index 4bb3197b6646d24ff04833ed346a91308223dae3..d312471580e00cfbae5461f6e18fc33ffbe1f93c 100644 --- a/packages/uni-mp-qq/package.json +++ b/packages/uni-mp-qq/package.json @@ -14,7 +14,9 @@ "license": "Apache-2.0", "uni-app": { "name": "mp-qq", - "title": "QQ小程序" + "title": "QQ小程序", + "apply": "mp-qq", + "main": "dist/uni.compiler.js" }, "gitHead": "1efa8efd0a9eddeabdba75c020d015ebf31b8177" } diff --git a/packages/uni-mp-qq/src/plugin/fix2648.ts b/packages/uni-mp-qq/src/plugin/fix2648.ts new file mode 100644 index 0000000000000000000000000000000000000000..0d82f4c842d3d5984d693f82fc9beb4c92a55092 --- /dev/null +++ b/packages/uni-mp-qq/src/plugin/fix2648.ts @@ -0,0 +1,35 @@ +import path from 'path' +import fs from 'fs-extra' + +import { OutputAsset, OutputBundle } from 'rollup' + +let isFixed = false +export function fix2648(bundle: OutputBundle) { + if (isFixed) { + return + } + const appJsonAsset = bundle['app.json'] as OutputAsset + if (!appJsonAsset) { + return + } + try { + const { usingComponents } = JSON.parse(appJsonAsset.source.toString()) as { + usingComponents?: Record + } + if (usingComponents && !Object.keys(usingComponents).length) { + fs.outputFileSync( + path.resolve(process.env.UNI_OUTPUT_DIR, 'fix-2648.json'), + `{"component":true}` + ) + fs.outputFileSync( + path.resolve(process.env.UNI_OUTPUT_DIR, 'fix-2648.qml'), + `` + ) + fs.outputFileSync( + path.resolve(process.env.UNI_OUTPUT_DIR, 'fix-2648.js'), + `Component({})` + ) + } + isFixed = true + } catch {} +} diff --git a/packages/uni-mp-qq/src/plugin/index.ts b/packages/uni-mp-qq/src/plugin/index.ts new file mode 100644 index 0000000000000000000000000000000000000000..096dbf03f3721cdf9405fd6da1e7d2f5e7c63f42 --- /dev/null +++ b/packages/uni-mp-qq/src/plugin/index.ts @@ -0,0 +1,85 @@ +import { Plugin } from 'vite' +import { addComponentBindLink, resolveBuiltIn } from '@dcloudio/uni-cli-shared' +import initMiniProgramPlugin, { + UniMiniProgramPluginOptions, +} from '@dcloudio/uni-mp-vite' + +import source from './project.config.json' +import { fix2648 } from './fix2648' + +const uniMiniProgramWeixinPlugin: Plugin = { + name: 'vite:uni-mp-qq', + config() { + return { + define: { + __VUE_CREATED_DEFERRED__: false, + }, + } + }, + writeBundle(_, bundle) { + fix2648(bundle) + }, +} + +const options: UniMiniProgramPluginOptions = { + vite: { + inject: { + uni: [ + resolveBuiltIn('@dcloudio/uni-mp-qq/dist/uni.api.esm.js'), + 'default', + ], + }, + alias: { + 'uni-mp-runtime': resolveBuiltIn( + '@dcloudio/uni-mp-qq/dist/uni.mp.esm.js' + ), + }, + copyOptions: { + assets: ['wxcomponents'], + targets: [ + { + src: ['custom-tab-bar'], + get dest() { + return process.env.UNI_OUTPUT_DIR + }, + }, + ], + }, + }, + global: 'qq', + app: { + darkmode: true, + subpackages: true, + }, + project: { + filename: 'project.config.json', + source, + }, + template: { + filter: { + extname: '.qs', + lang: 'wxs', + generate(filter, filename) { + if (filename) { + return `` + } + return ` +${filter.code} +` + }, + }, + slot: { + fallback: false, + }, + extname: '.qml', + directive: 'qq:', + compilerOptions: { + nodeTransforms: [addComponentBindLink], + }, + }, + style: { + extname: '.qss', + }, +} + +export default [uniMiniProgramWeixinPlugin, ...initMiniProgramPlugin(options)] diff --git a/packages/uni-mp-qq/src/plugin/project.config.json b/packages/uni-mp-qq/src/plugin/project.config.json new file mode 100644 index 0000000000000000000000000000000000000000..a3ebd4c40b002b640a3959ef07e228e24c532940 --- /dev/null +++ b/packages/uni-mp-qq/src/plugin/project.config.json @@ -0,0 +1,36 @@ +{ + "description": "项目配置文件。", + "packOptions": { + "ignore": [] + }, + "setting": { + "urlCheck": true, + "es6": true, + "postcss": false, + "minified": false, + "newFeature": true, + "nodeModules": false + }, + "compileType": "miniprogram", + "libVersion": "", + "appid": "touristappid", + "projectname": "", + "condition": { + "search": { + "current": -1, + "list": [] + }, + "conversation": { + "current": -1, + "list": [] + }, + "game": { + "current": -1, + "list": [] + }, + "miniprogram": { + "current": -1, + "list": [] + } + } +} diff --git a/packages/uni-mp-vite/src/plugin/build.ts b/packages/uni-mp-vite/src/plugin/build.ts index dcf36256fc05a30fc759a1c1b3699dd61b6c6755..a8670764c77c823f066ead0a742b2b5829cb4614 100644 --- a/packages/uni-mp-vite/src/plugin/build.ts +++ b/packages/uni-mp-vite/src/plugin/build.ts @@ -31,7 +31,7 @@ export function buildOptions(): UserConfig['build'] { } return { // sourcemap: 'inline', // TODO - target: ['chrome53'], + // target: ['chrome53'], // 由小程序自己启用 es6 编译 emptyOutDir: false, // 不清空输出目录,否则会影响自定义的一些文件输出,比如wxml assetsInlineLimit: 40 * 1024, // 40kb lib: { diff --git a/packages/uni-mp-vite/src/plugin/configResolved.ts b/packages/uni-mp-vite/src/plugin/configResolved.ts index 68eebb66966f2be1310049e1904f00ea8c7bc479..6312d431a62435d9d09a3466eb5d0e66d475b471 100644 --- a/packages/uni-mp-vite/src/plugin/configResolved.ts +++ b/packages/uni-mp-vite/src/plugin/configResolved.ts @@ -67,7 +67,7 @@ export function createConfigResolved({ const normalized = normalizePath(filename) if (nvueCssPaths.find((pageCssPath) => pageCssPath === normalized)) { debugNVueCss(normalized) - return `@import "/nvue.wxss";\n` + cssCode + return `@import "/nvue${extname}";\n` + cssCode } return cssCode }, diff --git a/packages/uni-mp-weixin/dist/uni.compiler.js b/packages/uni-mp-weixin/dist/uni.compiler.js index 3fcc00a35c6cc2918372e016aa31582a63857ae7..c4d09bb672a4582437170bdb1ff7737f186d2bb2 100644 --- a/packages/uni-mp-weixin/dist/uni.compiler.js +++ b/packages/uni-mp-weixin/dist/uni.compiler.js @@ -14,7 +14,7 @@ var packOptions = { }; var setting = { urlCheck: true, - es6: false, + es6: true, postcss: false, minified: false, newFeature: true @@ -110,9 +110,9 @@ const options = { filter: { extname: '.wxs', lang: 'wxs', - generate(filter) { - if (filter.src) { - return ``; + generate(filter, filename) { + if (filename) { + return ``; } return ` ${filter.code} diff --git a/packages/uni-mp-weixin/src/plugin/index.ts b/packages/uni-mp-weixin/src/plugin/index.ts index af8dbde63f1e9738f48cf4bd516b45a38767ca07..b5c90d693ede45f78dde6c4900ad8f9f4c737f72 100644 --- a/packages/uni-mp-weixin/src/plugin/index.ts +++ b/packages/uni-mp-weixin/src/plugin/index.ts @@ -64,9 +64,9 @@ const options: UniMiniProgramPluginOptions = { filter: { extname: '.wxs', lang: 'wxs', - generate(filter) { - if (filter.src) { - return `` + generate(filter, filename) { + if (filename) { + return `` } return ` ${filter.code} diff --git a/packages/uni-mp-weixin/src/plugin/project.config.json b/packages/uni-mp-weixin/src/plugin/project.config.json index 4b015db77f56655d884d49a952391afd3ee752b1..1c7a51297f58ef6773b01783bbea74ca36c9a721 100644 --- a/packages/uni-mp-weixin/src/plugin/project.config.json +++ b/packages/uni-mp-weixin/src/plugin/project.config.json @@ -5,7 +5,7 @@ }, "setting": { "urlCheck": true, - "es6": false, + "es6": true, "postcss": false, "minified": false, "newFeature": true