Babel Plugin/Preset 文件不允许导出对象,只能导出函数 [英] Babel Plugin/Preset files are not allowed to export objects, only functions

查看:40
本文介绍了Babel Plugin/Preset 文件不允许导出对象,只能导出函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

这是我目前的规格:网络包:3.19.0巴别塔/核心:7.0.0-beta.34babel-loader: 8.0.0-beta.0

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

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

Please refer to my packages.json if needed:

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

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

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

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

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

Resuming, something old, that is built like this:

(  window.global = { } )();

这是我的 webpack 配置:

This is my webpack config:

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:

插件/预设文件不允许导出对象,只能导出函数.

所以,我错过了什么吗?

So, am i missing something ?

感谢您的帮助.

推荐答案

我在 babel 7.x"babel-loader": "^8.0.4"

我通过更改 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"
}

.babelrc

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

或在 package.json

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

如果您使用的是 npm,请使用以下内容

If you are using npm then use the following

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

这篇关于Babel Plugin/Preset 文件不允许导出对象,只能导出函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