0

请不要将此标记为任何 Babel 6问题的重复,因为这是专门关于 Babel 7的!

我正在尝试新的 Babel 7 beta(@babel/polyfill@7.0.0-beta.32等),但在使用@babel/polyfill.

概括

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

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

系统规格

  • macOS High Sierra 开发者测试版 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)

包.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'
}
4

1 回答 1

1

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

于 2017-12-01T13:25:44.900 回答