1

尝试storybook使用Webpack@4.17.2and启动时出现此错误mini-css-extract-plugin。我正在使用mini-css-extract-plugin而不是extract-text-webpack-plugin因为它不支持Webpack > 4.x.x

任何想法如何在不降级到 webpack < 4.xx 错误的情况下解决此问题:

    ERROR in ./src/styles/styles.less
Module build failed: TypeError: Cannot read property 'thisCompilation' of undefined
    at NodeTemplatePlugin.apply (C:\Users\sbr\Documents\Github\my-app\node_modules\webpack\lib\node\NodeTemplatePlugin.js:19:18)
    at Object.pitch (C:\Users\sbr\Documents\Github\my-app\node_modules\mini-css-extract-plugin\dist\loader.js:79:51)

包json内容:

{
  "name": "my-app",
  "version": "1.0.0",
  "description": "my app",
  "main": "app.js",
  "repository": "https://github.com",
  "author": "sbr",
  "license": "MIT",
  "private": false,
  "scripts": {
    "start": "webpack-dev-server",
    "build": "webpack -p",
    "storybook": "start-storybook -p 6006",
    "build-storybook": "build-storybook"
  },
  "dependencies": {
    "moment": "^2.22.2",
    "react": "^16.4.2",
    "react-dom": "^16.4.2"
  },
  "devDependencies": {
    "@storybook/addon-actions": "^3.4.10",
    "@storybook/addon-links": "^3.4.10",
    "@storybook/addons": "^3.4.10",
    "@storybook/react": "^3.4.10",
    "babel-core": "^6.26.3",
    "babel-loader": "7",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "babel-runtime": "^6.26.0",
    "css-loader": "^1.0.0",
    "html-webpack-plugin": "^3.2.0",
    "less": "^3.8.1",
    "less-loader": "^4.1.0",
    "mini-css-extract-plugin": "^0.4.2",
    "path": "^0.12.7",
    "style-loader": "^0.23.0",
    "webpack": "^4.17.2",
    "webpack-cli": "^3.1.0",
    "webpack-dev-server": "^3.1.8"
  }
}

Storybook webpack.config 内容:

const ExtractTextPlugin = require('mini-css-extract-plugin');
const extractTextPlugin = new ExtractTextPlugin('src/styles/styles.less');

module.exports = {
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"]
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: "babel-loader"
      }, {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        use: "babel-loader"
      },
      {
        test: /\.less$/,
        use: [ExtractTextPlugin.loader, 'css-loader','less-loader']
      }
    ]
  }
};
4

1 回答 1

1

我认为 SamVK 在评论中触及了简短的答案。正如 Storybook 问题所述,要使 Storybook Webpack 4 兼容,还有很多工作要做。有一些解决方法,但您的里程可能会有所不同。

截至 2018 年 12 月,您最好的选择是确保您使用的是最新版本的 Webpack 4.x、St​​orybook 4.x,并确保您包含正确版本的 babel 核心和 babel 加载器,基于您拥有的其他内容包括。

https://github.com/storybooks/storybook/issues/3044

https://github.com/storybooks/storybook/pull/3148

于 2018-12-05T19:35:43.683 回答