热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

cssloader往CSS中添加了,但是html中没有添加对应的hash,导致两者对应不上

问题出现的环境背景及自己尝试过哪些方法

问题出现的环境背景及自己尝试过哪些方法

1
2
目前在整理自己的学习目录时碰到一个问题:使用css-loader中localIdentName: [name]__[local]--[hash:base64:5]为样式添加hash等,最后添加成功了,但是HTML模版里的却没有添加成功。只能使用[local]了。

也使用了HtmlWebpackPlugin,但是无效

图片描述

相关代码

webpack.base.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const path = require('path');

const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin')



module.exports = {

  context: path.resolve(__dirname, '../../'),

  output: {

    publicPath: './',

    path: path.resolve(__dirname, '../../dist'), // 打包后的文件存放的地方

    filename: '[name].[hash:8].js'// 打包后输出文件的文件名

  },

  resolve: {

    extensions: ['.js', '.vue', '.json'],

    alias: {

      vue$: 'vue/dist/vue.esm.js',

      '@': path.resolve('src')

    }

  },

  module: {

    rules: [

      {

        test: /(\.html|\.xml)$/,

        use: [

          {

            loader: 'html-loader',

          }

        ],

        exclude: /node_modules/

      },

      {

        test: /(\.jsx|\.js)$/,

        use: {

          loader: 'babel-loader',

          options: {

            presets: [

              'env',

              'react'

            ]

          }

        },

        exclude: /node_modules/

      },

      {

        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,

        loader: 'url-loader',

        options: {

          limit: 10000,

          name: "[name].[hash:7].[ext]",

          publicPath: "./images/", // 打包后CSS引入的基础路径

          outputPath: "images/" // 打包后输出目录

        }

      },

      {

        test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,

        loader: 'url-loader',

        options: {

          limit: 10000,

          name: 'media/[name].[hash:7].[ext]'

        }

      },

      {

        test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,

        loader: 'url-loader',

        options: {

          limit: 10000,

          name: 'fonts/[name].[hash:7].[ext]'

        }

      }

    ]

  },

  plugins: [

    new OptimizeCSSAssetsPlugin({}),

  ],

};

webpack.pro.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const webpack = require('webpack');

const program = require('commander');

const HtmlWebpackPlugin = require('html-webpack-plugin');

const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin');

const CleanWebpackPlugin = require('clean-webpack-plugin');

const path = require('path');

const baseCOnfig= require('../config/webpack.base.config');

const merge = require('webpack-merge');

const ora = require('ora');

const MiniCssExtractPlugin = require("mini-css-extract-plugin");



const spinner = ora({

  spinner: {

    frames:['←','↑','→','↓'],

    interval: 80,

  },

  color: 'red',

  text: 'Loading...',

});



program

  .command('project [file]')

  .action((project, file) => {

    const entry = path.resolve(__dirname, '../../', 'projects', project, file, 'main.js');

    const inlineCOnfig= merge(baseConfig, {

      entry: function setEntry() {

        return entry; // 入口文件

      },

      mode: 'production',

      module: {

        rules: [

          {

            test: /\.(sa|sc|c)ss$/,

            use: [

              {

                loader: "style-loader"

              },

              {

                loader: MiniCssExtractPlugin.loader,

              },

              {

                loader: "css-loader",

                options: {

                  modules: true, // 指定使用CSS modules

                  localIdentName: '[name]__[local]--[hash:base64:5]' // 指定css的类名格式

                }

              },

              {

                loader: "postcss-loader",

                options: {           // 如果没有options这个选项将会报错 No PostCSS Config found

                  config: {

                    path: './'

                  }

              }

              }

            ]

          }

        ]

      },

      plugins: [

        new MiniCssExtractPlugin({

          filename: "[name].[chunkhash:8].css",

          chunkFilename: "[id].css"

        }),

        new HtmlWebpackPlugin({

          template: path.resolve(__dirname,'../../', 'projects', project, file, 'index.html'),// template

          minify: {

              removeAttributeQuotes:true

            },

            hash: true,

            filename:'index.html'

        }),

        new FriendlyErrorsPlugin({

          compilationSuccessInfo: {

            messages: ['Your application build successed\n'],

          },

        }),

        new CleanWebpackPlugin('../../dist', {

          root: __dirname,

          verbose: true,

          dry: false

        })

      ],

    });

    spinner.start();

    webpack(inlineConfig, (err, stats) => {

      spinner.stop();

      if (err) {

        console.log(err);

      }

      console.log('build successed');

    });

  });



program.parse(process.argv);

webpack.dev.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const webpack = require('webpack');

const program = require('commander');

const HtmlWebpackPlugin = require('html-webpack-plugin');

const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin');

const path = require('path');

const baseCOnfig= require('../config/webpack.base.config');

const serverCOnfig= require('../server/devServer');

const merge = require('webpack-merge');

const webpackDevMiddleware = require('webpack-dev-middleware');

const webpackHotMiddleware = require('webpack-hot-middleware');

const express = require('express');

const opn = require('opn');



const app = express();



let entry;



