關於webpack的項目文件:https://gitee.com/codeFarmerPen/webpackjavascript
下載插件:css
$ cnpm install less less-loader --save-dev
順帶把css插件也安裝了html
$ cnpm install css-loader style-loader --save-dev
webpack.config.js loaders加入java
, { test: /\.less$/, use: [ 'style-loader', 'css-loader', 'less-loader' ] }
webpack.config.js終文件:webpack
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 } }; 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: /\.less$/, use: [ 'style-loader', 'css-loader', 'less-loader' ] } ] } , plugins: [ //new一個模板出來測試一下 new htmlwebpackplugin(get_html("home","main1")) ] }; module.exports=export_html;
app目錄下新建less目錄,其下新建style.less文件git
style.lessweb
@f:white; p{ color: red; } p{ a{ color: @f; } }
app/js/main.jsnpm
require("../less/style.less");
生成文件:數組
_build/js/main.js裏面能夠找到這行瀏覽器
// module exports.push([module.i, "p {\n color: red;\n}\np a {\n color: white;\n}\n", ""]);
說明其已經編譯成css,且被放入了main.js的生成文件內。
咱們再用瀏覽器打開_build/home.html
發現字體變成紅色,編譯成功!下一篇進階篇,會用插件把less生成的css分離出來
webpack配置(第一步:配置前提):http://www.javashuo.com/article/p-zhierewo-eo.html
webpack配置(第二步:入門篇):http://www.javashuo.com/article/p-zgertkmc-mq.html
webpack配置(第三步:ES6篇):http://www.javashuo.com/article/p-yornwkpw-dt.html
webpack配置(第四步:html篇(基礎篇)):http://www.javashuo.com/article/p-ykguohhe-do.html
webpack配置(第四步:html篇(進階篇)):http://www.javashuo.com/article/p-nqgmvdah-hz.html
webpack配置(第四步:html篇(模板篇)):http://www.javashuo.com/article/p-cfrvmuzd-ea.html
webpack配置(第五步:less/css篇(基礎篇)):http://www.javashuo.com/article/p-ftegbnmw-p.html
webpack配置(第五步:less/css篇(進階篇)):http://www.javashuo.com/article/p-bkdzfetz-mx.html
webpack配置(第五步:less/css篇(url圖片篇)):http://www.javashuo.com/article/p-adtmrppc-mq.html