babel-register
for SVG files.™
npm install --save-dev inline-svg-register
require("inline-svg-register")
in any file, as long as it comes before you require()
any SVG files.
require("inline-svg-register");
var svg = require("./my-cool.svg");
inline-svg-register
's export is a function that, when called, will unregister the require()
hook that it added.
var unhook = require("inline-svg-register");
var svg = require("./my-cool.svg");
unhook();
Imagine that you have a Webpack-bundled JavaScript app that uses the raw-loader
to require()
in SVG file contents as a string.
You also have a very simple tape
test setup that just require()
s in whatever files that you want to test.
If you try to test any files that contain require()
calls to SVG files, node will throw a parsing error on those calls because node expects only JavaScript files to be passed to require()
.
CoffeeScript and Babel both get around this constraint by extending node's file type support using a deprecated feature: require.extensions
. This feature allows node to compile CoffeeScript or ES2015 on-the-fly to JavaScript that node can parse. I adapted Babel's solution, called babel-register
, to compile an SVG file on-the-fly to a JavaScript string.