简体   繁体   English

使用相对路径从root用户加载图像 - > webpack - file-loader - Angularjs

[英]Image loading from root using relative path using--> webpack - file-loader - Angularjs

I'm loading images on my HTML, CSS and JS using Webpack. 我正在使用Webpack在我的HTML,CSS和JS上加载图像。

My Config is : 我的配置是:

{
var path = require('path');
var webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

var config = {
    entry: [
        'angular', './src/lib.js', './src/app.js',
    ],
output: {
    path: path.join(__dirname, 'dist'),
    filename: '[name].js',
},
module: {
    rules: [{
        test: /\.(jpe?g|png|gif|svg)$/i,
         loader: ['file-loader?name=[name].[ext]&outputhPath=images/&publicPath=images/'],
    },{
        test: /\.css|scss$/,
        use: ExtractTextPlugin.extract({
            fallback: "style-loader",
            use: ['css-loader']
        })
    },{
        test: /\.html$/,
        exclude: [
            path.join(__dirname, '/src/index.html')
        ],
        use: [
            { loader: 'ngtemplate-loader?relativeTo=' + (path.resolve(__dirname, './src'))},
            { loader:  'html-loader'},
            ]
        }],
    },
    plugins: [
        new HtmlWebpackPlugin({
            title: 'Project Demo,
            minify: {
                collapseWhitespace: true,
            },
            cache: true,
            hash: true,
            inject: true,
            filename: './index.html',
            template: 'src/index.html'
        }),
        new ExtractTextPlugin({
            filename: "app.css",
            disable: false,
            allChunks: true
        })
    ],
    devServer: {
        port: 9000
    },
};

    module.exports = config;
}

my Project Folder Structure : 我的项目文件夹结构:

- dist
    - images [folder]
    - index.html
    - app.css
    - main.js 
- node_modules
- src
    - images
    - javascripts/pages/HTML Files
    - stylesheets
    - app.js (BootStrapping angular)
    - lib.js 
    - index.html
- webpack.config.js
- package.js

I just want to refer include images from HTML, CSS and JS (Image location atleast in JS) but currently all my image paths are relative to image folder to template file. 我只想引用包含来自HTML,CSS和JS的图像(图像位置至少在JS中),但目前我所有的图像路径都是相对于图像文件夹到模板文件。

In this case, configuring path for every single HTML and JS file is like hell. 在这种情况下,为每个HTML和JS文件配置路径就像地狱一样。

so my qustions is : How can I have generic path so that in HTML, JS or CSS- I'll just refer image name in any file and generic path will find that out in images folder. 所以我的问题是: 我如何拥有通用路径,以便在HTML,JS或CSS中 - 我只会在任何文件中引用图像名称,而通用路径将在图像文件夹中找到它。

Help is most appreciated !!! 非常感谢帮助!!!

Ok - I've figured it out that how to setup common images path so images can be loaded from any HTML, JS or CSS in your project and just need to give same public path at every instance. 好的 - 我已经弄清楚如何设置常见的图像路径,以便可以从项目中的任何HTML,JS或CSS加载图像,只需要在每个实例中提供相同的公共路径。

Changes needed is that : 需要进行的更改是:

  1. In Webpack.config.js file, We need to add new Plugins 在Webpack.config.js文件中,我们需要添加新的插件

     new CopyWebpackPlugin([{ from: './src/images', to: 'images' }]), 
  2. In Webpack.config.js only - We need to setup publicPath in output section as : 仅在Webpack.config.js中 - 我们需要在输出节中设置publicPath:

     output: { path: path.join(__dirname, 'mcaid'), filename: 'bundle.js', publicPath: '/', <----- this is been added }, 
  3. In HTML, JS or CSS File, you can refer images like : 在HTML,JS或CSS文件中,您可以参考如下图像:

     <img src="/images/some_Image_file.png"/> 

/images in src is -- '/' is publicPath and 'images' is images from src folder for 'dev' and 'images' from 'dist' for 'prod' build. / src中的图像是 - '/'是publicPath,'images'是来自'dev'的src文件夹中的图像,'dist'中的'images'是'prod'构建的图像。

Try to use the resolve.alias property, to define an alias to your assets: 尝试使用resolve.alias属性来定义资产的别名:

resolve: {
  alias: {
    images: path.resolve(__dirname, 'src/images')
  }
},

Then you should be able to reference your images in Html like: 然后你应该能够在Html中引用你的图像,如:

<img src="~images/some_image.png" alt="image alt text" />

Your css-loader (or html-loader, I am not quite sure about that) will then resolve the correct path. 您的css-loader(或html-loader,我不太确定)将解析正确的路径。

EDIT 编辑

Edit the css loader like this: 像这样编辑css加载器:

use: ExtractTextPlugin.extract({
  fallback: "style-loader",
  use: [
    {loader: 'css-loader?url=false'}
  ]
})

Forgot this. 忘了这个。 the url=false parameter tells the css-loader not to handle urls directly. url = false参数告诉css-loader不要直接处理url。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM