Skip to content

Commit

Permalink
Fix SVG transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
rafeca committed May 30, 2018
1 parent 8da7767 commit b5b8034
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 14 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"dependencies": {
"react": "16.3.1",
"react-native": "0.55.4",
"react-native-svg": "^6.3.1",
"react-native-svg-loader": "^0.1.5"
},
"devDependencies": {
Expand Down
17 changes: 11 additions & 6 deletions rn-cli.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
module.exports = {
const defaultAssetExts = require('metro/src/defaults').assetExts;

module.exports = {
assetTransforms: true,
getPlatforms: () => ["ios", "android"],
getTransformModulePath() {
return require.resolve('./svg_transformer')
getTransformModulePath() {
return require.resolve('./svg_transformer')
},
getAssetExts() {
return ["svg"];
}
};
return defaultAssetExts.filter(assetExt => assetExt !== 'svg');
},
getSourceExts() {
return ['svg'];
},
};
15 changes: 10 additions & 5 deletions svg_transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,27 @@ const path = require('path')
const transformSvgSync = require('react-native-svg-loader');
// For React Native version 0.52 or later
var upstreamTransformer = require("metro/src/transformer");


const babylon = require('babylon');

// For React Native version 0.47-0.51
// var upstreamTransformer = require("metro-bundler/src/transformer");

// For React Native version 0.46
// var upstreamTransformer = require("metro-bundler/build/transformer");

module.exports.transform = function({ src, filename, options }) {
module.exports.transform = function({ src, filename, options }) {
const basename = path.basename(filename)
console.log(basename)

if (filename.endsWith('.svg')) {
console.log('==============================================')
console.log('SVG!', filename)
return transformSvgSync(src);

return {
ast: babylon.parse(transformSvgSync(src), {sourceType: 'module'}),
}
} else {
return upstreamTransformer.transform({ src, filename, options });
}
};
};
38 changes: 35 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1541,20 +1541,34 @@ collection-visit@^1.0.0:
map-visit "^1.0.0"
object-visit "^1.0.0"

color-convert@^1.9.0:
color-convert@^1.9.0, color-convert@^1.9.1:
version "1.9.1"
resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-1.9.1.tgz#c1261107aeb2f294ebffec9ed9ecad529a6097ed"
dependencies:
color-name "^1.1.1"

color-name@^1.1.1:
color-name@^1.0.0, color-name@^1.1.1:
version "1.1.3"
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.3.tgz#a7d0558bd89c42f795dd42328f740831ca53bc25"

color-string@^1.5.2:
version "1.5.2"
resolved "https://registry.yarnpkg.com/color-string/-/color-string-1.5.2.tgz#26e45814bc3c9a7cbd6751648a41434514a773a9"
dependencies:
color-name "^1.0.0"
simple-swizzle "^0.2.2"

color-support@^1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/color-support/-/color-support-1.1.3.tgz#93834379a1cc9a0c61f82f52f0d04322251bd5a2"

color@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/color/-/color-2.0.1.tgz#e4ed78a3c4603d0891eba5430b04b86314f4c839"
dependencies:
color-convert "^1.9.1"
color-string "^1.5.2"

combined-stream@1.0.6, combined-stream@~1.0.5:
version "1.0.6"
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.6.tgz#723e7df6e801ac5613113a7e445a9b69cb632818"
Expand Down Expand Up @@ -2514,6 +2528,10 @@ is-arrayish@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/is-arrayish/-/is-arrayish-0.2.1.tgz#77c99840527aa8ecb1a8ba697b80645a7a926a9d"

is-arrayish@^0.3.1:
version "0.3.1"
resolved "https://registry.yarnpkg.com/is-arrayish/-/is-arrayish-0.3.1.tgz#c2dfc386abaa0c3e33c48db3fe87059e69065efd"

is-buffer@^1.1.5:
version "1.1.6"
resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.6.tgz#efaa2ea9daa0d7ab2ea13a97b2b8ad51fefbe8be"
Expand Down Expand Up @@ -3295,7 +3313,7 @@ lodash@^3.5.0:
version "3.10.1"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-3.10.1.tgz#5bf45e8e49ba4189e17d482789dfd15bd140b7b6"

lodash@^4.13.1, lodash@^4.17.10, lodash@^4.17.4, lodash@^4.17.5, lodash@^4.3.0, lodash@^4.6.1:
lodash@^4.13.1, lodash@^4.16.6, lodash@^4.17.10, lodash@^4.17.4, lodash@^4.17.5, lodash@^4.3.0, lodash@^4.6.1:
version "4.17.10"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.10.tgz#1b7793cf7259ea38fb3661d4d38b3260af8ae4e7"

Expand Down Expand Up @@ -4153,6 +4171,14 @@ react-native-svg-loader@^0.1.5:
dependencies:
xmldom "^0.1.27"

react-native-svg@^6.3.1:
version "6.3.1"
resolved "https://registry.yarnpkg.com/react-native-svg/-/react-native-svg-6.3.1.tgz#06a573f17c59ce953a56fd62e776fd79d632d765"
dependencies:
color "^2.0.1"
lodash "^4.16.6"
pegjs "^0.10.0"

react-native@0.55.4:
version "0.55.4"
resolved "https://registry.yarnpkg.com/react-native/-/react-native-0.55.4.tgz#eecffada3750a928e2ddd07cf11d857ae9751c30"
Expand Down Expand Up @@ -4635,6 +4661,12 @@ simple-plist@^0.2.1:
bplist-parser "0.1.1"
plist "2.0.1"

simple-swizzle@^0.2.2:
version "0.2.2"
resolved "https://registry.yarnpkg.com/simple-swizzle/-/simple-swizzle-0.2.2.tgz#a4da6b635ffcccca33f70d17cb92592de95e557a"
dependencies:
is-arrayish "^0.3.1"

slash@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/slash/-/slash-1.0.0.tgz#c41f2f6c39fc16d1cd17ad4b5d896114ae470d55"
Expand Down

0 comments on commit b5b8034

Please sign in to comment.