webpack配置(第五步:less/css篇(url圖片篇))

下載url-loaderjavascript

$ cnpm install file-loader url-loader --save-dev

webpack.config.jscss

const path = require('path');

let htmlwebpackplugin = require('html-webpack-plugin');//引入html-webpack-plugin插件 
let get_html = function(name,chunk){//封裝
    return {
        template: './app/ejs/generate/'+ name + '.ejs',
        filename:  name+ '.html',
        chunks  : ['main',chunk||null],//這裏引入公共文件main.js,另一個文件按需引入,固然也能夠把這個的值設爲數組,傳入function的第二個值用數組就行
        chunksSortMode: 'manual',//將chunks按引入的順序排序
        inject  : true,//全部JavaScript資源插入到body元素的底部
        hash    : true,
		xhtml	: true
    }
};

//配置css、less文件入口
let ExtractTextPlugin = require('extract-text-webpack-plugin');

let export_html= {
    entry:  {
    	main:__dirname+"/app/js/main.js",//入口文件
    	main1:__dirname+"/app/js/main1.js",//入口文件
    },
    output: {
        path: __dirname+"/_build/",
        filename: "js/[name].js",//產出文件,name根據entry的入口文件鍵名定
    },
    module: {
        loaders: [
			{
			    test: /(\.jsx|\.js)$/,
			    loader: 'babel-loader',
			    query: {
			      presets: ['es2015']
			    }
			}
            ,
            { 
            	test: /\.ejs$/, 
            	loader: 'ejs-loader?variable=data' 
            },
            {
                test: /\.css$/,
				loader: ExtractTextPlugin.extract({
					publicPath: "../../_build/",
				    fallback: "style-loader",
				    use: "css-loader"
				})
            },
            {
                test: /\.less$/,
				loader: ExtractTextPlugin.extract({
					publicPath: "../../_build/",
				    fallback: "style-loader",
				    use: [
	                    {
	                        loader: "css-loader"
	                    },
	                    {
	                        loader: "less-loader"
	                    }
	                ]
				})
            },
            {
                test: /\.(png|gif|jpg|jpeg|bmp|svg)/,
                use:[
                    {
                        loader: "url-loader",
	                    options: {
							limit: 8192,
                        	name: path.posix.join('static', './css_img/[hash:8].[name].[ext]')
						}
                    }
                ]
            }
        ]
    }
    ,
 	plugins: [
  		new ExtractTextPlugin("./css/[name].css"),
 		//new一個模板出來測試一下
  		new htmlwebpackplugin(get_html("home","main1"))
 	]
    
};
module.exports=export_html;

能夠對比一下,此次引入的文件只是在loaders裏面加入了html

,
            {
                test: /\.(png|gif|jpg|jpeg|bmp|svg)/,
                use:[
                    {
                        loader: "url-loader",
	                    options: {
							limit: 8192,
                        	name: path.posix.join('static', './css_img/[hash:8].[name].[ext]')
						}
                    }
                ]
            }

 path.posix.join('static', './css_img/[hash:8].[name].[ext]'),java

括號內的內容:static:是生成文件的主目錄,第二個值是生成文件的位置(產出目錄下面)webpack

目錄結構web

app目錄下新建static目錄,再建css_img,其下放入圖片npm

style.less數組

@f:white;
p{
    color: red;
}
p{
    width: 100%;
    height: 100px;
    background: url(../static/css_img/32132131.jpg) no-repeat center center;
    background-size: 100%; 
    a{
        color: @f;
    }
}

產出文件後的結構瀏覽器

能夠看見,在_build目錄下生成了編寫的文件babel

main.css

p {
  color: red;
}
p {
  width: 100%;
  height: 100px;
  background: url(../../_build/static/css_img/183f148e.32132131.jpg) no-repeat center center;
  background-size: 100%;
}
p a {
  color: white;
}

瀏覽器打開home.html

成功!

相關文章
相關標籤/搜索