Skip to content

Commit

Permalink
feat: added info.sourceFilename
Browse files Browse the repository at this point in the history
  • Loading branch information
webdeveric committed Oct 26, 2022
1 parent 7af4675 commit f8b3378
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ const {
* @template T
* @typedef {Object} InternalWorkerOptions
* @property {string} filename
* @property {AssetInfo=} info
* @property {Buffer} input
* @property {Transformer<T> | Transformer<T>[]} transformer
* @property {string} [severityError]
Expand Down Expand Up @@ -326,7 +327,7 @@ class ImageMinimizerPlugin {
const { RawSource } = compiler.webpack.sources;

const scheduledTasks = assetsForTransformers.map((asset) => async () => {
const { name, inputSource, cacheItem, transformer } = asset;
const { name, info, inputSource, cacheItem, transformer } = asset;
let { output } = asset;
let input;

Expand All @@ -343,6 +344,7 @@ class ImageMinimizerPlugin {
/** @type {InternalWorkerOptions<T>} */
({
filename: name,
info,
input,
severityError: this.options.severityError,
transformer,
Expand Down
2 changes: 1 addition & 1 deletion src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ async function loader(content) {
if (presets.length > 1) {
callback(
new Error(
"Found several identical pereset names, the 'preset' option should be unique"
"Found several identical preset names, the 'preset' option should be unique"
)
);

Expand Down
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const path = require("path");
*/

/**
* Run tasks with limited concurency.
* Run tasks with limited concurrency.
* @template T
* @param {number} limit - Limit of tasks that run at once.
* @param {Task<T>[]} tasks - List of tasks to run.
Expand Down
11 changes: 10 additions & 1 deletion src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,16 @@ async function worker(options) {
filename: options.filename,
warnings: [],
errors: [],
info: {},
info: {
sourceFilename:
options.info &&
typeof options.info === "object" &&
typeof options.info.sourceFilename === "string"
? options.info.sourceFilename
: typeof options.filename === "string"
? options.filename
: undefined,
},
};

if (!result.data) {
Expand Down
2 changes: 1 addition & 1 deletion test/ImageminPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ describe("imagemin plugin", () => {
const stringStats = stats.toString({ relatedAssets: true });

expect(stringStats).toMatch(
/asset loader-test.webp.+\[from: loader-test.webp\] \[generated\]/
/asset loader-test.webp.+\[from: loader-test.png\] \[generated\]/
);
});

Expand Down
2 changes: 1 addition & 1 deletion test/loader-generator-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe("loader generator option", () => {
expect(warnings).toHaveLength(0);
expect(errors).toHaveLength(1);
expect(errors[0].message).toMatch(
/Found several identical pereset names, the 'preset' option should be unique/
/Found several identical preset names, the 'preset' option should be unique/
);
});

Expand Down
4 changes: 3 additions & 1 deletion test/worker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,8 @@ describe("minify", () => {

expect(result.warnings).toHaveLength(0);
expect(result.errors).toHaveLength(0);
expect(result.info).toEqual({});
expect(result.info).toEqual({
sourceFilename: filename,
});
});
});
2 changes: 2 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export = ImageMinimizerPlugin;
* @template T
* @typedef {Object} InternalWorkerOptions
* @property {string} filename
* @property {AssetInfo=} info
* @property {Buffer} input
* @property {Transformer<T> | Transformer<T>[]} transformer
* @property {string} [severityError]
Expand Down Expand Up @@ -344,6 +345,7 @@ type Minimizer<T> = Omit<Transformer<T>, "preset" | "type">;
type Generator<T> = Transformer<T>;
type InternalWorkerOptions<T> = {
filename: string;
info?: AssetInfo | undefined;
input: Buffer;
transformer: Transformer<T> | Transformer<T>[];
severityError?: string | undefined;
Expand Down
2 changes: 1 addition & 1 deletion types/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export type SizeSuffix = (width: number, height: number) => string;
* @typedef {() => Promise<T>} Task
*/
/**
* Run tasks with limited concurency.
* Run tasks with limited concurrency.
* @template T
* @param {number} limit - Limit of tasks that run at once.
* @param {Task<T>[]} tasks - List of tasks to run.
Expand Down

0 comments on commit f8b3378

Please sign in to comment.