Skip to content

Commit

Permalink
feat: remove cjs wrapper and improve types format (#463)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Dec 16, 2021
1 parent d5146c9 commit 34fadde
Show file tree
Hide file tree
Showing 9 changed files with 520 additions and 145 deletions.
350 changes: 350 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
"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": ">= 10.13.0"
},
"scripts": {
"clean": "del-cli dist types",
"prebuild": "npm run clean",
"build:types": "tsc --declaration --emitDeclarationOnly --outDir types && prettier \"types/**/*.ts\" --write && prettier types --write",
"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",
Expand Down Expand Up @@ -91,6 +91,7 @@
"typescript": "^4.3.5",
"uglify-js": "^3.14.1",
"webpack": "^5.48.0",
"webpack-cli": "^4.9.1",
"worker-loader": "^3.0.8"
},
"keywords": [
Expand Down
3 changes: 0 additions & 3 deletions src/cjs.js

This file was deleted.

24 changes: 12 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import * as path from "path";
import * as os from "os";
const path = require("path");
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,
terserMinify,
uglifyJsMinify,
swcMinify,
esbuildMinify,
} from "./utils";
} = require("./utils");

import * as schema from "./options.json";
import { minify as minimize } from "./minify";
const schema = require("./options.json");
const { minify } = require("./minify");

