Last active
July 27, 2026 14:07
-
-
Save cyyynthia/c18e2f3d0a282f873fe3a17ae7fdbcd5 to your computer and use it in GitHub Desktop.
Cursed (but functional!) Rspack compatibility layer for `vite-plugin-magical-svg`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*! | |
| * SPDX-FileCopyrightText: 2026 Cynthia Rey | |
| * SPDX-License-Identifier: 0BSD | |
| */ | |
| async function process() { | |
| const plugin = this._compilation.options.plugins.find((p) => p.__is_magical_svg__) | |
| const ctx = { | |
| error: (msg) => this.emitError(new Error(msg)), | |
| warn: (msg) => this.emitWarning(new Error(msg)), | |
| } | |
| const { code } = await plugin.instance.load.handler.call(ctx, this.resource) | |
| const { code: res } = await plugin.instance.transform.handler.call(ctx, code, this.resource) | |
| return res | |
| } | |
| export default function () { | |
| const cb = this.async() | |
| process.call(this) | |
| .then( | |
| (r) => cb(null, r), | |
| (e) => cb(e), | |
| ) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*! | |
| * SPDX-FileCopyrightText: 2026 Cynthia Rey | |
| * SPDX-License-Identifier: 0BSD | |
| */ | |
| import { sources } from '@rspack/core' | |
| import magicalSvgPlugin from 'vite-plugin-magical-svg' | |
| export default class MagicalSvg { | |
| __is_magical_svg__ = true | |
| constructor(opts) { | |
| this.instance = magicalSvgPlugin(opts) | |
| } | |
| apply(compiler) { | |
| compiler.hooks.compilation.tap('MagicalSvg', (compilation) => { | |
| compilation.hooks.processAssets.tapPromise( | |
| { name: 'MagicalSvg', stage: compiler.rspack.PROCESS_ASSETS_STAGE_PRE_PROCESS }, | |
| (assets) => { | |
| let current | |
| const ctx = { | |
| getModuleInfo: () => ({}), | |
| emitFile: ({ fileName, source }) => | |
| compilation.emitAsset( | |
| fileName, | |
| new sources.RawSource(source), | |
| { immutable: true, minimized: true }, | |
| ), | |
| error: ({ message, cause, loc: { line, column } }) => { | |
| const err = new Error(`${message} (at ${line}:${column})`) | |
| err.cause = cause | |
| err.file = current | |
| throw err | |
| }, | |
| } | |
| for (const assetName in assets) { | |
| const asset = assets[current = assetName] | |
| try { | |
| const { code: transformed } = this.instance.renderChunk.call(ctx, asset.source().toString()) | |
| if (transformed) compilation.updateAsset(assetName, new sources.RawSource(transformed.toString())) | |
| } catch (e) { | |
| compilation.errors.push(e) | |
| } | |
| } | |
| return this.instance.generateBundle.call(ctx) | |
| } | |
| ) | |
| }) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*! | |
| * SPDX-FileCopyrightText: 2026 Cynthia Rey | |
| * SPDX-License-Identifier: 0BSD | |
| */ | |
| import MagicalSvg from './magical-svg-plugin.mjs' | |
| export default { | |
| entry: { | |
| main: '...' | |
| }, | |
| module: { | |
| rules: [ | |
| { | |
| test: /\.svg$/, | |
| loader: './magical-svg-loader.mjs', | |
| type: 'javascript/esm', | |
| }, | |
| ], | |
| }, | |
| plugins: [ | |
| new MagicalSvg(/* magical-svg options... */), | |
| ], | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment