提交 b464a2f6 编写于 作者: fxy060608's avatar fxy060608

wip(app): confusion

上级 260f39cb
......@@ -9,7 +9,7 @@ import {
UNI_EASYCOM_EXCLUDE,
} from '@dcloudio/uni-cli-shared'
import { plugins as nvuePlugins } from '@dcloudio/uni-cli-nvue'
import { UniAppPlugin } from './plugin'
import { uniAppPlugin } from './plugin'
import { uniTemplatePlugin } from './plugins/template'
import { uniMainJsPlugin } from './plugins/mainJs'
import { uniManifestJsonPlugin } from './plugins/manifestJson'
......@@ -53,7 +53,7 @@ const plugins = [
uniRenderjsPlugin(),
uniTemplatePlugin(),
uniStatsPlugin(),
UniAppPlugin,
uniAppPlugin(),
]
const uniCssScopedPluginOptions = initUniCssScopedPluginOptions()
......
import fs from 'fs'
import path from 'path'
import { UserConfig } from 'vite'
import { ConfigEnv, UserConfig } from 'vite'
import {
emptyDir,
normalizePath,
isConfusionFile,
hasConfusionFile,
resolveMainPathOnce,
} from '@dcloudio/uni-cli-shared'
export function buildOptions(): UserConfig['build'] {
export function buildOptions(configEnv: ConfigEnv): UserConfig['build'] {
const inputDir = process.env.UNI_INPUT_DIR
const outputDir = process.env.UNI_OUTPUT_DIR
// 开始编译时,清空输出目录
......@@ -41,7 +43,16 @@ export function buildOptions(): UserConfig['build'] {
}
return 'uni-app:///' + sourcePath
},
manualChunks: {},
manualChunks(id) {
if (hasConfusionFile()) {
if (
configEnv.mode === 'production' &&
isConfusionFile(path.relative(inputDir, id))
) {
return 'app-confusion.js'
}
}
},
chunkFileNames(chunk) {
if (chunk.isDynamicEntry && chunk.facadeModuleId) {
const filepath = path.relative(
......
......@@ -4,18 +4,15 @@ import { uniOptions } from './uni'
import { buildOptions } from './build'
import { configResolved } from './configResolved'
export const UniAppPlugin: UniVitePlugin = {
name: 'vite:uni-app',
uni: uniOptions(),
config() {
return {
build: buildOptions(),
}
},
configResolved,
// resolveId(id) {
// if (id === 'vue') {
// return resolveBuiltIn('@dcloudio/uni-app-vue')
// }
// },
export function uniAppPlugin(): UniVitePlugin {
return {
name: 'vite:uni-app',
uni: uniOptions(),
config(_, env) {
return {
build: buildOptions(env),
}
},
configResolved,
}
}
......@@ -11,6 +11,7 @@ import {
export function uniManifestJsonPlugin(): Plugin {
return defineUniManifestJsonPlugin((opts) => {
const inputDir = process.env.UNI_INPUT_DIR
return {
name: 'vite:uni-app-manifest-json',
enforce: 'pre',
......@@ -18,22 +19,16 @@ export function uniManifestJsonPlugin(): Plugin {
if (!opts.filter(id)) {
return
}
this.addWatchFile(
path.resolve(process.env.UNI_INPUT_DIR, 'manifest.json')
)
getLocaleFiles(
path.resolve(process.env.UNI_INPUT_DIR, 'locale')
).forEach((filepath) => {
this.addWatchFile(path.resolve(inputDir, 'manifest.json'))
getLocaleFiles(path.resolve(inputDir, 'locale')).forEach((filepath) => {
this.addWatchFile(filepath)
})
const manifestJson = normalizeAppManifestJson(
parseJson(code),
parsePagesJsonOnce(
process.env.UNI_INPUT_DIR,
process.env.UNI_PLATFORM
)
parsePagesJsonOnce(inputDir, process.env.UNI_PLATFORM)
)
// 生成一个空的app-config.js,兼容基座已有规范
// 生成一个空的 app-config.js,兼容基座已有规范
this.emitFile({
fileName: `app-config.js`,
type: 'asset',
......
import path from 'path'
import { normalizePath } from '../../../utils'
import { EXTNAME_JS_RE } from '../../../constants'
function isJsFile(filename: string) {
return EXTNAME_JS_RE.test(filename)
}
function isStaticJsFile(filename: string) {
return (
filename.indexOf('hybrid/html') === 0 ||
filename.indexOf('static/') === 0 ||
filename.indexOf('/static/') !== -1
) // subpackages, uni_modules 中的 static 目录
}
const dynamicConfusionJsFiles: string[] = []
export function isConfusionFile(filename: string) {
return dynamicConfusionJsFiles.includes(normalizePath(filename))
}
export function hasConfusionFile() {
return !!dynamicConfusionJsFiles.length
}
export function initConfusion(manifestJson: Record<string, any>) {
dynamicConfusionJsFiles.length = 0
if (!manifestJson.plus.confusion?.resources) {
return
}
......@@ -7,26 +33,27 @@ export function initConfusion(manifestJson: Record<string, any>) {
string,
string
>
manifestJson.plus.confusion.resources = Object.keys(resources).reduce(
(res, name) => {
const extname = path.extname(name)
if (extname === '.nvue') {
res[name.replace('.nvue', '.js')] = resources[name]
} else if (extname === '.js') {
// 仅指定目录的js允许加密
if (
name.indexOf('hybrid/html') === 0 ||
name.indexOf('static/') === 0 ||
name.indexOf('/static/') !== -1 // subpackages, uni_modules 中的 static 目录
) {
res[name] = resources[name]
}
manifestJson.plus.confusion.resources = Object.keys(resources).reduce<
Record<string, string>
>((res, name) => {
const extname = path.extname(name)
if (extname === '.nvue') {
res[name.replace('.nvue', '.js')] = resources[name]
} else if (isJsFile(name)) {
// 静态 js 加密
if (isStaticJsFile(name)) {
res[name] = resources[name]
} else {
throw new Error(`原生混淆仅支持 nvue 页面,错误的页面路径:${name}`)
// 非静态 js 将被合并进 app-confusion.js
dynamicConfusionJsFiles.push(name)
}
// TODO 旧编译器会检查要加密的 nvue 页面(包括subnvue)是否被使用?后续有时间再考虑支持吧,意义不太大
return res
},
{} as Record<string, string>
)
} else {
throw new Error(`原生混淆仅支持 nvue 页面,错误的页面路径:${name}`)
}
// TODO 旧编译器会检查要加密的 nvue 页面(包括subnvue)是否被使用?后续有时间再考虑支持吧,意义不太大
return res
}, {})
if (dynamicConfusionJsFiles.length) {
manifestJson.plus.confusion.resources['app-confusion.js'] = {}
}
}
......@@ -38,7 +38,7 @@ export function normalizeAppManifestJson(
}
export * from './env'
export { isConfusionFile, hasConfusionFile } from './confusion'
export {
getNVueCompiler,
getNVueStyleCompiler,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册