【问题标题】:Image loading from root using relative path using--> webpack - file-loader - Angularjs使用相对路径从根目录加载图像--> webpack - 文件加载器 - Angularjs
【发布时间】:2018-04-29 22:39:33
【问题描述】:

我正在使用 Webpack 在我的 HTML、CSS 和 JS 上加载图像。

我的配置是:

{
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;
}

我的项目文件夹结构:

- 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

我只想引用来自 HTML、CSS 和 JS 的图像(至少在 JS 中的图像位置),但目前我所有的图像路径都是相对于图像文件夹到模板文件的。

在这种情况下,为每个 HTML 和 JS 文件配置路径就像地狱一样。

所以我的问题是:我怎样才能有通用路径,以便在 HTML、JS 或 CSS 中使用 - 我会 只需在任何文件中引用图像名称,通用路径就会在图像文件夹中找到它。

非常感谢您的帮助!!!

【问题讨论】:

    标签: javascript angularjs webpack html-webpack-plugin webpack-file-loader


    【解决方案1】:

    好的 - 我已经弄清楚如何设置公共图像路径,以便可以从项目中的任何 HTML、JS 或 CSS 加载图像,并且只需要在每个实例中提供相同的公共路径。

    需要改变的是:

    1. 在 Webpack.config.js 文件中,我们需要添加新的插件

      new CopyWebpackPlugin([{
          from: './src/images',
          to: 'images'
      }]),
      
    2. 仅在 Webpack.config.js 中 - 我们需要在输出部分将 publicPath 设置为:

      output: {
                path: path.join(__dirname, 'mcaid'),
                filename: 'bundle.js',
                publicPath: '/',     <----- this is been added
      },
      
    3. 在 HTML、JS 或 CSS 文件中,您可以引用如下图片:

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

    /images 是 -- '/' 是 publicPath,'images' 是 'dev' 的 src 文件夹中的图像,'prod' 构建的 'dist' 中的'images'。

    【讨论】:

      【解决方案2】:

      尝试使用resolve.alias 属性为您的资产定义别名:

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

      那么您应该能够在 Html 中引用您的图像,例如:

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

      然后您的 css-loader(或 html-loader,我不太确定)将解析正确的路径。

      编辑

      像这样编辑 css 加载器:

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

      忘记了。 url=false 参数告诉 css-loader 不要直接处理 url。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-23
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 2018-12-28
      • 2016-10-06
      相关资源
      最近更新 更多