From 3262a9aee2c89e6aed00c4f76443e0d1e07489f0 Mon Sep 17 00:00:00 2001 From: Alexander Akait <4567934+alexander-akait@users.noreply.github.com> Date: Thu, 16 Dec 2021 16:54:34 +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 | 8 +- src/cjs.js | 3 - src/index.js | 22 +-- src/minify.js | 3 +- src/utils.js | 2 +- test/cache-option.test.js | 18 +++ test/cjs.test.js | 8 -- types/cjs.d.ts | 3 - types/index.d.ts | 286 +++++++++++++++++++++----------------- 9 files changed, 196 insertions(+), 157 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 ba7bd0e..743ae50 100644 --- a/package.json +++ b/package.json @@ -11,16 +11,16 @@ "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", - "prebuild": "npm run clean", - "build:types": "tsc --declaration --emitDeclarationOnly --outDir types && prettier \"types/**/*.ts\" --write && prettier types --write", + "prebuild": "npm run clean types", + "build:types": "tsc --declaration --emitDeclarationOnly --outDir types && prettier \"types/**/*.ts\" --write", "build:code": "cross-env NODE_ENV=production babel src -d dist --copy-files", "build": "npm-run-all -p \"build:**\"", "commitlint": "commitlint --from=master", diff --git a/src/cjs.js b/src/cjs.js deleted file mode 100644 index 54bc4ed..0000000 --- a/src/cjs.js +++ /dev/null @@ -1,3 +0,0 @@ -const plugin = require("./index"); - -module.exports = plugin.default; diff --git a/src/index.js b/src/index.js index 2da234c..89106bc 100644 --- a/src/index.js +++ b/src/index.js @@ -1,20 +1,20 @@ -import * as os from "os"; +const os = require("os"); -import { SourceMapConsumer } from "source-map"; -import { validate } from "schema-utils"; -import serialize from "serialize-javascript"; -import { Worker } from "jest-worker"; +const { SourceMapConsumer } = require("source-map"); +const { validate } = require("schema-utils"); +const serialize = require("serialize-javascript"); +const { Worker } = require("jest-worker"); -import { +const { throttleAll, cssnanoMinify, cssoMinify, cleanCssMinify, esbuildMinify, -} from "./utils"; +} = require("./utils"); -import * as schema from "./options.json"; -import { minify as minifyFn } from "./minify"; +const schema = require("./options.json"); +const { minify } = require("./minify"); /** @typedef {import("schema-utils/declarations/validate").Schema} Schema */ /** @typedef {import("webpack").Compiler} Compiler */ @@ -529,7 +529,7 @@ class CssMinimizerPlugin { try { result = await (getWorker ? getWorker().transform(serialize(options)) - : minifyFn(options)); + : minify(options)); } catch (error) { const hasSourceMap = inputSourceMap && CssMinimizerPlugin.isSourceMap(inputSourceMap); @@ -715,4 +715,4 @@ CssMinimizerPlugin.cssoMinify = cssoMinify; CssMinimizerPlugin.cleanCssMinify = cleanCssMinify; CssMinimizerPlugin.esbuildMinify = esbuildMinify; -export default CssMinimizerPlugin; +module.exports = CssMinimizerPlugin; diff --git a/src/minify.js b/src/minify.js index 8ac5915..eca5aaa 100644 --- a/src/minify.js +++ b/src/minify.js @@ -82,5 +82,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 fe73c44..c993c0d 100644 --- a/src/utils.js +++ b/src/utils.js @@ -347,7 +347,7 @@ async function esbuildMinify(input, sourceMap, minimizerOptions) { }; } -export { +module.exports = { throttleAll, cssnanoMinify, cssoMinify, diff --git a/test/cache-option.test.js b/test/cache-option.test.js index a52c4f7..dc9a0b8 100644 --- a/test/cache-option.test.js +++ b/test/cache-option.test.js @@ -92,6 +92,12 @@ describe('"cache" option', () => { expect(readAssets(compiler, newStats, /\.css$/)).toMatchSnapshot("assets"); expect(getErrors(newStats)).toMatchSnapshot("errors"); expect(getWarnings(newStats)).toMatchSnapshot("warnings"); + + await new Promise((resolve) => { + compiler.close(() => { + resolve(); + }); + }); }); it('should work with the "memory" value for the "cache.type" option', async () => { @@ -154,6 +160,12 @@ describe('"cache" option', () => { expect(readAssets(compiler, newStats, /\.css$/)).toMatchSnapshot("assets"); expect(getErrors(newStats)).toMatchSnapshot("errors"); expect(getWarnings(newStats)).toMatchSnapshot("warnings"); + + await new Promise((resolve) => { + compiler.close(() => { + resolve(); + }); + }); }); it('should work with the "filesystem" value for the "cache.type" option', async () => { @@ -217,6 +229,12 @@ describe('"cache" option', () => { expect(readAssets(compiler, newStats, /\.css$/)).toMatchSnapshot("assets"); expect(getErrors(newStats)).toMatchSnapshot("errors"); expect(getWarnings(newStats)).toMatchSnapshot("warnings"); + + await new Promise((resolve) => { + compiler.close(() => { + resolve(); + }); + }); }); it('should work with the "filesystem" value for the "cache.type" option and source maps', async () => { 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 2cb80fb..0000000 --- a/types/cjs.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -declare const _exports: typeof plugin.default; -export = _exports; -import plugin = require("./index"); diff --git a/types/index.d.ts b/types/index.d.ts index dd13dc6..42adac5 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,61 +1,189 @@ -export default CssMinimizerPlugin; -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 JestWorker = import("jest-worker").Worker; -export type RawSourceMap = import("source-map").RawSourceMap; -export type CssNanoOptions = import("cssnano").CssNanoOptions; -export type Asset = import("webpack").Asset; -export type ProcessOptions = import("postcss").ProcessOptions; -export type Syntax = import("postcss").Syntax; -export type Parser = import("postcss").Parser; -export type Stringifier = import("postcss").Stringifier; -export type Warning = +export = CssMinimizerPlugin; +/** + * @template [T=CssNanoOptionsExtended] + */ +declare class CssMinimizerPlugin { + /** + * @private + * @param {any} input + * @returns {boolean} + */ + private static isSourceMap; + /** + * @private + * @param {Warning | WarningObject | string} warning + * @param {string} file + * @param {WarningsFilter} [warningsFilter] + * @param {SourceMapConsumer} [sourceMap] + * @param {Compilation["requestShortener"]} [requestShortener] + * @returns {Error & { hideStack?: boolean, file?: string } | undefined} + */ + private static buildWarning; + /** + * @private + * @param {Error | ErrorObject | string} error + * @param {string} file + * @param {SourceMapConsumer} [sourceMap] + * @param {Compilation["requestShortener"]} [requestShortener] + * @returns {Error} + */ + private static buildError; + /** + * @private + * @param {Parallel} parallel + * @returns {number} + */ + private static getAvailableNumberOfCores; + /** + * @param {BasePluginOptions & DefinedDefaultMinimizerAndOptions} [options] + */ + constructor( + options?: + | (BasePluginOptions & DefinedDefaultMinimizerAndOptions) + | undefined + ); + /** + * @private + * @type {InternalPluginOptions} + */ + private options; + /** + * @private + * @param {Compiler} compiler + * @param {Compilation} compilation + * @param {Record} assets + * @param {{availableNumberOfCores: number}} optimizeOptions + * @returns {Promise} + */ + private optimize; + /** + * @param {Compiler} compiler + * @returns {void} + */ + apply(compiler: Compiler): void; +} +declare namespace CssMinimizerPlugin { + export { + cssnanoMinify, + cssoMinify, + cleanCssMinify, + esbuildMinify, + Schema, + Compiler, + Compilation, + WebpackError, + JestWorker, + RawSourceMap, + CssNanoOptions, + Asset, + ProcessOptions, + Syntax, + Parser, + Stringifier, + Warning, + WarningObject, + ErrorObject, + MinimizedResult, + Input, + CustomOptions, + InferDefaultType, + BasicMinimizerImplementation, + MinimizerImplementation, + MinimizerOptions, + InternalOptions, + InternalResult, + Parallel, + Rule, + Rules, + WarningsFilter, + BasePluginOptions, + MinimizerWorker, + ProcessOptionsExtender, + CssNanoOptionsExtended, + DefinedDefaultMinimizerAndOptions, + InternalPluginOptions, + }; +} +type CssNanoOptionsExtended = CssNanoOptions & { + processorOptions?: ProcessOptionsExtender; +}; +type Compiler = import("webpack").Compiler; +type BasePluginOptions = { + test?: Rules | undefined; + include?: Rules | undefined; + exclude?: Rules | undefined; + warningsFilter?: WarningsFilter | undefined; + parallel?: Parallel; +}; +type DefinedDefaultMinimizerAndOptions = T extends CssNanoOptionsExtended + ? { + minify?: MinimizerImplementation | undefined; + minimizerOptions?: MinimizerOptions | undefined; + } + : { + minify: MinimizerImplementation; + minimizerOptions?: MinimizerOptions | undefined; + }; +import { cssnanoMinify } from "./utils"; +import { cssoMinify } from "./utils"; +import { cleanCssMinify } from "./utils"; +import { esbuildMinify } from "./utils"; +type Schema = import("schema-utils/declarations/validate").Schema; +type Compilation = import("webpack").Compilation; +type WebpackError = import("webpack").WebpackError; +type JestWorker = import("jest-worker").Worker; +type RawSourceMap = import("source-map").RawSourceMap; +type CssNanoOptions = import("cssnano").CssNanoOptions; +type Asset = import("webpack").Asset; +type ProcessOptions = import("postcss").ProcessOptions; +type Syntax = import("postcss").Syntax; +type Parser = import("postcss").Parser; +type Stringifier = import("postcss").Stringifier; +type Warning = | (Error & { plugin?: string; text?: string; source?: string; }) | string; -export type WarningObject = { +type WarningObject = { message: string; plugin?: string | undefined; text?: string | undefined; line?: number | undefined; column?: number | undefined; }; -export type ErrorObject = { +type ErrorObject = { message: string; line?: number | undefined; column?: number | undefined; stack?: string | undefined; }; -export type MinimizedResult = { +type MinimizedResult = { code: string; map?: import("source-map").RawSourceMap | undefined; errors?: (string | Error | ErrorObject)[] | undefined; warnings?: (Warning | WarningObject)[] | undefined; }; -export type Input = { +type Input = { [file: string]: string; }; -export type CustomOptions = { +type CustomOptions = { [key: string]: any; }; -export type InferDefaultType = T extends infer U ? U : CustomOptions; -export type BasicMinimizerImplementation = ( +type InferDefaultType = T extends infer U ? U : CustomOptions; +type BasicMinimizerImplementation = ( input: Input, sourceMap: RawSourceMap | undefined, minifyOptions: InferDefaultType ) => Promise; -export type MinimizerImplementation = T extends any[] +type MinimizerImplementation = T extends any[] ? { [P in keyof T]: BasicMinimizerImplementation } : BasicMinimizerImplementation; -export type MinimizerOptions = T extends any[] +type MinimizerOptions = T extends any[] ? { [P in keyof T]?: InferDefaultType | undefined } : InferDefaultType; -export type InternalOptions = { +type InternalOptions = { name: string; input: string; inputSourceMap: RawSourceMap | undefined; @@ -64,7 +192,7 @@ export type InternalOptions = { options: MinimizerOptions; }; }; -export type InternalResult = { +type InternalResult = { outputs: Array<{ code: string; map: RawSourceMap | undefined; @@ -72,26 +200,19 @@ export type InternalResult = { warnings: Array; errors: Array; }; -export type Parallel = undefined | boolean | number; -export type Rule = RegExp | string; -export type Rules = Rule[] | Rule; -export type WarningsFilter = ( +type Parallel = undefined | boolean | number; +type Rule = RegExp | string; +type Rules = Rule[] | Rule; +type WarningsFilter = ( warning: Warning | WarningObject | string, file: string, source?: string | undefined ) => boolean; -export type BasePluginOptions = { - test?: Rules | undefined; - include?: Rules | undefined; - exclude?: Rules | undefined; - warningsFilter?: WarningsFilter | undefined; - parallel?: Parallel; -}; -export type MinimizerWorker = Worker & { +type MinimizerWorker = Worker & { transform: (options: string) => InternalResult; minify: (options: InternalOptions) => InternalResult; }; -export type ProcessOptionsExtender = +type ProcessOptionsExtender = | ProcessOptions | { from?: string; @@ -100,96 +221,11 @@ export type ProcessOptionsExtender = stringifier?: string | Syntax | Stringifier; syntax?: string | Syntax; }; -export type CssNanoOptionsExtended = CssNanoOptions & { - processorOptions?: ProcessOptionsExtender; -}; -export type DefinedDefaultMinimizerAndOptions = - T extends CssNanoOptionsExtended - ? { - minify?: MinimizerImplementation | undefined; - minimizerOptions?: MinimizerOptions | undefined; - } - : { - minify: MinimizerImplementation; - minimizerOptions?: MinimizerOptions | undefined; - }; -export type InternalPluginOptions = BasePluginOptions & { +type InternalPluginOptions = BasePluginOptions & { minimizer: { implementation: MinimizerImplementation; options: MinimizerOptions; }; }; -/** - * @template [T=CssNanoOptionsExtended] - */ -declare class CssMinimizerPlugin { - /** - * @private - * @param {any} input - * @returns {boolean} - */ - private static isSourceMap; - /** - * @private - * @param {Warning | WarningObject | string} warning - * @param {string} file - * @param {WarningsFilter} [warningsFilter] - * @param {SourceMapConsumer} [sourceMap] - * @param {Compilation["requestShortener"]} [requestShortener] - * @returns {Error & { hideStack?: boolean, file?: string } | undefined} - */ - private static buildWarning; - /** - * @private - * @param {Error | ErrorObject | string} error - * @param {string} file - * @param {SourceMapConsumer} [sourceMap] - * @param {Compilation["requestShortener"]} [requestShortener] - * @returns {Error} - */ - private static buildError; - /** - * @private - * @param {Parallel} parallel - * @returns {number} - */ - private static getAvailableNumberOfCores; - /** - * @param {BasePluginOptions & DefinedDefaultMinimizerAndOptions} [options] - */ - constructor( - options?: - | (BasePluginOptions & DefinedDefaultMinimizerAndOptions) - | undefined - ); - /** - * @private - * @type {InternalPluginOptions} - */ - private options; - /** - * @private - * @param {Compiler} compiler - * @param {Compilation} compilation - * @param {Record} assets - * @param {{availableNumberOfCores: number}} optimizeOptions - * @returns {Promise} - */ - private optimize; - /** - * @param {Compiler} compiler - * @returns {void} - */ - apply(compiler: Compiler): void; -} -declare namespace CssMinimizerPlugin { - export { cssnanoMinify }; - export { cssoMinify }; - export { cleanCssMinify }; - export { esbuildMinify }; -} +import { minify } from "./minify"; import { Worker } from "jest-worker"; -import { cssnanoMinify } from "./utils"; -import { cssoMinify } from "./utils"; -import { cleanCssMinify } from "./utils"; -import { esbuildMinify } from "./utils";