Skip to content

Commit 03c6274

Browse files
refactor: code (webpack-contrib#236)
1 parent 8a7542a commit 03c6274

File tree

3 files changed

+33
-25
lines changed

3 files changed

+33
-25
lines changed

src/index.js

+1-25
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { validate } = require("schema-utils");
44

55
const {
66
throttleAll,
7+
memoize,
78
cssnanoMinify,
89
cssoMinify,
910
cleanCssMinify,
@@ -151,31 +152,6 @@ const { minify: minifyWorker } = require("./minify");
151152

152153
const warningRegex = /\s.+:+([0-9]+):+([0-9]+)/;
153154

154-
/**
155-
* @template T
156-
* @param fn {(function(): any) | undefined}
157-
* @returns {function(): T}
158-
*/
159-
const memoize = (fn) => {
160-
let cache = false;
161-
/** @type {T} */
162-
let result;
163-
164-
return () => {
165-
if (cache) {
166-
return result;
167-
}
168-
result = /** @type {function(): any} */ (fn)();
169-
cache = true;
170-
// Allow to clean up memory for fn
171-
// and all dependent resources
172-
// eslint-disable-next-line no-undefined, no-param-reassign
173-
fn = undefined;
174-
175-
return result;
176-
};
177-
};
178-
179155
const getSerializeJavascript = memoize(() =>
180156
// eslint-disable-next-line global-require
181157
require("serialize-javascript")

src/utils.js

+26
Original file line numberDiff line numberDiff line change
@@ -490,8 +490,34 @@ async function swcMinify(input, sourceMap, minimizerOptions) {
490490
};
491491
}
492492

493+
/**
494+
* @template T
495+
* @param fn {(function(): any) | undefined}
496+
* @returns {function(): T}
497+
*/
498+
function memoize(fn) {
499+
let cache = false;
500+
/** @type {T} */
501+
let result;
502+
503+
return () => {
504+
if (cache) {
505+
return result;
506+
}
507+
result = /** @type {function(): any} */ (fn)();
508+
cache = true;
509+
// Allow to clean up memory for fn
510+
// and all dependent resources
511+
// eslint-disable-next-line no-undefined, no-param-reassign
512+
fn = undefined;
513+
514+
return result;
515+
};
516+
}
517+
493518
module.exports = {
494519
throttleAll,
520+
memoize,
495521
cssnanoMinify,
496522
cssoMinify,
497523
cleanCssMinify,

types/utils.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ export type Postcss = import("postcss").Postcss;
1717
* @returns {Promise<T[]>} A promise that fulfills to an array of the results
1818
*/
1919
export function throttleAll<T>(limit: number, tasks: Task<T>[]): Promise<T[]>;
20+
/**
21+
* @template T
22+
* @param fn {(function(): any) | undefined}
23+
* @returns {function(): T}
24+
*/
25+
export function memoize<T>(fn: (() => any) | undefined): () => T;
2026
/**
2127
* @param {Input} input
2228
* @param {RawSourceMap | undefined} sourceMap

0 commit comments

Comments
 (0)