zoukankan      html  css  js  c++  java
  • webpack + less

    使用less需要安装 'style-loader','css-loader','less-loader' 三个loader。

    安装之后在webpack.config.js配置

    const path = require('path');
    const HtmlWebpackPlugin = require('html-webpack-plugin');
    const CleanWebpackPlugin = require('clean-webpack-plugin');
    module.exports = {
      entry: {//入口文件
        app: './src/index.js'
      },
      devtool: 'inline-source-map',
      devServer: {
        contentBase: './dist'
      },
      plugins:[
        new CleanWebpackPlugin(['dist']),
        new HtmlWebpackPlugin({
           title: 'Output Management'
        }),
      ],
      output: {//输出文件
        filename: '[name].bundle.js',
        path: path.resolve(__dirname, 'dist'),
        publicPath: '/'
      },
      module: {
        rules: [
          {
             test: /.(png|svg|jpg|gif)$/,
             use: [
               'file-loader'
             ]
          },
          {
             test: /.(less|css)$/,
             use: [
               'style-loader',
               'css-loader',
               'less-loader'
             ]
          },
        ]
      }
    };
    View Code

    执行命令,这个时候会发现样式写在页面<head>标签的里面。

    如果样式少,这样看着还可以,但是多的话,还是外链的方式比较好,所以我们用 extract-text-webpack-plugin 来进行处理,先安装,接着继续修改配置。

    const path = require('path');
    const HtmlWebpackPlugin = require('html-webpack-plugin');
    const CleanWebpackPlugin = require('clean-webpack-plugin');
    const ExtractTextPlugin = require('extract-text-webpack-plugin');
    module.exports = {
      entry: {//入口文件
        app: './src/index.js'
      },
      devtool: 'inline-source-map',
      devServer: {
        contentBase: './dist'
      },
      plugins:[
        new CleanWebpackPlugin(['dist']),
        new HtmlWebpackPlugin({
           title: 'Output Management'
        }),
        new ExtractTextPlugin({
          filename: 'index.css',
          disable: false,
          allChunks: true,
        }),
      ],
      output: {//输出文件
        filename: '[name].bundle.js',
        path: path.resolve(__dirname, 'dist'),
        publicPath: '/'
      },
      module: {
        rules: [
          {
             test: /.(png|svg|jpg|gif)$/,
             use: [
               'file-loader'
             ]
          },
          {
             test: /.(less|css)$/,
             use: ExtractTextPlugin.extract({
              use:[ 'css-loader','less-loader'],
              fallback: 'style-loader',
            }),
          },
        ]
      }
    };
    View Code

    这时候执行命令结束之后,就会达到我们想要的结果。

  • 相关阅读:
    27 Spring Cloud Feign整合Hystrix实现容错处理
    26 Spring Cloud使用Hystrix实现容错处理
    25 Spring Cloud Hystrix缓存与合并请求
    24 Spring Cloud Hystrix资源隔离策略(线程、信号量)
    23 Spring Cloud Hystrix(熔断器)介绍及使用
    22 Spring Cloud Feign的自定义配置及使用
    21 Spring Cloud使用Feign调用服务接口
    20 Spring Cloud Ribbon配置详解
    19 Spring Cloud Ribbon自定义负载均衡策略
    18 Spring Cloud Ribbon负载均衡策略介绍
  • 原文地址:https://www.cnblogs.com/vipp/p/9896556.html
Copyright © 2011-2022 走看看