/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
/** @typedef {import("webpack").Compiler} Compiler */
Expand Down Expand Up @@ -495,7 +495,7 @@ class TerserPlugin {
try {
output = await (getWorker
? getWorker().transform(serialize(options))
: minimize(options));
: minify(options));
} catch (error) {
const hasSourceMap =
inputSourceMap && TerserPlugin.isSourceMap(inputSourceMap);
Expand Down Expand Up @@ -866,4 +866,4 @@ TerserPlugin.uglifyJsMinify = uglifyJsMinify;
TerserPlugin.swcMinify = swcMinify;
TerserPlugin.esbuildMinify = esbuildMinify;

export default TerserPlugin;
module.exports = TerserPlugin;
3 changes: 1 addition & 2 deletions src/minify.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,4 @@ async function transform(options) {
return minify(evaluatedOptions);
}

module.exports.minify = minify;
module.exports.transform = transform;
module.exports = { minify, transform };
8 changes: 7 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -720,4 +720,10 @@ esbuildMinify.getMinimizerVersion = () => {
return packageJson && packageJson.version;
};

export { throttleAll, terserMinify, uglifyJsMinify, swcMinify, esbuildMinify };
module.exports = {
throttleAll,
terserMinify,
uglifyJsMinify,
swcMinify,
esbuildMinify,
};
8 changes: 0 additions & 8 deletions test/cjs.test.js

This file was deleted.

3 changes: 0 additions & 3 deletions types/cjs.d.ts

This file was deleted.

259 changes: 146 additions & 113 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,111 +1,4 @@
export default TerserPlugin;
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 TerserECMA = import("./utils.js").TerserECMA;
export type TerserOptions = import("./utils.js").TerserOptions;
export type JestWorker = import("jest-worker").Worker;
export type RawSourceMap = import("source-map").RawSourceMap;
export type Rule = RegExp | string;
export type Rules = Rule[] | Rule;
export type ExtractCommentsFunction = (
astNode: any,
comment: {
value: string;
type: "comment1" | "comment2" | "comment3" | "comment4";
pos: number;
line: number;
col: number;
}
) => boolean;
export type ExtractCommentsCondition =
| boolean
| "all"
| "some"
| RegExp
| ExtractCommentsFunction;
export type ExtractCommentsFilename = string | ((fileData: any) => string);
export type ExtractCommentsBanner =
| string
| boolean
| ((commentsFile: string) => string);
export type ExtractCommentsObject = {
condition?: ExtractCommentsCondition | undefined;
filename?: ExtractCommentsFilename | undefined;
banner?: ExtractCommentsBanner | undefined;
};
export type ExtractCommentsOptions =
| ExtractCommentsCondition
| ExtractCommentsObject;
export type MinimizedResult = {
code: string;
map?: import("source-map").RawSourceMap | undefined;
errors?: (string | Error)[] | undefined;
warnings?: (string | Error)[] | undefined;
extractedComments?: string[] | undefined;
};
export type Input = {
[file: string]: string;
};
export type CustomOptions = {
[key: string]: any;
};
export type InferDefaultType<T> = T extends infer U ? U : CustomOptions;
export type PredefinedOptions = {
module?: boolean | undefined;
ecma?: any;
};
export type MinimizerOptions<T> = PredefinedOptions & InferDefaultType<T>;
export type BasicMinimizerImplementation<T> = (
input: Input,
sourceMap: RawSourceMap | undefined,
minifyOptions: MinimizerOptions<T>,
extractComments: ExtractCommentsOptions | undefined
) => Promise<MinimizedResult>;
export type MinimizeFunctionHelpers = {
getMinimizerVersion?: (() => string | undefined) | undefined;
};
export type MinimizerImplementation<T> = BasicMinimizerImplementation<T> &
MinimizeFunctionHelpers;
export type InternalOptions<T> = {
name: string;
input: string;
inputSourceMap: RawSourceMap | undefined;
extractComments: ExtractCommentsOptions | undefined;
minimizer: {
implementation: MinimizerImplementation<T>;
options: MinimizerOptions<T>;
};
};
export type MinimizerWorker<T> = Worker & {
transform: (options: string) => MinimizedResult;
minify: (options: InternalOptions<T>) => MinimizedResult;
};
export type Parallel = undefined | boolean | number;
export type BasePluginOptions = {
test?: Rules | undefined;
include?: Rules | undefined;
exclude?: Rules | undefined;
extractComments?: ExtractCommentsOptions | undefined;
parallel?: Parallel;
};
export type DefinedDefaultMinimizerAndOptions<T> = T extends TerserOptions
? {
minify?: MinimizerImplementation<T> | undefined;
terserOptions?: MinimizerOptions<T> | undefined;
}
: {
minify: MinimizerImplementation<T>;
terserOptions?: MinimizerOptions<T> | undefined;
};
export type InternalPluginOptions<T> = BasePluginOptions & {
minimizer: {
implementation: MinimizerImplementation<T>;
options: MinimizerOptions<T>;
};
};
export = TerserPlugin;
/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
/** @typedef {import("webpack").Compiler} Compiler */
/** @typedef {import("webpack").Compilation} Compilation */
Expand Down Expand Up @@ -284,13 +177,153 @@ declare class TerserPlugin<T = import("terser").MinifyOptions> {
apply(compiler: Compiler): void;
}
declare namespace TerserPlugin {
export { terserMinify };
export { uglifyJsMinify };
export { swcMinify };
export { esbuildMinify };
export {
terserMinify,
uglifyJsMinify,
swcMinify,
esbuildMinify,
Schema,
Compiler,
Compilation,
WebpackError,
Asset,
TerserECMA,
TerserOptions,
JestWorker,
RawSourceMap,
Rule,
Rules,
ExtractCommentsFunction,
ExtractCommentsCondition,
ExtractCommentsFilename,
ExtractCommentsBanner,
ExtractCommentsObject,
ExtractCommentsOptions,
MinimizedResult,
Input,
CustomOptions,
InferDefaultType,
PredefinedOptions,
MinimizerOptions,
BasicMinimizerImplementation,
MinimizeFunctionHelpers,
MinimizerImplementation,
InternalOptions,
MinimizerWorker,
Parallel,
BasePluginOptions,
DefinedDefaultMinimizerAndOptions,
InternalPluginOptions,
};
}
import { Worker } from "jest-worker";
type Compiler = import("webpack").Compiler;
type BasePluginOptions = {
test?: Rules | undefined;
include?: Rules | undefined;
exclude?: Rules | undefined;
extractComments?: ExtractCommentsOptions | undefined;
parallel?: Parallel;
};
type DefinedDefaultMinimizerAndOptions<T> = T extends TerserOptions
? {
minify?: MinimizerImplementation<T> | undefined;
terserOptions?: MinimizerOptions<T> | undefined;
}
: {
minify: MinimizerImplementation<T>;
terserOptions?: MinimizerOptions<T> | undefined;
};
import { terserMinify } from "./utils";
import { uglifyJsMinify } from "./utils";
import { swcMinify } 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 Asset = import("webpack").Asset;
type TerserECMA = import("./utils.js").TerserECMA;
type TerserOptions = import("./utils.js").TerserOptions;
type JestWorker = import("jest-worker").Worker;
type RawSourceMap = import("source-map").RawSourceMap;
type Rule = RegExp | string;
type Rules = Rule[] | Rule;
type ExtractCommentsFunction = (
astNode: any,
comment: {
value: string;
type: "comment1" | "comment2" | "comment3" | "comment4";
pos: number;
line: number;
col: number;
}
) => boolean;
type ExtractCommentsCondition =
| boolean
| "all"
| "some"
| RegExp
| ExtractCommentsFunction;
type ExtractCommentsFilename = string | ((fileData: any) => string);
type ExtractCommentsBanner =
| string
| boolean
| ((commentsFile: string) => string);
type ExtractCommentsObject = {
condition?: ExtractCommentsCondition | undefined;
filename?: ExtractCommentsFilename | undefined;
banner?: ExtractCommentsBanner | undefined;
};
type ExtractCommentsOptions = ExtractCommentsCondition | ExtractCommentsObject;
type MinimizedResult = {
code: string;
map?: import("source-map").RawSourceMap | undefined;
errors?: (string | Error)[] | undefined;
warnings?: (string | Error)[] | undefined;
extractedComments?: string[] | undefined;
};
type Input = {
[file: string]: string;
};
type CustomOptions = {
[key: string]: any;
};
type InferDefaultType<T> = T extends infer U ? U : CustomOptions;
type PredefinedOptions = {
module?: boolean | undefined;
ecma?: any;
};
type MinimizerOptions<T> = PredefinedOptions & InferDefaultType<T>;
type BasicMinimizerImplementation<T> = (
input: Input,
sourceMap: RawSourceMap | undefined,
minifyOptions: MinimizerOptions<T>,
extractComments: ExtractCommentsOptions | undefined
) => Promise<MinimizedResult>;
type MinimizeFunctionHelpers = {
getMinimizerVersion?: (() => string | undefined) | undefined;
};
type MinimizerImplementation<T> = BasicMinimizerImplementation<T> &
MinimizeFunctionHelpers;
type InternalOptions<T> = {
name: string;
input: string;
inputSourceMap: RawSourceMap | undefined;
extractComments: ExtractCommentsOptions | undefined;
minimizer: {
implementation: MinimizerImplementation<T>;
options: MinimizerOptions<T>;
};
};
type MinimizerWorker<T> = Worker & {
transform: (options: string) => MinimizedResult;
minify: (options: InternalOptions<T>) => MinimizedResult;
};
type Parallel = undefined | boolean | number;
type InternalPluginOptions<T> = BasePluginOptions & {
minimizer: {
implementation: MinimizerImplementation<T>;
options: MinimizerOptions<T>;
};
};
import { minify } from "./minify";
import { Worker } from "jest-worker";

0 comments on commit 34fadde

Please sign in to comment.