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

feat: add uni-preprocess

上级 d9289278
......@@ -48,7 +48,7 @@ module.exports = {
'jest.config.js',
'rollup.config.mjs',
'scripts/**',
'packages/{uni-cli-shared,uni-cli-utils,uni-app-vite,uni-h5-vite,uni-mp-vite,uni-mp-compiler,vite-plugin-uni,uts,uni-uts-v1,uni-app-uts}/**',
'packages/{uni-cli-shared,uni-cli-utils,uni-app-vite,uni-h5-vite,uni-mp-vite,uni-mp-compiler,vite-plugin-uni,uts,uni-uts-v1,uni-app-uts,uni-preprocess}/**',
'packages/*/vite.config.ts',
],
rules: {
......
......@@ -11,6 +11,7 @@ packages/uts/dist
packages/uni-uts-v1/dist
packages/uni-app-uts/dist
packages/uni-cli-utils/dist
packages/uni-preprocess/dist
.DS_Store
......
### usage
```ts
import { preprocess } from '@dcloudio/uni-preprocess'
const { code, map } = preprocess(
`a
// #ifdef B
b
// #endif
// #ifdef C
c
// #endif
// #ifndef D
d
// #endif
e
`,
{
/**
* 可选值 js | html | auto
* 如果是处理 js、ts、uts、css、scss、less 等文件(识别的是单行或多行注释`// #ifdef `和`\/* #ifdef *\/`),可以传 js
* 如果是处理 html 等模板文件(识别的是<!-- #ifdef -->),可以传 html
* 如果是处理 vue、nvue、uvue 文件(需要同时识别上述两种),可以传 auto
**/
type: 'js',
context: { B: true },
sourceMap: {
/**
* Whether the mapping should be high-resolution.
* Hi-res mappings map every single character, meaning (for example) your devtools will always
* be able to pinpoint the exact location of function calls and so on.
* With lo-res mappings, devtools may only be able to identify the correct
* line - but they're quicker to generate and less bulky.
* You can also set `"boundary"` to generate a semi-hi-res mappings segmented per word boundary
* instead of per character, suitable for string semantics that are separated by words.
* If sourcemap locations have been specified with s.addSourceMapLocation(), they will be used here.
*/
hires: true,
/**
* The filename where you plan to write the sourcemap.
*/
file: 'test',
/**
* The filename of the file containing the original source.
*/
source: 'test.uts',
/**
* Whether to include the original content in the map's sourcesContent array.
*/
includeContent: false,
},
}
)
console.log('code:\n', code)
console.log('sourceMap:\n', map)
```
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`preprocess html #ifdef match 1`] = `
{
"code": "<template>
<view>a</view>
<view>b</view>
<view>c</view>
<template>
",
"map": SourceMap {
"file": "test",
"mappings": "AAAA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACV,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAEd,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAEd,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACd,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACV,CAAC",
"names": [],
"sources": [
"test",
],
"sourcesContent": [
"<template>
<view>a</view>
<!-- #ifdef B -->
<view>b</view>
<!-- #endif -->
<view>c</view>
<template>
",
],
"version": 3,
},
}
`;
exports[`preprocess html #ifdef not match 1`] = `
{
"code": "<template>
<view>a</view>
<view>c</view>
<template>
",
"map": SourceMap {
"file": "test",
"mappings": "AAAA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACV,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAId,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACd,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;",
"names": [],
"sources": [
"test",
],
"sourcesContent": [
"<template>
<view>a</view>
<!-- #ifdef B -->
<view>b</view>
<!-- #endif -->
<view>c</view>
<template>
",
],
"version": 3,
},
}
`;
exports[`preprocess html and js 1`] = `
{
"code": "<template>
<view>a</view>
<view>b</view>
<view>b1</view>
<view>b11</view>
<view>b3</view>
<view>c</view>
<template>
<script>
a
b
b1
b11
b3
c
</script>
<style>
a
b
b1
b11
b3
c
</style>
",
"map": SourceMap {
"file": "test",
"mappings": "AAAA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACV,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAEd,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAEd,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAEf,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAMhB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAEf,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACd,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACV,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACR,CAAC;AAED,CAAC;AAED,CAAC,CAAC;AAEF,CAAC,CAAC,CAAC;AAMH,CAAC,CAAC;AAEF,CAAC;AACD,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACT,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACP,CAAC;AAED,CAAC;AAED,CAAC,CAAC;AAEF,CAAC,CAAC,CAAC;AAMH,CAAC,CAAC;AAEF,CAAC;AACD,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;",
"names": [],
"sources": [
"test",
],
"sourcesContent": [
"<template>
<view>a</view>
<!-- #ifdef B -->
<view>b</view>
<!-- #ifdef B1 -->
<view>b1</view>
<!-- #ifdef B11 -->
<view>b11</view>
<!-- #endif -->
<!-- #endif -->
<!-- #ifdef B2 -->
<view>b2</view>
<!-- #endif -->
<view>b3</view>
<!-- #endif -->
<view>c</view>
<template>
<script>
a
// #ifdef B
b
// #ifdef B1
b1
// #ifdef B11
b11
// #endif
// #endif
// #ifdef B2
b2
// #endif
b3
// #endif
c
</script>
<style>
a
/* #ifdef B */
b
/* #ifdef B1 */
b1
/* #ifdef B11 */
b11
/* #endif */
/* #endif */
/* #ifdef B2 */
b2
/* #endif */
b3
/* #endif */
c
</style>
",
],
"version": 3,
},
}
`;
exports[`preprocess js #ifdef match 1`] = `
{
"code": "a
b
c
",
"map": SourceMap {
"file": "test",
"mappings": "AAAA,CAAC;AAED,CAAC;AAED,CAAC;",
"names": [],
"sources": [
"test",
],
"sourcesContent": [
"a
// #ifdef B
b
// #endif
c
",
],
"version": 3,
},
}
`;
exports[`preprocess js #ifdef nesting match 1`] = `
{
"code": "a
b
b1
b11
b3
c
",
"map": SourceMap {
"file": "test",
"mappings": "AAAA,CAAC;AAED,CAAC;AAED,CAAC,CAAC;AAEF,CAAC,CAAC,CAAC;AAMH,CAAC,CAAC;AAEF,CAAC;",
"names": [],
"sources": [
"test",
],
"sourcesContent": [
"a
// #ifdef B
b
// #ifdef B1
b1
// #ifdef B11
b11
// #endif
// #endif
// #ifdef B2
b2
// #endif
b3
// #endif
c
",
],
"version": 3,
},
}
`;
exports[`preprocess js #ifdef not match 1`] = `
{
"code": "a
c
",
"map": SourceMap {
"file": "test",
"mappings": "AAAA,CAAC;AAID,CAAC;",
"names": [],
"sources": [
"test",
],
"sourcesContent": [
"a
// #ifdef B
b
// #endif
c
",
],
"version": 3,
},
}
`;
exports[`preprocess js #ifndef match 1`] = `
{
"code": "a
b
c
",
"map": SourceMap {
"file": "test",
"mappings": "AAAA,CAAC;AAED,CAAC;AAED,CAAC;",
"names": [],
"sources": [
"test",
],
"sourcesContent": [
"a
// #ifndef B
b
// #endif
c
",
],
"version": 3,
},
}
`;
exports[`preprocess js #ifndef not match 1`] = `
{
"code": "a
c
",
"map": SourceMap {
"file": "test",
"mappings": "AAAA,CAAC;AAID,CAAC;",
"names": [],
"sources": [
"test",
],
"sourcesContent": [
"a
// #ifndef B
b
// #endif
c
",
],
"version": 3,
},
}
`;
exports[`preprocess js Unmatched use of #ifdef/#endif. 1`] = `"Unbalanced right delimiter found in string at position 32"`;
// import { writeFileSync } from 'node:fs'
// import { resolve } from 'node:path'
import { preprocess, SourceMapOptions } from '../src/index'
describe('preprocess', () => {
const sourceMap: SourceMapOptions = {
includeContent: true,
hires: true,
file: 'test',
source: 'test',
}
test('html and js', () => {
const res = preprocess(
`<template>
<view>a</view>
<!-- #ifdef B -->
<view>b</view>
<!-- #ifdef B1 -->
<view>b1</view>
<!-- #ifdef B11 -->
<view>b11</view>
<!-- #endif -->
<!-- #endif -->
<!-- #ifdef B2 -->
<view>b2</view>
<!-- #endif -->
<view>b3</view>
<!-- #endif -->
<view>c</view>
<template>
<script>
a
// #ifdef B
b
// #ifdef B1
b1
// #ifdef B11
b11
// #endif
// #endif
// #ifdef B2
b2
// #endif
b3
// #endif
c
</script>
<style>
a
/* #ifdef B */
b
/* #ifdef B1 */
b1
/* #ifdef B11 */
b11
/* #endif */
/* #endif */
/* #ifdef B2 */
b2
/* #endif */
b3
/* #endif */
c
</style>
`,
{
context: { B: true, B1: true, B11: true, B2: false },
sourceMap: sourceMap,
}
)
// 内联sourceMap
// writeFileSync(
// resolve(__dirname, './test.js'),
// `${res.code}
// //# sourceMappingURL=data:application/json;charset=utf-8;base64,${Buffer.from(
// JSON.stringify(res.map)
// ).toString('base64')}`
// )
expect(res).toMatchSnapshot()
})
describe('html', () => {
test(`#ifdef match`, () => {
const res = preprocess(
`<template>
<view>a</view>
<!-- #ifdef B -->
<view>b</view>
<!-- #endif -->
<view>c</view>
<template>
`,
{ type: 'html', context: { B: true }, sourceMap: sourceMap }
)
expect(res).toMatchSnapshot()
})
test(`#ifdef not match`, () => {
const res = preprocess(
`<template>
<view>a</view>
<!-- #ifdef B -->
<view>b</view>
<!-- #endif -->
<view>c</view>
<template>
`,
{ type: 'html', context: {}, sourceMap: sourceMap }
)
expect(res).toMatchSnapshot()
})
})
describe('js', () => {
test(`#ifdef match`, () => {
const res = preprocess(
`a
// #ifdef B
b
// #endif
c
`,
{ type: 'js', context: { B: true }, sourceMap: sourceMap }
)
expect(res).toMatchSnapshot()
})
test(`#ifdef not match`, () => {
const res = preprocess(
`a
// #ifdef B
b
// #endif
c
`,
{ type: 'js', context: {}, sourceMap: sourceMap }
)
expect(res).toMatchSnapshot()
})
test(`#ifndef match`, () => {
const res = preprocess(
`a
// #ifndef B
b
// #endif
c
`,
{ type: 'js', context: {}, sourceMap: sourceMap }
)
expect(res).toMatchSnapshot()
})
test(`#ifndef not match`, () => {
const res = preprocess(
`a
// #ifndef B
b
// #endif
c
`,
{ type: 'js', context: { B: true }, sourceMap: sourceMap }
)
expect(res).toMatchSnapshot()
})
test(`#ifdef nesting match`, () => {
const res = preprocess(
`a
// #ifdef B
b
// #ifdef B1
b1
// #ifdef B11
b11
// #endif
// #endif
// #ifdef B2
b2
// #endif
b3
// #endif
c
`,
{
type: 'js',
context: { B: true, B1: true, B11: true, B2: false },
sourceMap: sourceMap,
}
)
expect(res).toMatchSnapshot()
})
test(`Unmatched use of #ifdef/#endif.`, () => {
expect(() => {
preprocess(
`a
// #ifdef B
b
// #endif
// #endif
c
`,
{ type: 'js', context: { B: true }, sourceMap: sourceMap }
)
}).toThrowErrorMatchingSnapshot()
})
})
})
<template>
<view>a</view>
<view>b</view>
<view>b1</view>
<view>b11</view>
<view>b3</view>
<view>c</view>
<template>
<script>
a
b
b1
b11
b3
c
</script>
<style>
a
b
b1
b11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QUMsQ0FBQyxDQUFDLENBQUM7QUFDUCxDQUFDO0FBRUQsQ0FBQztBQUVELENBQUMsQ0FBQztBQUVGLENBQUMsQ0FBQyxDQUFDO0FBTUgsQ0FBQyxDQUFDO0FBRUYsQ0FBQztBQUNELENBQUMsQ0FBQyxDQUFDLENBQUMsQ0FBQyxDQUFDLENBQUMsQ0FBQzsifQ==
\ No newline at end of file
{
"extends": "../../api-extractor.json",
"mainEntryPointFilePath": "./dist/packages/<unscopedPackageName>/src/index.d.ts",
"dtsRollup": {
"publicTrimmedFilePath": "./dist/<unscopedPackageName>.d.ts"
}
}
\ No newline at end of file
[
{
"input": {
"src/index.ts": ["dist/index.cjs.js"]
}
},
{
"input": {
"src/index.ts": ["dist/index.es.js"]
}
}
]
{
"name": "@dcloudio/uni-preprocess",
"version": "3.0.0-alpha-4000020231226001",
"description": "@dcloudio/uni-preprocess",
"main": "dist/index.cjs.js",
"module": "dist/index.es.js",
"types": "dist/uni-preprocess.d.ts",
"files": [
"dist"
],
"sideEffects": false,
"repository": {
"type": "git",
"url": "git+https://github.com/dcloudio/uni-app.git",
"directory": "packages/uni-preprocess"
},
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/dcloudio/uni-app/issues"
},
"dependencies": {
"magic-string": "^0.30.0",
"xregexp": "^5.1.1"
}
}
export { preprocess } from './preprocess'
export type {
PreprocessOptions,
ProcessContext,
SourceMapOptions,
} from './preprocess'
import MagicString, { SourceMapOptions } from 'magic-string'
import { matchRecursive } from 'xregexp'
export type { SourceMapOptions } from 'magic-string'
export type ProcessContext = Record<string, any>
export interface PreprocessOptions {
type?: 'js' | 'html'
context: ProcessContext
sourceMap?: SourceMapOptions
}
export function preprocess(source: string, options: PreprocessOptions) {
if (!source.includes('#endif')) {
return {
code: source,
map: null,
}
}
const context = options.context || {}
const s = new MagicString(source)
function preprocessByType(type: PreprocessType) {
replaceRecursive(
type,
source,
s,
(s, startMatches, endMatches, include, recurse) => {
// I need to recurse first, so I don't catch "inner" else-directives
recurse(include.value, include.start)
const variant = startMatches.value[1]
const test = (startMatches.value[2] || '').trim()
switch (variant) {
case 'ifdef':
if (testPasses(test, context)) {
s.remove(startMatches.start, startMatches.end)
s.remove(endMatches.start, endMatches.end)
} else {
s.remove(startMatches.start, endMatches.end)
}
return
case 'ifndef':
if (!testPasses(test, context)) {
s.remove(startMatches.start, startMatches.end)
s.remove(endMatches.start, endMatches.end)
} else {
s.remove(startMatches.start, endMatches.end)
}
return
default:
throw new Error('Unknown if variant ' + variant + '.')
}
}
)
}
const type = options.type || 'auto'
if (type === 'auto' || type === 'js') {
preprocessByType(TYPES.js)
}
if (type === 'auto' || type === 'html') {
preprocessByType(TYPES.html)
}
return {
code: s.toString(),
map: options.sourceMap ? s.generateMap(options.sourceMap) : null,
}
}
type Processor = (
s: MagicString,
startMatches: MatchGroupLeft,
endMatches: MatchGroupRight,
include: MatchGroupMatch,
recurse: (content: string, startOffset: number) => void
) => void
const startPattern =
'[ \t]*(?://|/\\*)[ \t]*#(ifndef|ifdef)[ \t]+([^\n*]*)(?:\\*(?:\\*|/))?(?:[ \t]*\n+)?'
const endPattern =
'[ \t]*(?://|/\\*)[ \t]*#endif[ \t]*(?:\\*(?:\\*|/))?(?:[ \t]*\n)?'
const startPatternHTML =
'[ \t]*<!--[ \t]*#(ifndef|ifdef|if)[ \t]+(.*?)[ \t]*(?:-->|!>)(?:[ \t]*\n+)?'
const endPatternHTML =
'[ \t]*<!(?:--)?[ \t]*#endif[ \t]*(?:-->|!>)(?:[ \t]*\n)?'
interface PreprocessType {
start: {
pattern: string
regex: RegExp
}
end: {
pattern: string
regex: RegExp
}
}
const TYPES: Record<string, PreprocessType> = {
js: {
start: {
pattern: startPattern,
regex: new RegExp(startPattern, 'mi'),
},
end: {
pattern: endPattern,
regex: new RegExp(endPattern, 'mi'),
},
},
html: {
start: {
pattern: startPatternHTML,
regex: new RegExp(startPatternHTML, 'mi'),
},
end: {
pattern: endPatternHTML,
regex: new RegExp(endPatternHTML, 'mi'),
},
},
}
interface MatchGroupItem {
start: number
end: number
}
interface MatchGroupMatch extends MatchGroupItem {
value: string
}
interface MatchGroupLeft extends MatchGroupItem {
value: RegExpExecArray
}
type MatchGroupRight = MatchGroupLeft
function replaceRecursive(
type: PreprocessType,
source: string,
s: MagicString,
processor: Processor
) {
function matchReplacePass(content: string, startOffset: number = 0) {
const matches = matchRecursive(
content,
type.start.pattern,
type.end.pattern,
'gmi',
{
valueNames: ['between', 'left', 'match', 'right'],
}
)
let left: MatchGroupLeft | null = null
let match: MatchGroupMatch | null = null
let right: MatchGroupRight | null = null
matches.forEach(function ({ name, value, start, end }) {
start = start + startOffset
end = end + startOffset
switch (name) {
case 'between':
break
case 'left':
left = {
start,
end,
value: type.start.regex.exec(value)!,
}
break
case 'match':
match = {
start,
end,
value,
}
break
case 'right':
right = {
start,
end,
value: type.end.regex.exec(value)!,
}
if (left && match && right) {
processor(s, left, right, match, matchReplacePass)
left = null
match = null
right = null
}
break
}
})
}
matchReplacePass(source)
}
function getTestTemplate(test: string) {
test = test || 'true'
test = test.trim()
// force single equals replacement
// 不替换,会影响 >= 等判断
// test = test.replace(/([^=!])=([^=])/g, '$1==$2');
test = test.replace(/-/g, '_')
return new Function(
'context',
'with (context||{}){ return ( ' + test + ' ); }'
)
}
function testPasses(test: string, context: ProcessContext) {
var testFn = getTestTemplate(test)
try {
return testFn(context)
} catch (e) {}
return false
}
{
"compilerOptions": {
"outDir": "dist",
"sourceMap": false,
"declaration": true,
"declarationDir": "./dist/packages/uni-preprocess/src",
"target": "ESNext",
"module": "esnext",
"moduleResolution": "node",
"allowJs": false,
"strict": true,
"noUnusedLocals": true,
"experimentalDecorators": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"removeComments": false,
"lib": ["ESNext"],
"types": ["node"]
},
"include": ["src"]
}
......@@ -18,8 +18,8 @@ importers:
specifier: ^7.20.2
version: 7.20.2(@babel/core@7.21.3)
'@dcloudio/types':
specifier: 3.3.2
version: 3.3.2
specifier: 3.4.6
version: 3.4.6
'@dcloudio/uni-api':
specifier: 3.0.0-alpha-4000020231227002
version: link:packages/uni-api
......@@ -280,8 +280,8 @@ importers:
packages/uni-app:
dependencies:
'@dcloudio/types':
specifier: ^3.3.2
version: 3.3.2
specifier: ^3.4.6
version: 3.4.6
'@dcloudio/uni-cloud':
specifier: 3.0.0-alpha-4000020231227002
version: link:../uni-cloud
......@@ -1326,6 +1326,15 @@ importers:
specifier: ^8.4.23
version: 8.4.23
packages/uni-preprocess:
dependencies:
magic-string:
specifier: ^0.30.0
version: 0.30.5
xregexp:
specifier: ^5.1.1
version: 5.1.1
packages/uni-push:
dependencies:
'@dcloudio/uni-cli-shared':
......@@ -2723,6 +2732,14 @@ packages:
/@babel/regjsgen@0.8.0:
resolution: {integrity: sha512-x/rqGMdzj+fWZvCOYForTghzbtqPDZ5gPwaoNGHdgDfF2QA/XZbCBp4Moo5scrkAMPhB7z26XM/AaHuIJdgauA==}
/@babel/runtime-corejs3@7.23.7:
resolution: {integrity: sha512-ER55qzLREVA5YxeyQ3Qu48tgsF2ZrFjFjUS6V6wF0cikSw+goBJgB9PBRM1T6+Ah4iiM+sxmfS/Sy/jdzFfhiQ==}
engines: {node: '>=6.9.0'}
dependencies:
core-js-pure: 3.35.0
regenerator-runtime: 0.14.1
dev: false
/@babel/runtime@7.21.5:
resolution: {integrity: sha512-8jI69toZqqcsnqGGqwGS4Qb1VwLOEp4hz+CXPywcvjs60u3B4Pom/U/7rm4W8tMOYEB+E9wgD0mW1l3r8qlI9Q==}
engines: {node: '>=6.9.0'}
......@@ -2817,8 +2834,8 @@ packages:
resolution: {integrity: sha512-mrCMwcINy1IFjU9VUqLeWBkj404yWs5paLDttBcA+eqUjanuUQbBcTVPqlrGgkyzLXDcV2oDDZRSNxNpXi4kMQ==}
dev: true
/@dcloudio/types@3.3.2:
resolution: {integrity: sha512-O+7M55RXqYJMCTh3MC86IGJvDPTR+v214yUeaoz1YUIHa2gJdgObs4RRwfAwZoLMtdoZ9J5BElZZqRy5oi+UTg==}
/@dcloudio/types@3.4.6:
resolution: {integrity: sha512-6w7oR4QPPa0uPezCO9P/ljLimqIGvteJU9QKNHVksdtnQfQl84ZYMpOWj2SdMtC8+Mn8Fckz81PD5XBa9z506Q==}
/@dcloudio/uni-app-x@0.6.5:
resolution: {integrity: sha512-B66i9CzGgJux1JoaPj2QWLNWAF4kAoXOw1rOuCovjll7vH3bUcOEI3dPFU5udp56cdq2lrRzdsdJ57XVujaVUw==}
......@@ -5845,6 +5862,11 @@ packages:
dependencies:
browserslist: 4.21.5
/core-js-pure@3.35.0:
resolution: {integrity: sha512-f+eRYmkou59uh7BPcyJ8MC76DiGhspj1KMxVIcF24tzP8NA9HVa1uC7BTW2tgx7E1QVCzDzsgp7kArrzhlz8Ew==}
requiresBuild: true
dev: false
/core-js@2.6.12:
resolution: {integrity: sha512-Kb2wC0fvsWfQrgk8HU5lW6U/Lcs8+9aaYcy4ZFc6DDlo4nZ7n70dEgE5rtR0oG6ufKDUnrwfWL1mXR5ljDatrQ==}
deprecated: core-js@<3.23.3 is no longer maintained and not recommended for usage due to the number of issues. Because of the V8 engine whims, feature detection in old core-js versions could cause a slowdown up to 100x even if nothing is polyfilled. Some versions have web compatibility issues. Please, upgrade your dependencies to the actual version of core-js.
......@@ -9742,6 +9764,10 @@ packages:
/regenerator-runtime@0.13.11:
resolution: {integrity: sha512-kY1AZVr2Ra+t+piVaJ4gxaFaReZVH40AKNo7UCX6W+dEwBo/2oZJzqfuN1qLq1oL45o56cPaTXELwrTh8Fpggg==}
/regenerator-runtime@0.14.1:
resolution: {integrity: sha512-dYnhHh0nJoMfnkZs6GmmhFknAGRrLznOu5nc9ML+EJxGvrx6H7teuevqVqCuPcPK//3eDrrjQhehXVx9cnkGdw==}
dev: false
/regenerator-transform@0.15.1:
resolution: {integrity: sha512-knzmNAcuyxV+gQCufkYcvOqX/qIIfHLv0u5x79kRxuGojfYVky1f15TzZEu2Avte8QGepvUNTnLskf8E6X6Vyg==}
dependencies:
......@@ -11128,6 +11154,12 @@ packages:
resolution: {integrity: sha1-FNhGHgvdOCJL/uUDmgiY/EL80zY=}
dev: false
/xregexp@5.1.1:
resolution: {integrity: sha512-fKXeVorD+CzWvFs7VBuKTYIW63YD1e1osxwQ8caZ6o1jg6pDAbABDG54LCIq0j5cy7PjRvGIq6sef9DYPXpncg==}
dependencies:
'@babel/runtime-corejs3': 7.23.7
dev: false
/xtend@2.0.6:
resolution: {integrity: sha512-fOZg4ECOlrMl+A6Msr7EIFcON1L26mb4NY5rurSkOex/TWhazOrg6eXD/B0XkuiYcYhQDWLXzQxLMVJ7LXwokg==}
engines: {node: '>=0.4'}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册