Skip to content

Commit

Permalink
[New] Add support for export default from
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Oct 22, 2018
1 parent a39dfa5 commit 4ed020e
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 11 deletions.
2 changes: 1 addition & 1 deletion 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.1",
"version": "1.0.0",
"description": "A babel plugin that optimizes and inlines SVGs for your react components.",
"main": "lib/index.js",
"scripts": {
Expand Down
47 changes: 37 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,33 @@ export default declare(({
}) => {
assertVersion(7);

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

if (SVG_NAME !== 'default') {
return template(namedTemplate)({ SVG_NAME, SVG_CODE, SVG_DEFAULT_PROPS_CODE });
}
return template(anonymousTemplate)({ SVG_CODE, SVG_DEFAULT_PROPS_CODE, EXPORT_FILENAME });
};

const buildSvgWithDefaults = template(`
var SVG_NAME = function SVG_NAME(props) { return SVG_CODE; };
SVG_NAME.defaultProps = SVG_DEFAULT_PROPS_CODE;
`);

function applyPlugin(importIdentifier, importPath, path, state) {
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 +88,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 All @@ -91,7 +110,7 @@ export default declare(({
}

if (opts.SVG_DEFAULT_PROPS_CODE) {
const svgReplacement = buildSvgWithDefaults(opts);
const svgReplacement = buildSvg(opts);
path.replaceWithMultiple(svgReplacement);
} else {
const svgReplacement = buildSvg(opts);
Expand Down Expand Up @@ -137,6 +156,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') {

This comment has been minimized.

Copy link
@ntucker

ntucker May 10, 2019

I'm getting "TypeError: Cannot read property 'name' of undefined" on this line

This comment has been minimized.

Copy link
@ljharb

ljharb May 11, 2019

Author Collaborator

Please file an issue with repro details.

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 4ed020e

Please sign in to comment.