如何在浏览器中集成 System.js 和 babel

How can I integrate System.js and babel in browsers?

本文关键字:System js babel 集成 浏览器      更新时间:2023-09-26

请帮忙!

如何在浏览器中集成 System.js 和 babel?

我想在开发模式下在浏览器中使用纯 es6 源代码,在生产模式下将文件捆绑在一起。所以我配置系统.js和巴别塔,如下所示。

我通过 npm 安装了最新版本 systemjs (0.19.24) 和 babel-standalone (6.6.5)。

我的索引.html如下

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>System.js demo</title>
</head>
<body>
  <script src="./node_modules/systemjs/dist/system.src.js"></script>
  <script>
    System.config({
      baseURL: './',
      // or 'traceur' or 'typescript'
      transpiler: 'Babel',
      // or traceurOptions or typescriptOptions
      babelOptions: {
        presets: ['es2015']
      },
      map: {
        Babel: './node_modules/babel-standalone/babel.js'
      }
    });
    System.import('boot.js');
  </script>
</body>
</html>

我的靴子.js在下面

import { app } from './app.js'
app.run();

我的应用程序.js在下面

export app = {
  run: function(){
    console.log('app')
    alert('app')
  }
}

当我访问该页面时,控制台出现错误。

system.src.js:57 未捕获(承诺中)错误:引用错误:[BABEL] http://v:3000/boot.js:使用已删除的 Babel 5 选项:base.modules - 在plugins选项中使用相应的模块转换插件。退房 http://babeljs.io/docs/plugins/#modules(...

然后我检查了 babel 文档,并在 babelOptions 中添加了一个选项,现在是我的系统.js配置,如下所示

System.config({
      baseURL: './',
      // or 'traceur' or 'typescript'
      transpiler: 'Babel',
      // or traceurOptions or typescriptOptions
      babelOptions: {
        plugins: ["transform-es2015-modules-systemjs"],
        presets: ['es2015']
      },
      map: {
        Babel: './node_modules/babel-standalone/babel.js'
      }
    });
    System.import('boot.js');

但是发生了同样的错误。我不知道如何在浏览器中集成 System.js 和 babel 以加载 es6 源代码模块。

谁能帮我?非常感谢!!

编辑

我检查了system.src.js,并在babelTranspile函数中删除了options.modules = 'system';。 然后上面的错误消失了。 但是发生了新的错误。

如果我没有在 babelOptions 中包含plugins: ["transform-es2015-modules-systemjs"],js 文件将转换为以下内容

(function(System, SystemJS) {(function(__moduleName){'use strict';
var _app = require('./app.js');
_app.app.run();
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImJvb3QuanMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7OztBQUVBLFNBQUksR0FBSiIsImZpbGUiOiJib290LmpzIiwic291cmNlc0NvbnRlbnQiOlsiaW1wb3J0IHsgYXBwIH0gZnJvbSAnLi9hcHAuanMnXG5cbmFwcC5ydW4oKTtcbiJdfQ==
})("http://v:3000/boot.js");
})(System, System);

但是我得到了错误

require不是函数。

如果我在 babelOptions 中包含plugins: ["transform-es2015-modules-systemjs"],则文件不会转换,并且会在应用程序中引发错误.js

system.src.js:57 未捕获(在承诺中)错误:语法错误:意外的令牌导出

请帮助我如何解决此问题。等待回复。非常感谢

我解决了这个问题。我发现这是我的错。我写错误 es6 语法。我应该使用export const app = ..而不是export app = ...

现在我的系统配置在下面

System.config({
      baseURL: './',
      // or 'traceur' or 'typescript'
      transpiler: 'Babel',
      // or traceurOptions or typescriptOptions
      babelOptions: {
        plugins: ["transform-es2015-modules-systemjs"],
        presets: ['es2015', 'react']
      },
      map: {
        Babel: './node_modules/babel-standalone/babel.js'
      }
    });
System.import('boot.js');

现在,每个薄片都按预期工作。快乐编码~