古腾堡插件创建结构和文件

Gutenberg Plugin Creation Structure and Files

我最近参考了几个 Gutenberg 兼容的插件。示例:https://github.com/HardeepAsrani/gutenberg-boilerplate。我无法了解以下文件的创建方式

https://github.com/HardeepAsrani/gutenberg-boilerplate/blob/master/dist/block.js

我安装了 npm,它为我提供了以下文件 Files

也不知道 webpack.config.js 文件是如何 运行。

谁能给我解释一下?

我最近使用了相同的样板文件,仅供参考,package.json 的结构不合理并且有一些旧的依赖项。我必须更改该文件中的一些代码才能使其正常工作。这是我的 package.json:

{
    "name": "gutenberg-boilerplate",
    "scripts": {
        "dev": "cross-env BABEL_ENV=default webpack --watch"
    },
    "description": "This plugin add meta boxes in gutenberg's sidebar",
    "main": "src/index.js",
    "devDependencies": {
        "@babel/core": "^7.2.2",
        "@babel/preset-env": "^7.2.0",
        "babel-core": "^6.26.3",
        "babel-loader": "^8.0.4",
        "babel-plugin-add-module-exports": "^1.0.0",
        "babel-plugin-transform-react-jsx": "^6.24.1",
        "babel-preset-env": "^1.7.0",
        "browser-sync": "^2.18.13",
        "browser-sync-webpack-plugin": "^1.2.0",
        "classnames": "^2.2.5",
        "cross-env": "^5.1.1",
        "css-loader": "^0.28.7",
        "eslint": "^4.14.0",
        "eslint-config-prettier": "^2.9.0",
        "eslint-config-wordpress": "^2.0.0",
        "eslint-plugin-prettier": "^2.7.0",
        "extract-text-webpack-plugin": "^3.0.2",
        "node-sass": "^4.7.2",
        "postcss-loader": "^2.0.9",
        "prettier": "^1.15.3",
        "raw-loader": "^0.5.1",
        "sass-loader": "^6.0.6",
        "style-loader": "^0.19.1",
        "webpack": "^3.10.0"
    },
    "version": "1.0.0",
    "license": "MIT"
}

此外,您必须使用以下代码在您的根目录中添加一个 .babelrc 文件:

{
  "presets": ["@babel/preset-env"],
  "plugins": [
    ["transform-react-jsx", {
      "pragma": "wp.element.createElement"
    }]
  ]
}

这确保了 webpack 配置实际工作。

感谢您回答您的第一个问题。文件 /dist/block.js 作为 webpack.config.js 输出的结果生成。是自动创建的。

这是我的 webpack.config.js 文件:

const path = require( 'path' );
const webpack = require( 'webpack' );
const ExtractTextPlugin = require( 'extract-text-webpack-plugin' );
// const BrowserSyncPlugin = require( 'browser-sync-webpack-plugin' );

// Set different CSS extraction for editor only and common block styles
const blockCSSPlugin = new ExtractTextPlugin( {
  filename: './dist/block.css',
} );

// Configuration for the ExtractTextPlugin.
const extractConfig = {
  use: [
    { loader: 'raw-loader' },
    {
      loader: 'postcss-loader',
      options: {
        plugins: [ require( 'autoprefixer' ) ],
      },
    },
    {
      loader: 'sass-loader',
      query: {
        outputStyle:
          'production' === process.env.NODE_ENV ? 'compressed' : 'nested',
      },
    },
  ],
};

module.exports = {
  entry: {
    './dist/block' : './src/index.js',
  },
  output: {
    path: path.resolve( __dirname ),
    filename: '[name].js',
  },
  watch: true,
  devtool: 'cheap-eval-source-map',
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader',
        },
      },
      {
        test: /style\.s?css$/,
        use: blockCSSPlugin.extract( extractConfig ),
      },
    ]
  },
  plugins: [
    blockCSSPlugin
  ],
};

为了 运行 您只需在终端中输入配置 npm run dev,因为这是 package.json 中定义的脚本:

"scripts": {
        "dev": "cross-env BABEL_ENV=default webpack --watch"
    },

我希望这能帮助您入门 ;)