为了添加外部库,我们可以使用这个HtmlWebpackExternalsPlugin
。我们应该在我们的webpack.config.js文件中添加这个。
因此,这HtmlWebpackExternalsPlugin
将创建供应商文件夹,并在其中添加缩小的文件。它还会将缩小文件路径添加到index.html库中,类似于此<script type="text/javascript" src="vendor/jquery/dist/jquery.min.js"></script>
这是我的webpack.config.js文件。
在这里,我已经包括了jQuery,引导表,引导库
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var bootstrapEntryPoints = require('./webpack.bootstrap.config.js');
var path = require("path");
var webpack = require('webpack');
var HtmlWebpackExternalsPlugin = require('html-webpack-externals-plugin');
var bootstrapConfig =bootstrapEntryPoints.prod;
module.exports={
entry:{app:['./app.js','./public/app.css'],
//vendor: './app/vendor.ts'
bootstrap:bootstrapConfig
},
output:{
path:__dirname+ '/public/',
filename:'[name].bundle.js'},
node: {
fs: 'empty',
net: 'empty',
tls: 'empty'
},
module:{
rules:[
{
test: /\.js$/,
exclude: /node_modules/,
use: 'babel-loader'
},
{ test: /\.css$/, use: ExtractTextPlugin.extract({ fallback: 'style-loader', use: [ 'css-loader' ] }) },
{ test: /\.(woff2?|svg)$/, loader: 'url-loader?limit=10000&name=fonts/[name].[ext]' },
{ test: /\.(ttf|eot)$/, loader: 'file-loader?name=fonts/[name].[ext]' },
{ test:/bootstrap-sass[\/\\]assets[\/\\]javascripts[\/\\]/, loader: 'imports-loader?jQuery=jquery' }
]
},
plugins: [
new HtmlWebpackPlugin({
title: 'Monitoring Status',
minify:{collapseWhitespace:true
},
hash:true,
template: 'views/index.ejs' // Load a custom template (ejs by default see the FAQ for details)
}),
new HtmlWebpackExternalsPlugin({
externals: [
{
module: 'jquery',
entry: 'dist/jquery.min.js',
global: 'jQuery',
},
],
}),
new HtmlWebpackExternalsPlugin({
externals: [
{
module: 'bootstrap',
entry: ['dist/css/bootstrap.min.css','dist/js/bootstrap.min.js']
},
],
}),
new HtmlWebpackExternalsPlugin({
externals: [
{
module: 'bootstrap-table',
entry: ['dist/bootstrap-table.min.css','dist/bootstrap-table.min.js']
},
],
}),
new ExtractTextPlugin({
filename:'css/[name].css',
allChunks:true
})
]
}
我想引导只在index.html.Bootstrap在bundle.js.So可如果我有bundle.js它应该工作没有引导表和jquery ..所以我的疑问是我应该添加类似于要求('jquery')在HTML中,以便它可以在HTML?@Danny –
jquery和系绳需要在引导之前加载。 在你的webpack配置中,你可以输入'entry:['./app.js','bootstrap']'; in'app.js'你会有 '从'jquery'导入JQuery;从'系绳'进口系绳;' –