问题描述
我有一个 ES6
应用程序(带有 Babel 6.5
和 Webpack
),它成功地导入了我的模块,如下所示:
I have an ES6
application (with Babel 6.5
and Webpack
) and it successfully imports my modules like this:
import $ from 'jquery';
我想安装 https://github.com/robflaherty/riveted/blob/master/riveted.js(Google Analytics 的一个插件),但正如你所看到的,代码没有像 module.exports = ...
这样的东西,它只有定义了一个全局变量 riveted
,但它有一个明显有效的 package.json
指向 riveted.js
.
I wanted to install https://github.com/robflaherty/riveted/blob/master/riveted.js (a plugin for Google Analytics), but as you can see, the code doesn't have something like module.exports = ...
, it only defines a global variable riveted
, but it has an apparently valid package.json
pointing to riveted.js
.
所以做类似的事情
import riveted from 'riveted'
riveted.init();
抛出错误:
_riveted2.default.init 不是函数
import riveted from 'riveted'
riveted.init();
import 'riveted'
riveted.init();
抛出错误:
铆接未定义
import * as riveted from 'riveted'
riveted.init();
抛出错误:
riveted.init 不是函数
如何访问 riveted 的 init() 函数?
How can I access riveted's init() function?
推荐答案
您可以使用 webpack 导出加载器:
You can use the webpack exports loader:
var riveted = require("exports?riveted!riveted")
见 shiming 模块概述 详情
这篇关于如何导入“旧"ES6 中的 ES5 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!