Tag Archives: webpack

OTS parsing error: invalid version tag [How to Solve]

Target: package font file png|woff|woff2|svg|ttf|eot

Tools: use the webpack tool

Foundation: suitable for developers with node foundation

Plug in package to download

css-loader loader for parsing css files
file-loader loader for processing files
html-webpack-plugin plugin for creating an html page in virtual memory”

style-loader A loader that inserts the parsed css style file into the style and places the head tag
url-loader A loader that parses the url path
webpack A tool for compiling code
webpack-cli provides developers with a flexible set of commands to Improve speed when defining webpack projects
webpack-dev-server dev virtual server server

Static compilation: webpack

Dynamic compilation [enable dev to access through HTTP protocol]: NPM run dev


1. Download the iconfont font package from the website

2. Build CSS files according to the prompts in the font file package

3. Import HTML file test

[this step is omitted, and there are cases in the font package]


Configure man.js

//import the example
import './css/index.css';

index.htm

<!DOCTYPE html>
<html lang="zh">
<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></title>
	<!-- <link rel="stylesheet" href="./css/index.css" /> -->
</head>
<body>
	<span class="iconfont afont">&#xe60d;</span>
	<span class="iconfont afont">&#xe60c;</span>
	<span class="iconfont afont">&#xe637;</span>
	<span class="iconfont afont">&#xe63b;</span>
	<span class="iconfont afont">&#xe620;</span>
</body>
</html>

index.css

@font-face {
  font-family: 'iconfont';
  src: url('./icon/iconfont.ttf?t=1650505701581') format('truetype');
}
.iconfont {
  font-family: "iconfont" !important;
  font-size: 16px;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

span{
	height: 100px;
	width: 100px;
	/* display: block; */

}
.afont{
	font-size: 150px;	
}

Project initialization

Module download is not cumbersome here

Key: Type: ‘JavaScript/auto’// you must specify the type. Although you don’t know what, it seems that you must specify the type before which version to display that the loading is normal. If you know, please tell us the specific reasons in the comment area below.

No type specified: ‘JavaScript/auto’

Failed to decode downloaded font: http://localhost:3000/36bdff0eed0ef8904943.ttf?t=1650505701581
localhost/:1 OTS parsing error: invalid version tag

webpack.config..js file configuration [complete solution]


const {resolve} = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
	entry:'./src/main.js',
	output:{
		filename:'bundle.js',
		path:resolve(__dirname,'dist')
	},
	module:{
		rules:[
			{test:/\.css$/, use:['style-loader','css-loader']},//css
			// {exclude: /\.(css|js|html)$/, //Exclude the packaging of other resources (in addition to html js css resources unexpected resources) this static packaging font file
			// loader:'file-loader'
			// },
 //{test:/\.(png|woff|woff2|svg|ttf|eot)$/, use:'url-loader?esModule=false&limit=10*1024&name=[hash:8]-[name].[ext]' ,type: 'javascript/auto'},
			{test:/\.(png|woff|woff2|svg|ttf|eot)$/,
			loader:'url-loader',
			options: {
				limit: 10*1024,  //This should be large enough so that all the font icons are packed into the css
				esModule:false,
				name:"[hash:8]-[name]. [ext]"
				},
			// type: 'javascript/auto' // must specify type
			}
			]
			},
	plugins:[
		new HtmlWebpackPlugin({
			template:'./src/index.html',
			filename:'index.html'
		})
	],
	mode:'development'
}

Start the virtual server with NPM run dev

Enter http://localhost:3000 in the browser address and the iconfont font file of the page is displayed normally


vue.config.js build Package UglifyJsPlugin to clear console and print console.log Error: `warnings` is not a supported option

We want to log out all the printing functions. It is certainly unrealistic to log out one by one. It is a waste of energy and time, and there will be omissions

Here we implement it with the help of uglifyjs-webpack-plugin

install

cnpm i uglifyjs-webpack-plugin --save-dev

vue.config.js

// vue.config.js

const UglifyJsPlugin = require('uglifyjs-webpack-plugin')

module.exports = {
...
configureWebpack: config => {
    if (process.env.NODE_ENV == 'production') {
    
      config.mode = 'production'
      config.optimization.minimizer = [
        new UglifyJsPlugin({
          uglifyOptions: {
			output: {
				comments: false
			},
			// deleteconsole debugger
			compress: {     
				warnings: false,	//	error
				drop_console: true, // console
				drop_debugger: false,
				pure_funcs: ['console.log']// 移除console
			}
          }
        })
      ]
    } else {
      config.mode = 'development'
    }
   }


...

}

warnings is not a supported option

Here is a knowledge point. Warnings will report errors.

Some UglifyJsPlugin versions do not support setting the warnings parameter in the compress of the uglifyOptions object, but directly set warnings as a property in the uglifyOptions object.

If an error is reported. Then write warnings as the uglifyOptions attribute in the following way

// vue.config.js

const UglifyJsPlugin = require('uglifyjs-webpack-plugin')

