Skip to content

Mpx Compiler Options

Soon Wang edited this page Jul 8, 2025 · 8 revisions

Mpx VS Code 插件支持读取 tsconfig.json 文件中的 mpxCompilerOptions 编译配置选项来实现更高级地自定义功能。该配置选项只会用于插件使用,不会影响项目实际的编译时和运行时。

tsconfig.json 示例:

{
  "compilerOptions": {
    "lib": ["esnext", "dom"],
    "strict": true,
    "noUncheckedIndexedAccess": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "skipLibCheck": true,
    "allowJs": true,
    "declaration": true,
    "noEmit": true,
    "jsx": "preserve",
    "baseUrl": ".",
  },
+ "mpxCompilerOptions": {
+   "optionsComponentCtor": ["createComponent", "createComponentNew"]
+   "optionsPageCtor": ["createPage", "createPageNew"]
+   // ...
+ },
  "include": ["**/*"]
}

optionsComponentCtor

  • Type : string[]
  • Default : ["createComponent"]

选项式语法组件中,用于创建组件的方法名,默认是用 @mpxjs/core 导入的 createComponent,如果有使用自定义的创建方法,比如自定义的 createComponentNew,可以通过该选项配置。注意需要和 createComponent 遵循相同的类型定义。

optionsPageCtor

  • Type : string[]
  • Default : ["createPage"]

选项式语法组件中,用于创建页面的方法名,默认是用 @mpxjs/core 导入的 createPage,如果有使用自定义的创建方法,比如自定义的 createPageNew,可以通过该选项配置。注意需要和 createPage 遵循相同的类型定义。

skipTemplateCheck

  • Type : boolean
  • Default : false

true 表示跳过 <templaye> 模块部分的类型检查、定义跳转等等 TS 功能,默认 false 不跳过。

templateGlobalDefs

  • Type : string[]
  • Default : []

为在 <template> 模板中使用的全局声明常量提供类型支持,比如编译注入的全局常量 __version__,并且在诸如 global.d.ts 全局声明文件中声明了类型,那么在组件 <templaye> 模板中使用该变量时能够正确识别声明类型。

Note

  • 如果未配置此选项,在组合式组件 <templaye> 模块中使用全局声明常量,可能会报错提示找不到对应属性变量。
  • 内置支持 Mpx 编译相关的 __mpx_mode__、__mpx_env__ 全局常量,不用额外配置。

Clone this wiki locally