首先万分感谢这个教程的制做者。少走了许多弯路,正在学习webpack的小伙伴能够来看看,版本的坑暂时没有!!!! 2017_7_6又加了一个很是有用的教程javascript
连接以下:webpack-react之webpack篇 --上
连接2以下: webpack+reactcss
"scripts": { "test": "echo \"Error: no test specified\" && exit 1", "dev": "webpack-dev-server --devtool eval-source-map --progress --content-base ./build", "build": "webpack" }
以后当启动服务器以后咱们要在路径中加上 localhost:xxxx/webpack-dev-server 其中xxxx为端口号,可选范围内随意。以后咱们就能够经过谷歌的调试插件看到局部更新的效果了。html
"webpack":"webpack --config webpack.config.js(这里的名字想怎么改就怎么改) --progress --display --colors"
filename:'[name]-[chunkhash].js'
,根据教程,使用html-webpack-plugin来达成咱们的目的。const htmlWebpackPlugin=require('html-webpack-plusin');
,而后咱们在output后面加上plugins;[new htmlWebpackPlugin()]
,以后能够发现咱们的插件已经生效,html已经生成了引用。不过在这个时候,它是自动生成的html文件.const path=require('path'); const htmlWebpackPlugin=require('html-webpack-plugin'); module.exports={ entry:'./src/script/main.js', output:{ // filename:'bundle.js', filename:'[name]-[chunkhash].js', path:path.resolve(__dirname,'./dist/js') }, plugins:[ new htmlWebpackPlugin({ template:'index.html' }) ] }
添加了template后的做用是,让输出的html以咱们template上下文中html为蓝本输出index.htmljava
3.webpack中添加loader的方法有三种,一种是直接在require中添加,好比node
require('style-loader!css-loader!./style.css');
webpack --module-bind 'css=style!css'
这种方法是在命令行中添加module:{ loaders:[{ test:/\.js$/, loader:'babel-loader', query:{ presets:["es2015"] } }] }
另外,关于loader的串联,咱们在require文件中只要写"!"就行了,可是咱们在配置文件中的模块中须要这样写react
{ test:/\.css$/, loader:["style-loader","css-loader"] }
注意,模块名要写全,不能直接写style,否则报警了
另外还有其余的写法,下图来自官方文档==========》webpack
{ module: { loaders: [ { test: /\.jade$/, loader: "jade" }, // => "jade" loader is used for ".jade" files { test: /\.css$/, loader: "style!css" }, // => "style" and "css" loader is used for ".css" files // Alternative syntax: { test: /\.css$/, loaders: ["style", "css"] }, ] } }
4.关于webpack是如何出html模板文件的呢?主要仍是采用loader的机制。我经过下面几张截图来大概阐释一下。同时我感受这跟Vue,React等有类似的地方。
---首先,这是index.html里面的东西git
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <div id="app"> </div> </body> </html>
----这是app.js里面的构建,其实就是将咱们的Layer挂载在div#app中github
import Layer from './components/layer/layer.js'; import './css/common.css'; const App=function(){ var dom=document.getElementById('#app'); var layer=new Layer() dom.innerHTML=layer.tpl; } new App();
-----这个是咱们所挂载的东西web
import tpl from './layer.html'; import './layer.less'; function layer(){ return { name:'layer', tpl:tpl } } export default layer;
---以后采用html-loader来读取这个文件。直接用html-loader这种状况是指不用任何模板的时候。
除此以外,还有使用其余模板来编译html的,好比说ejs,pug(jade)
下图文件为layer.tpl,固然,也能够取名为.ejs格式的,以后从js中import进去,用document.getElementById以后挂载上去
<div class="layer"> <div>this is <%= name %> layer</div> <% for (var i=0;i<arr.length;i++){ %> <%=arr[i]%> <% } %> </div>
在webpack的loader之中这样写
{ test:/\.tpl$/, loader:'ejs-loader' }
Babel相关
1.由于以前老是使用babel-core,babel-loader安装,就会发现经常又少了东西.这个问题待更吧。
2.为何babel打包的那么慢呢?问题出在loader上,由于loader是很是耗时的东西。咱们能够经过过滤掉一些不用编译的文件来提升速度。其实主要就是用include和exclude来包含或者排除范围。示范以下:
module:{ loaders:[{ test:/\.js$/, loader:'babel-loader', exclude:/node_modules/, query:{ presets:["es2015"] } }] }
!!可是问题来了,其实这样的速度提高并不明显,用include才能明显地提高速度
module:{ loaders:[{ test:/\.js$/, loader:'babel-loader', exclude:/node_modules/, include:/src/, query:{ presets:["es2015"] } }] }
关于包含和排除的文件,也能够调用path的resolve来实现,当咱们使用path.resolve(__dirname,'xx')的时候,咱们能够发现,咱们的速度变得更快了。
var path=require('path'); // var htmlWebpackPlugin=require('html-webpack-plugin'); // console.log("如今的路径是"+path.resolve(__dirname,'/the_last_webpack/app/')); //若是是 path.resolve(__dirname,'/app/'); //它会输入 ==> E:/app module.exports={ entry:"./app/main.js", output:{ path:path.resolve(__dirname,'./build/'), filename:'bundle.js' }, module:{ loaders: [{ test:/\.html$/, loader:'html-loader' },{ test:/\.(js|jsx)$/, loader:"babel-loader", // exclude:path.resolve(__dirname,'/the_last_webpack/node_modules/'), exclude:/node_modules/, //个人上面和下面都是对的,再下方也是对的,可是,为何path.resolve(__dirname,/app/)是错的? // include:/app/, 这样也是对的 include:path.resolve(__dirname,'app'), // include:path.resolve(__dirname,'/app/main.js'), query:{ presets:["react","es2015"] } }] } // plugins:[ // new htmlWebpackPlugin({ // filename:'index.html', // filename:'index.html' // }) // ] }