webpack入门

webpack的优势

1.webpack是以commonJS的形式来书写,但对AMD/CMD的支持也很全面,方便旧项目进行代码迁移。
2.可以对js,css,img进行模块化。
3.开发便捷,能代替grunt/glup的工作,比如打包,压缩,图片转base64。
4.扩展性强,插件机制完善。

阮一峰的教程
http://www.uxebu.com/index.html%3Fp=6615.html
使用方法

使用

 echo  "document.write("It work.")" > entry.js

index.html

<html>
  <head></head>
  <body>
    <script type="text/javascript" src="bundle.js" charset="utf-8"></script>
  </body>
</html>

然后执行:

webpack ./entry.js bundle.js

2.升级
content.js

module.exports = "It works from content.js.";

update entry.js

document.write(require("./content.js"));

使用loader
webpack can only handle JavaScript natively, so we need the css-loader to process CSS files. We also need thestyle-loader to apply the styles in the CSS file.
update entry.js

require("!style!css!./style.css")

We don’t want to write such long requires require("!style!css!./style.css"); .

require("./style.css")

webpack ./entry.js bundle.js --module-bind 'css=style!css'

3.use config file
add webpack.config.js

module.exports = {
    entry: "./entry.js",
    output: {
        path: __dirname,
        filename: "bundle.js"
    },
    module: {
        loaders: [
            {test: /\.css$/, loader: "style!css"}
        ]
    }
}

执行webpack

webpack常用命令

don't want to manually recompile after every change

webpack --progress --colors --watch

development server

npm install webpack-dev-server -g
webpack-dev-server --progress --colors
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 写在前面 第一次接触webpack,是在一个react项目参与中,刚开始使用的时候,甚至不知道是做什么用的,只看到...
    默默先生Alec阅读 667评论 0 3
  • 题记 本年度的前端研究方向之一是webpack,之前就去看过一个webpack的学习视频,自己按照课程写了一些配置...
    baxiamali阅读 330评论 0 0
  • 2017.03.11 周六 每日所思所悟化为文字,抒发自我内心深处情感! 今天在做完三个微信群的分享之后,收获很...
    志晓阅读 173评论 0 0
  • 如何减少自己被欺骗,有多少人平时会思考这个问题,至少我好想很久都没有思考过这样的问题了,或许只有在淘宝购物的时候,...
    工具控思维控阅读 732评论 0 0
  • let声明常亮var声明变量有自动推断的功能,也可以指明数据类型 任何类型的数据不会被隐式转换成其他类型的数据,就...
    爆炸的白菜君阅读 138评论 0 0