@ babel / polyfill(Babel 7 beta)导致语法错误

时间:2017-12-01 13:03:28

标签: javascript webpack babeljs babel-polyfill

请不要将此标记为任何Babel 6 问题的副本,因为这特别是关于Babel 7 < / p>

我正在尝试新的Babel 7测试版(@babel/polyfill@7.0.0-beta.32等)并且我在使用@babel/polyfill时遇到了麻烦。

摘要

  • 当我使用Webpack构建项目时,一切正常。
  • 当我尝试从命令行运行它时,我得到一个SyntaxError。

我可能只是忘记了一些简单的事情,但我不能为我的生活弄清楚什么。任何想法是什么问题?

系统规范

  • macOS High Sierra Developer Beta 5
  • 节点8.9.1
  • NPM 5.5.1

文件

.babelrc.js

module.exports = {
  presets: [
    ['@babel/preset-env', { targets: { node: 6 } }],
    ['@babel/preset-stage-3'],
    ['@babel/preset-flow']
  ]
}

的src / index.js

require('@babel/polyfill')
module.exports = require('./main')

的src / main.js

import 'dotenv-safe/config'

命令行

$ node --trace-warnings ./src/index.js

错误消息

import 'dotenv-safe/config'
^^^^^^

SyntaxError: Unexpected token import
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:599:28)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Module.require (module.js:579:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/Users/deniz/Code/my-project/src/index.js:3:18)

的package.json

{
  "name": "foobar",
  "private": true,
  "version": "1.0.0",
  "description": "lol",
  "main": "./dist/bundle.js",
  "module": "./src/main.js",
  "scripts": {
    "clean": "rimraf dist",
    "build": "webpack",
    "watch": "webpack -w",
    "flow": "flow",
    "flow-typed": "flow-typed",
    "dev": "node --trace-warnings ./src/index.js",
    "start": "node ./dist/bundle.js"
  },
  "devDependencies": {
    "@babel/cli": "^7.0.0-beta.32",
    "@babel/core": "^7.0.0-beta.32",
    "@babel/preset-env": "^7.0.0-beta.32",
    "@babel/preset-flow": "^7.0.0-beta.32",
    "@babel/preset-stage-3": "^7.0.0-beta.32",
    "babel-eslint": "^8.0.2",
    "babel-loader": "^8.0.0-beta.0",
    "dotenv-safe": "^4.0.4",
    "eslint": "^4.12.0",
    "eslint-config-prettier": "^2.9.0",
    "eslint-plugin-flowtype": "^2.39.1",
    "eslint-plugin-prettier": "^2.3.1",
    "flow-bin": "^0.59.0",
    "flow-typed": "^2.2.3",
    "prettier": "^1.8.2",
    "rimraf": "^2.6.2",
    "webpack": "^3.9.1"
  },
  "dependencies": {
    "@babel/polyfill": "^7.0.0-beta.32",
    "node-schedule": "^1.2.5"
  }
}

webpack.config.js

const path = require('path')

module.exports = {
  target: 'node',
  entry: ['./src/index.js'],
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        include: [path.resolve(__dirname, 'src')],
        loader: 'babel-loader'
      }
    ]
  },
  externals: [
    {
      mariasql: true,
      msnodesql: true,
      msnodesqlv8: true,
      mysql: true,
      mysql2: true,
      oracle: true,
      oracledb: true,
      'pg-query-stream': true,
      pg: true,
      sqlite3: true,
      'strong-oracle': true,
      tds: true
    }
  ],
  devtool: 'source-map'
}

1 个答案:

答案 0 :(得分:1)

尝试将入口点entry更改为:entry: ['babel-polyfill', './src/index.js']。根据文档,polyfill必须位于入口点的顶部。 https://babeljs.io/docs/usage/polyfill