diff --git a/.github/workflows/size-check.yml b/.github/workflows/size-check.yml index 8f07c45821aa7c9e5ce1b28b4788b7472a354b4e..43f07c59e385fb8a18a4d5df9aa8ee301abab27e 100644 --- a/.github/workflows/size-check.yml +++ b/.github/workflows/size-check.yml @@ -19,3 +19,4 @@ jobs: github_token: ${{ secrets.GITHUB_TOKEN }} build_script: build files: packages/size-check/dist/size-check.es.js packages/size-check/dist/style.css packages/uni-app/dist/uni-app.es.js packages/uni-h5-vue/dist/vue.runtime.esm.js packages/uni-mp-vue/dist/vue.runtime.esm.js packages/uni-mp-alipay/dist/uni.api.esm.js packages/uni-mp-alipay/dist/uni.mp.esm.js packages/uni-mp-baidu/dist/uni.api.esm.js packages/uni-mp-baidu/dist/uni.mp.esm.js packages/uni-mp-qq/dist/uni.api.esm.js packages/uni-mp-qq/dist/uni.mp.esm.js packages/uni-mp-toutiao/dist/uni.api.esm.js packages/uni-mp-toutiao/dist/uni.mp.esm.js packages/uni-mp-weixin/dist/uni.api.esm.js packages/uni-mp-weixin/dist/uni.mp.esm.js packages/uni-quickapp-webview/dist/uni.api.esm.js packages/uni-quickapp-webview/dist/uni.mp.esm.js + - run: npm run test diff --git a/package.json b/package.json index 36753e76d788870ebd297f8d24cce58b4b47e8c0..e94373c20f378e268b099705d773aa28971d83e8 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ ], "scripts": { "build": "node scripts/build.js", - "size": "size-limit", + "size": "npm run build size-check", "lint": "eslint packages/*/src/**/*.ts", "format": "prettier --write --parser typescript \"packages/**/*.ts?(x)\"", "ls-lint": "ls-lint", diff --git a/packages/uni-api/__tests__/helpers/api.spec.ts b/packages/uni-api/__tests__/helpers/api.spec.ts index bd92b3d1c8d2628899457c1a5514d879b1651e62..e3e8965e554884e445714c1fde2575bc31329829 100644 --- a/packages/uni-api/__tests__/helpers/api.spec.ts +++ b/packages/uni-api/__tests__/helpers/api.spec.ts @@ -1,4 +1,3 @@ -import { createOnApi } from '../../src/helpers/api/index' import { normalizeErrMsg } from '../../src/helpers/api/callback' describe('api', () => { test('normalizeErrMsg', () => { @@ -13,10 +12,4 @@ describe('api', () => { normalizeErrMsg('redirectTo:fail page not found', 'navigateTo') ).toEqual('navigateTo:fail page not found') }) - test('createOnApi', () => { - createOnApi( - 'onCompassChange', - (callback: Function) => {} - ) - }) }) diff --git a/packages/uni-api/__tests__/helpers/protocol.spec.ts b/packages/uni-api/__tests__/helpers/protocol.spec.ts index 0d90ef9a3fd47d2acd9ae133a3a4d7bffc80b270..d42fd5b0cce7d857d5b2ead5edf81ef448763d6b 100644 --- a/packages/uni-api/__tests__/helpers/protocol.spec.ts +++ b/packages/uni-api/__tests__/helpers/protocol.spec.ts @@ -1,7 +1,4 @@ -import { - validateProtocols, - API_TYPE_ON_PROTOCOLS, -} from '../../src/helpers/protocol' +import { validateProtocols } from '../../src/helpers/protocol' import { Upx2pxProtocol } from '../../src/protocols/base/upx2px' import { CreateCanvasContextProtocol } from '../../src/protocols/context/context' describe('protocol', () => { @@ -10,26 +7,14 @@ describe('protocol', () => { }) test('validate upx2px', () => { expect(validateProtocols('upx2px', [1], Upx2pxProtocol)).toBeFalsy() - expect(validateProtocols('upx2px', [], Upx2pxProtocol)).toEqual({ - errMsg: `upx2px:fail Missing required args: "upx"`, + validateProtocols('upx2px', [], Upx2pxProtocol, (name, msg) => { + expect(name).toBe('upx2px') + expect(msg).toBe(`Missing required args: "upx"`) }) - expect(validateProtocols('upx2px', [true], Upx2pxProtocol)).toEqual({ - errMsg: `upx2px:fail Invalid args: type check failed for args "upx". Expected Number, String, got Boolean with value true.`, - }) - }) - test('validate onLocationChange', () => { - expect( - validateProtocols('onLocationChange', [() => {}], API_TYPE_ON_PROTOCOLS) - ).toBeFalsy() - expect( - validateProtocols('onLocationChange', [], API_TYPE_ON_PROTOCOLS) - ).toEqual({ - errMsg: `onLocationChange:fail Missing required args: "callback"`, - }) - expect( - validateProtocols('onLocationChange', [1], API_TYPE_ON_PROTOCOLS) - ).toEqual({ - errMsg: `onLocationChange:fail Invalid args: type check failed for args "callback". Expected Function, got Number with value 1.`, + validateProtocols('upx2px', [true], Upx2pxProtocol, (_name, msg) => { + expect(msg).toBe( + `Invalid args: type check failed for args "upx". Expected Number, String, got Boolean with value true.` + ) }) }) test('validate createCanvasContext', () => { @@ -40,15 +25,24 @@ describe('protocol', () => { CreateCanvasContextProtocol ) ).toBeFalsy() - expect( - validateProtocols('createCanvasContext', [], CreateCanvasContextProtocol) - ).toEqual({ - errMsg: `createCanvasContext:fail Missing required args: "canvasId"`, - }) - expect( - validateProtocols('createCanvasContext', [1], CreateCanvasContextProtocol) - ).toEqual({ - errMsg: `createCanvasContext:fail Invalid args: type check failed for args "canvasId". Expected String with value "1", got Number with value 1.`, - }) + validateProtocols( + 'createCanvasContext', + [], + CreateCanvasContextProtocol, + (_name, msg) => { + expect(msg).toBe(`Missing required args: "canvasId"`) + } + ) + + validateProtocols( + 'createCanvasContext', + [1], + CreateCanvasContextProtocol, + (_name, msg) => { + expect(msg).toBe( + `Invalid args: type check failed for args "canvasId". Expected String with value "1", got Number with value 1.` + ) + } + ) }) }) diff --git a/packages/uni-api/src/helpers/protocol.ts b/packages/uni-api/src/helpers/protocol.ts index 9b858be230b59a797e2d4c01b285aa3fc8854e62..88d06894abaded905f1fb6f507fbea4428a70a49 100644 --- a/packages/uni-api/src/helpers/protocol.ts +++ b/packages/uni-api/src/helpers/protocol.ts @@ -51,8 +51,12 @@ function validateProtocolFail(name: string, msg: string) { function validateProtocol( name: string, data: Record, - protocol?: ApiProtocol + protocol?: ApiProtocol, + onFail?: (name: string, msg: string) => void ) { + if (!onFail) { + onFail = validateProtocolFail + } for (const key in protocol) { const errMsg = validateProp( key, @@ -61,7 +65,7 @@ function validateProtocol( !hasOwn(data, key) ) if (isString(errMsg)) { - validateProtocolFail(name, errMsg) + onFail(name, errMsg) } } } @@ -69,13 +73,19 @@ function validateProtocol( export function validateProtocols( name: string, args: any[], - protocol?: ApiProtocols + protocol?: ApiProtocols, + onFail?: (name: string, msg: string) => void ) { if (!protocol) { return } if (!isArray(protocol)) { - return validateProtocol(name, args[0] || Object.create(null), protocol) + return validateProtocol( + name, + args[0] || Object.create(null), + protocol, + onFail + ) } const len = protocol.length const argsLen = args.length @@ -85,7 +95,7 @@ export function validateProtocols( if (argsLen > i) { data[opts.name!] = args[i] } - validateProtocol(name, data, { [opts.name!]: opts }) + validateProtocol(name, data, { [opts.name!]: opts }, onFail) } } diff --git a/packages/uni-h5/dist/uni-h5.cjs.js b/packages/uni-h5/dist/uni-h5.cjs.js index 3c2ce64ac0cc81880f86a4de65f502127dbb94d8..a83fbd091dbc7e4cf5e4ef2843a7e4532ab75268 100644 --- a/packages/uni-h5/dist/uni-h5.cjs.js +++ b/packages/uni-h5/dist/uni-h5.cjs.js @@ -476,20 +476,23 @@ function elemInArray(str, arr) { function validateProtocolFail(name, msg) { console.warn(`${name}: ${msg}`); } -function validateProtocol(name, data, protocol) { +function validateProtocol(name, data, protocol, onFail) { + if (!onFail) { + onFail = validateProtocolFail; + } for (const key in protocol) { const errMsg = validateProp(key, data[key], protocol[key], !shared.hasOwn(data, key)); if (shared.isString(errMsg)) { - validateProtocolFail(name, errMsg); + onFail(name, errMsg); } } } -function validateProtocols(name, args, protocol) { +function validateProtocols(name, args, protocol, onFail) { if (!protocol) { return; } if (!shared.isArray(protocol)) { - return validateProtocol(name, args[0] || Object.create(null), protocol); + return validateProtocol(name, args[0] || Object.create(null), protocol, onFail); } const len = protocol.length; const argsLen = args.length; @@ -499,7 +502,7 @@ function validateProtocols(name, args, protocol) { if (argsLen > i2) { data[opts.name] = args[i2]; } - validateProtocol(name, data, {[opts.name]: opts}); + validateProtocol(name, data, {[opts.name]: opts}, onFail); } } function validateProp(name, value, prop, isAbsent) { diff --git a/packages/uni-h5/dist/uni-h5.es.js b/packages/uni-h5/dist/uni-h5.es.js index 669811750e0b3c6de2fab299ecd1fd74d1b77ad9..08e792fa0377788b7d0977a57ad995522276ed83 100644 --- a/packages/uni-h5/dist/uni-h5.es.js +++ b/packages/uni-h5/dist/uni-h5.es.js @@ -1270,20 +1270,23 @@ function elemsInArray(strArr, optionalVal) { function validateProtocolFail(name, msg) { console.warn(`${name}: ${msg}`); } -function validateProtocol(name, data, protocol) { +function validateProtocol(name, data, protocol, onFail) { + if (!onFail) { + onFail = validateProtocolFail; + } for (const key in protocol) { const errMsg = validateProp(key, data[key], protocol[key], !hasOwn(data, key)); if (isString(errMsg)) { - validateProtocolFail(name, errMsg); + onFail(name, errMsg); } } } -function validateProtocols(name, args, protocol) { +function validateProtocols(name, args, protocol, onFail) { if (!protocol) { return; } if (!isArray(protocol)) { - return validateProtocol(name, args[0] || Object.create(null), protocol); + return validateProtocol(name, args[0] || Object.create(null), protocol, onFail); } const len = protocol.length; const argsLen = args.length; @@ -1293,7 +1296,7 @@ function validateProtocols(name, args, protocol) { if (argsLen > i2) { data[opts.name] = args[i2]; } - validateProtocol(name, data, {[opts.name]: opts}); + validateProtocol(name, data, {[opts.name]: opts}, onFail); } } function validateProp(name, value, prop, isAbsent) { diff --git a/packages/uni-mp-alipay/dist/uni.api.esm.js b/packages/uni-mp-alipay/dist/uni.api.esm.js index de3ef33ca05888069113fc476965e01dbd9a2e35..cba8aa68f469c5f106b9b5a5a69e362a9c66b18a 100644 --- a/packages/uni-mp-alipay/dist/uni.api.esm.js +++ b/packages/uni-mp-alipay/dist/uni.api.esm.js @@ -3,20 +3,23 @@ import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawTy function validateProtocolFail(name, msg) { console.warn(`${name}: ${msg}`); } -function validateProtocol(name, data, protocol) { +function validateProtocol(name, data, protocol, onFail) { + if (!onFail) { + onFail = validateProtocolFail; + } for (const key in protocol) { const errMsg = validateProp(key, data[key], protocol[key], !hasOwn(data, key)); if (isString(errMsg)) { - validateProtocolFail(name, errMsg); + onFail(name, errMsg); } } } -function validateProtocols(name, args, protocol) { +function validateProtocols(name, args, protocol, onFail) { if (!protocol) { return; } if (!isArray(protocol)) { - return validateProtocol(name, args[0] || Object.create(null), protocol); + return validateProtocol(name, args[0] || Object.create(null), protocol, onFail); } const len = protocol.length; const argsLen = args.length; @@ -26,7 +29,7 @@ function validateProtocols(name, args, protocol) { if (argsLen > i) { data[opts.name] = args[i]; } - validateProtocol(name, data, { [opts.name]: opts }); + validateProtocol(name, data, { [opts.name]: opts }, onFail); } } function validateProp(name, value, prop, isAbsent) { diff --git a/packages/uni-mp-baidu/dist/uni.api.esm.js b/packages/uni-mp-baidu/dist/uni.api.esm.js index 83cc58434a11cf365f956c7debdaac88792d5373..b3f5adf1711ebd83a8aa95ea8919ea0f59b9883e 100644 --- a/packages/uni-mp-baidu/dist/uni.api.esm.js +++ b/packages/uni-mp-baidu/dist/uni.api.esm.js @@ -3,20 +3,23 @@ import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawTy function validateProtocolFail(name, msg) { console.warn(`${name}: ${msg}`); } -function validateProtocol(name, data, protocol) { +function validateProtocol(name, data, protocol, onFail) { + if (!onFail) { + onFail = validateProtocolFail; + } for (const key in protocol) { const errMsg = validateProp(key, data[key], protocol[key], !hasOwn(data, key)); if (isString(errMsg)) { - validateProtocolFail(name, errMsg); + onFail(name, errMsg); } } } -function validateProtocols(name, args, protocol) { +function validateProtocols(name, args, protocol, onFail) { if (!protocol) { return; } if (!isArray(protocol)) { - return validateProtocol(name, args[0] || Object.create(null), protocol); + return validateProtocol(name, args[0] || Object.create(null), protocol, onFail); } const len = protocol.length; const argsLen = args.length; @@ -26,7 +29,7 @@ function validateProtocols(name, args, protocol) { if (argsLen > i) { data[opts.name] = args[i]; } - validateProtocol(name, data, { [opts.name]: opts }); + validateProtocol(name, data, { [opts.name]: opts }, onFail); } } function validateProp(name, value, prop, isAbsent) { diff --git a/packages/uni-mp-qq/dist/uni.api.esm.js b/packages/uni-mp-qq/dist/uni.api.esm.js index cbf86372e64790df0666dd343f3be0a87c1a6459..c6b7724d571a00d2d6460a119d19733cd1d4e119 100644 --- a/packages/uni-mp-qq/dist/uni.api.esm.js +++ b/packages/uni-mp-qq/dist/uni.api.esm.js @@ -3,20 +3,23 @@ import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawTy function validateProtocolFail(name, msg) { console.warn(`${name}: ${msg}`); } -function validateProtocol(name, data, protocol) { +function validateProtocol(name, data, protocol, onFail) { + if (!onFail) { + onFail = validateProtocolFail; + } for (const key in protocol) { const errMsg = validateProp(key, data[key], protocol[key], !hasOwn(data, key)); if (isString(errMsg)) { - validateProtocolFail(name, errMsg); + onFail(name, errMsg); } } } -function validateProtocols(name, args, protocol) { +function validateProtocols(name, args, protocol, onFail) { if (!protocol) { return; } if (!isArray(protocol)) { - return validateProtocol(name, args[0] || Object.create(null), protocol); + return validateProtocol(name, args[0] || Object.create(null), protocol, onFail); } const len = protocol.length; const argsLen = args.length; @@ -26,7 +29,7 @@ function validateProtocols(name, args, protocol) { if (argsLen > i) { data[opts.name] = args[i]; } - validateProtocol(name, data, { [opts.name]: opts }); + validateProtocol(name, data, { [opts.name]: opts }, onFail); } } function validateProp(name, value, prop, isAbsent) { diff --git a/packages/uni-mp-toutiao/dist/uni.api.esm.js b/packages/uni-mp-toutiao/dist/uni.api.esm.js index ac733d9b9aaafe157e4cde8e70348444e47ea78b..e70d103a21e77dfd79419ed9971642de33665709 100644 --- a/packages/uni-mp-toutiao/dist/uni.api.esm.js +++ b/packages/uni-mp-toutiao/dist/uni.api.esm.js @@ -3,20 +3,23 @@ import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawTy function validateProtocolFail(name, msg) { console.warn(`${name}: ${msg}`); } -function validateProtocol(name, data, protocol) { +function validateProtocol(name, data, protocol, onFail) { + if (!onFail) { + onFail = validateProtocolFail; + } for (const key in protocol) { const errMsg = validateProp(key, data[key], protocol[key], !hasOwn(data, key)); if (isString(errMsg)) { - validateProtocolFail(name, errMsg); + onFail(name, errMsg); } } } -function validateProtocols(name, args, protocol) { +function validateProtocols(name, args, protocol, onFail) { if (!protocol) { return; } if (!isArray(protocol)) { - return validateProtocol(name, args[0] || Object.create(null), protocol); + return validateProtocol(name, args[0] || Object.create(null), protocol, onFail); } const len = protocol.length; const argsLen = args.length; @@ -26,7 +29,7 @@ function validateProtocols(name, args, protocol) { if (argsLen > i) { data[opts.name] = args[i]; } - validateProtocol(name, data, { [opts.name]: opts }); + validateProtocol(name, data, { [opts.name]: opts }, onFail); } } function validateProp(name, value, prop, isAbsent) { diff --git a/packages/uni-mp-weixin/dist/uni.api.esm.js b/packages/uni-mp-weixin/dist/uni.api.esm.js index 86b9e131b83a361aa99abf5b7840eb42db1111d4..980b857003f256cde7726f8455026a2b33082a1e 100644 --- a/packages/uni-mp-weixin/dist/uni.api.esm.js +++ b/packages/uni-mp-weixin/dist/uni.api.esm.js @@ -3,20 +3,23 @@ import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawTy function validateProtocolFail(name, msg) { console.warn(`${name}: ${msg}`); } -function validateProtocol(name, data, protocol) { +function validateProtocol(name, data, protocol, onFail) { + if (!onFail) { + onFail = validateProtocolFail; + } for (const key in protocol) { const errMsg = validateProp(key, data[key], protocol[key], !hasOwn(data, key)); if (isString(errMsg)) { - validateProtocolFail(name, errMsg); + onFail(name, errMsg); } } } -function validateProtocols(name, args, protocol) { +function validateProtocols(name, args, protocol, onFail) { if (!protocol) { return; } if (!isArray(protocol)) { - return validateProtocol(name, args[0] || Object.create(null), protocol); + return validateProtocol(name, args[0] || Object.create(null), protocol, onFail); } const len = protocol.length; const argsLen = args.length; @@ -26,7 +29,7 @@ function validateProtocols(name, args, protocol) { if (argsLen > i) { data[opts.name] = args[i]; } - validateProtocol(name, data, { [opts.name]: opts }); + validateProtocol(name, data, { [opts.name]: opts }, onFail); } } function validateProp(name, value, prop, isAbsent) { diff --git a/packages/uni-quickapp-webview/dist/uni.api.esm.js b/packages/uni-quickapp-webview/dist/uni.api.esm.js index fe3f878b7f03f07c357ecd8cd815fa2b6ed40bc0..bbc0f3b7370fa007c7ef3726621f7304747b6706 100644 --- a/packages/uni-quickapp-webview/dist/uni.api.esm.js +++ b/packages/uni-quickapp-webview/dist/uni.api.esm.js @@ -3,20 +3,23 @@ import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawTy function validateProtocolFail(name, msg) { console.warn(`${name}: ${msg}`); } -function validateProtocol(name, data, protocol) { +function validateProtocol(name, data, protocol, onFail) { + if (!onFail) { + onFail = validateProtocolFail; + } for (const key in protocol) { const errMsg = validateProp(key, data[key], protocol[key], !hasOwn(data, key)); if (isString(errMsg)) { - validateProtocolFail(name, errMsg); + onFail(name, errMsg); } } } -function validateProtocols(name, args, protocol) { +function validateProtocols(name, args, protocol, onFail) { if (!protocol) { return; } if (!isArray(protocol)) { - return validateProtocol(name, args[0] || Object.create(null), protocol); + return validateProtocol(name, args[0] || Object.create(null), protocol, onFail); } const len = protocol.length; const argsLen = args.length; @@ -26,7 +29,7 @@ function validateProtocols(name, args, protocol) { if (argsLen > i) { data[opts.name] = args[i]; } - validateProtocol(name, data, { [opts.name]: opts }); + validateProtocol(name, data, { [opts.name]: opts }, onFail); } } function validateProp(name, value, prop, isAbsent) { diff --git a/packages/vite-plugin-uni/__tests__/easycom.spec.ts b/packages/vite-plugin-uni/__tests__/easycom.spec.ts index 0f29e9f587425ca8310d00f71786e45a26564dd9..91072ddc2d483b3e3c3e0ef69224c6ec465e2a8c 100644 --- a/packages/vite-plugin-uni/__tests__/easycom.spec.ts +++ b/packages/vite-plugin-uni/__tests__/easycom.spec.ts @@ -1,16 +1,12 @@ import path from 'path' -import { initEasycom, matchEasycom } from '../src/easycom' +import { initEasycoms, matchEasycom } from '../src/utils' const rootDir = path.resolve(__dirname, 'example') -const dirs = [ - path.resolve(__dirname, 'example/components'), - path.resolve(__dirname, 'example/uni_modules/plugin/components'), -] describe('easycom', () => { test('initEasycom with dirs', () => { - expect(initEasycom({ dirs, rootDir })).toEqual([ + expect(initEasycoms(rootDir, 'h5').easycoms).toEqual([ { pattern: new RegExp('^test$'), replacement: '@/components/test/test.vue', @@ -23,46 +19,16 @@ describe('easycom', () => { pattern: new RegExp('^test2$'), replacement: '@/uni_modules/plugin/components/test2/test2.vue', }, + { + pattern: new RegExp('^uni-(.*)'), + replacement: '@/components/uni-$1.vue', + }, ]) expect(matchEasycom('test')).toBe('@/components/test/test.vue') expect(matchEasycom('test1')).toBe('@/components/test1/test1.vue') expect(matchEasycom('test2')).toBe( '@/uni_modules/plugin/components/test2/test2.vue' ) - }) - test('initEasycom with custom', () => { - expect( - initEasycom({ custom: { '^uni-(.*)': '@/components/uni-$1.vue' } }) - ).toEqual([ - { - pattern: new RegExp('^uni-(.*)'), - replacement: '@/components/uni-$1.vue', - }, - ]) - expect(matchEasycom('test')).toBe(false) expect(matchEasycom('uni-test1')).toBe('@/components/uni-test1.vue') }) - test('initEasycom with dirs and custom', () => { - expect( - initEasycom({ - dirs, - rootDir, - custom: { '^test$': '@/components/uni-test.vue' }, - }) - ).toEqual([ - { - pattern: new RegExp('^test$'), - replacement: '@/components/uni-test.vue', - }, - { - pattern: new RegExp('^test1$'), - replacement: '@/components/test1/test1.vue', - }, - { - pattern: new RegExp('^test2$'), - replacement: '@/uni_modules/plugin/components/test2/test2.vue', - }, - ]) - expect(matchEasycom('test')).toBe('@/components/uni-test.vue') - }) }) diff --git a/packages/vite-plugin-uni/__tests__/example/pages.json b/packages/vite-plugin-uni/__tests__/example/pages.json new file mode 100644 index 0000000000000000000000000000000000000000..8ad26a8aef36f038378ed03ac7dd27932c8217d0 --- /dev/null +++ b/packages/vite-plugin-uni/__tests__/example/pages.json @@ -0,0 +1,12 @@ +{ + "pages": [ + { + "path": "pages/index/index" + } + ], + "easycom": { + "custom": { + "^uni-(.*)": "@/components/uni-$1.vue" + } + } +} diff --git a/packages/vite-plugin-uni/src/config/define.ts b/packages/vite-plugin-uni/src/config/define.ts index 190f4c71a43d0b4fa856710ea7a874d2a8db1f4a..720176ca323f77dacfe1d6674289953bdd69060e 100644 --- a/packages/vite-plugin-uni/src/config/define.ts +++ b/packages/vite-plugin-uni/src/config/define.ts @@ -4,11 +4,11 @@ import { parsePagesJsonOnce, parseManifestJsonOnce, } from '@dcloudio/uni-cli-shared' -import { initFeatures } from '../utils' +import { initFeatures, isSsr, isSsrManifest } from '../utils' export function createDefine( { inputDir, platform }: VitePluginUniResolvedOptions, - { server }: UserConfig, + config: UserConfig, { command }: ConfigEnv ): UserConfig['define'] { return initFeatures({ @@ -17,6 +17,6 @@ export function createDefine( platform, pagesJson: parsePagesJsonOnce(inputDir, platform), manifestJson: parseManifestJsonOnce(inputDir), - ssr: !!(server && server.middlewareMode), + ssr: isSsr(command, config) || isSsrManifest(command, config), }) } diff --git a/packages/vite-plugin-uni/src/configResolved/config.ts b/packages/vite-plugin-uni/src/configResolved/config.ts index 015fd4621919259b8665b827cb914a04c6c0c841..d25fb13fa17455e8fe20d01dae2921ad26b58db8 100644 --- a/packages/vite-plugin-uni/src/configResolved/config.ts +++ b/packages/vite-plugin-uni/src/configResolved/config.ts @@ -1,12 +1,9 @@ import { ResolvedConfig } from 'vite' -import { rewriteSsrNativeTag, rewriteSsrRenderStyle } from '../utils' +import { isSsr, rewriteSsrNativeTag, rewriteSsrRenderStyle } from '../utils' // import alias from 'module-alias' export function initConfig(config: ResolvedConfig) { - if ( - (config.command === 'serve' && config.server.middlewareMode) || - (config.command === 'build' && config.build.ssr) - ) { + if (isSsr(config.command, config)) { rewriteSsrNativeTag() rewriteSsrRenderStyle(process.env.UNI_INPUT_DIR) } diff --git a/packages/vite-plugin-uni/src/configResolved/plugins/mainJs.ts b/packages/vite-plugin-uni/src/configResolved/plugins/mainJs.ts index 2711434ada3615c4e673ad8229181544ae2b283d..c8d901f21a088963238b86f54731e34fcc726a6e 100644 --- a/packages/vite-plugin-uni/src/configResolved/plugins/mainJs.ts +++ b/packages/vite-plugin-uni/src/configResolved/plugins/mainJs.ts @@ -2,6 +2,7 @@ import path from 'path' import slash from 'slash' import { Plugin, ResolvedConfig } from 'vite' import { VitePluginUniResolvedOptions } from '../..' +import { isSsr, isSsrManifest } from '../../utils' export function uniMainJsPlugin( config: ResolvedConfig, @@ -12,11 +13,7 @@ export function uniMainJsPlugin( const mainTsPath = mainPath + '.ts' const pagesJsonJsPath = slash(path.resolve(options.inputDir, 'pages.json.js')) const isSSR = - config.command === 'serve' - ? !!config.server.middlewareMode - : config.command === 'build' - ? !!(config.build.ssr || config.build.ssrManifest) - : false + isSsr(config.command, config) || isSsrManifest(config.command, config) return { name: 'vite:uni-main-js', transform(code, id, ssr) { diff --git a/packages/vite-plugin-uni/src/utils/easycom.ts b/packages/vite-plugin-uni/src/utils/easycom.ts index d8723dc67fb37bbf6d60e7d6196b19e0ab2268f4..fbbf896bc7037a9e9c95231807b9c0558d6771a1 100644 --- a/packages/vite-plugin-uni/src/utils/easycom.ts +++ b/packages/vite-plugin-uni/src/utils/easycom.ts @@ -38,52 +38,53 @@ function clearEasycom() { easycomsInvalidCache.clear() } -export const initEasycomsOnce = once( - (inputDir: string, platform: UniApp.PLATFORM) => { - const buildInComponentsDir = path.resolve( - require.resolve('@dcloudio/uni-components'), - '../lib' - ) - const componentsDir = path.resolve(inputDir, 'components') - const uniModulesDir = path.resolve(inputDir, 'uni_modules') - const initEasycomOptions = (pagesJson?: UniApp.PagesJson) => { - // 初始化时,从once中读取缓存,refresh时,实时读取 - const { easycom } = pagesJson || parsePagesJson(inputDir, platform, false) - const easycomOptions: EasycomOption = { - dirs: - easycom && easycom.autoscan === false - ? [buildInComponentsDir] // 禁止自动扫描 - : [ - buildInComponentsDir, - componentsDir, - ...initUniModulesEasycomDirs(uniModulesDir), - ], - rootDir: inputDir, - autoscan: !!(easycom && easycom.autoscan), - custom: (easycom && easycom.custom) || {}, - } - debugEasycom(easycomOptions) - return easycomOptions - } - const options = initEasycomOptions(parsePagesJsonOnce(inputDir, platform)) - initEasycom(options) - const res = { - options, - filter: createFilter( - ['components/*/*.vue', 'uni_modules/*/components/*/*.vue'], - [], - { - resolve: inputDir, - } - ), - refresh() { - res.options = initEasycomOptions() - initEasycom(res.options) - }, +export function initEasycoms(inputDir: string, platform: UniApp.PLATFORM) { + const buildInComponentsDir = path.resolve( + require.resolve('@dcloudio/uni-components'), + '../lib' + ) + const componentsDir = path.resolve(inputDir, 'components') + const uniModulesDir = path.resolve(inputDir, 'uni_modules') + const initEasycomOptions = (pagesJson?: UniApp.PagesJson) => { + // 初始化时,从once中读取缓存,refresh时,实时读取 + const { easycom } = pagesJson || parsePagesJson(inputDir, platform, false) + const easycomOptions: EasycomOption = { + dirs: + easycom && easycom.autoscan === false + ? [buildInComponentsDir] // 禁止自动扫描 + : [ + buildInComponentsDir, + componentsDir, + ...initUniModulesEasycomDirs(uniModulesDir), + ], + rootDir: inputDir, + autoscan: !!(easycom && easycom.autoscan), + custom: (easycom && easycom.custom) || {}, } - return res + debugEasycom(easycomOptions) + return easycomOptions } -) + const options = initEasycomOptions(parsePagesJsonOnce(inputDir, platform)) + initEasycom(options) + const res = { + options, + filter: createFilter( + ['components/*/*.vue', 'uni_modules/*/components/*/*.vue'], + [], + { + resolve: inputDir, + } + ), + refresh() { + res.options = initEasycomOptions() + initEasycom(res.options) + }, + easycoms, + } + return res +} + +export const initEasycomsOnce = once(initEasycoms) function initUniModulesEasycomDirs(uniModulesDir: string) { if (!fs.existsSync(uniModulesDir)) { diff --git a/packages/vite-plugin-uni/src/utils/ssr.ts b/packages/vite-plugin-uni/src/utils/ssr.ts index bc52d4249c47aad15c4a09f1754abf0ee8cfc75d..ebc9426a7ae93525e0fdca053051f8da98d1d93a 100644 --- a/packages/vite-plugin-uni/src/utils/ssr.ts +++ b/packages/vite-plugin-uni/src/utils/ssr.ts @@ -7,6 +7,30 @@ import { Rpx2UnitOptions, } from '@dcloudio/uni-shared' import { parseRpx2UnitOnce } from '@dcloudio/uni-cli-shared' +import { ConfigEnv, ResolvedConfig, UserConfig } from 'vite' + +export function isSsr( + command: ConfigEnv['command'], + config: UserConfig | ResolvedConfig +) { + if (command === 'serve') { + return !!(config.server && config.server.middlewareMode) + } + if (command === 'build') { + return !!(config.build && config.build.ssr) + } + return false +} + +export function isSsrManifest( + command: ConfigEnv['command'], + config: UserConfig | ResolvedConfig +) { + if (command === 'build') { + return !!(config.build && config.build.ssrManifest) + } + return false +} function serializeDefine(define: Record): string { let res = `{`