如何在 ES6 中导入 "old" ES5 代码

How to import "old" ES5 code in ES6

我有一个 ES6 应用程序(带有 Babel 6.5Webpack)并且它成功地导入了我的模块,如下所示:

import $ from 'jquery';

我想安装 https://github.com/robflaherty/riveted/blob/master/riveted.js(Google Analytics 的插件),但是如您所见,代码中没有 module.exports = ... 之类的东西,它只定义了一个全局变量 riveted,但它有一个明显有效的 package.json 指向 riveted.js

所以做一些像

import riveted from 'riveted'
riveted.init();

抛出错误:

_riveted2.default.init is not a function

import riveted from 'riveted'
riveted.init();
import 'riveted'
riveted.init();

抛出错误:

riveted is not defined

import * as riveted from 'riveted'
riveted.init();

抛出错误:

riveted.init is not a function

如何访问 riveted 的 init() 函数?

您可以使用 webpack 导出加载器:

var riveted = require("exports?riveted!riveted")

详情见the shiming modules overview