块编辑器开发文档

@wordpress/readable-js-assets-webpack-plugin

💡 云策文档标注

概述

@wordpress/readable-js-assets-webpack-plugin 是一个 Webpack 插件,用于为每个 .min.js 资产生成可读的非压缩 JS 文件。它遵循 WordPress 约定,同时提供可读文件以支持国际化机制。

关键要点

  • 为每个 JS 入口点生成可读的 .js 文件和压缩的 .min.js 文件
  • 遵循 WordPress 的 .min.js 后缀约定,便于 Gutenberg 使用
  • 需要 Node.js 长期支持版本,不兼容旧版本
  • 不支持多个插件实例,且假设 Webpack 管道已生成 .min.js 文件
  • 作为 Gutenberg 项目的一部分,以 monorepo 形式组织

代码示例

// webpack.config.js
const ReadableJsAssetsWebpackPlugin = require( '@wordpress/readable-js-assets-webpack-plugin' );

module.exports = {
    // …snip
    plugins: [ new ReadableJsAssetsWebpackPlugin() ],
};

注意事项

  • 安装时使用 npm install @wordpress/readable-js-assets-webpack-plugin --save-dev
  • 更多 Webpack 概念请参考官方文档
  • 贡献指南请阅读 Gutenberg 项目的主要贡献者指南

📄 原文内容

Generate a readable non-minified JS file for each .min.js asset.

The end result is that for each JS entrypoint, we get a set of readable and non-minimized .js file and a minimized .min.js. This allows Gutenberg to follow the WordPress convention of adding a .min.js suffix to minimized JS files, while still providing a readable and unminized files that play well with the WordPress i18n machinery.

Consult the webpack website for additional information on webpack concepts.

Installation

Install the module

npm install @wordpress/readable-js-assets-webpack-plugin --save-dev

Note: This package requires Node.js version with long-term support status (check Active LTS or Maintenance LTS releases). It is not compatible with older versions.

Usage

Webpack

Use this plugin as you would other webpack plugins:

// webpack.config.js
const ReadableJsAssetsWebpackPlugin = require( '@wordpress/readable-js-assets-webpack-plugin' );

module.exports = {
    // …snip
    plugins: [ new ReadableJsAssetsWebpackPlugin() ],
};

Note:

  • Multiple instances of the plugin are not supported and may produced unexpected results;
  • It assumes your webpack pipeline is already generating a .min.js JS asset file for each JS entry-point.

Contributing to this package

This is an individual package that’s part of the Gutenberg project. The project is organized as a monorepo. It’s made up of multiple self-contained software packages, each with a specific purpose. The packages in this monorepo are published to npm and used by WordPress as well as other software projects.

To find out more about contributing to this package or Gutenberg as a whole, please read the project’s main contributor guide.