5. webpack处理不需要处理的资源(如字体图标)

以打包字体图标为例

  • 使用的loader:file-loader
  • 图标库是阿里图标库下载的
  1. 下载file-loader (还需要安装html-webpack-plugin,css-loader, style-loader)

    npm i file-loader -D
    
  2. 创建入口文件(src/index.js)并引入图标库的入口文件

    import './css/iconfont.css'
    
  3. 创建模板文件(src/index.html)

    <p class="iconfont icon-shang"></p>
    <p class="iconfont icon-suo"></p>
    <p class="iconfont icon-suo1"></p>
    
  4. 配置webpack的配置文件webpack.config.js

    const path = require('path');
    
    const HtmlWebpackPlugin = require('html-webpack-plugin')
    
    module.exports = {
        mode: 'development',
        entry: './src/index.js',
        output: {
            filename: './built.js',
            path: path.resolve(__dirname, 'build')
        },
        module: {
            rules: [{
                    test: /\.css$/,
                    use: ['style-loader', 'css-loader']
                },
                {
                    //test表示匹配,explode表示排除
                    exclude: /\.(css|js|html)$/,
                    loader: 'file-loader'
                }
            ]
        },
        plugins: [
            new HtmlWebpackPlugin({
                template: './src/index.html'
            })
        ]
    }
    
  5. 打包

    webpack
    
  6. 在输出文件夹build中可以看到index.html文件然后运行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值