Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"typescript.tsdk": "node_modules/typescript/lib",
"js/ts.tsdk.path": "node_modules/typescript/lib",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit",
"source.organizeImports": "never"
Expand Down
1 change: 0 additions & 1 deletion examples/vue3+vite+ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"devDependencies": {
"@dcloudio/types": "catalog:uniapp",
"@dcloudio/uni-automator": "catalog:uniapp",
"@dcloudio/uni-cli-shared": "catalog:uniapp",
"@dcloudio/vite-plugin-uni": "catalog:uniapp",
"@https-enable/types": "^0.1.1",
"@types/lodash": "catalog:",
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
"scripts": {
"build": "tsdown",
"dev": "tsdown --watch",
"test": "vitest run",
"test:watch": "vitest",
"release": "npm run build && bumpp",
"prepublishOnly": "npm run build",
"prepare": "simple-git-hooks && npm run build",
Expand All @@ -62,7 +64,6 @@
"vue": "^3.0.0"
},
"dependencies": {
"@dcloudio/uni-cli-shared": "3.0.0-4020820240925001",
"@vue/compiler-sfc": "catalog:",
"ast-kit": "^2.2.0",
"chalk": "4.1.2",
Expand All @@ -84,7 +85,8 @@
"simple-git-hooks": "^2.11.1",
"tsdown": "^0.18.1",
"typescript": "^5.7.3",
"vite": "^4.0.0"
"vite": "^4.0.0",
"vitest": "0.34.6"
},
"simple-git-hooks": {
"pre-commit": "pnpm lint-staged",
Expand Down
467 changes: 311 additions & 156 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ catalogs:
'@dcloudio/types': 3.4.14
'@dcloudio/uni-app': 3.0.0-4060620250520001
'@dcloudio/uni-automator': 3.0.0-4060620250520001
'@dcloudio/uni-cli-shared': 3.0.0-4060620250520001
'@dcloudio/uni-components': 3.0.0-4060620250520001
'@dcloudio/uni-h5': 3.0.0-4060620250520001
'@dcloudio/uni-mp-weixin': 3.0.0-4060620250520001
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/subpackages-optimization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import type { Plugin } from 'vite'
import type { ISubPkgsInfo, ManualChunkMeta, ManualChunksOption, ModuleInfo, OptimizationOptions } from '../type'
import fs from 'node:fs'
import path from 'node:path'
import { parseManifestJsonOnce, parseMiniProgramPagesJson } from '@dcloudio/uni-cli-shared'
import { logger } from '../common/Logger'
import { EXT_RE, EXTNAME_JS_RE, ROOT_DIR, UNI_INPUT_DIR } from '../constants'
import { moduleIdProcessor as _moduleIdProcessor, normalizePath, parseQuerystring, parseVirtualPath } from '../utils'
import { parseManifestJsonOnce, parseMiniProgramPagesJson } from '../utils/uniapp'

/**
* ### uniapp 分包优化插件
Expand Down
272 changes: 272 additions & 0 deletions src/utils/uniapp/config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import process from 'node:process'
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import {
parseManifestJson,
parseManifestJsonOnce,
parseMiniProgramPagesJson,
} from './config'

let inputDir = ''
let originalCompileTarget: string | undefined
let originalPlatform: string | undefined

beforeEach(() => {
inputDir = fs.mkdtempSync(path.join(os.tmpdir(), 'bundle-optimizer-'))
originalCompileTarget = process.env.UNI_COMPILE_TARGET
originalPlatform = process.env.UNI_PLATFORM
})

afterEach(() => {
fs.rmSync(inputDir, { recursive: true, force: true })
restoreEnv('UNI_COMPILE_TARGET', originalCompileTarget)
restoreEnv('UNI_PLATFORM', originalPlatform)
})

describe('parseManifestJson', () => {
it('parses JSONC without applying uni conditional blocks', () => {
writeFile('manifest.json', `{
// comment
"name": "demo",
"mp-weixin": {
/* #ifdef MP-WEIXIN */
"optimization": {
"subPackages": true,
},
/* #endif */
}
}`)

expect(parseManifestJson(inputDir)).toEqual({
'name': 'demo',
'mp-weixin': {
optimization: {
subPackages: true,
},
},
})
})

it('keeps JSON duplicate key behavior when conditional comments wrap manifest keys', () => {
writeFile('manifest.json', `{
// #ifdef MP-WEIXIN
"mp-weixin": {
"optimization": {
"subPackages": false
}
},
// #endif

// #ifndef MP-WEIXIN
"mp-weixin": {
"optimization": {
"subPackages": true
}
}
// #endif
}`)

expect(parseManifestJson(inputDir)).toEqual({
'mp-weixin': {
optimization: {
subPackages: true,
},
},
})
})

it('caches manifest parsing per input directory', () => {
writeFile('manifest.json', `{
"name": "first"
}`)

expect(parseManifestJsonOnce(inputDir).name).toBe('first')

writeFile('manifest.json', `{
"name": "second"
}`)

expect(parseManifestJson(inputDir).name).toBe('second')
expect(parseManifestJsonOnce(inputDir).name).toBe('first')
})

it('returns empty config for missing manifest when compiling special targets', () => {
process.env.UNI_COMPILE_TARGET = 'ext-api'

expect(parseManifestJson(inputDir)).toEqual({})
})
})

describe('parseMiniProgramPagesJson', () => {
it('keeps subpackage structure when subpackages option is enabled', () => {
const { appJson, nvuePages, pageJsons } = parseMiniProgramPagesJson(`{
"pages": [
// #ifdef MP-WEIXIN
{
"path": "pages/weixin",
},
// #else
{
"path": "pages/other"
},
// #endif
{
"path": "pages/common"
}
],
"subpackages": [
{
"root": "sub/",
"independent": true,
"pages": [
{
"path": "index",
}
],
}
],
}`, 'mp-weixin', { subpackages: true })

expect(appJson).toEqual({
pages: ['pages/weixin', 'pages/common'],
subPackages: [
{
root: 'sub/',
pages: ['index'],
independent: true,
},
],
})
expect(pageJsons).toEqual({})
expect(nvuePages).toEqual([])
})

it('flattens subpackages into pages by default', () => {
const { appJson } = parseMiniProgramPagesJson(`{
"pages": [
{
"path": "pages/index"
}
],
"subPackages": [
{
"root": "pkg/",
"pages": [
{
"path": "index"
},
{
"path": "nested/detail"
}
]
}
]
}`, 'mp-weixin')

expect(appJson).toEqual({
pages: ['pages/index', 'pkg/index', 'pkg/nested/detail'],
})
})

it('supports compound conditions', () => {
const { appJson } = parseMiniProgramPagesJson(`{
"pages": [
// #if MP-WEIXIN && !APP
{
"path": "pages/mp"
},
// #else
{
"path": "pages/fallback"
},
// #endif
]
}`, 'mp-weixin')

expect(appJson.pages).toEqual(['pages/mp'])
})

it('supports multiple platforms separated by or operators', () => {
const { appJson } = parseMiniProgramPagesJson(`{
"pages": [
// #ifdef MP-WEIXIN || H5
{
"path": "pages/shared"
},
// #else
{
"path": "pages/fallback"
},
// #endif
]
}`, 'h5')

expect(appJson.pages).toEqual(['pages/shared'])
})

it('normalizes condition keys with underscores like the official preprocessor', () => {
const { appJson } = parseMiniProgramPagesJson(`{
"pages": [
// #ifdef MP_WEIXIN
{
"path": "pages/weixin"
},
// #else
{
"path": "pages/other"
},
// #endif
]
}`, 'mp-weixin')

expect(appJson.pages).toEqual(['pages/weixin'])
})

it('treats elif as regular inactive content like the official preprocessor', () => {
const { appJson } = parseMiniProgramPagesJson(`{
"pages": [
// #if H5
{
"path": "pages/h5"
},
// #elif MP-WEIXIN
{
"path": "pages/weixin"
},
// #else
{
"path": "pages/fallback"
},
// #endif
]
}`, 'mp-weixin')

expect(appJson.pages).toEqual(['pages/fallback'])
})

it('throws when pages are duplicated', () => {
expect(() => parseMiniProgramPagesJson(`{
"pages": [
{
"path": "pages/index"
},
{
"path": "pages/index"
}
]
}`, 'mp-weixin')).toThrow('pages.json->pages/index duplication')
})
})

function writeFile(filename: string, content: string) {
fs.writeFileSync(path.join(inputDir, filename), content)
}

function restoreEnv(name: 'UNI_COMPILE_TARGET' | 'UNI_PLATFORM', value: string | undefined) {
if (value === undefined)
delete process.env[name]
else
process.env[name] = value
}
Loading
Loading