babel / babel-loader

📦 Babel loader for webpack
MIT License
4.83k stars 451 forks source link

SyntaxError: Unexpected token import @babel/runtime-corejs2/helpers/esm/asyncToGenerator.js:1 when migrating webpack/babel #797

Open Vadorequest opened 5 years ago

Vadorequest commented 5 years ago

I'm submitting a bug report

Webpack Version: 4.35.2

Babel Core Version: "@babel/core": "7.5.0"

Babel Loader Version: "babel-loader": "8.0.6"

Please tell us about your environment: OSX 10.x

Current behavior: Compilation fails at runtime

{
"errorMessage": "Error while loading server",
"errorType": "SyntaxError",
"stackTrace": [
"/Users/vadorequest/dev/loan-advisor/node_modules/@babel/runtime-corejs2/helpers/esm/asyncToGenerator.js:1",
"(function (exports, require, module, __filename, __dirname) { import _Promise from \"../../core-js/promise\";",
"^^^^^^",
"",
"SyntaxError: Unexpected token import",
"at createScript (vm.js:80:10)",
"at Object.runInThisContext (vm.js:139:10)",
"at Module._compile (module.js:616:28)",
"at Object.Module._extensions..js (module.js:663:10)",
"at Module.load (module.js:565:32)",
"at tryModuleLoad (module.js:505:12)",
"at Function.Module._load (module.js:497:3)",
"at Module.require (module.js:596:17)",
"at require (internal/module.js:11:18)",
"at Object.<anonymous> (/Users/vadorequest/dev/loan-advisor/.webpack/service/src/functions/server/server.js:1:3074)",
"at n (/Users/vadorequest/dev/loan-advisor/.webpack/service/src/functions/server/server.js:1:220)",
"at Module.<anonymous> (/Users/vadorequest/dev/loan-advisor/.webpack/service/src/functions/server/server.js:1:67842)",
"at Module.<anonymous> (/Users/vadorequest/dev/loan-advisor/.webpack/service/src/functions/server/server.js:1:72269)",
"at n (/Users/vadorequest/dev/loan-advisor/.webpack/service/src/functions/server/server.js:1:220)",
"at Module.<anonymous> (/Users/vadorequest/dev/loan-advisor/.webpack/service/src/functions/server/server.js:1:67459)",
"at n (/Users/vadorequest/dev/loan-advisor/.webpack/service/src/functions/server/server.js:1:220)",
"at Object.SENTRY_RELEASE.id (/Users/vadorequest/dev/loan-advisor/.webpack/service/src/functions/server/server.js:1:67121)",
"at n (/Users/vadorequest/dev/loan-advisor/.webpack/service/src/functions/server/server.js:1:220)",
"at /Users/vadorequest/dev/loan-advisor/.webpack/service/src/functions/server/server.js:1:1019",
"at Object.<anonymous> (/Users/vadorequest/dev/loan-advisor/.webpack/service/src/functions/server/server.js:1:1029)",
"at Module._compile (module.js:652:30)",
"at Object.Module._extensions..js (module.js:663:10)",
"at Module.load (module.js:565:32)",
"at tryModuleLoad (module.js:505:12)",
"at Function.Module._load (module.js:497:3)",
"at Module.require (module.js:596:17)",
"at require (internal/module.js:11:18)",
"at Object.createHandler (/Users/vadorequest/dev/loan-advisor/node_modules/serverless-offline/src/functionHelper.js:192:17)",
"at handler (/Users/vadorequest/dev/loan-advisor/node_modules/serverless-offline/src/index.js:609:40)",
"at Object.internals.handler (/Users/vadorequest/dev/loan-advisor/node_modules/hapi/lib/handler.js:101:51)",
"at request._protect.run (/Users/vadorequest/dev/loan-advisor/node_modules/hapi/lib/handler.js:32:23)",
"at module.exports.internals.Protect.internals.Protect.run (/Users/vadorequest/dev/loan-advisor/node_modules/hapi/lib/protect.js:60:12)",
"at exports.execute (/Users/vadorequest/dev/loan-advisor/node_modules/hapi/lib/handler.js:26:22)",
"at each (/Users/vadorequest/dev/loan-advisor/node_modules/hapi/lib/request.js:401:16)",
"at iterate (/Users/vadorequest/dev/loan-advisor/node_modules/items/lib/index.js:36:13)",
"at done (/Users/vadorequest/dev/loan-advisor/node_modules/items/lib/index.js:28:25)",
"at module.exports.internals.Auth.internals.Auth._authenticate (/Users/vadorequest/dev/loan-advisor/node_modules/hapi/lib/auth.js:222:16)",
"at internals.Auth.authenticate (/Users/vadorequest/dev/loan-advisor/node_modules/hapi/lib/auth.js:197:17)",
"at each (/Users/vadorequest/dev/loan-advisor/node_modules/hapi/lib/request.js:401:16)",
"at iterate (/Users/vadorequest/dev/loan-advisor/node_modules/items/lib/index.js:36:13)",
"at done (/Users/vadorequest/dev/loan-advisor/node_modules/items/lib/index.js:28:25)",
"at request.connection.states.parse (/Users/vadorequest/dev/loan-advisor/node_modules/hapi/lib/route.js:314:20)",
"at Items.serial (/Users/vadorequest/dev/loan-advisor/node_modules/statehood/lib/index.js:250:16)",
"at done (/Users/vadorequest/dev/loan-advisor/node_modules/items/lib/index.js:31:25)",
"at Items.serial (/Users/vadorequest/dev/loan-advisor/node_modules/statehood/lib/index.js:190:20)",
"at iterate (/Users/vadorequest/dev/loan-advisor/node_modules/items/lib/index.js:36:13)",
"at done (/Users/vadorequest/dev/loan-advisor/node_modules/items/lib/index.js:28:25)",
"at Items.serial (/Users/vadorequest/dev/loan-advisor/node_modules/statehood/lib/index.js:190:20)",
"at iterate (/Users/vadorequest/dev/loan-advisor/node_modules/items/lib/index.js:36:13)",
"at done (/Users/vadorequest/dev/loan-advisor/node_modules/items/lib/index.js:28:25)",
"at Items.serial (/Users/vadorequest/dev/loan-advisor/node_modules/statehood/lib/index.js:190:20)",
"at iterate (/Users/vadorequest/dev/loan-advisor/node_modules/items/lib/index.js:36:13)",
"at done (/Users/vadorequest/dev/loan-advisor/node_modules/items/lib/index.js:28:25)",
"at Items.serial (/Users/vadorequest/dev/loan-advisor/node_modules/statehood/lib/index.js:190:20)",
"at iterate (/Users/vadorequest/dev/loan-advisor/node_modules/items/lib/index.js:36:13)",
"at done (/Users/vadorequest/dev/loan-advisor/node_modules/items/lib/index.js:28:25)",
"at Items.serial (/Users/vadorequest/dev/loan-advisor/node_modules/statehood/lib/index.js:190:20)",
"at iterate (/Users/vadorequest/dev/loan-advisor/node_modules/items/lib/index.js:36:13)",
"at done (/Users/vadorequest/dev/loan-advisor/node_modules/items/lib/index.js:28:25)",
"at Items.serial (/Users/vadorequest/dev/loan-advisor/node_modules/statehood/lib/index.js:190:20)",
"at iterate (/Users/vadorequest/dev/loan-advisor/node_modules/items/lib/index.js:36:13)",
"at done (/Users/vadorequest/dev/loan-advisor/node_modules/items/lib/index.js:28:25)",
"at Items.serial (/Users/vadorequest/dev/loan-advisor/node_modules/statehood/lib/index.js:190:20)",
"at iterate (/Users/vadorequest/dev/loan-advisor/node_modules/items/lib/index.js:36:13)",
"at Object.exports.serial (/Users/vadorequest/dev/loan-advisor/node_modules/items/lib/index.js:39:9)",
"at exports.Definitions.internals.Definitions.internals.Definitions.parse (/Users/vadorequest/dev/loan-advisor/node_modules/statehood/lib/index.js:164:11)",
"at internals.state (/Users/vadorequest/dev/loan-advisor/node_modules/hapi/lib/route.js:295:31)",
"at each (/Users/vadorequest/dev/loan-advisor/node_modules/hapi/lib/request.js:401:16)",
"at iterate (/Users/vadorequest/dev/loan-advisor/node_modules/items/lib/index.js:36:13)",
"at Object.exports.serial (/Users/vadorequest/dev/loan-advisor/node_modules/items/lib/index.js:39:9)",
"at internals.Request._lifecycle (/Users/vadorequest/dev/loan-advisor/node_modules/hapi/lib/request.js:404:18)",
"at internals.Request._match (/Users/vadorequest/dev/loan-advisor/node_modules/hapi/lib/request.js:381:17)",
"at internals.Request._execute (/Users/vadorequest/dev/loan-advisor/node_modules/hapi/lib/request.js:335:21)",
"at Domain.request._protect.enter (/Users/vadorequest/dev/loan-advisor/node_modules/hapi/lib/connection.js:275:25)",
"at Domain.run (domain.js:242:14)",
"at module.exports.internals.Protect.internals.Protect.enter (/Users/vadorequest/dev/loan-advisor/node_modules/hapi/lib/protect.js:76:17)",
"at Server.<anonymous> (/Users/vadorequest/dev/loan-advisor/node_modules/hapi/lib/connection.js:273:30)",
"at emitTwo (events.js:126:13)",
"at Server.emit (events.js:214:7)",
"at parserOnIncoming (_http_server.js:619:12)"
]
}

.babelrc

{
  "plugins": [
    "@babel/transform-runtime"
  ],
  "presets": [
    [
      "next/babel",
      {
        "styled-jsx": {
          "optimizeForSpeed": true,
          "vendorPrefixes": true,
          "sourceMaps": true,
          "plugins": [
            "styled-jsx-plugin-sass"
          ]
        }
      }
    ]
  ]
}

webpack.config.js

module.exports = {
  entry: slsw.lib.entries,
  target: 'node',
  mode: process.env.NODE_ENV === 'development' ? 'development' : 'production',

  // Necessary for __dirname and __filename to work correctly when bundling with Webpack for the dev environment.
  // XXX See https://github.com/webpack/webpack/issues/1599
  node: {
    __dirname: true,
    __filename: true,
    // fs: 'empty',
    // module: "empty",
  },
  plugins,
  // Generate sourcemaps for proper error messages
  devtool: 'source-map',
  // We use webpack-node-externals to excludes all node deps. (like aws-sdk)
  // You can manually set the externals too.
  externals: [nodeExternals()],
  // Run babel on all .js files and skip those in node_modules
  module: {
    rules: [
      {
        test: /\.js$/,
        loader: 'babel-loader',
        include: __dirname,
        exclude: /node_modules/,
      },
    ],
  },
};

Dependencies:

"devDependencies": {
    "@babel/core": "7.5.0",
    "@babel/plugin-transform-runtime": "7.5.0",
    "@babel/preset-env": "7.5.0",
    "@babel/preset-react": "7.0.0",
    "babel-loader": "8.0.6",
    "babel-plugin-source-map-support": "2.0.1",
    "copy-webpack-plugin": "4.4.2",
    "serverless": "1.40.0",
    "serverless-offline": "5.6.1",
    "serverless-webpack": "5.3.1",
    "webpack": "4.35.2",
    "webpack-bundle-analyzer": "2.11.1",
    "webpack-node-externals": "1.7.2"
  },
  "dependencies": {
    "@babel/runtime": "7.5.1",
    "source-map-support": "0.5.3",
    "styled-jsx": "3.2.1",
    "styled-jsx-plugin-sass": "1.0.0",
  }

It's very hard to update babel/webpack between versions, so many changes in versions with so little understanding of what happens behind those.

Vadorequest commented 5 years ago

I was having this issue before updating versions: https://github.com/babel/babel-loader/issues/560