Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

encoding-plugin

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

encoding-plugin - npm Package Compare versions

Comparing version 1.2.0 to 2.0.0

CHANGELOG.md

74

index.d.ts

@@ -1,38 +0,68 @@

import * as webpack from 'webpack';
import { WebpackPluginInstance, Compiler } from 'webpack';
export = EncodingPlugin;
/**
* Take control over the encoding of emitted `webpack` assets.
*/
declare class EncodingPlugin implements WebpackPluginInstance {
constructor(options?: EncodingPlugin.Options);
apply(compiler: Compiler): void;
}
declare namespace EncodingPlugin {
/** Filtering rule as regex or string */
type Rule = string | RegExp;
/** Filtering rules */
type Rules = Rule | ReadonlyArray<Rule>;
interface FileInfo {
/** original asset filename */
file: string;
query: string;
fragment: string;
/** path of the original asset */
path: string;
base: string;
name: string;
ext: string;
}
type FilenameFunction = (pathData: FileInfo) => string;
interface Options {
/**
* Target encoding https://github.com/ashtuchkin/iconv-lite/wiki/Supported-Encodings
* Target encoding. A list of supported encodings can be found here: https://github.com/ashtuchkin/iconv-lite/wiki/Supported-Encodings
*/
encoding: string;
/**
* RegExp or array of RegExps to filter processed files
*
* Default: /(\.js|\.css)($|\?)/i
* Include all assets that pass test assertion.
*/
test?: RegExp | RegExp[];
test?: Rules;
/**
* RegExp or array of RegExps to filter processed files
* Include all assets matching any of these conditions.
*/
include?: Rules;
/**
* Exclude all assets matching any of these conditions.
*/
exclude?: Rules;
/**
* The target asset filename.
*/
filename?: string | FilenameFunction;
/**
* Whether to replace `utf-8` to target encoding from `webpack` runtime code or not.
*
* Default: undefined
* @default true
*/
include?: RegExp | RegExp[];
patchWebpackBootstrap?: boolean;
/**
* RegExp or array of RegExps to filter processed files
* Whether to delete the original assets or not.
*
* Default: undefined
* @default true
*/
exclude?: RegExp | RegExp[];
deleteOriginalAssets?: boolean | 'keep-source-map';
}
}
declare class EncodingPlugin extends webpack.Plugin {
constructor(encodingOrOptions: string | EncodingPlugin.Options);
apply(compiler: webpack.Compiler): void;
}
export = EncodingPlugin;
{
"name": "encoding-plugin",
"version": "1.2.0",
"version": "2.0.0",
"description": "Control Webpack output encoding",
"main": "EncodingPlugin.js",
"license": "MIT",
"repository": "dromru/encoding-plugin",
"author": "Ivan Rudoy <rudooy@gmail.com>",
"homepage": "https://github.com/dromru/encoding-plugin",
"bugs": "https://github.com/dromru/encoding-plugin/issues",
"main": "dist/cjs.js",
"types": "index.d.ts",
"engines": {
"node": ">= 10.13.0"
},
"scripts": {
"test": "jest",
"start": "yarn build -- -w",
"clean": "rimraf dist",
"prebuild": "yarn clean",
"build": "cross-env NODE_ENV=production babel src -d dist --copy-files",
"test:only": "cross-env NODE_ENV=test jest",
"test:watch": "yarn test:only --watch",
"test:coverage": "yarn test:only --collectCoverageFrom=\"src/**/*.js\" --coverage",
"pretest": "yarn lint",
"test": "yarn test:coverage",
"prepublish": "yarn build",
"lint": "eslint",
"lint:all": "yarn lint '**/*.js'"
"lint:all": "yarn lint '**/*.js'",
"commit": "git-cz",
"prettify": "prettier",
"release": "standard-version"
},
"repository": {
"type": "git",
"url": "https://github.com/dromru/encoding-plugin.git"
"files": [
"dist/**/*",
"index.d.ts"
],
"peerDependencies": {},
"dependencies": {
"encoding": "^0.1.13",
"schema-utils": "^3.0.0",
"serialize-javascript": "^5.0.1"
},
"devDependencies": {
"@babel/cli": "^7.12.10",
"@babel/core": "^7.12.10",
"@babel/preset-env": "^7.12.11",
"@commitlint/cli": "^11.0.0",
"@commitlint/config-conventional": "^11.0.0",
"@commitlint/prompt": "^11.0.0",
"commitizen": "^4.2.2",
"cross-env": "^7.0.3",
"css-loader": "^5.0.1",
"eslint": "7.17.0",
"eslint-config-airbnb-base": "14.2.1",
"eslint-config-prettier": "^7.1.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-prettier": "^3.3.1",
"husky": "^4.3.7",
"jest": "^26.6.3",
"lint-staged": "^10.5.3",
"memory-fs": "^0.5.0",
"mini-css-extract-plugin": "^1.3.4",
"prettier": "^2.2.1",
"standard-version": "^9.1.0",
"webpack": "^5.14.0"
},
"keywords": [

@@ -19,21 +70,49 @@ "webpack",

],
"author": "Ivan Rudoy <rudooy@gmail.com>",
"license": "MIT",
"devDependencies": {
"css-loader": "^3.2.0",
"eslint": "6.7.0",
"eslint-config-airbnb-base": "14.1.0",
"eslint-config-prettier": "^6.0.0",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-prettier": "^3.1.0",
"jest": "^24.8.0",
"memory-fs": "^0.5.0",
"mini-css-extract-plugin": "^0.9.0",
"prettier": "^2.0.0"
"husky": {
"hooks": {
"pre-commit": "yarn build && lint-staged",
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
},
"dependencies": {
"encoding": "^0.1.12",
"webpack": "^4.39.3",
"webpack-sources": "^1.4.1"
"config": {
"commitizen": {
"path": "@commitlint/prompt"
}
},
"standard-version": {
"types": [
{
"type": "feat",
"section": "Features"
},
{
"type": "fix",
"section": "Bug Fixes"
},
{
"type": "chore",
"section": "Chore"
},
{
"type": "perf",
"section": "Optimizations"
},
{
"type": "refactor",
"hidden": true
},
{
"type": "docs",
"hidden": true
},
{
"type": "test",
"hidden": true
},
{
"type": "style",
"hidden": true
}
]
}
}
# Webpack Encoding Plugin
[![Greenkeeper badge](https://badges.greenkeeper.io/dromru/encoding-plugin.svg)](https://greenkeeper.io/) [![CircleCI](https://circleci.com/gh/dromru/encoding-plugin.svg?style=svg)](https://circleci.com/gh/dromru/encoding-plugin) [![codecov](https://codecov.io/gh/dromru/encoding-plugin/branch/master/graph/badge.svg)](https://codecov.io/gh/dromru/encoding-plugin) [![npm](https://img.shields.io/npm/v/encoding-plugin.svg)](https://www.npmjs.com/package/encoding-plugin)
[![codecov](https://codecov.io/gh/dromru/encoding-plugin/branch/master/graph/badge.svg)](https://codecov.io/gh/dromru/encoding-plugin) [![npm](https://img.shields.io/npm/v/encoding-plugin.svg)](https://www.npmjs.com/package/encoding-plugin)
Take control over the encoding of emitted webpack assets.
Take control over the encoding of emitted `webpack` assets.
This can be useful, if the delivering webserver enforces a specific content-type,
so that your js-code is not interpreted as utf-8 by the browser.
so that your code is not interpreted as utf-8 by the browser.
## Usage
> ℹ️ **EncodingPlugin v2 only works with Webpack 5 and above. Use v1 for Webpack <= 4.**
Install package
## Getting Started
npm install encoding-plugin
Install package:
Setup webpack config
```console
$ yarn add --dev encoding-plugin
```
``` javascript
Add the plugin to your `webpack` config. For example:
``` js
const EncodingPlugin = require('encoding-plugin');
module.exports = {

@@ -28,12 +33,225 @@ plugins: [

Additional options:
## Options
`test`, `include`, `exclude` RegExp or array of RegExps to filter processed files
(default `test` is `/(\.js|\.css)($|\?)/i`)
| Name | Type | Default | Description |
| :---------------------------------------------------: | :---------------------------------------: | :-----------------------: | :----------------------------------------------------------------------------------- |
| **[`encoding`](#encoding)** | `{String}` | `undefined` | Target encoding |
| **[`test`](#test)** | `{String\|RegExp\|Array<String\|RegExp>}` | `/(\.js|\.css)(\?.*)?$/i` | Include all assets that pass test assertion |
| **[`include`](#include)** | `{String\|RegExp\|Array<String\|RegExp>}` | `undefined` | Include all assets matching any of these conditions |
| **[`exclude`](#exclude)** | `{String\|RegExp\|Array<String\|RegExp>}` | `undefined` | Exclude all assets matching any of these conditions |
| **[`filename`](#filename)** | `{String\|Function}` | `undefined` | The target asset filename |
| **[`patchWebpackBootstrap`](#patchWebpackBootstrap)** | `{Boolean}` | `true` | Whether to replace `utf-8` to target encoding from `webpack` runtime code or not |
| **[`deleteOriginalAssets`](#deleteoriginalassets)** | `{Boolean\|'keep-source-map'}` | `true` | Whether to delete the original assets or not |
## Encodings
### `encoding`
Type: `String`
Default: `undefined`
The Plugin uses [iconv-lite](https://www.npmjs.com/package/iconv-lite) to handle the encoding.
A list of supported encodings can be found [here](https://github.com/ashtuchkin/iconv-lite/wiki/Supported-Encodings)
**webpack.config.js**
``` js
module.exports = {
plugins: [
new EncodingPlugin({
encoding: 'iso-8859-1',
}),
],
};
```
### `test`
Type: `String|RegExp|Array<String|RegExp>`
Default: `/(\.js|\.css)(\?.*)?$/i`
Include all assets that pass test assertion.
**webpack.config.js**
```js
module.exports = {
plugins: [
new EncodingPlugin({
encoding: 'iso-8859-1',
test: /\.js(\?.*)?$/i,
}),
],
};
```
### `include`
Type: `String|RegExp|Array<String|RegExp>`
Default: `undefined`
Include all assets matching any of these conditions.
**webpack.config.js**
```js
module.exports = {
plugins: [
new EncodingPlugin({
encoding: 'iso-8859-1',
include: /\/includes/,
}),
],
};
```
### `exclude`
Type: `String|RegExp|Array<String|RegExp>`
Default: `undefined`
Exclude all assets matching any of these conditions.
**webpack.config.js**
```js
module.exports = {
plugins: [
new EncodingPlugin({
encoding: 'iso-8859-1',
exclude: /\/excludes/,
}),
],
};
```
### `filename`
Type: `String|Function`
Default: `undefined`
The target asset filename.
#### `String`
For example we have `assets/scripts/main.js?foo=bar#hash`:
`[path]` is replaced with the directories to the original asset, included trailing `/` (`assets/scripts/`).
`[file]` is replaced with the path of original asset (`assets/scripts/main.js`).
`[base]` is replaced with the base (`[name]` + `[ext]`) of the original asset (`main.js`).
`[name]` is replaced with the name of the original asset (`main`).
`[ext]` is replaced with the extension of the original asset, included `.` (`.js`).
`[query]` is replaced with the query of the original asset, included `?` (`?foo=bar`).
`[fragment]` is replaced with the fragment (in the concept of URL it is called `hash`) of the original asset (`#hash`).
**webpack.config.js**
```js
module.exports = {
plugins: [
new EncodingPlugin({
encoding: 'iso-8859-1',
filename: "[path][base].iso-8859-1[ext]",
}),
],
};
```
#### `Function`
**webpack.config.js**
```js
module.exports = {
plugins: [
new EncodingPlugin({
encoding: 'iso-8859-1',
filename(pathData) {
// The `pathData` argument contains all placeholders - `path`/`name`/`ext`/etc
// Available properties described above, for the `String` notation
if (/\.css$/.test(pathData.file)) {
return "assets/stylesheets/[path][base].iso-8859-1[ext]";
}
return "assets/scripts/[path][base].iso-8859-1[ext]";
},
}),
],
};
```
### `patchWebpackBootstrap`
Type: `Boolean`
Default: `true`
Whether to replace `utf-8` to target encoding from `webpack` runtime code or not.
**webpack.config.js**
```js
module.exports = {
plugins: [
new EncodingPlugin({
encoding: 'iso-8859-1',
patchWebpackBootstrap: true,
}),
],
};
```
Example Webpack runtime code:
**patchWebpackBootstrap: false**
```js
/******/ script = document.createElement('script');
/******/ script.charset = 'utf-8';
/******/ script.timeout = 120;
```
**patchWebpackBootstrap: true**
```js
/******/ script = document.createElement('script');
/******/ script.charset = 'iso-8859-1';
/******/ script.timeout = 120;
```
### `deleteOriginalAssets`
Type: `Boolean | 'keep-source-map'`
Default: `true`
Whether to delete the original assets or not.
**webpack.config.js**
```js
module.exports = {
plugins: [
new EncodingPlugin({
encoding: 'iso-8859-1',
deleteOriginalAssets: false,
}),
],
};
```
To exclude sourcemaps from encoding
```js
module.exports = {
plugins: [
new EncodingPlugin({
exclude: /.map$/
deleteOriginalAssets: 'keep-source-map',
}),
],
};
```
## webpack-dev-server

@@ -52,1 +270,5 @@

```
## License
MIT

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc