让前端飞程序员JavaScript 进阶营

webpack初试

2017-12-11  本文已影响80人  tiancai啊呆

1: 为什么要用webpack?

webpack已经是大部分前端项目打包工具的首选,它可以将许多松散的模块按照依赖和规则打包成符合生产环境部署的前端资源。

2: webpack的简单使用。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <h3>hello world</h3>
    <script src="build/js/bundle.js"></script>
</body>
</html>

main.js如下:

var hello = require('./hello')
hello('I love the world')

hello.js如下:

module.exports = function (str) {
    alert(str)
}

webpack-config.js如下:

module.exports = {
    entry: __dirname +'/src/js/main.js', //入口文件,__dirname指向当前执行脚本所在的目录。 
    output:{
        path: __dirname + '/build/js',     //打包文件的路径
        filename:'bundle.js'      //打包文件名
    }
}
output:{
    path: __dirname + '/build/js',
    filename:'[name]-[hash].js' 
},

此时打包后的结果如下:


图5

打包后生成的文件名就是红色箭头所示。

3: webpack的pulgins说明。

var cleanWebpackPlugin = require("clean-webpack-plugin");
module.exports = {
    entry: __dirname +'/src/js/main.js',
    output:{
        path: __dirname + '/build/js',
        filename:'[name]-[hash].js'
    },
    plugins:[
       new cleanWebpackPlugin('build/js/*.js')
    ]
}

如此即可保证build/js目录下始终只有最新打包生成的js文件。

var cleanWebpackPlugin = require("clean-webpack-plugin")
var htmlWebpackPlugin = require("html-webpack-plugin")
module.exports = {
    entry: __dirname +'/src/js/main.js',
    output:{
        path: __dirname + '/build/js',
        filename:'[name]-[hash].js'
    },
    plugins:[
       new cleanWebpackPlugin('build/js/*.js'),
       new htmlWebpackPlugin({
           template: 'index.html'
       })
    ]
}

如此打包之后,在build/js目录下就可以看到一个index.html,该文件引入了打包后的js文件。打开该html文件就可以看到图4的效果。

var cleanWebpackPlugin = require("clean-webpack-plugin")
var htmlWebpackPlugin = require("html-webpack-plugin")
var webpack = require('webpack');
module.exports = {
    entry: __dirname +'/src/js/main.js',
    output:{
        path: __dirname + '/build/js',
        filename:'[name]-[hash].js'
    },
    plugins:[
       new cleanWebpackPlugin('build/js/*.js'),
       new htmlWebpackPlugin({
           template: 'index.html'
       }),
       new webpack.optimize.UglifyJsPlugin()
    ]
}

此时打包后的文件是压缩的。

4: webpack的loader说明。

html body {
    background: red;
}

然后在我们的main.js里面引入该css文件。

var hello = require('./hello')
require('./../css/main.css')
hello('I love the world....')

最后打包,我们会发现报错,webpack无法处理css文件,此时就需要我们的loader登场了。安装下载css-loaderstyle-loader,然后在webpack.config.js中进行配置:

var cleanWebpackPlugin = require("clean-webpack-plugin")
var htmlWebpackPlugin = require("html-webpack-plugin")
var webpack = require('webpack');
module.exports = {
    entry: __dirname +'/src/js/main.js',
    output:{
        path: __dirname + '/build/js',
        filename:'[name]-[hash].js'
    },
    plugins:[
       new cleanWebpackPlugin('build/js/*.js'),
       new htmlWebpackPlugin({
           template: 'index.html'
       }),
       new webpack.optimize.UglifyJsPlugin()
    ],
    module:{
        rules:[
            {
                test: /\.css/,
                use: ['style-loader', 'css-loader']
            }
        ]
    }
}

此时在打包就成功了,此时打开build目录下的index.html文件,我们发现css生效了。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <h3>hello world</h3>
    <img src="${require('./src/img/1.jpg')}" alt="">
</body>
</html>

main.js:

var hello = require('./hello')
require('./../css/main.css')
require('./../img/1.jpg')
hello('I love the world....')

webpack.config.js

var cleanWebpackPlugin = require("clean-webpack-plugin")
var htmlWebpackPlugin = require("html-webpack-plugin")
var webpack = require('webpack');
module.exports = {
    entry: __dirname +'/src/js/main.js',
    output:{
        path: __dirname + '/build/js',
        filename:'[name]-[hash].js'
    },
    plugins:[
       new cleanWebpackPlugin('build/js/*.js'),
       new htmlWebpackPlugin({
           template: 'index.html'
       }),
       new webpack.optimize.UglifyJsPlugin()
    ],
    module:{
        rules:[
            {
                test: /\.css/,
                use: ['style-loader', 'css-loader']
            },
            {
                test: /\.png|jpg|svg|gif/i,
                use: ['file-loader?name=[name]-[hash].[ext]']
            }
        ]
    }
}

安装file-loader,然后进行打包,此时在我们的build/js目录下会有一个打包后的图片,并且打包后的index.html文件也正确的引入了该图片。
打包结果:

图6
此时打开build目录下的index.html文件,发现运行正常。
图7

5: 总结。

以上只是webpack的简单使用,关于webpack的更多介绍请去官网查看,包括各种插件和loader的使用。

上一篇下一篇

猜你喜欢

热点阅读