svg-spritemap-webpack-plugin
Version:
Generates symbol-based SVG spritemap from all .svg files in a directory
83 lines (82 loc) • 3.63 kB
JavaScript
import path from 'node:path';
import assert from 'node:assert';
import webpack from 'webpack';
import { rimraf } from 'rimraf';
import { describe, it, afterEach } from 'node:test';
import SVGSpritemapPlugin from './index.js'; // eslint-disable-line @onefinity/eslint-config/import-grouping
afterEach(() => {
rimraf.sync(path.resolve(import.meta.dirname, '../dist/'));
});
describe('Options', () => {
const options = {
entry: 'data:text/javascript,',
mode: 'development'
};
it('does not generate spritemap when no sprites are found', (context, done) => {
webpack(Object.assign(Object.assign({}, options), { plugins: [
new SVGSpritemapPlugin('no-matches/**/*.svg')
] }), (errors, stats) => {
assert.strictEqual(errors, null);
assert.strictEqual(stats === null || stats === void 0 ? void 0 : stats.compilation.emittedAssets.size, 1);
done();
});
});
it('generates spritemap when sprites are found', (context, done) => {
webpack(Object.assign(Object.assign({}, options), { plugins: [
new SVGSpritemapPlugin([
'tests/input/svg/single.svg',
'tests/input/svg/title-tag.svg'
])
] }), (errors, stats) => {
assert.strictEqual(errors, null);
assert.ok(stats === null || stats === void 0 ? void 0 : stats.compilation.emittedAssets.has('spritemap.svg'));
done();
});
});
it('generates styles when enabled', (context, done) => {
webpack(Object.assign(Object.assign({}, options), { plugins: [
new SVGSpritemapPlugin('tests/input/svg/single.svg', {
styles: {
filename: 'sprite.scss'
}
})
] }), (errors, stats) => {
assert.ok(stats === null || stats === void 0 ? void 0 : stats.compilation.emittedAssets.has('sprite.scss'));
assert.strictEqual(errors, null);
rimraf.sync(path.resolve(import.meta.dirname, 'sprite.scss'));
done();
});
});
it('throws on invalid options', () => {
assert.throws(() => {
webpack(Object.assign(Object.assign({}, options), { plugins: [
new SVGSpritemapPlugin('tests/input/svg/single.svg', {
output: {
// @ts-expect-error - Invalid options
filename: 123
}
})
] }));
});
});
it('adds the svg4everybody helper to each entry', (context, done) => {
webpack(Object.assign(Object.assign({}, options), { entry: {
a: 'data:text/javascript,',
b: 'data:text/javascript,'
}, plugins: [
new SVGSpritemapPlugin('tests/input/svg/single.svg', {
output: {
svg4everybody: true
}
})
] }), (errors, stats) => {
assert.strictEqual(errors, null);
stats === null || stats === void 0 ? void 0 : stats.compilation.entries.forEach((entry) => {
var _a, _b;
assert.match((_b = (_a = entry.dependencies.at(1)) === null || _a === void 0 ? void 0 : _a.getResourceIdentifier()) !== null && _b !== void 0 ? _b : '', /svg4everybody-helper.js$/);
});
rimraf.sync(path.resolve(import.meta.dirname, 'svg4everybody-helper.js'));
done();
});
});
});