module.exports = {
...
configureWebpack: config => {
    if (process.env.NODE_ENV == 'production') {
    
      config.mode = 'production'
      config.optimization.minimizer = [
        new UglifyJsPlugin({
          uglifyOptions: {
			output: {
				comments: false
			},
			// delete console debugger
			compress: {     
				drop_console: true, // console
				drop_debugger: false,
				pure_funcs: ['console.log']// remove console
			}, 
			warnings: false,	//	error
          }
        })
      ]
    } else {
      config.mode = 'development'
    }
   }


...

}

[Solved] npm Error: Error: Cannot find module ‘postcss-loader‘

1. Problem
an error is reported when executing the command NPM run serve:

Error: Cannot find module 'postcss-loader'

2. Solution

The most thorough solution is to uninstall the current nodejs version and install the nodejs version consistent with the module version.

If you don’t want to reinstall nodejs, you can use the following methods:
install module:

npm install postcss-loader

If you continue to report errors:

npm ERR! Could not resolve dependency:
npm ERR! postcss-loader@"*" from the root project
npm ERR!
npm ERR! Conflicting peer dependency: [email protected]
npm ERR! node_modules/webpack
npm ERR!   peer webpack@"^5.0.0" from [email protected]
npm ERR!   node_modules/postcss-loader
npm ERR!     postcss-loader@"*" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.

Take the – force or — legacy-peer-deps parameter after the command:

npm install postcss-loader --legacy-peer-deps

[Solved] Vue Start Project Error: ValidationError: webpack Dev Server Invalid Options

#Vue project development error
premise: during the development of Vue project, error: validationerror: webpack dev server invalid options appear when the project is started

problem analysis:
according to the problem description, the value of the “agent” object used to solve cross domain problems in the configuration file is empty

Solution:
find webpack.config.JS or Vue.config.JS
then find devserver and comment out the proxy or fill in the complete configuration

webpack4 Use webpack-dev-server Error [How to Solve]

Today, I reported an error with webpack dev server in the project to visually check the version problem
the webpack version used in the project is "webpack": "^ 4.28.4"
the default is "webpack dev server": "^ 4.7.3" , and the reduced version is "webpack dev server": "^ 3.1.14" . The problem is solved

Then start the error report:
check the data and find that the startup command is changed to NPX webpack server -- config webpack dev.js

[Solved] webpack Package Error: TypeError: this.getOptions is not a function style-loader

I used style-loader in the project today, and found an error in packaging: typeerror: this Getoptions is not a function
visual inspection is a version problem. The default version of style-loader is 3.3.1. The reduced version is 2.0.0, and the problem is solved
PS: the webpack version used is 4.28.4

Vue Package Image Error: Syntax Error: Error: ‘**\node_modules\pngquant-bin\vendor\pngquant.exe‘

When packaging, an error is displayed at a picture path
Syntax Error: Error: ‘D:\work\tiny-sample\src\frontend\web\packages\src-entry\node_modules\pngquant-bin\vendor\pngquant.exe’

It can be seen that the package pngquant-bin reports an error, and the package is derived from the dependency of image-webpack-loader, so the root cause is image-webpack-loader.
Solution: delete the dependency image-webpack-loader, and then reinstall it. If it doesn’t work, try a few more times.

npm uninstall image-webpack-loader
npm install image-webpack-loader

If it still fails, set the image for NPM and repeat the above two steps

[Solved] Vue Project Error: Error: Cannot find module ‘webpack‘ Require stack以及Error: Rule can only have one resource so

Error 1: NPM run serve reports an error when Vue project is started: error: cannot find module ‘webpack’ require stack

After the project is pulled down and the dependencies are installed normally, start the project and report error: cannot find module ‘webpack’ require stack. It is found that the webpack module is missing,

Error: Cannot find module 'webpack'
Require stack:
  at Function.Module._load (internal/modules/cjs/loader.js:725:27)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)

However, I have already installed webpack. Here, I need to re open CMD to install webpack globally

reason:

even if you installed webpack before, it must be a non global installation

Solution:

npm install  -g webpack

After the installation is complete, I start the project and find that it reports an error again!
Reported error: Error: Rule can only have one resource source (provided resource and test + include + exclude) in


Error 2: Project Error: Error: Rule can only have one resource source (provided resource and test + include + exclude) in

Error: Rule can only have one resource source (provided resource and test + include + exclude) in {
  "exclude": [
    null
  ]

reason:

package. Webpack version conflict in JSON

Solution::

delete the webpack and reinstall the previous version

npm uninstall webpack
npm install webpack@^4.0.0 --save-dev

How to Solve Webpack packag iconfont font error

ERROR in ./src/fontsize/iconfont.css 1:0
Module parse failed: Unexpected character '@' (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> @font-face {
|   font-family: "iconfont"; /* Project id 2969850 */
|   src: url('iconfont.woff2?t=1638862344321') format('woff2'),
 @ ./src/index.js 2:0-32

 

 

Solution: The webpack.config.js file is wrong, the configuration file should be