diff --git a/packages/build-plugin-pwc/src/utils/error.ts b/packages/build-plugin-pwc/src/utils/error.ts index bd98843..cdfd3ec 100644 --- a/packages/build-plugin-pwc/src/utils/error.ts +++ b/packages/build-plugin-pwc/src/utils/error.ts @@ -1,13 +1,32 @@ import type { RollupError } from 'rollup'; +import type { CompilerError } from '@pwc/compiler'; export function createRollupError( id: string, - error: SyntaxError, + error: SyntaxError | CompilerError, ): RollupError { - return { - id, - plugin: 'pwc', - message: error.message, - parserError: error, - }; + if ('code' in error) { + return { + id, + plugin: 'pwc', + pluginCode: String(error.code), + message: error.message, + frame: error.loc!.source, + parserError: error, + loc: error.loc + ? { + file: id, + line: error.loc.start.line, + column: error.loc.start.column, + } + : undefined, + }; + } else { + return { + id, + plugin: 'pwc', + message: error.message, + parserError: error, + }; + } } diff --git a/packages/pwc-compiler/package.json b/packages/pwc-compiler/package.json index 59b594a..bd3f8ae 100644 --- a/packages/pwc-compiler/package.json +++ b/packages/pwc-compiler/package.json @@ -41,14 +41,13 @@ "npm": ">=3.0.0" }, "dependencies": { - "@babel/core": "^7.17.9", "@babel/generator": "^7.17.7", "@babel/parser": "^7.17.9", "@babel/plugin-proposal-decorators": "^7.17.2", "@babel/traverse": "^7.17.3", "@babel/types": "^7.17.0", "@swc/helpers": "^0.3.8", - "parse5": "^6.0.1", + "parse5": "^7.0.0", "postcss": "^8.4.12", "rfdc": "^1.3.0", "source-map": "^0.7.3" diff --git a/packages/pwc-compiler/src/compileScript.ts b/packages/pwc-compiler/src/compileScript.ts index d4a31c5..57eccb7 100644 --- a/packages/pwc-compiler/src/compileScript.ts +++ b/packages/pwc-compiler/src/compileScript.ts @@ -1,4 +1,5 @@ import generate from '@babel/generator'; +import { join } from 'path'; import rfdc from 'rfdc'; import type { SFCDescriptor, SFCScriptBlock } from './parse.js'; import { compileTemplate } from './compileTemplate.js'; @@ -8,9 +9,11 @@ const deepClone = rfdc(); export interface SFCScriptCompileResult extends SFCScriptBlock { filename: string; + sourceRoot: string; } export function compileScript(descriptor: SFCDescriptor): SFCScriptCompileResult { + const rootContext = process.cwd(); const { script, filename } = descriptor; const ast = deepClone(descriptor.script.ast); @@ -26,8 +29,11 @@ export function compileScript(descriptor: SFCDescriptor): SFCScriptCompileResult transformScript(ast, { templateString: null }); } + const sourceRoot = join(rootContext, 'src'); const { code, map } = generate.default(ast, { - sourceMaps: false, + sourceMaps: true, + sourceRoot, + sourceFileName: filename, decoratorsBeforeExport: true, }); @@ -35,6 +41,7 @@ export function compileScript(descriptor: SFCDescriptor): SFCScriptCompileResult return { ...script, filename, + sourceRoot, content: code, map, }; diff --git a/packages/pwc-compiler/src/errors.ts b/packages/pwc-compiler/src/errors.ts new file mode 100644 index 0000000..7366edd --- /dev/null +++ b/packages/pwc-compiler/src/errors.ts @@ -0,0 +1,46 @@ +import type { SourceLocation } from './parse.js'; + +export interface CompilerError extends SyntaxError { + code: number | string; + loc?: SourceLocation; +} + +export function createCompilerError( + code: number | string, + loc?: SourceLocation, +): CompilerError { + const msg = typeof code === 'string' ? code : errorMessages[code]; + const error = new SyntaxError(String(msg)) as CompilerError; + error.code = code; + error.loc = loc; + return error; +} + +export const enum ErrorCodes { + // parse errors + MISSING_SCRIPT_TAG, + DUPLICATE_SCRIPT_TAG, + DUPLICATE_TEMPLATE_TAG, + DUPLICATE_STYLE_TAG, + // script errors + MISSING_EXPORT_DEFAULT, + MISSING_EXPORT_CLASS, + MISSING_EXPORT_CLASS_EXTENDED_FROM_HTMLELEMENT, + // transform errors + X_V_IF_NO_EXPRESSION, +} + +export const errorMessages: Record = { + // parse errors + [ErrorCodes.MISSING_SCRIPT_TAG]: 'PWC must contain one