Gutenberg插件创建结构和文件

时间:2018-12-13 10:16:01

标签: reactjs npm wordpress-gutenberg

我最近推荐了一些与Gutenberg兼容的插件。示例:https://github.com/HardeepAsrani/gutenberg-boilerplate。我无法了解如何创建以下文件

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

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

也不了解如何运行webpack.config.js文件。

任何人都可以向我解释吗?

1 个答案:

答案 0 :(得分:1)

我最近使用了相同的样板,仅供参考,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"
    },

希望这对您有所帮助;)