From 8664d36f2ea36afc52cc01998f543c055dc1da15 Mon Sep 17 00:00:00 2001 From: Alexander Akait <4567934+alexander-akait@users.noreply.github.com> Date: Thu, 16 Dec 2021 14:43:11 +0300 Subject: [PATCH] feat: remove cjs wrapper and generate types in commonjs format (#277) --- package.json | 6 +- src/cjs.js | 1 - src/index.js | 12 ++-- test/cjs.test.js | 8 --- types/cjs.d.ts | 2 - types/index.d.ts | 179 +++++++++++++++++++++++++++-------------------- 6 files changed, 111 insertions(+), 97 deletions(-) delete mode 100644 src/cjs.js delete mode 100644 test/cjs.test.js delete mode 100644 types/cjs.d.ts diff --git a/package.json b/package.json index 303c372..42dd108 100644 --- a/package.json +++ b/package.json @@ -11,14 +11,14 @@ "type": "opencollective", "url": "https://opencollective.com/webpack" }, - "main": "dist/cjs.js", - "types": "types/cjs.d.ts", + "main": "dist/index.js", + "types": "types/index.d.ts", "engines": { "node": ">= 12.13.0" }, "scripts": { "start": "npm run build -- -w", - "clean": "del-cli dist", + "clean": "del-cli dist types", "prebuild": "npm run clean", "build:types": "tsc --declaration --emitDeclarationOnly --outDir types && prettier \"types/**/*.ts\" --write && prettier types --write", "build:code": "cross-env NODE_ENV=production babel src -d dist --copy-files", diff --git a/src/cjs.js b/src/cjs.js deleted file mode 100644 index 0f29dbe..0000000 --- a/src/cjs.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require("./index").default; diff --git a/src/index.js b/src/index.js index 4faa6db..0a67da5 100644 --- a/src/index.js +++ b/src/index.js @@ -3,13 +3,13 @@ Author Tobias Koppers @sokra */ -import path from "path"; -import crypto from "crypto"; +const path = require("path"); +const crypto = require("crypto"); -import { validate } from "schema-utils"; -import serialize from "serialize-javascript"; +const { validate } = require("schema-utils"); +const serialize = require("serialize-javascript"); -import schema from "./options.json"; +const schema = require("./options.json"); /** @typedef {import("schema-utils/declarations/validate").Schema} Schema */ /** @typedef {import("webpack").Compiler} Compiler */ @@ -432,4 +432,4 @@ class CompressionPlugin { } } -export default CompressionPlugin; +module.exports = CompressionPlugin; diff --git a/test/cjs.test.js b/test/cjs.test.js deleted file mode 100644 index dc8321a..0000000 --- a/test/cjs.test.js +++ /dev/null @@ -1,8 +0,0 @@ -import src from "../src"; -import cjs from "../src/cjs"; - -describe("cjs", () => { - it("should exported", () => { - expect(cjs).toEqual(src); - }); -}); diff --git a/types/cjs.d.ts b/types/cjs.d.ts deleted file mode 100644 index a7e7d3c..0000000 --- a/types/cjs.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -declare const _exports: typeof import("./index").default; -export = _exports; diff --git a/types/index.d.ts b/types/index.d.ts index 59197db..75a4f29 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,81 +1,5 @@ /// -export default CompressionPlugin; -export type Schema = import("schema-utils/declarations/validate").Schema; -export type Compiler = import("webpack").Compiler; -export type WebpackPluginInstance = import("webpack").WebpackPluginInstance; -export type Compilation = import("webpack").Compilation; -export type Source = import("webpack").sources.Source; -export type Asset = import("webpack").Asset; -export type WebpackError = import("webpack").WebpackError; -export type WithImplicitCoercion = - | T - | { - valueOf(): T; - }; -export type Rule = RegExp | string; -export type Rules = Rule[] | Rule; -export type CustomOptions = { - [key: string]: any; -}; -export type InferDefaultType = T extends infer U ? U : CustomOptions; -export type CompressionOptions = InferDefaultType; -export type AlgorithmFunction = ( - input: Buffer, - options: CompressionOptions, - callback: ( - error: Error | null | undefined, - result: - | string - | ArrayBuffer - | SharedArrayBuffer - | Uint8Array - | readonly number[] - | { - valueOf(): ArrayBuffer | SharedArrayBuffer; - } - | { - valueOf(): string | Uint8Array | readonly number[]; - } - | { - valueOf(): string; - } - | { - [Symbol.toPrimitive](hint: "string"): string; - } - ) => void -) => any; -export type PathData = { - [key: string]: any; -}; -export type Filename = string | ((fileData: PathData) => string); -export type DeleteOriginalAssets = boolean | "keep-source-map"; -export type BasePluginOptions = { - test?: Rules | undefined; - include?: Rules | undefined; - exclude?: Rules | undefined; - threshold?: number | undefined; - minRatio?: number | undefined; - deleteOriginalAssets?: DeleteOriginalAssets | undefined; - filename?: Filename | undefined; -}; -export type ZlibOptions = import("zlib").ZlibOptions; -export type DefinedDefaultAlgorithmAndOptions = T extends ZlibOptions - ? { - algorithm?: string | AlgorithmFunction | undefined; - compressionOptions?: CompressionOptions | undefined; - } - : { - algorithm: string | AlgorithmFunction; - compressionOptions?: CompressionOptions | undefined; - }; -export type InternalPluginOptions = BasePluginOptions & { - algorithm: string | AlgorithmFunction; - compressionOptions: CompressionOptions; - threshold: number; - minRatio: number; - deleteOriginalAssets: DeleteOriginalAssets; - filename: Filename; -}; +export = CompressionPlugin; /** @typedef {import("schema-utils/declarations/validate").Schema} Schema */ /** @typedef {import("webpack").Compiler} Compiler */ /** @typedef {import("webpack").WebpackPluginInstance} WebpackPluginInstance */ @@ -183,3 +107,104 @@ declare class CompressionPlugin */ apply(compiler: Compiler): void; } +declare namespace CompressionPlugin { + export { + Schema, + Compiler, + WebpackPluginInstance, + Compilation, + Source, + Asset, + WebpackError, + WithImplicitCoercion, + Rule, + Rules, + CustomOptions, + InferDefaultType, + CompressionOptions, + AlgorithmFunction, + PathData, + Filename, + DeleteOriginalAssets, + BasePluginOptions, + ZlibOptions, + DefinedDefaultAlgorithmAndOptions, + InternalPluginOptions, + }; +} +type WebpackPluginInstance = import("webpack").WebpackPluginInstance; +type Compiler = import("webpack").Compiler; +type BasePluginOptions = { + test?: Rules | undefined; + include?: Rules | undefined; + exclude?: Rules | undefined; + threshold?: number | undefined; + minRatio?: number | undefined; + deleteOriginalAssets?: DeleteOriginalAssets | undefined; + filename?: Filename | undefined; +}; +type DefinedDefaultAlgorithmAndOptions = T extends ZlibOptions + ? { + algorithm?: string | AlgorithmFunction | undefined; + compressionOptions?: CompressionOptions | undefined; + } + : { + algorithm: string | AlgorithmFunction; + compressionOptions?: CompressionOptions | undefined; + }; +type Schema = import("schema-utils/declarations/validate").Schema; +type Compilation = import("webpack").Compilation; +type Source = import("webpack").sources.Source; +type Asset = import("webpack").Asset; +type WebpackError = import("webpack").WebpackError; +type WithImplicitCoercion = + | T + | { + valueOf(): T; + }; +type Rule = RegExp | string; +type Rules = Rule[] | Rule; +type CustomOptions = { + [key: string]: any; +}; +type InferDefaultType = T extends infer U ? U : CustomOptions; +type CompressionOptions = InferDefaultType; +type AlgorithmFunction = ( + input: Buffer, + options: CompressionOptions, + callback: ( + error: Error | null | undefined, + result: + | string + | ArrayBuffer + | SharedArrayBuffer + | Uint8Array + | readonly number[] + | { + valueOf(): ArrayBuffer | SharedArrayBuffer; + } + | { + valueOf(): string | Uint8Array | readonly number[]; + } + | { + valueOf(): string; + } + | { + [Symbol.toPrimitive](hint: "string"): string; + } + ) => void +) => any; +type PathData = { + [key: string]: any; +}; +type Filename = string | ((fileData: PathData) => string); +type DeleteOriginalAssets = boolean | "keep-source-map"; +type ZlibOptions = import("zlib").ZlibOptions; +type InternalPluginOptions = BasePluginOptions & { + algorithm: string | AlgorithmFunction; + compressionOptions: CompressionOptions; + threshold: number; + minRatio: number; + deleteOriginalAssets: DeleteOriginalAssets; + filename: Filename; +};