From e4c64c8c9d0cee2f6545893252738626d51503f1 Mon Sep 17 00:00:00 2001 From: Alexander Akait <4567934+alexander-akait@users.noreply.github.com> Date: Thu, 16 Dec 2021 15:27:33 +0300 Subject: [PATCH] feat: removed cjs wrapper and generated types in commonjs format (`export =` and `namespaces` used in types), now you can directly use exported types --- package.json | 6 +- src/cjs.js | 1 - src/index.js | 18 ++--- src/minify.js | 3 +- src/utils.js | 2 +- test/cjs.test.js | 8 --- types/cjs.d.ts | 2 - types/index.d.ts | 183 +++++++++++++++++++++++++++-------------------- 8 files changed, 118 insertions(+), 105 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 d4b5f28..40c5212 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", "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 29bdb5a..0c1a4c4 100644 --- a/src/index.js +++ b/src/index.js @@ -1,13 +1,13 @@ -import os from "os"; +const os = require("os"); -import { validate } from "schema-utils"; -import serialize from "serialize-javascript"; -import { Worker } from "jest-worker"; +const { validate } = require("schema-utils"); +const serialize = require("serialize-javascript"); +const { Worker } = require("jest-worker"); -import schema from "./options.json"; +const schema = require("./options.json"); -import { htmlMinifierTerser, throttleAll } from "./utils"; -import { minify as minifyFn } from "./minify"; +const { htmlMinifierTerser, throttleAll } = require("./utils"); +const { minify } = require("./minify"); /** @typedef {import("schema-utils/declarations/validate").Schema} Schema */ /** @typedef {import("webpack").Compiler} Compiler */ @@ -378,7 +378,7 @@ class HtmlMinimizerPlugin { try { output = await (getWorker ? getWorker().transform(serialize(options)) - : minifyFn(options)); + : minify(options)); } catch (error) { compilation.errors.push( /** @type {WebpackError} */ @@ -475,4 +475,4 @@ class HtmlMinimizerPlugin { HtmlMinimizerPlugin.htmlMinifierTerser = htmlMinifierTerser; -export default HtmlMinimizerPlugin; +module.exports = HtmlMinimizerPlugin; diff --git a/src/minify.js b/src/minify.js index 8d4a809..eb84a4c 100644 --- a/src/minify.js +++ b/src/minify.js @@ -63,5 +63,4 @@ async function transform(options) { return minify(evaluatedOptions); } -module.exports.minify = minify; -module.exports.transform = transform; +module.exports = { minify, transform }; diff --git a/src/utils.js b/src/utils.js index 0d9e1c4..209ba85 100644 --- a/src/utils.js +++ b/src/utils.js @@ -102,4 +102,4 @@ async function htmlMinifierTerser(input, minimizerOptions = {}) { return { code: result }; } -export { throttleAll, htmlMinifierTerser }; +module.exports = { throttleAll, htmlMinifierTerser }; diff --git a/test/cjs.test.js b/test/cjs.test.js deleted file mode 100644 index 06cab73..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 export loader", () => { - 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 fad39dc..a231236 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,80 +1,4 @@ -export default HtmlMinimizerPlugin; -export type Schema = import("schema-utils/declarations/validate").Schema; -export type Compiler = import("webpack").Compiler; -export type Compilation = import("webpack").Compilation; -export type WebpackError = import("webpack").WebpackError; -export type Asset = import("webpack").Asset; -export type JestWorker = import("jest-worker").Worker; -export type HtmlMinifierTerserOptions = - import("./utils.js").HtmlMinifierTerserOptions; -export type Rule = RegExp | string; -export type Rules = Rule[] | Rule; -export type MinimizedResult = { - code: string; - errors?: unknown[] | undefined; - warnings?: unknown[] | undefined; -}; -export type Input = { - [file: string]: string; -}; -export type CustomOptions = { - [key: string]: any; -}; -export type InferDefaultType = T extends infer U ? U : CustomOptions; -export type MinimizerOptions = InferDefaultType | undefined; -export type MinimizerImplementation = ( - input: Input, - minimizerOptions?: MinimizerOptions -) => Promise; -export type Minimizer = { - implementation: MinimizerImplementation; - options?: MinimizerOptions | undefined; -}; -export type InternalOptions = { - name: string; - input: string; - minimizer: T extends any[] - ? { [P in keyof T]: Minimizer } - : Minimizer; -}; -export type InternalResult = { - code: string; - warnings: Array; - errors: Array; -}; -export type MinimizerWorker = Worker & { - transform: (options: string) => InternalResult; - minify: (options: InternalOptions) => InternalResult; -}; -export type Parallel = undefined | boolean | number; -export type BasePluginOptions = { - test?: Rules | undefined; - include?: Rules | undefined; - exclude?: Rules | undefined; - parallel?: Parallel; -}; -export type InternalPluginOptions = BasePluginOptions & { - minimizer: T extends any[] - ? { [P in keyof T]: Minimizer } - : Minimizer; -}; -export type DefinedDefaultMinimizerAndOptions = - T extends import("html-minifier-terser").Options - ? { - minify?: MinimizerImplementation | undefined; - minimizerOptions?: MinimizerOptions | undefined; - } - : T extends any[] - ? { - minify: { [P in keyof T]: MinimizerImplementation }; - minimizerOptions?: - | { [P_1 in keyof T]?: MinimizerOptions } - | undefined; - } - : { - minify: MinimizerImplementation; - minimizerOptions?: MinimizerOptions | undefined; - }; +export = HtmlMinimizerPlugin; /** @typedef {import("schema-utils/declarations/validate").Schema} Schema */ /** @typedef {import("webpack").Compiler} Compiler */ /** @typedef {import("webpack").Compilation} Compilation */ @@ -209,7 +133,108 @@ declare class HtmlMinimizerPlugin { apply(compiler: Compiler): void; } declare namespace HtmlMinimizerPlugin { - export { htmlMinifierTerser }; + export { + htmlMinifierTerser, + Schema, + Compiler, + Compilation, + WebpackError, + Asset, + JestWorker, + HtmlMinifierTerserOptions, + Rule, + Rules, + MinimizedResult, + Input, + CustomOptions, + InferDefaultType, + MinimizerOptions, + MinimizerImplementation, + Minimizer, + InternalOptions, + InternalResult, + MinimizerWorker, + Parallel, + BasePluginOptions, + InternalPluginOptions, + DefinedDefaultMinimizerAndOptions, + }; } -import { Worker } from "jest-worker"; +type Compiler = import("webpack").Compiler; +type BasePluginOptions = { + test?: Rules | undefined; + include?: Rules | undefined; + exclude?: Rules | undefined; + parallel?: Parallel; +}; +type DefinedDefaultMinimizerAndOptions = + T extends import("html-minifier-terser").Options + ? { + minify?: MinimizerImplementation | undefined; + minimizerOptions?: MinimizerOptions | undefined; + } + : T extends any[] + ? { + minify: { [P in keyof T]: MinimizerImplementation }; + minimizerOptions?: + | { [P_1 in keyof T]?: MinimizerOptions } + | undefined; + } + : { + minify: MinimizerImplementation; + minimizerOptions?: MinimizerOptions | undefined; + }; import { htmlMinifierTerser } from "./utils"; +type Schema = import("schema-utils/declarations/validate").Schema; +type Compilation = import("webpack").Compilation; +type WebpackError = import("webpack").WebpackError; +type Asset = import("webpack").Asset; +type JestWorker = import("jest-worker").Worker; +type HtmlMinifierTerserOptions = import("./utils.js").HtmlMinifierTerserOptions; +type Rule = RegExp | string; +type Rules = Rule[] | Rule; +type MinimizedResult = { + code: string; + errors?: unknown[] | undefined; + warnings?: unknown[] | undefined; +}; +type Input = { + [file: string]: string; +}; +type CustomOptions = { + [key: string]: any; +}; +type InferDefaultType = T extends infer U ? U : CustomOptions; +type MinimizerOptions = InferDefaultType | undefined; +type MinimizerImplementation = ( + input: Input, + minimizerOptions?: MinimizerOptions +) => Promise; +type Minimizer = { + implementation: MinimizerImplementation; + options?: MinimizerOptions | undefined; +}; +type InternalOptions = { + name: string; + input: string; + minimizer: T extends any[] + ? { [P in keyof T]: Minimizer } + : Minimizer; +}; +type InternalResult = { + code: string; + warnings: Array; + errors: Array; +}; +type MinimizerWorker = Worker & { + transform: (options: string) => InternalResult; + minify: (options: InternalOptions) => InternalResult; +}; +type Parallel = undefined | boolean | number; +type InternalPluginOptions = BasePluginOptions & { + minimizer: T extends any[] + ? { [P in keyof T]: Minimizer } + : Minimizer; +}; +import { minify } from "./minify"; +import { Worker } from "jest-worker";