在本文中,我们将详细介绍Webpack:从html模板加载图像的各个方面,并为您提供关于webpack加载图片的相关解答,同时,我们也将为您带来关于05webpack中html-webpack-plu
在本文中,我们将详细介绍Webpack:从html模板加载图像的各个方面,并为您提供关于webpack加载图片的相关解答,同时,我们也将为您带来关于05webpack中html-webpack-plugin的2个作用、645 webpack常用plugins:clean-webpack-plugin,html-webpack-plugin,webpack.DefinePlugin,copy-webpack-plug、Angular 2 Web Pack 404 Html模板URL、chainWebpack 和 htmlWebpackPlugin搭配使用的有用知识。
本文目录一览:- Webpack:从html模板加载图像(webpack加载图片)
- 05webpack中html-webpack-plugin的2个作用
- 645 webpack常用plugins:clean-webpack-plugin,html-webpack-plugin,webpack.DefinePlugin,copy-webpack-plug
- Angular 2 Web Pack 404 Html模板URL
- chainWebpack 和 htmlWebpackPlugin搭配使用
Webpack:从html模板加载图像(webpack加载图片)
我正在尝试使用Webpack设置一个有角度的项目,但是我不知道如何从html模板中引用图像并将其包含在构建中。
我的项目树如下:
package.jsonapp/- images/ - foo.png- scripts/- styles/- templates/
我正在尝试html-loader
与url-loader
和一起使用,file-loader
但这只是没有发生。
这是一个示例模板: app/templates/foo.html
<img src="../images/foo.png" />
问题#1
:我希望能够参考相对于的图片app/
。现在,路径需要相对于模板文件,这将变得非常丑陋(../../../images/foo.png
)。
问题2 :即使我指定了相对路径(如我上面所做的那样),项目也可以成功构建,但实际上没有任何反应。路径保持不变,并且中没有图像dist/
。
这是我的webpack配置:
var path = require(''path'');var webpack = require(''webpack'');var ngminPlugin = require(''ngmin-webpack-plugin'');var HtmlWebpackPlugin = require(''html-webpack-plugin'');var ExtractTextPlugin = require(''extract-text-webpack-plugin'');var ngAnnotatePlugin = require(''ng-annotate-webpack-plugin'');module.exports = function(config, env) { var appRoot = path.join(__dirname, ''app/'') if(!env) env = ''development''; var webpackConfig = { cache: true, debug: true, contentBase: appRoot, entry: { app: path.join(appRoot, ''/scripts/app.coffee'') }, output: { path: path.join(__dirname, ''dist/), publicPath: ''/'', libraryTarget: ''var'', filename: ''scripts/[name].[hash].js'', chunkFilename: ''[name].[chunkhash].js'' }, module: { loaders: [ { test: /\.css$/, loader: ExtractTextPlugin.extract("style-loader", "css-loader") }, { test: /\.scss$/, loader: ExtractTextPlugin.extract(''style-loader'', ''css-loader!sass-loader?outputStyle=expanded&includePaths[]=./node_modules/foundation/scss/'') }, { test: /\.coffee$/, loader: ''coffee-loader'' }, { loader: ''ngtemplate?relativeTo='' + (path.resolve(__dirname, ''./app'')) + ''/!html'' }, { test: /\.png$/, loader: "url-loader?limit=100000&mimetype=image/png&name=[path][name].[hash].[ext]" }, { test: /\.jpg$/, loader: "file-loader?name=[path][name].[hash].[ext]" }, { test: /\.(woff|woff2)(\?(.*))?$/, loader: ''url?prefix=factorynts/&limit=5000&mimetype=application/font-woff'' }, { test: /\.ttf(\?(.*))?$/, loader: ''file?prefix=fonts/'' }, { test: /\.eot(\?(.*))?$/, loader: ''file?prefix=fonts/'' }, { test: /\.svg(\?(.*))?$/, loader: ''file?prefix=fonts/'' }, { test: /\.json$/, loader: ''json'' } ] }, resolve: { extensions: [ '''', ''.js'', ''.coffee'', ''.scss'', ''.css'' ], root: [appRoot], }, singleRun: true, plugins: [ new webpack.ContextReplacementPlugin(/.*$/, /a^/), new webpack.ProvidePlugin({ ''_'': ''lodash'' }), new ExtractTextPlugin("styles/[name].[chunkhash].css", {allChunks: true}), new HtmlWebpackPlugin({ template: appRoot + ''/app.html'', filename: ''app.html'', inject: ''body'', chunks: [''app''] }) ], devtool: ''eval'' } if(env === ''production'') { webpackConfig.plugins = webpackConfig.plugins.concat( new ngAnnotatePlugin(), new webpack.optimize.UglifyJsPlugin(), new webpack.DefinePlugin({ ''process-env'': { ''NODE_ENV'': JSON.stringify(''production'') } }), new webpack.optimize.DedupePlugin(), new webpack.optimize.UglifyJsPlugin() ); webpackConfig.devtool = false; webpackConfig.debug = false; } return webpackConfig;
}
答案1
小编典典- 是的,您必须这样做才能从其他路径加载图像。
- 我有类似的问题,我使用
file
加载程序解决了这个问题:
。
loaders: [{ // JS LOADER test: /\.js$/, loader: ''babel-loader?optional[]=runtime'', exclude: /node_modules/}, { // ASSET LOADER test: /\.(woff|woff2|ttf|eot)$/, loader: ''file-loader''},{ //IMAGE LOADER test: /\.(jpe?g|png|gif|svg)$/i, loader:''file-loader''},{ // HTML LOADER test: /\.html$/, loader: ''html-loader''},{ //SCSS LOADER test: /\.scss$/, loaders: ["style-loader", "css-loader", "sass-loader?indentedSyntax"]}]
祝好运
05webpack中html-webpack-plugin的2个作用
<!-- 15 html-webpack-plugin的2个作用 下载 cnpm i html-webpack-plugin -D 作用在==>内存中生成页面
可以在package.json中去查看是否有 在webpack中 导入在内存中生成的HTML页面的插件 // 只要是webpack的插件 都要放入 plugins 这个数组中去 const htmlwebpackPlugin=require("html-webpack-plugin") plugins: [ new webpack.HotModuleReplacementPlugin(),这是热跟新的配置 new htmlwebpackPlugin({ 创建一个 在内存中生成 HTML页面的插件 template:path.join(__dirname,'./src/index.html'),1)">指定模板页面,将来会根据指定的页面路径,去生成内存中的 页面 filename:"index.html" 指定生成的页面名称 }) ] // 当我们使用html-webpack-plugin之后,我们不需要手动处理bundle.js的引用路径, (我们可以将index.html中的 <script src="../dist/bundle.js"></script>注释掉 ) 因为这个插件,已经帮我们自动创建一个 合适的script,并且引用了正确的路径 这个插件有两个作用的 在内存中帮我们生成一个页面 帮我们自动创建一个合适的script标签 并且引入正确的路径
运行的命令 npm run dev
完整代码
package.json
src下的main.js
webpack.config.js
src下的index.html
645 webpack常用plugins:clean-webpack-plugin,html-webpack-plugin,webpack.DefinePlugin,copy-webpack-plug
-
- 前端小菜鸟,喜欢前端,不断学习
- 微信:jie178463596
- 微信小群:纯粹讨论技术、面试、工作为主,划水少,拒绝广告
认识Plugin
CleanWebpackPlugin
HtmlWebpackPlugin
生成的index.html分析
自定义HTML模板
自定义模板数据填充
DefinePlugin的介绍
DefinePlugin的使用
copyWebpackPlugin
目录结构
wk.config.js
const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { DefinePlugin } = require('webpack'); // DefinePlugin是webpack内置插件
const copyWebpackPlugin = require('copy-webpack-plugin');
module.exports = {
entry: "./src/main.js",
output: {
filename: "js/bundle.js",
// 必须是一个绝对路径
path: path.resolve(__dirname, "./build"),
// assetmodulefilename: "img/[name].[hash:6][ext]"
},
module: {
rules: [
{
// 规则使用正则表达式
test: /\.css$/, // 匹配资源
use: [
// { loader: "css-loader" },
// 注意: 编写顺序(从下往上, 从右往做, 从后往前)
"style-loader",
{
loader: "css-loader",
options: {
importLoaders: 1
}
},
"postcss-loader"
],
// loader: "css-loader"
},
{
test: /\.less$/,
use: [
"style-loader",
{
loader: "css-loader",
options: {
importLoaders: 2
}
},
"postcss-loader",
"less-loader"
]
},
{
test: /\.(png|jpe?g|gif|svg)$/,
// type: "asset/resource", file-loader的效果
// type: "asset/inline", url-loader
type: "asset",
generator: {
filename: "img/[name].[hash:6][ext]"
},
parser: {
dataUrlCondition: {
maxSize: 100 * 1024
}
}
},
{
test: /\.ttf|eot|woff2?$/i,
type: "asset/resource",
generator: {
filename: "font/[name].[hash:6][ext]"
}
}
]
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
title: "哈哈 webpack",
template: "./public/index.html"
}),
new DefinePlugin({
// 要包裹两层引号
BASE_URL: '"./"'
}),
new copyWebpackPlugin({
patterns: [
{
// to: xxx, // 不用写,默认会使用output.path
from: "public",
globOptions: {
ignore: [
"**/index.html",
"**/.DS_Store",
"**/abc.txt"
]
}
}
]
})
]
}
publuc/index.html
<!DOCTYPE html>
<html lang="">
<head>
<Meta charset="utf-8">
<Meta http-equiv="X-UA-Compatible" content="IE=edge">
<Meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
<title><%= htmlWebpackPlugin.options.title %></title>
</head>
<body>
<noscript>
<strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
</noscript>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>
build/index.html
<!DOCTYPE html>
<html lang="">
<head>
<Meta charset="utf-8">
<Meta http-equiv="X-UA-Compatible" content="IE=edge">
<Meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="icon" href="./favicon.ico">
<title>杰帅的webpack</title>
<script defer src="js/bundle.js"></script></head>
<body>
<noscript>
<strong>We're sorry but 杰帅的webpack doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
</noscript>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>
Angular 2 Web Pack 404 Html模板URL
app.component.ts片段
@Component({ moduleId:module.id,selector: 'body',templateUrl: 'app.component.html',})
webpack.config.js片段
module:{ loaders:[ {test:/\.ts$/,loader:'ts',exclude: /node_modules/},{test:/\.html$/,loader:'html' },],},
错误
browser_adapter.js?0526:84错误:未捕获(在承诺中):无法加载/app.component.html
解决方法
使用这样的模板 –
template: require('./app.component.html'),styles: [require('./app.component.css')]
代替
templateUrl: './app.component.html',styleUrls: ['./app.component.css']
看看这是否有帮助.
chainWebpack 和 htmlWebpackPlugin搭配使用
const HtmlWebpackPlugin = require(''html-webpack-plugin'');
...
chainWebpack: config => {
config
.plugin(''html'')
.use(HtmlWebpackPlugin)
.tap(options => {
options.BASE_URL = ''sss''
options.template = __dirname + ''/public/index.html''
options.title = ''sssssssssssss''
return options
})
}
注意看vue-cli3引用的html-webpack-plugin默认配置
constructor (options) {
// Default options
this.options = _.extend({
template: path.join(__dirname, ''default_index.ejs''),
templateParameters: templateParametersGenerator,
filename: ''index.html'',
hash: false,
inject: true,
compile: true,
favicon: false,
minify: false,
cache: true,
showErrors: true,
chunks: ''all'',
excludeChunks: [],
chunksSortMode: ''auto'',
meta: {},
title: ''Webpack App'',
xhtml: false
}, options);
}
今天关于Webpack:从html模板加载图像和webpack加载图片的讲解已经结束,谢谢您的阅读,如果想了解更多关于05webpack中html-webpack-plugin的2个作用、645 webpack常用plugins:clean-webpack-plugin,html-webpack-plugin,webpack.DefinePlugin,copy-webpack-plug、Angular 2 Web Pack 404 Html模板URL、chainWebpack 和 htmlWebpackPlugin搭配使用的相关知识,请在本站搜索。
本文标签: