Babel 6 轉換運行時:$export 不是函數 (Babel 6 transform-runtime: $export is not a function)


問題描述

Babel 6 轉換運行時:$export 不是函數 (Babel 6 transform‑runtime: $export is not a function)

我正在嘗試合併 Babel 的轉換運行時以使我的代碼與 IE9 兼容。但是自從集成它之後,代碼甚至無法在 Chrome 上運行。我在 es6.object.define‑property.js:3 上收到錯誤 Uncaught TypeError: $export is not a function。如果我的 .babelrc 中沒有“transform‑runtime”行,一切都運行良好。有什麼想法嗎?

這是我的 .babelrc:

{
  "plugins": [
    "transform‑runtime"
  ],
  "presets": [
    "es2015",
    "react"
  ]
}

還有我的 webpack.config.js:

var webpack = require('webpack');

var commonsPlugin = new webpack.optimize.CommonsChunkPlugin('common.js');

module.exports = {
  entry: {
    EventAdmin: './src/event_admin',
    EventRender: './src/event_render'
  },
  output: {
    path: '../public/js2',
    filename: '[name].js' // Template based on keys in entry above
  },
  externals: {
    // require("jquery") is external and available
    //  on the global var jQuery
    'jquery': 'jQuery'
  },
  plugins: [commonsPlugin],
  devtool: 'source‑map',
  module: {
    loaders: [
      { test: /\.css$/, loader: 'style‑loader!css‑loader' },
      {
        test: /\.js$/,
        loader: 'babel‑loader'
      },
    ]
  }
};

在此處輸入圖片描述


參考解法

方法 1:

Try adding exclude: /node_modules/ after loader: 'babel‑loader'. I had the same problem when trying to run the runtime transformer without excluding node_modules. I am not aware of the underlying problem, though.

方法 2:

Hello I have the same issue and finally found a solution that works for me. See:

loaders: [
  {
    test: /.js/,
    loader: 'babel',
    query: {
      presets: ['es2015', 'es2017'],
      plugins: [
        ['transform‑runtime', {
          helpers: false,
          polyfill: false,
          regenerator: true, }],
        'transform‑es2015‑destructuring',
        'transform‑object‑rest‑spread',
        'transform‑async‑to‑generator',
        ],
     },
  },
]

See the 'transform‑runtime' part. I hope this helps.

方法 3:

You can try replace "exclude" by "include", following the recomendations from documentation.

Try to prefer "include" when possible...

This worked for me.

{
  "test": /\.js/,
  "loader": "babel",
  "include": [path.resolve(__dirname, './src')]
}

方法 4:

At first you must installed babel‑plugin‑transform‑runtime and then use it like me:

{
  "presets": [
    "es2015",
    "react",
    "stage‑0"
  ],
  "plugins": [
    "transform‑runtime"
  ]
}

After it you must add exclude key to your babel‑loader inside webpack configuration file:

{
    test: /\.(js|jsx)$/,
    exclude: /node_modules/,
    use: [
        {
            loader: 'babel‑loader',
        }
    ]
}

Attention: please write /node_modules/ not /(node_modules\/)/ or /node_modules\//, it's weird but this way works.

方法 5:

It looks to be a problem with running core‑js files through Babel 6 because Babel 6 no longer converts require('something') to require('something').default as Babel 5 did. I even tried running it through this plugin https://www.npmjs.com/package/babel‑plugin‑add‑module‑exports but no matter what I did, it wouldn't correct the require statements properly. I ultimately just had to exclude the core‑js and various Babel related files from being processed by the babel‑loader by setting the exclude property to this:

[ /node_modules\/babel‑/m, /node_modules\/core‑js\//m, /node_modules\/regenerator‑runtime\//m ]

As a side note, I hadn't reinstalled my node_modules since converting to Babel 6 and that caused the same issue but for some other mysterious reason.

(by Ted AveryPierre WahlgrenCarlos GalarzaAndré MoraesAmerllicAResist Design)

參考文件

  1. Babel 6 transform‑runtime: $export is not a function (CC BY‑SA 2.5/3.0/4.0)

#Webpack #javascript #babeljs






相關問題

Babel 6 轉換運行時:$export 不是函數 (Babel 6 transform-runtime: $export is not a function)

Webpack 和 Sass 正確處理 background: url() 圖像,但是在與 webpack-dev-server 一起使用時找不到它 (Webpack and Sass correctly processes background: url() image but then it's not found when used with webpack-dev-server)

Babel 和 ES6 出現意外的“Uncaught TypeError: XXX is not a constructor”錯誤 (Unexpected "Uncaught TypeError: XXX is not a constructor" errors with Babel and ES6)

Webpack bundle ENOENT:沒有這樣的文件或目錄 fs.readdirSync (Webpack bundle ENOENT: no such file or directory fs.readdirSync)

有沒有辦法通過替換一些資產和組件從單個應用程序構建多個應用程序? (Is there a way to build multiple apps from single one with replacing some assets and components?)

Babel 7 不能正確轉換 index.js (Babel 7 don't convert index.js properly)

使用 Yarn 安裝的軟件包開始出現 sweetalert2 之類的錯誤 (packages installed with Yarn start to give error like sweetalert2)

找不到模塊 RecipeStyle.css (cannot find module RecipeStyle.css)

用於文件、子文件的 Webpack 加載器並將它們添加到跟踪列表 (Webpack loader for file, subfiles and add them to tracking list)

Webpack:沒有加載器來處理 SCSS 是輸入存在 (Webpack: no loader to handle the SCSS is input is present)

Webpack 在初始構建後不構建捆綁包 (Webpack not building bundle after initial build)

從其他機器訪問 webpack DevServer 子 URL 時出現 ERR_SSL_PROTOCOL_ERROR (ERR_SSL_PROTOCOL_ERROR when accessing webpack DevServer sub-URLs from a different machine)







留言討論