javascript - 请问 es6 用 webpack 转 es5 后,怎样在 es5 中 调用 之前打包好的代码?
大家讲道理
大家讲道理 2017-05-19 10:14:10
0
1
564

es6 代码 test.js

class test {
  constructor() {
    console.log("init");
  }
}

module.exports = test;

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>drawBoard</title>
</head>
<body>
</body>
<script type="text/javascript" src="../build/test.js"></script>
<script type="text/javascript">
  console.log(typeof test)        // undefined
</script>
</html>

这样直接调用肯定是会失败的。

我看了 webpack 打包后的代码,理解是将 打包了一个匿名函数,然后在匿名函数中返回了 module.exports 的 类 test
es6 写可以用 import,可是用 es5 怎样调用呢?

求解,谢谢。

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(1)
左手右手慢动作

webpack 的配置里面

output.libraryTarget 设置为 'umd'

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板