好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

webpack基础_html/css_WEB-ITnose

转载请注明出处:

安装

1.全局:

npm install -g webpack 

2.项目:

npm install webpack --save-dev 

3. 添加 content.js

module.exports = 'It workd from content.js' 

4. 添加入口文件 entry.js

console.log(require('./content.js')) 

5. 编译文件打包

webpack ./entry.js bundle.js 

6. index.html引入 bundle.js

               

7. 查看结果

输出

It workd from content.js 

使用Loader加载css

webpack默认只支持js模块, 要打包css文件需要使用 css-loader处理css文件, style-loader应用样式

npm install css-loader style-loader 

1 新建 style.css

body {    background-color: yellow;} 

2 入口文件引入 style.css

console.log(require('./content.js'))require('!style!css!./style.css') 

3 重新编译, 打开html文件, 样式已经加载

绑定Loader

require('!style!css!./style.css')每次都写很长的加载器前缀很麻烦, 可以在编译命令指定加载器绑定到文件后缀名

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

此时 entry.js可以简化

require('./style.css') 

配置文件

将配置信息添加到 webpack.config.js配置文件后, 每次只需要执行 webpack即可完成打包任务

module.exports = {    entry: './entry.js',    output: {}} 

查看更多关于webpack基础_html/css_WEB-ITnose的详细内容...

  阅读:29次