Skip to content

Instantly share code, notes, and snippets.

@shisama
Last active May 11, 2017 00:49
Show Gist options
  • Save shisama/23fd4fcb66197cdc8f22444679077a54 to your computer and use it in GitHub Desktop.
Save shisama/23fd4fcb66197cdc8f22444679077a54 to your computer and use it in GitHub Desktop.
import React from "react";
import svg from "./sample.svg";
export default class Sample extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
<span className="logo" dangerouslySetInnerHTML={{__hrml: svg}} />
</div>
)
}
}
module.exports = {
entry: entries,
output: {
path: dst,
filename: '[name].bundle.js'
},
resolve: {
root: [path.resolve('./vlsn/js')],
extensions: ['', '.js', '.jsx', '.es6']
},
plugins: [],
module: {
loaders: [
{
test: /\.(js|jsx|es6)$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
plugins: ["transform-class-properties"]
}
},
{
test: /\.css?$/,
loaders: ['style-loader', 'css-loader?modules&localIdentName=[path][name]__[local]--[hash:base64:5]']
},
{
test: /\.svg$/,
loader: 'svg-inline-loader?classPrefix'
}
]
}
};
@shisama
Copy link
Author

shisama commented May 11, 2017

svgファイルをReactコンポーネント化してもいいのですが、面倒くさい場合とかに有効そう。
imgタグで読み込んでもいいけど、jsに入れ込みたい需要はどこかでありそう。
肝はwebpackのloaderでsvg-inline-loaderを呼んでいるところ。
これでcss-modulesみたいにsvgをimportすることができるようになる

でも、このやりかただとstorybookでは正しく表示されなかった。
innderHTMLだからかな・・・

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment