通过es6教程加载模块问题与webpack

时间:2017-03-27 17:56:41

标签: webpack es6-modules

我正在关注THIS教程试图让模块加载工作,但我一直遇到导入语句不起作用的问题。 import Mortgage from './mortgage2'; - 未捕获的语法错误:意外的令牌输入。

我只有一个文件js/main.js,所以这可能是问题(是不是应该编译代码?)

我正在运行npm run webpack,然后是npm start

Webpack配置:

 var path = require('path');
 var webpack = require('webpack');

 module.exports = {
     entry: './js/main.js',
     output: {
         path: path.resolve(__dirname, 'build'),
         filename: 'main.bundle.js'
     },
     module: {
         loaders: [
             {
                 test: /\.js$/,
                 loader: 'babel-loader',
                 query: {
                     presets: ['es2015']
                 }
             }
         ]
     },
     stats: {
         colors: true
     },
     devtool: 'source-map'
 };

的package.json

{
  "name": "es6-tutorial",
  "version": "1.0.0",
  "description": "Start the tutorial [here](http://ccoenraets.github.io/es6-tutorial).",
  "main": "index.js",
  "scripts": {
    "babel": "babel --presets es2015 js/main.js -o build/main.bundle.js",
    "start": "http-server",
    "webpack": "webpack"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/ccoenraets/es6-tutorial.git"
  },
  "author": "",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/ccoenraets/es6-tutorial/issues"
  },
  "homepage": "https://github.com/ccoenraets/es6-tutorial#readme",
  "devDependencies": {
    "babel-cli": "^6.24.0",
    "babel-core": "^6.24.0",
    "babel-loader": "^6.4.1",
    "babel-preset-es2015": "^6.24.0",
    "http-server": "^0.9.0",
    "webpack": "^2.3.2"
  },
  "dependencies": {
    "babel-cli": "^6.24.0",
    "babel-core": "^6.24.0",
    "babel-loader": "^6.4.1",
    "babel-preset-es2015": "^6.24.0",
    "http-server": "^0.9.0",
    "webpack": "^2.3.2"
  }
}

控制台输出:

E:\es6-tutorial>npm run webpack

> es6-tutorial@1.0.0 webpack E:\es6-tutorial
> webpack

(node:22340) DeprecationWarning: loaderUtils.parseQuery() received a non-string
value which can be problematic, see https://github.com/webpack/loader-utils/issu
es/56
parseQuery() will be replaced with getOptions() in the next major version of loa
der-utils.
Hash: 8536c97add80f6d10d01
Version: webpack 2.3.2
Time: 980ms
             Asset     Size  Chunks             Chunk Names
    main.bundle.js  6.64 kB       0  [emitted]  main
main.bundle.js.map  7.62 kB       0  [emitted]  main
   [0] ./js/mortgage2.js 2.23 kB {0} [built]
   [1] ./js/main.js 1.64 kB {0} [built]

请注意,当我加载localhost时,最后两个文件不会出现在chrome dev工具中。

1 个答案:

答案 0 :(得分:0)

错过了一步:

在代码编辑器中打开index.html,并按如下方式修改标记,以加载build / main.bundle.js,js / main.js的编译版本: