webpack4.29.6版本
方法一:
首先,您需要安装expose-loader:
$ npm install expose-loader --save-dev
例如,假设您希望将jQuery公开为全局调用$:
02.js
require("expose-loader?$!jquery");
方法二:
直接在js头部引用
require('../jq/jquery-1.3.1.js');
第三种方法:
无法引用本地jquery
01.html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Custom template</title>
<body>
<script src="https://cdn.bootcss.com/jquery/1.3.1/jquery.js"></script>
</body>
</html>
webpack.config.js
externals : {
'jquery' : 'window.jQuery'
}
01.js
const $ = require("jquery");
$("#content").html("<h1>hello world</h1>");
顺便说一下HTML的打包吧:
安装
npm install --save-dev html-webpack-plugin
webpack.config.js
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports={
....//省略
},
plugins:[
new HtmlWebpackPlugin({
template: './src/view/index.html', //指定要打包的html路径和文件名
filename:'view/index.html', //指定输出路径和文件名
inject:true,
hash:true
})
]