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

wip(uvue): web端增加pages.json校验

上级 b6fe6550
......@@ -2,6 +2,7 @@ import path from 'path'
import fs from 'fs-extra'
import {
PAGES_JSON_UTS,
createRollupError,
normalizeUniAppXAppPagesJson,
parseArguments,
} from '@dcloudio/uni-cli-shared'
......@@ -10,7 +11,6 @@ import type { Plugin } from 'vite'
import { ENTRY_FILENAME, genClassName, stringifyMap } from './utils'
import { isPages } from '../utils'
import { createRollupError } from './uvue/error'
export function uniAppPagesPlugin(): Plugin {
const pagesJsonPath = path.resolve(process.env.UNI_INPUT_DIR, 'pages.json')
......
import type { RollupError } from 'rollup'
import type { CompilerError } from './compiler/errors'
import { generateCodeFrame, locToStartAndEnd } from '@dcloudio/uni-cli-shared'
export function createRollupError(
plugin: string,
id: string,
error: CompilerError | SyntaxError,
source?: string
): RollupError {
const { message, name, stack } = error
const rollupError: RollupError = {
id,
plugin,
message,
name,
stack,
}
if ('code' in error && error.loc) {
rollupError.loc = {
file: id,
line: error.loc.start.line,
column: error.loc.start.column,
}
if (source && source.length > 0) {
if ('offsetStart' in error && 'offsetEnd' in error) {
rollupError.frame = generateCodeFrame(
source,
error.offsetStart as number,
error.offsetEnd as number
).replace(/\t/g, ' ')
} else {
const { start, end } = locToStartAndEnd(source, error.loc)
rollupError.frame = generateCodeFrame(source, start, end).replace(
/\t/g,
' '
)
}
}
}
return rollupError
}
......@@ -13,6 +13,7 @@ import type { SourceMapInput, TransformPluginContext } from 'rollup'
import { isString } from '@vue/shared'
import {
AutoImportOptions,
createRollupError,
matchEasycom,
normalizePath,
parseUTSComponent,
......@@ -36,7 +37,6 @@ import {
getDescriptor,
getSrcDescriptor,
} from './descriptorCache'
import { createRollupError } from './error'
import {
addAutoImports,
addExtApiComponents,
......
import type { Plugin } from 'vite'
import type { PluginContext, RollupError } from 'rollup'
import type { PluginContext } from 'rollup'
import fs from 'fs-extra'
import { CompilerError, SFCBlock, SFCDescriptor } from '@vue/compiler-sfc'
import { SFCBlock, SFCDescriptor } from '@vue/compiler-sfc'
import {
createRollupError,
hash,
parseVueRequest,
preNVueHtml,
......@@ -104,7 +105,9 @@ function createAppDescriptor(
descriptor.id = id
if (errors.length) {
errors.forEach((error: any) =>
pluginContext.error(createRollupError(filename, error))
pluginContext.error(
createRollupError('uni:app-nvue-app-style', filename, error)
)
)
}
appDescriptor = descriptor
......@@ -112,30 +115,6 @@ function createAppDescriptor(
return appDescriptor
}
export function createRollupError(
id: string,
error: CompilerError | SyntaxError
): RollupError {
const { message, name, stack } = error
const rollupError: RollupError = {
id,
plugin: 'vue',
message,
name,
stack,
}
if ('code' in error && error.loc) {
rollupError.loc = {
file: id,
line: error.loc.start.line,
column: error.loc.start.column,
}
}
return rollupError
}
// these are built-in query parameters so should be ignored
// if the user happen to add them as attrs
const ignoreList = ['id', 'index', 'src', 'type', 'lang', 'module']
......
......@@ -7,4 +7,5 @@ export * from './theme'
export {
normalizeUniAppXAppPagesJson,
normalizeUniAppXAppConfig,
checkPagesJson,
} from './uni-x'
import type { ConfigEnv, ResolvedConfig, UserConfig } from 'vite'
import { generateCodeFrame, locToStartAndEnd } from '../plugins/vitejs/utils'
import { RollupError } from 'rollup'
import { CompilerError } from '@vue/compiler-sfc'
export function withSourcemap(config: ResolvedConfig) {
if (config.command === 'serve') {
......@@ -23,3 +26,43 @@ export function isSsr(
}
return false
}
export function createRollupError(
plugin: string,
id: string,
error: CompilerError | SyntaxError,
source?: string
): RollupError {
const { message, name, stack } = error
const rollupError: RollupError = {
id,
plugin,
message,
name,
stack,
}
if ('code' in error && error.loc) {
rollupError.loc = {
file: id,
line: error.loc.start.line,
column: error.loc.start.column,
}
if (source && source.length > 0) {
if ('offsetStart' in error && 'offsetEnd' in error) {
rollupError.frame = generateCodeFrame(
source,
error.offsetStart as number,
error.offsetEnd as number
).replace(/\t/g, ' ')
} else {
const { start, end } = locToStartAndEnd(source, error.loc)
rollupError.frame = generateCodeFrame(source, start, end).replace(
/\t/g,
' '
)
}
}
}
return rollupError
}
......@@ -12,6 +12,8 @@ import {
isEnableTreeShaking,
parseManifestJsonOnce,
MANIFEST_JSON_JS,
checkPagesJson,
createRollupError,
} from '@dcloudio/uni-cli-shared'
import { isSSR } from '../utils'
......@@ -24,6 +26,25 @@ export function uniPagesJsonPlugin(): Plugin {
if (opts.filter(id)) {
const { resolvedConfig } = opts
const ssr = isSSR(opt)
if (process.env.UNI_APP_X === 'true') {
// 调整换行符,确保 parseTree 的loc正确
code = code.replace(/\r\n/g, '\n')
try {
checkPagesJson(code, process.env.UNI_INPUT_DIR)
} catch (err: any) {
if (err.loc) {
const error = createRollupError(
'uni:app-pages',
'pages.json',
err,
code
)
this.error(error)
} else {
throw err
}
}
}
return {
code:
registerGlobalCode(resolvedConfig, ssr) +
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册