错误 运行 带有 react-grid-layout 的 webpack:Plugin/Preset 文件不允许导出对象,只能导出函数
Error running webpack with react-grid-layout: Plugin/Preset files are not allowed to export objects, only functions
我正在尝试使用 react-grid-layout in a .NET core MVC application. I've set up node.js and react using this guide,效果很好。现在我正在尝试包含一个 react-grid-layout 组件,但是在 运行 webpack 时出现错误。首先它要求安装 babel 预设 es2015
,即使我已经在使用 @babel/preset-env
。安装 es2015
后,错误更改为:
Module build failed: Error: Plugin/Preset files are not allowed to export objects, only functions.
搜索其他遇到此问题的人时,我只找到了帖子,其中的解决方案是使用 babel 预设 @babel/preset-react
和 @babel/preset-env
,我已经这样做了。这是我目前的发现:
我的webpack.config.js
:
const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const extractCSS = new ExtractTextPlugin('allstyles.css')
module.exports = {
entry: {'main': './wwwroot/src/app.js'},
output: {
path: path.resolve(__dirname, 'wwwroot/dist'),
filename: 'bundle.js',
publicPath: 'dist/'
},
plugins: [
extractCSS,
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery',
Popper: ['popper.js', 'default']
}),
new webpack.optimize.UglifyJsPlugin()
],
module: {
rules: [
{
test: /\.css$/,
use: extractCSS.extract(['css-loader? minimize'])
},
{
test: /\.js?$/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-react' ,'@babel/preset-env']
}
}
}]
}
};
还有我的package.json
{
"name": "ASPNetCoreReact",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"wbp": "webpack"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"babel-preset-es2015": "^6.24.1"
},
"devDependencies": {
"@babel/core": "^7.0.0-beta.40",
"@babel/preset-env": "^7.0.0-beta.40",
"@babel/preset-react": "^7.0.0-beta.40",
"aspnet-webpack": "^2.0.3",
"babel-loader": "^8.0.0-beta.2",
"bootstrap": "^4.0.0",
"css-loader": "^0.28.10",
"extract-text-webpack-plugin": "^3.0.2",
"isomorphic-fetch": "^2.2.1",
"jquery": "^3.3.1",
"popper.js": "^1.13.0",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-grid-layout": "^0.16.5",
"style-loader": "^0.20.2",
"uglifyjs-webpack-plugin": "^1.2.2",
"webpack": "^3.11.0",
"webpack-hot-middleware": "^2.21.1"
},
"description": ""
}
我还推送了一个 example project to github 可以重现错误的地方。
知道错误可能来自哪里吗?也许我只是设置错了什么?我对 .net 核心、节点和反应完全陌生,所以这很有可能。
提前致谢。
我终于自己找到了解决方案。正如 this issue at github I downgraded babel-loader 8.0.0-beta
, @babel/core
, @babel/preset-env
and @babel/preset-react
to babel-loader 7.1.4
, babel-core
, babel-preset-env
and babel-preset-react
. Additionally I had to upgrade es2015
to env
in the .babelrc
of react-grid-layout
and react-resizeable
as suggested in this pull request.
中所建议的
我正在尝试使用 react-grid-layout in a .NET core MVC application. I've set up node.js and react using this guide,效果很好。现在我正在尝试包含一个 react-grid-layout 组件,但是在 运行 webpack 时出现错误。首先它要求安装 babel 预设 es2015
,即使我已经在使用 @babel/preset-env
。安装 es2015
后,错误更改为:
Module build failed: Error: Plugin/Preset files are not allowed to export objects, only functions.
搜索其他遇到此问题的人时,我只找到了帖子,其中的解决方案是使用 babel 预设 @babel/preset-react
和 @babel/preset-env
,我已经这样做了。这是我目前的发现:
我的webpack.config.js
:
const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const extractCSS = new ExtractTextPlugin('allstyles.css')
module.exports = {
entry: {'main': './wwwroot/src/app.js'},
output: {
path: path.resolve(__dirname, 'wwwroot/dist'),
filename: 'bundle.js',
publicPath: 'dist/'
},
plugins: [
extractCSS,
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery',
Popper: ['popper.js', 'default']
}),
new webpack.optimize.UglifyJsPlugin()
],
module: {
rules: [
{
test: /\.css$/,
use: extractCSS.extract(['css-loader? minimize'])
},
{
test: /\.js?$/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-react' ,'@babel/preset-env']
}
}
}]
}
};
还有我的package.json
{
"name": "ASPNetCoreReact",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"wbp": "webpack"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"babel-preset-es2015": "^6.24.1"
},
"devDependencies": {
"@babel/core": "^7.0.0-beta.40",
"@babel/preset-env": "^7.0.0-beta.40",
"@babel/preset-react": "^7.0.0-beta.40",
"aspnet-webpack": "^2.0.3",
"babel-loader": "^8.0.0-beta.2",
"bootstrap": "^4.0.0",
"css-loader": "^0.28.10",
"extract-text-webpack-plugin": "^3.0.2",
"isomorphic-fetch": "^2.2.1",
"jquery": "^3.3.1",
"popper.js": "^1.13.0",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-grid-layout": "^0.16.5",
"style-loader": "^0.20.2",
"uglifyjs-webpack-plugin": "^1.2.2",
"webpack": "^3.11.0",
"webpack-hot-middleware": "^2.21.1"
},
"description": ""
}
我还推送了一个 example project to github 可以重现错误的地方。
知道错误可能来自哪里吗?也许我只是设置错了什么?我对 .net 核心、节点和反应完全陌生,所以这很有可能。 提前致谢。
我终于自己找到了解决方案。正如 this issue at github I downgraded babel-loader 8.0.0-beta
, @babel/core
, @babel/preset-env
and @babel/preset-react
to babel-loader 7.1.4
, babel-core
, babel-preset-env
and babel-preset-react
. Additionally I had to upgrade es2015
to env
in the .babelrc
of react-grid-layout
and react-resizeable
as suggested in this pull request.