program

  .command('project [file]')

  .action((project, file) => {

    entry = path.resolve(__dirname, '../../', 'projects', project, file, 'main.js');

    const inlineCOnfig= merge(baseConfig, {

      entry: function setEntry() {

        return [entry, 'webpack-hot-middleware/client?reload=true']; // 入口文件

      },

      mode: 'development',

      devtool: 'source-map',

      devServer: serverConfig,

      module: {

        rules: [

          {

            test: /\.(sa|sc|c)ss$/,

            use: [

              {

                loader: "style-loader"

              },

              {

                loader: "css-loader",

                options: {

                  modules: true, // 指定使用CSS modules

                  localIdentName: '[name]__[local]--[hash:base64:5]' // 指定css的类名格式

                }

              },

              {

                loader: "postcss-loader",

                options: {           // 如果没有options这个选项将会报错 No PostCSS Config found

                  config: {

                    path: './'

                  }

              }

              }

            ]

          }

        ]

      },

      plugins: [

        new webpack.HotModuleReplacementPlugin(),

        new FriendlyErrorsPlugin({

          compilationSuccessInfo: {

            messages: [`Your application is running: http://${serverConfig.host}:${serverConfig.port}\n`],

          },

        }),

        new HtmlWebpackPlugin({

          template: path.resolve(__dirname,'../../', 'projects', project, file, 'index.html')// template

        }),

      ],

    });

    const compiler = webpack(inlineConfig);



    app.use(webpackDevMiddleware(compiler, {

      logLevel: 'error',

      progress: true,

      logTime: true,

    }));

    app.use(webpackHotMiddleware(compiler, {

      noInfo: true,

      log: false,

      heartbeat: 2000,

    }));

    app.listen(3000);

    // opn('http://localhost:3000');

  });



program.parse(process.argv);



推荐阅读
  • Webpack5内置处理图片资源的配置方法
    本文介绍了在Webpack5中处理图片资源的配置方法。在Webpack4中,我们需要使用file-loader和url-loader来处理图片资源,但是在Webpack5中,这两个Loader的功能已经被内置到Webpack中,我们只需要简单配置即可实现图片资源的处理。本文还介绍了一些常用的配置方法,如匹配不同类型的图片文件、设置输出路径等。通过本文的学习,读者可以快速掌握Webpack5处理图片资源的方法。 ... [详细]
  • VueCLI多页分目录打包的步骤记录
    本文介绍了使用VueCLI进行多页分目录打包的步骤,包括页面目录结构、安装依赖、获取Vue CLI需要的多页对象等内容。同时还提供了自定义不同模块页面标题的方法。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文记录了在vue cli 3.x中移除console的一些采坑经验,通过使用uglifyjs-webpack-plugin插件,在vue.config.js中进行相关配置,包括设置minimizer、UglifyJsPlugin和compress等参数,最终成功移除了console。同时,还包括了一些可能出现的报错情况和解决方法。 ... [详细]
  • 本文讨论了将HashRouter改为Router后,页面全部变为空白页且没有报错的问题。作者提到了在实际部署中需要在服务端进行配置以避免刷新404的问题,并分享了route/index.js中hash模式的配置。文章还提到了在vueJs项目中遇到过类似的问题。 ... [详细]
  • 本文涉及源码版本为2.6.9准备工作down一份Vue源码,从package.json入手,找我们需要的代码1、package.json中的scripts,build:nodesc ... [详细]
  • 本文讨论了在手机移动端如何使用HTML5和JavaScript实现视频上传并压缩视频质量,或者降低手机摄像头拍摄质量的问题。作者指出HTML5和JavaScript无法直接压缩视频,只能通过将视频传送到服务器端由后端进行压缩。对于控制相机拍摄质量,只有使用JAVA编写Android客户端才能实现压缩。此外,作者还解释了在交作业时使用zip格式压缩包导致CSS文件和图片音乐丢失的原因,并提供了解决方法。最后,作者还介绍了一个用于处理图片的类,可以实现图片剪裁处理和生成缩略图的功能。 ... [详细]
  • 本文介绍了如何使用Express App提供静态文件,同时提到了一些不需要使用的文件,如package.json和/.ssh/known_hosts,并解释了为什么app.get('*')无法捕获所有请求以及为什么app.use(express.static(__dirname))可能会提供不需要的文件。 ... [详细]
  • 用Vue实现的Demo商品管理效果图及实现代码
    本文介绍了一个使用Vue实现的Demo商品管理的效果图及实现代码。 ... [详细]
  • 在package.json中有如下两个对象:husky:{hooks:{pre-commit:lint-staged}},lint-staged:{src** ... [详细]
  • 本文介绍了自学Vue的第01天的内容,包括学习目标、学习资料的收集和学习方法的选择。作者解释了为什么要学习Vue以及选择Vue的原因,包括完善的中文文档、较低的学习曲线、使用人数众多等。作者还列举了自己选择的学习资料,包括全新vue2.5核心技术全方位讲解+实战精讲教程、全新vue2.5项目实战全家桶单页面仿京东电商等。最后,作者提出了学习方法,包括简单的入门课程和实战课程。 ... [详细]
  • VUE中引用路径的配置
    在vue项目开发中经常引用JS、CSS、IMG文件。当项目较大时文件层级很多,导致路径很长,我们可以通过在bulidwebpack.base.conf.js设置简便的引用路径一、 ... [详细]
  • loader资源模块加载器webpack资源模块加载webpack内部(内部loader)默认只会处理javascript文件,也就是说它会把打包过程中所有遇到的 ... [详细]
  • 1223  drf引入以及restful规范
    [toc]前后台的数据交互前台安装axios插件,进行与后台的数据交互安装axios,并在main.js中设置params传递拼接参数data携带数据包参数headers中发送头部 ... [详细]
  • 加密、解密、揭秘
    谈PHP中信息加密技术同样是一道面试答错的问题,面试官问我非对称加密算法中有哪些经典的算法?当时我愣了一下,因为我把非对称加密与单项散列加 ... [详细]
author-avatar
小豇豆和花椰菜
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有