ES6 + SystemJs - 为什么我必须使用.js扩展名来导入es6模块?

时间:2016-05-25 15:14:21

标签: javascript ecmascript-6 systemjs es6-module-loader traceur

为什么我必须使用.js扩展名来导入带有systemjs的es6模块?例如:

import { multiplier } from "adder.js"; // ok
import { double, square } from 'modules'; // error

var timesTwo = multiplier(2);

console.log(timesTwo(4));

错误消息:

  

获取http://local-host/projects/es6/src/modules 404(未找到)Z @   system.js:4(匿名函数)@ system.js:4(匿名函数)@   system.js:4(匿名函数)@ system.js:5(匿名函数)@   system.js:5(匿名函数)@ system.js:5(匿名函数)@   system.js:5(匿名函数)@ system.js:5(匿名函数)@   system.js:5(匿名函数)@ system.js:4 system.js:4 Uncaught(in   promise)错误:错误:XHR错误(404 Not Found)加载   http://local-host/projects/es6/src/modules(...)

这是我的带有system.js的HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>ES2015 Module Example</title>
</head>
<body>
    <script src="lib/system.js"></script>
    <script>
        System.config({
            "baseURL": "src",
            // 'plugin-babel' or 'traceur' or 'typescript'
            transpiler: 'plugin-babel',
            // or traceurOptions or typescriptOptions
            babelOptions: {

            },
            map: {
                'traceur': './lib/traceur.min.js',
                'plugin-babel': './lib/plugin-babel/plugin-babel.js',
                'systemjs-babel-build': './lib/plugin-babel/systemjs-babel-browser.js'
            }
          }
        });
        System.import("app.js");
    </script>
</body>
</html>

我错过了哪些想法?

1 个答案:

答案 0 :(得分:2)

因为系统js没有将.js附加到模块名称。一些模块加载器/捆绑器(Node,webpack等)正在执行此操作,但没有标准指定此已完成

这有一个configuration option,但它将被弃用。