aboutsummaryrefslogtreecommitdiff
path: root/src/atlas-plugin.js
blob: 69f11742eed03659f30d6d94071b4f0be5e60660 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
const fs = require("fs")
const path = require("path")
const { RawSource } = require("webpack-sources")
const { generateAtlasViews } = require("./utils.js")


class SVGSymbolAtlasViewPlugin {
	constructor(options = {}) {
		this.suffix = options.suffix || ".svg"
		this.symbolId = options.symbolId || null
	}

	apply(compiler) {
		compiler.hooks.thisCompilation.tap("SVGSymbolAtlasViewPlugin", (compilation) => {
			compilation.hooks.processAssets.tap(
				{
					name: "SVGSymbolAtlasViewPlugin",
					stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE,
				},
				(assets) => {
					for (const assetName in assets) {
						if (assetName.endsWith(this.suffix)) {
							const originalSVG = assets[assetName].source().toString()
							const atlasSVG = generateAtlasViews(originalSVG, this.symbolId)

							// Replace old SVG asset with new SVG
							compilation.updateAsset(assetName, new RawSource(atlasSVG))
						}
					}
				}
			)
		})
	}
}

module.exports = SVGSymbolAtlasViewPlugin