WEBPACK.CONFIG.JS
1. 使用UMD
module.exports={
mode:'development',
entry:'./yourentry.js',
output:{
path:path.resolve(__dirname,"dist"),
filename:'main.js',
publicPath:'/dist/',
libraryTarget:'umd',
library:'rstate',
umdNamedDefine: true,
libraryExport: 'default'
}
}
index.html
<script src="dist/main.js"></script>
<script>
window.onload = function () {
rstate()=>{}
</script>
main.js
export default function rstate(){
console.log("i called from html")
}
2.使用 VAR
module.exports={
mode:'development',
entry:'./yourentry.js',
output:{
path:path.resolve(__dirname,"dist"),
filename:'main.js',
publicPath:'/dist/',
libraryTarget:'var',
library: 'EntryPoint'
}
}
index.html
<script>
window.onload = function () {
EntryPoint.rstate()=>{}
</script>
main.js
module.exports={
rstate=function(){
console.log("hi module")
}
}
3.使用AMD作为库时,我们可以像这样使用(适用于那些想要创建库的人)
define(['jquery', './aux-lib.js'], function ($) { ..(1).. });
onload
方法中缺少类似于var EntryPoint = require('EntryPoint')
的东西。 - MartyIXrequire
的东西,但和下面的import
一样,它会显示“未定义require”。我试图使用来自纯JavaScript的打包内容,难道不需要再次使用某个框架才能使用require
吗?但我正在尝试避免这种情况。希望有意义。 - Raven