Javascript Babel Plugin/Preset 文件不允许导出对象,只能导出函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47830273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:57:22  来源:igfitidea点击:

Babel Plugin/Preset files are not allowed to export objects, only functions

javascriptwebpackbabel

提问by Gabriel Slomka

I`m tryng to use Babel-loader on an old project, and i notice some issues regarding when babel loader is working on wrapped objects, is this its default behaviour ? I am not sure if this is a bug or something im doing wrong, i could not find much about it over google, so this is my last resource.

我正在尝试在旧项目上使用 Babel-loader,我注意到一些关于 babel loader 何时处理包装对象的问题,这是它的默认行为吗?我不确定这是一个错误还是我做错了什么,我在谷歌上找不到太多关于它的信息,所以这是我的最后一个资源。

Would i need to change something to my code to make it work ?

我是否需要对我的代码进行一些更改才能使其正常工作?

This are my current specs: Webpack: 3.19.0 babel/core: 7.0.0-beta.34 babel-loader: 8.0.0-beta.0

这是我目前的规格: Webpack: 3.19.0 babel/core: 7.0.0-beta.34 babel-loader: 8.0.0-beta.0

Please refer to my packages.json if needed:

如果需要,请参考我的packages.json:

http://paste.ubuntu.com/26187880/

http://paste.ubuntu.com/26187880/

I`m tryng to load a single file wrapped in a function:

我正在尝试加载包含在函数中的单个文件:

http://paste.ubuntu.com/26187814/

http://paste.ubuntu.com/26187814/

Resuming, something old, that is built like this:

恢复一些旧的东西,它是这样构建的:

(  window.global = { } )();

This is my webpack config:

这是我的 webpack 配置:

const webpackConfig = {
    context: __dirname,
    entry: {
        app: '../../JavaScript/Namespacing.js'
    },
    module: {
        rules: [
          {
            test: /.jsx?$/,
            exclude: /(node_modules|bower_components)/,
            use: {
              loader: 'babel-loader',
            }
          }
        ]
    },
    output: {
      path: __dirname + "/../../static/js",
      filename: "[name].js"
    },
    plugins: [
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery",
            "window.jQuery": "jquery"
        })
    ],
}

And the error i get on my file is the following:

我在我的文件中得到的错误如下:

Plugin/Preset files are not allowed to export objects, only functions.

Plugin/Preset files are not allowed to export objects, only functions.

So, am i missing something ?

那么,我错过了什么吗?

Thanks for any help.

谢谢你的帮助。

回答by Prince Francis

I got the same error with babel 7.xand and "babel-loader": "^8.0.4"

我遇到了同样的错误babel 7.x"babel-loader": "^8.0.4"

I solved the issue by changing the following dependencies in package.json.I got the solution from these link

我通过更改以下依赖项解决了这个问题,package.json.我从这些链接中得到了解决方案

"devDependencies": {
    "@babel/core": "^7.1.6",
    "@babel/preset-env": "^7.1.6",
    "@babel/preset-react": "^7.0.0",
    "babel-loader": "^8.0.4",
    "webpack": "^4.25.1",
    "webpack-cli": "^3.1.2"
}

and in .babelrc

并在 .babelrc

{
    "presets": ["@babel/env", "@babel/react"]
}

or in package.json

或在 package.json

"babel": {
    "presets": [
        "@babel/env",
        "@babel/react"
    ]
},

If you are using npmthen use the following

如果您正在使用,请npm使用以下内容

npm install @babel/core --save-dev
npm install @babel/preset-env --save-dev
npm install @babel/preset-react --save-dev
npm install babel-loader --save-dev
npm install webpack --save-dev
npm install webpack-cli --save-dev

If you are using yarn, then use the following

如果您使用的是纱线,则使用以下内容

yarn add @babel/core --dev
yarn add @babel/preset-env --dev
yarn add @babel/preset-react --dev
yarn add babel-loader --dev
yarn add webpack --dev
yarn add webpack-cli --dev

回答by Spain Train

From your package.json, I can see that you are using older plugins and presets meant for Babel v6, which will result in this error message. You need to switch to e.g., @babel/preset-env, then update your .babelrcaccordingly (if you provide .babelrc, more specific guidance can be given).

从您的 package.json 中,我可以看到您使用的是用于 Babel v6 的旧插件和预设,这将导致此错误消息。您需要切换到例如 ,@babel/preset-env然后相应地更新您的.babelrc(如果您提供.babelrc,可以给出更具体的指导)。

Here is a related ticket with some explanation - https://github.com/babel/babel-loader/issues/540

这是一张带有一些解释的相关票证 - https://github.com/babel/babel-loader/issues/540

A few more semi-related notes on what I see in package.json:

关于我在 中看到的内容的更多半相关注释package.json

The old babel-coredependency is still there. Remove this or update it to version 7.0.0-bridge.0. Similarly, the old react preset is in there, remove it.

旧的babel-core依赖仍然存在。删除它或将其更新到 version 7.0.0-bridge.0。同样,旧的反应预设在那里,删除它。

If you are using the envpreset, you do not need to use the es2015preset at all. Remove it.

如果您正在使用env预设,则根本不需要使用es2015预设。去掉它。

回答by Chiraag Mittal

this worked for me:

这对我有用:

npm uninstall --save babel-loader

npm uninstall --save babel-loader

npm uninstall --save @babel/core

npm uninstall --save @babel/core

npm install --save-dev babel-loader@^7

npm install --save-dev babel-loader@^7

and in babelrc:

在 babelrc 中:

"presets" : ["env", "react"]

“预设”:[“环境”,“反应”]