te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Image loading from root using relative path using--> webpack - file-loader - Angularjs - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Image loading from root using relative path using--> webpack - file-loader - Angularjs - Stack Overflow

programmeradmin4浏览0评论

I'm loading images on my HTML, CSS and JS using Webpack.

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.

In this case, configuring path for every single HTML and JS file is like hell.

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.

Help is most appreciated !!!

I'm loading images on my HTML, CSS and JS using Webpack.

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.

In this case, configuring path for every single HTML and JS file is like hell.

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.

Help is most appreciated !!!

Share Improve this question edited Nov 21, 2017 at 23:34 Enigma asked Nov 16, 2017 at 11:46 EnigmaEnigma 8591 gold badge18 silver badges36 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

Try to use the resolve.alias property, to define an alias to your assets:

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

Then you should be able to reference your images in Html like:

<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.

EDIT

Edit the css loader like this:

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.

Ok - I've figured it out that how to setup mon 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.

Changes needed is that :

  1. In Webpack.config.js file, We need to add new Plugins

    new CopyWebpackPlugin([{
        from: './src/images',
        to: 'images'
    }]),
    
  2. In Webpack.config.js only - We need to setup publicPath in output section as :

    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 :

    <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.

发布评论

评论列表(0)

  1. 暂无评论