Open jakub-g opened 5 years ago
Alright so for the future visitors, I managed to make this loader compatible with preact
v8 without using preact-compat
as follows:
return {
rules: [
{
test: /\.svg$/,
use: [
// Note the loaders are applied in reverse order!
// 2. Transform JSX to JS.
// Basically copied the existing babel config for JSX files here.
{
loader: 'babel-loader',
options: {
presets: [['env', presetEnvLegacySettings]],
plugins: [
['transform-object-rest-spread', { useBuiltIns: true }],
['transform-react-jsx', { pragma: 'h' }],
]
},
},
// 1. Load SVG and transform it to JSX
{
loader: 'react-svg-loader',
options: {
jsx: true,
},
},
],
},
- path.node.body.unshift(t.importDeclaration([t.importDefaultSpecifier(t.identifier("React"))], t.stringLiteral("react")));
+ path.node.body.unshift(t.importDeclaration([t.importSpecifier(t.identifier("h"), t.identifier("h"))], t.stringLiteral("preact")));
babel-loader
+ options: { jsx: true,},
but instead modify react-svg-core as follows:- presets: [jsx ? void 0 : "@babel/preset-react"].filter(Boolean),
+ presets: jsx ? [] : [["@babel/preset-react", {pragma: 'h'}]],
Another thing I tried but it didn't quite work: without passing { jsx: true }
to the plugin and add babel-loader, you could m
but this still requires preact-compat
to work (it is require
d but not used, but somehow webpack doesn't discard it).
Hello, I was trying to use the loader with preact (bundling with webpack) and here's what I've found for other interested people.
I used following webpack conf:
Outside of the box, the module does not work with
preact
because of a hard dependency onreact
. When trying to compile the app, there's an error like this:I managed to fix it by installing
preact-compat
and aliasing it asreact
in webpack config:This works but is not ideal, as
preact-compat
is 13kB (5 kB gzipped) which seems wasteful because the only thing necessary for the module to work isReact.createElement
which should be generally whatpreact.h
does.I will try to find out if there's some way to have a build-time replacement of this instead of runtime dependency on
preact-compat
. If anyone knows how to do it, please let me know.