Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: prettier default #220

Merged
merged 1 commit into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
root: true,
extends: ['@webpack-contrib/eslint-config-webpack', 'prettier'],
extends: ["@webpack-contrib/eslint-config-webpack", "prettier"],
};
1 change: 0 additions & 1 deletion .prettierrc.js

This file was deleted.

30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Then add the plugin to your `webpack` config. For example:
**webpack.config.js**

```js
const CompressionPlugin = require('compression-webpack-plugin');
const CompressionPlugin = require("compression-webpack-plugin");

module.exports = {
plugins: [new CompressionPlugin()],
Expand Down Expand Up @@ -129,7 +129,7 @@ The algorithm is taken from [zlib](https://nodejs.org/api/zlib.html).
module.exports = {
plugins: [
new CompressionPlugin({
algorithm: 'gzip',
algorithm: "gzip",
}),
],
};
Expand Down Expand Up @@ -259,7 +259,7 @@ For example we have `assets/images/image.png?foo=bar#hash`:
module.exports = {
plugins: [
new CompressionPlugin({
filename: '[path][base].gz',
filename: "[path][base].gz",
}),
],
};
Expand All @@ -277,10 +277,10 @@ module.exports = {
// The `pathData` argument contains all placeholders - `path`/`name`/`ext`/etc
// Available properties described above, for the `String` notation
if (/\.svg$/.test(pathData.file)) {
return 'assets/svg/[path][base].gz';
return "assets/svg/[path][base].gz";
}

return 'assets/js/[path][base].gz';
return "assets/js/[path][base].gz";
},
}),
],
Expand Down Expand Up @@ -355,7 +355,7 @@ Enable file caching and set path to cache directory.
module.exports = {
plugins: [
new CompressionPlugin({
cache: 'path/to/cache',
cache: "path/to/cache",
}),
],
};
Expand All @@ -378,7 +378,7 @@ $ npm install @gfx/zopfli --save-dev
**webpack.config.js**

```js
const zopfli = require('@gfx/zopfli');
const zopfli = require("@gfx/zopfli");

module.exports = {
plugins: [
Expand All @@ -405,13 +405,13 @@ We can take advantage of this built-in support for Brotli in Node 10.16.0 and la
**webpack.config.js**

```js
const zlib = require('zlib');
const zlib = require("zlib");

module.exports = {
plugins: [
new CompressionPlugin({
filename: '[path][base].br',
algorithm: 'brotliCompress',
filename: "[path][base].br",
algorithm: "brotliCompress",
test: /\.(js|css|html|svg)$/,
compressionOptions: {
params: {
Expand All @@ -434,20 +434,20 @@ You can find all Brotli’s options in [the relevant part of the zlib module doc
**webpack.config.js**

```js
const zlib = require('zlib');
const zlib = require("zlib");

module.exports = {
plugins: [
new CompressionPlugin({
filename: '[path][base].gz',
algorithm: 'gzip',
filename: "[path][base].gz",
algorithm: "gzip",
test: /\.js$|\.css$|\.html$/,
threshold: 10240,
minRatio: 0.8,
}),
new CompressionPlugin({
filename: '[path][base].br',
algorithm: 'brotliCompress',
filename: "[path][base].br",
algorithm: "brotliCompress",
test: /\.(js|css|html|svg)$/,
compressionOptions: {
params: {
Expand Down
4 changes: 2 additions & 2 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ module.exports = (api) => {
return {
presets: [
[
'@babel/preset-env',
"@babel/preset-env",
{
targets: {
node: '10.13.0',
node: "10.13.0",
},
},
],
Expand Down
2 changes: 1 addition & 1 deletion commitlint.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
extends: ['@commitlint/config-conventional'],
extends: ["@commitlint/config-conventional"],
};
4 changes: 2 additions & 2 deletions husky.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
hooks: {
'pre-commit': 'lint-staged',
'commit-msg': 'commitlint -E HUSKY_GIT_PARAMS',
"pre-commit": "lint-staged",
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
},
};
4 changes: 2 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
testEnvironment: 'node',
snapshotResolver: './test/helpers/snapshotResolver.js',
testEnvironment: "node",
snapshotResolver: "./test/helpers/snapshotResolver.js",
};
4 changes: 2 additions & 2 deletions lint-staged.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
'*.js': ['prettier --write', 'eslint --fix'],
'*.{json,md,yml,css,ts}': ['prettier --write'],
"*.js": ["eslint --fix", "prettier --write"],
"*.{json,md,yml,css,ts}": ["prettier --write"],
};
10 changes: 5 additions & 5 deletions src/Webpack4Cache.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os from 'os';
import os from "os";

import cacache from 'cacache';
import findCacheDir from 'find-cache-dir';
import serialize from 'serialize-javascript';
import cacache from "cacache";
import findCacheDir from "find-cache-dir";
import serialize from "serialize-javascript";

export default class Webpack4Cache {
constructor(compilation, options, weakCache) {
Expand All @@ -14,7 +14,7 @@ export default class Webpack4Cache {
}

static getCacheDirectory() {
return findCacheDir({ name: 'compression-webpack-plugin' }) || os.tmpdir();
return findCacheDir({ name: "compression-webpack-plugin" }) || os.tmpdir();
}

async get(cacheData, { RawSource }) {
Expand Down
2 changes: 1 addition & 1 deletion src/Webpack5Cache.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default class Cache {
constructor(compilation) {
this.cache = compilation.getCache('CompressionWebpackPlugin');
this.cache = compilation.getCache("CompressionWebpackPlugin");
}

async get(cacheData) {
Expand Down
2 changes: 1 addition & 1 deletion src/cjs.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('./index').default;
module.exports = require("./index").default;
64 changes: 32 additions & 32 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,37 @@
Author Tobias Koppers @sokra
*/

import crypto from 'crypto';
import path from 'path';
import crypto from "crypto";
import path from "path";

import webpack, {
ModuleFilenameHelpers,
version as webpackVersion,
} from 'webpack';
import { validate } from 'schema-utils';
import serialize from 'serialize-javascript';
} from "webpack";
import { validate } from "schema-utils";
import serialize from "serialize-javascript";

import schema from './options.json';
import schema from "./options.json";

const { RawSource } =
// eslint-disable-next-line global-require
webpack.sources || require('webpack-sources');
webpack.sources || require("webpack-sources");

class CompressionPlugin {
constructor(options = {}) {
validate(schema, options, {
name: 'Compression Plugin',
baseDataPath: 'options',
name: "Compression Plugin",
baseDataPath: "options",
});

const {
test,
include,
exclude,
cache = true,
algorithm = 'gzip',
algorithm = "gzip",
compressionOptions = {},
filename = '[path][base].gz',
filename = "[path][base].gz",
threshold = 0,
minRatio = 0.8,
deleteOriginalAssets = false,
Expand All @@ -54,9 +54,9 @@ class CompressionPlugin {

this.algorithm = this.options.algorithm;

if (typeof this.algorithm === 'string') {
if (typeof this.algorithm === "string") {
// eslint-disable-next-line global-require
const zlib = require('zlib');
const zlib = require("zlib");

this.algorithm = zlib[this.algorithm];

Expand Down Expand Up @@ -157,7 +157,7 @@ class CompressionPlugin {

async compress(compilation, assets, CacheEngine, weakCache) {
const assetNames = Object.keys(
typeof assets === 'undefined' ? compilation.assets : assets
typeof assets === "undefined" ? compilation.assets : assets
).filter((assetName) =>
// eslint-disable-next-line no-undefined
ModuleFilenameHelpers.matchObject.bind(undefined, this.options)(assetName)
Expand Down Expand Up @@ -188,18 +188,18 @@ class CompressionPlugin {

let relatedName;

if (typeof this.options.algorithm === 'function') {
if (typeof this.options.algorithm === "function") {
let filenameForRelatedName = this.options.filename;

const index = filenameForRelatedName.lastIndexOf('?');
const index = filenameForRelatedName.lastIndexOf("?");

if (index >= 0) {
filenameForRelatedName = filenameForRelatedName.substr(0, index);
}

relatedName = `${path.extname(filenameForRelatedName).slice(1)}ed`;
} else if (this.options.algorithm === 'gzip') {
relatedName = 'gzipped';
} else if (this.options.algorithm === "gzip") {
relatedName = "gzipped";
} else {
relatedName = `${this.options.algorithm}ed`;
}
Expand All @@ -224,12 +224,12 @@ class CompressionPlugin {
cacheData.cacheKeys = {
nodeVersion: process.version,
// eslint-disable-next-line global-require
'compression-webpack-plugin': require('../package.json').version,
"compression-webpack-plugin": require("../package.json").version,
algorithm: this.algorithm,
originalAlgorithm: this.options.algorithm,
compressionOptions: this.options.compressionOptions,
name,
contentHash: crypto.createHash('md4').update(input).digest('hex'),
contentHash: crypto.createHash("md4").update(input).digest("hex"),
};
} else {
cacheData.name = serialize({
Expand Down Expand Up @@ -261,8 +261,8 @@ class CompressionPlugin {

const match = /^([^?#]*)(\?[^#]*)?(#.*)?$/.exec(name);
const [, replacerFile] = match;
const replacerQuery = match[2] || '';
const replacerFragment = match[3] || '';
const replacerQuery = match[2] || "";
const replacerFragment = match[3] || "";
const replacerExt = path.extname(replacerFile);
const replacerBase = path.basename(replacerFile);
const replacerName = replacerBase.slice(
Expand All @@ -280,12 +280,12 @@ class CompressionPlugin {
path: replacerPath,
base: replacerBase,
name: replacerName,
ext: replacerExt || '',
ext: replacerExt || "",
};

let newFilename = this.options.filename;

if (typeof newFilename === 'function') {
if (typeof newFilename === "function") {
newFilename = newFilename(pathData);
}

Expand All @@ -301,7 +301,7 @@ class CompressionPlugin {
}

if (this.options.deleteOriginalAssets) {
if (this.options.deleteOriginalAssets === 'keep-source-map') {
if (this.options.deleteOriginalAssets === "keep-source-map") {
// TODO `...` required only for webpack@4
const updatedAssetInfo = {
...info,
Expand Down Expand Up @@ -341,15 +341,15 @@ class CompressionPlugin {
}

static isWebpack4() {
return webpackVersion[0] === '4';
return webpackVersion[0] === "4";
}

apply(compiler) {
const pluginName = this.constructor.name;

if (CompressionPlugin.isWebpack4()) {
// eslint-disable-next-line global-require
const CacheEngine = require('./Webpack4Cache').default;
const CacheEngine = require("./Webpack4Cache").default;
const weakCache = new WeakMap();

compiler.hooks.emit.tapPromise({ name: pluginName }, (compilation) =>
Expand All @@ -358,11 +358,11 @@ class CompressionPlugin {
);
} else {
// eslint-disable-next-line global-require
const CacheEngine = require('./Webpack5Cache').default;
const CacheEngine = require("./Webpack5Cache").default;

compiler.hooks.thisCompilation.tap(pluginName, (compilation) => {
// eslint-disable-next-line global-require
const Compilation = require('webpack/lib/Compilation');
const Compilation = require("webpack/lib/Compilation");

compilation.hooks.processAssets.tapPromise(
{
Expand All @@ -374,12 +374,12 @@ class CompressionPlugin {

compilation.hooks.statsPrinter.tap(pluginName, (stats) => {
stats.hooks.print
.for('asset.info.compressed')
.for("asset.info.compressed")
.tap(
'compression-webpack-plugin',
"compression-webpack-plugin",
(compressed, { green, formatFlag }) =>
// eslint-disable-next-line no-undefined
compressed ? green(formatFlag('compressed')) : undefined
compressed ? green(formatFlag("compressed")) : undefined
);
});
});
Expand Down
Loading