块编辑器开发文档

@wordpress/library-export-default-webpack-plugin

💡 云策文档标注

概述

这是一个用于Webpack的插件,专门处理使用ES6模块的库的默认属性导出问题。它基于Webpack核心插件ExportPropertyMainTemplatePlugin实现,但允许指定入口点名称,将默认导出分配给库目标。该插件已针对Webpack v5弃用,建议使用output.library.export替代。

关键要点

  • 插件功能:为选定的库导出默认属性,适用于ES6模块。
  • 兼容性:仅适用于Webpack v4,需要Node.js 12.0.0或更高版本。
  • 安装方式:通过npm安装@wordpress/library-export-default-webpack-plugin。
  • 使用方法:在Webpack配置的plugins中实例化LibraryExportDefaultPlugin,传入入口点名称数组。
  • 弃用说明:Webpack v5中已弃用,推荐使用output.library.export。

代码示例

const LibraryExportDefaultPlugin = require( '@wordpress/library-export-default-webpack-plugin' );

module.exports = {
    entry: {
        boo: './packages/boo',
        foo: './packages/foo',
    },
    output: {
        filename: 'build/[name].js',
        path: __dirname,
        library: [ 'wp', '[name]' ],
        libraryTarget: 'this',
    },
    plugins: [ new LibraryExportDefaultPlugin( [ 'boo' ] ) ],
};

注意事项

  • 此插件是Gutenberg项目的一部分,作为一个独立的npm包发布。
  • 贡献者应参考Gutenberg项目的贡献指南。

📄 原文内容

DEPRECATED for webpack v5: please use output.library.export instead.

Webpack plugin for exporting default property for selected libraries which use ES6 Modules. Implementation is based on the Webpack’s core plugin ExportPropertyMainTemplatePlugin. The only difference is that this plugin allows to include all entry point names where the default export of your entry point will be assigned to the library target.

Installation

Install the module

npm install @wordpress/library-export-default-webpack-plugin --save

Note: This package requires Node.js 12.0.0 or later. It is not compatible with older versions. It works only with webpack v4.

Usage

Construct an instance of LibraryExportDefaultPlugin in your Webpack configurations plugins entry, passing an array where values correspond to the entry point name.

The following example selects boo entry point to be updated by the plugin. When compiled, the built file will ensure that default value exported for the chunk will be assigned to the global variable wp.boo. foo chunk will remain untouched.

const LibraryExportDefaultPlugin = require( '@wordpress/library-export-default-webpack-plugin' );

module.exports = {
    // ...

    entry: {
        boo: './packages/boo',
        foo: './packages/foo',
    },

    output: {
        filename: 'build/[name].js',
        path: __dirname,
        library: [ 'wp', '[name]' ],
        libraryTarget: 'this',
    },

    plugins: [ new LibraryExportDefaultPlugin( [ 'boo' ] ) ],
};

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.