我使用webpack创建* .js包
var path = require('path');
var webpack = require('webpack');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
module.exports = {
entry: {
site: [
'./wwwroot/js/site.js',
'./node_modules/jquery/dist/jquery.js',
'./Scripts/jquery.global.js',
'./node_modules/jquery-validation/dist/jquery.validate.js',
'./node_modules/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js',
'./node_modules/popper.js/dist/popper.js',
'./node_modules/bootstrap/dist/js/bootstrap.js',
]
},
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'wwwroot/dist/')
},
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'ts-loader',
exclude: /node_modules/,
},
{
test: /\.css$/,
loader: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: 'css-loader'
})
},
]
},
resolve: {
extensions: [".tsx", ".ts", ".js", ".css"]
},
plugins: [
new ExtractTextPlugin('../css/bundle.css'),
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
Popper: ['popper.js', 'default']
})
]
};
然后在_Layout.cshtml
我有以下代码:
...
<div class="container body-content">
@RenderBody()
<hr />
<footer>
<p>© 2017 - MVC_Movie</p>
</footer>
</div>
<environment include="Development">
<script src="~/js/site.js" asp-append-version="true"></script>
<script src="~/dist/bundle.js" asp-append-version="true"></script>
</environment>
<environment exclude="Development">
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"
asp-fallback-src="~/lib/jquery/dist/jquery.min.js"
asp-fallback-test="window.jQuery"
crossorigin="anonymous"
integrity="">
</script>
<script src="http://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/bootstrap.min.js"
asp-fallback-src="~/lib/bootstrap/dist/js/bootstrap.min.js"
asp-fallback-test="window.jQuery && window.jQuery.fn && window.jQuery.fn.modal"
crossorigin="anonymous"
integrity="">
</script>
<script src="~/js/site.min.js" asp-append-version="true"></script>
</environment>
@RenderSection("Scripts", required: false)
<script>
$(document).ready(function () {
debugger;
//Ask ASP.NET what culture we prefer, because we stuck it in a meta tag
var data = $("meta[name='accept-language']").attr("content")
//Tell jQuery to figure it out also on the client side.
$.global.preferCulture(data);
//Tell the validator, for example,
// that we want numbers parsed a certain way!
$.validator.methods.number = function (value, element) {
if ($.global.parseFloat(value)) {
return true;
}
return false;
}
});
</script>
</body>
</html>
到目前为止,我没有收到任何错误来生成bundle.js
文件看起来很好,但_Layout.cshtml
底部的部分引发了异常
未捕获的ReferenceError:$未定义
我认为通过在webpack.config.js
插件部分中定义符号$
将被识别:
plugins: [
new ExtractTextPlugin('../css/bundle.css'),
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
Popper: ['popper.js', 'default']
})
]
知道如何让它发挥作用吗?
答案 0 :(得分:4)
似乎有一个解决方案是将jQuery模块暴露在webpack包之外。
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'ts-loader',
exclude: /node_modules/,
},
{
test: /\.css$/,
loader: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: 'css-loader'
})
},
{
// Exposes jQuery for use outside Webpack build
test: require.resolve('jquery'),
use: [{
loader: 'expose-loader',
options: 'jQuery'
}, {
loader: 'expose-loader',
options: '$'
}]
}
]
},
此处的解决方案:Managing jQuery plugin dependency in webpack @harmsmsrelrel
答案 1 :(得分:0)
在JS Bundle
之前附加jquery.js文件