Skip to content

Commit

Permalink
v1.0.1
Browse files Browse the repository at this point in the history
chore: add test fixture

feat: Add helper method for defaul export

feat: add test fixtures

wip: first pass

added prettier integration

fix: reverted prettier formating

fix: Indentation

changed package json

removed export template functions

fix: remove use of filename-> Component name

chore: cleanup opts names

reverted package name that i used to build a local version

add original npmrc
  • Loading branch information
ljharb authored and jb-san committed Nov 25, 2018
1 parent c4fe318 commit ff409ef
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
package-lock=false
package-lock=false
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "babel-plugin-inline-react-svg",
"version": "1.0.0",
"version": "1.0.1",
"description": "A babel plugin that optimizes and inlines SVGs for your react components.",
"main": "lib/index.js",
"scripts": {
Expand All @@ -14,7 +14,7 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/kesne/babel-plugin-inline-react-svg.git"
"url": "git@git.intern.folksam.se:frontend-modules/babel-plugin-inline-react-svg.git"
},
"keywords": [
"babel",
Expand Down
65 changes: 55 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { extname, dirname } from 'path';
import { extname, dirname, parse as parseFilename } from 'path';
import { readFileSync } from 'fs';
import { parse } from '@babel/parser';
import { declare } from '@babel/helper-plugin-utils';
Expand All @@ -19,16 +19,51 @@ export default declare(({
}) => {
assertVersion(7);

const buildSvg = template(`
var SVG_NAME = function SVG_NAME(props) { return SVG_CODE; };
`);

const buildSvgWithDefaults = template(`
var SVG_NAME = function SVG_NAME(props) { return SVG_CODE; };
SVG_NAME.defaultProps = SVG_DEFAULT_PROPS_CODE;
`);
const buildSvg = ({
IS_EXPORT, EXPORT_FILENAME, SVG_NAME, SVG_CODE,
}) => {
if (IS_EXPORT) {
if (SVG_NAME === 'default') {
return template(`
var Component = function(props) { return SVG_CODE; };
Component.displayName = 'EXPORT_FILENAME';
export default Component;
`)({ SVG_CODE, EXPORT_FILENAME });
}
return template(`
var SVG_NAME = function SVG_NAME(props) { return SVG_CODE; };
export { SVG_NAME };
`)({ SVG_NAME, SVG_CODE });
}
return template(`
var SVG_NAME = function SVG_NAME(props) { return SVG_CODE; };
`)({ SVG_NAME, SVG_CODE });
};

function applyPlugin(importIdentifier, importPath, path, state) {
const buildSvgWithDefaults = ({
IS_EXPORT, EXPORT_FILENAME, SVG_NAME, SVG_CODE, SVG_DEFAULT_PROPS_CODE,
}) => {
if (IS_EXPORT) {
if (SVG_NAME === 'default') {
return template(`
var Component = function(props) { return SVG_CODE; };
Component.defaultProps = SVG_DEFAULT_PROPS_CODE;
Component.displayName = 'EXPORT_FILENAME';
export default Component;
`)({ SVG_CODE, SVG_DEFAULT_PROPS_CODE, EXPORT_FILENAME });
}
return template(`
var SVG_NAME = function SVG_NAME(props) { return SVG_CODE; };
SVG_NAME.defaultProps = SVG_DEFAULT_PROPS_CODE;
export { SVG_NAME };
`)({ SVG_NAME, SVG_CODE, SVG_DEFAULT_PROPS_CODE });
}
return template(`
var SVG_NAME = function SVG_NAME(props) { return SVG_CODE; };
SVG_NAME.defaultProps = SVG_DEFAULT_PROPS_CODE;
`)({ SVG_NAME, SVG_CODE, SVG_DEFAULT_PROPS_CODE });
};
function applyPlugin(importIdentifier, importPath, path, state, isExport, exportFilename) {
if (typeof importPath !== 'string') {
throw new TypeError('`applyPlugin` `importPath` must be a string');
}
Expand Down Expand Up @@ -71,6 +106,8 @@ export default declare(({
const opts = {
SVG_NAME: importIdentifier,
SVG_CODE: svgCode,
IS_EXPORT: isExport,
EXPORT_FILENAME: exportFilename,
};

// Move props off of element and into defaultProps
Expand Down Expand Up @@ -137,6 +174,14 @@ export default declare(({
applyPlugin(node.specifiers[0].local, node.source.value, path, state);
}
},
ExportNamedDeclaration(path, state) {
const { node } = path;
if (node.specifiers.length > 0 && node.specifiers[0].local.name === 'default') {
const exportName = node.specifiers[0].exported.name;
const filename = parseFilename(node.source.value).name;
applyPlugin(exportName, node.source.value, path, state, true, filename);
}
},
},
};
});
2 changes: 2 additions & 0 deletions test/fixtures/close-a.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test/fixtures/test-export-default-as.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as IconClose } from "./close.svg";
1 change: 1 addition & 0 deletions test/fixtures/test-export-default.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "./close-a.svg";
20 changes: 20 additions & 0 deletions test/sanity.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,23 @@ transform(fs.readFileSync(filename), {
if (err) throw err;
console.log('test/fixtures/test-import-read-file.jsx', result.code);
});

transformFile('test/fixtures/test-export-default.jsx', {
presets: ['airbnb'],
plugins: [
inlineReactSvgPlugin,
],
}, (err, result) => {
if (err) throw err;
console.log('test/fixtures/test-export-default.jsx', result.code);
});

transformFile('test/fixtures/test-export-default-as.jsx', {
presets: ['airbnb'],
plugins: [
inlineReactSvgPlugin,
],
}, (err, result) => {
if (err) throw err;
console.log('test/fixtures/test-export-default-as.jsx', result.code);
});

0 comments on commit ff409ef

Please sign in to comment.