Skip to content

Commit

Permalink
Merge pull request #28 from jvoccia/fix-callback-already-called
Browse files Browse the repository at this point in the history
Fix callback already called
  • Loading branch information
yoriiis committed May 21, 2024
2 parents 00db1ee + 6f80fb9 commit 716262d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
26 changes: 13 additions & 13 deletions src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ export default async function SvgChunkWebpackLoader(
const compiler = this._compiler;
const callback = this.async();

// Check if the plugin is also imported
const plugin = compiler.options.plugins.find(
(plugin: any) => plugin.PLUGIN_NAME && plugin.PLUGIN_NAME === PACKAGE_NAME
);
if (typeof plugin === 'undefined') {
return callback(new Error(`${PACKAGE_NAME} requires the corresponding plugin`));
}

// Check if content is a SVG file
if (!content.includes('<svg')) {
return callback(new Error(`${PACKAGE_NAME} exception. ${content}`));
}

// Declare all SVG files as side effect
// https://github.com/webpack/webpack/issues/12202#issuecomment-745537821
this._module.factoryMeta = this._module.factoryMeta || {};
Expand All @@ -34,19 +47,6 @@ export default async function SvgChunkWebpackLoader(
// Flag all SVG files to find them more easily on the plugin side
this._module.buildInfo.SVG_CHUNK_WEBPACK_PLUGIN = true;

// Check if content is a SVG file
if (!content.includes('<svg')) {
callback(new Error(`${PACKAGE_NAME} exception. ${content}`));
}

// Check if the plugin is also imported
const plugin = compiler.options.plugins.find(
(plugin: any) => plugin.PLUGIN_NAME && plugin.PLUGIN_NAME === PACKAGE_NAME
);
if (typeof plugin === 'undefined') {
callback(new Error(`${PACKAGE_NAME} requires the corresponding plugin`));
}

try {
const { configFile } = options;
let config;
Expand Down
12 changes: 10 additions & 2 deletions tests/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,26 @@ describe('Loader with errors', () => {
it('Should call the loader function with a wrong SVG', async () => {
await loader.call(_this, 'wrong svg');

expect.assertions(1);
expect.assertions(3);
expect(callback).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledWith(new Error(`${PACKAGE_NAME} exception. wrong svg`));

// Don't set to process if SVG is bad.
expect(_this._module.buildInfo.SVG_CHUNK_WEBPACK_PLUGIN).toBeUndefined();
});

it('Should call the loader function without the plugin imported', async () => {
_this._compiler.options.plugins = [];

await loader.call(_this, '<svg></svg>');

expect.assertions(1);
expect.assertions(3);
expect(callback).toHaveBeenCalledTimes(1);
expect(callback).toHaveBeenCalledWith(
new Error(`${PACKAGE_NAME} requires the corresponding plugin`)
);

// Don't set to process if No Plugin is set to process it
expect(_this._module.buildInfo.SVG_CHUNK_WEBPACK_PLUGIN).toBeUndefined();
});
});

0 comments on commit 716262d

Please sign in to comment.