使用 @wordpress/create-block 生成的块插件,如何修改默认 Webpack 配置以支持 TypeScript?

解读

国内一线团队已普遍把 TypeScript 引入古腾堡开发,既能减少运行时错误,又能借助 IDE 提示提升协作效率。@wordpress/create-block 默认只给出 JS 脚手架,Webpack 配置被 @wordpress/scripts 深度封装在 node_modules 里,直接改源码会被下次 npm install 覆盖。因此面试考察的核心是:在不“eject”源码、不破坏官方升级路径的前提下,用 WordPress 生态允许的“钩子”思路,把 TS 编译链路无缝注入现有构建流程,并保证 eslint、jest、npm run build、npm start 等命令全部可用。

知识点

  1. @wordpress/scripts 的 webpack 配置加载顺序:
    先读 node_modules/@wordpress/scripts/config/webpack.config.js,再检测项目根目录是否存在 webpack.config.js,如有则自动合并(webpack-merge)。
  2. 官方合并策略是“浅合并”,对 module.rules 采用追加方式,因此只需在自定义 webpack.config.js 里声明 ts 规则即可,无需重写整份配置。
  3. 必须安装 peer 依赖:typescript、ts-loader(或 @babel/preset-typescript)、@types/react、@types/wordpress__block-editor 等。
  4. 为了让 eslint 不报错,需在 .eslintrc 里 extends @wordpress/eslint-plugin/recommended-typescript,并创建 tsconfig.json 指定 jsx: react-jsx、target: esnext、module: esnext。
  5. 若使用 npm start(即 webpack-dev-server),需把 ts-loader 的 transpileOnly 设为 true 并用 fork-ts-checker-webpack-plugin 把类型检查剥离到独立进程,保证热更新速度。
  6. 国内镜像源(如淘宝 npmmirror)需同步配置 registry,否则 @types 包常拉不下来,导致 CI 失败。
  7. 交付物必须保留 *.js 入口文件,因为 WordPress 插件目录审核只认 JS,TS 源文件放 src/,编译后输出到 build/,与官方约定保持一致。

答案

步骤一:安装依赖
npm i -D typescript ts-loader @types/react @types/wordpress__blocks @types/wordpress__block-editor @types/wordpress__components fork-ts-checker-webpack-plugin

步骤二:在项目根新增 tsconfig.json
{ "compilerOptions": { "target": "ES2020", "module": "ESNext", "jsx": "react-jsx", "moduleResolution": "node", "strict": true, "esModuleInterop": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true, "resolveJsonModule": true, "allowJs": true, "outDir": "build" }, "include": ["src/**/*"], "exclude": ["node_modules", "build"] }

步骤三:创建 webpack.config.js
const defaultConfig = require('@wordpress/scripts/config/webpack.config'); const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');

module.exports = { ...defaultConfig, module: { ...defaultConfig.module, rules: [ ...defaultConfig.module.rules, { test: /.tsx?$/, use: [ { loader: 'ts-loader', options: { transpileOnly: true, // 热更新提速 }, }, ], exclude: /node_modules/, }, ], }, resolve: { ...defaultConfig.resolve, extensions: [...defaultConfig.resolve.extensions, '.ts', '.tsx'], }, plugins: [ ...defaultConfig.plugins, new ForkTsCheckerWebpackPlugin({ typescript: { configFile: './tsconfig.json', }, }), ], };

步骤四:把 src/index.js 重命名为 src/index.tsx,并在文件头部加类型声明
import { registerBlockType } from '@wordpress/blocks'; import { BlockConfiguration } from '@wordpress/blocks';

const blockConfig: BlockConfiguration<any> = { … }; registerBlockType('create-block/guten-ts', blockConfig);

步骤五:验证
npm start // 热更新正常
npm run build // 生成 build/index.js,无类型错误
npm run lint:js // 自动识别 ts/tsx,零报错

至此,TypeScript 链路完全融入 @wordpress/create-block 脚手架,后续官方升级 @wordpress/scripts 时,只需 npm update 即可,无需再次改造。

拓展思考

  1. 大型项目往往把“公共类型”抽到 packages/types,用 monorepo + workspace 统一版本,如何在 webpack.config.js 里通过 alias 把 @types 指向 packages/types,并保证 VSCode 也能识别?
  2. 国内客户常要求“首屏 1s 内”,在 TS 编译阶段开启 babel-plugin-import 按需加载 @wordpress/components,可再降 20 kB,如何与 ts-loader 链式调用?
  3. GitHub Actions 或 Gitee Go 做 CI 时,如何把 tsc --noEmit 与 Jest 单元测试并行跑,且在上传至官方插件库 SVN 前自动剔除 ts 源文件?
  4. 当团队同时维护 React 17 与 React 18 双版本,如何在同一插件里利用 TS 条件类型动态声明 children,避免 @types/react 冲突?
  5. 未来 WordPress 核心若原生支持 import maps,是否还有必要把 TS 编译成 ES5?如何设计一份向后兼容的构建矩阵,既满足老浏览器又利用新特性?