Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix transforming via API #53

Merged
merged 2 commits into from
Oct 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ $ babel --plugins inline-react-svg script.js

```javascript
require('@babel/core').transform('code', {
plugins: ['inline-react-svg']
plugins: [
['inline-react-svg', { filename: 'filename representing the code' }],
]
}) // => { code, map, ast };
```

Expand Down
14 changes: 10 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export default declare(({
if (typeof importPath !== 'string') {
throw new TypeError('`applyPlugin` `importPath` must be a string');
}
const { ignorePattern, caseSensitive } = state.opts;
const { file } = state;
const { ignorePattern, caseSensitive, filename: providedFilename } = state.opts;
const { file, filename } = state;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like filename was originally coming from state.file.opts on ln 47, was this updated somewhere?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm no longer using that value, but it seems like it will be populated by state.filename when transforming a file, and state.opts.filename when the option is provided, so this seemed more straightforward.

if (ignorePattern) {
// Only set the ignoreRegex once:
ignoreRegex = ignoreRegex || new RegExp(ignorePattern);
Expand All @@ -44,7 +44,7 @@ export default declare(({
}
// This plugin only applies for SVGs:
if (extname(importPath) === '.svg') {
const iconPath = state.file.opts.filename;
const iconPath = filename || providedFilename;
const svgPath = resolve.sync(importPath, { basedir: dirname(iconPath) });
if (caseSensitive && !fileExistsWithCaseSync(svgPath)) {
throw new Error(`File path didn't match case of file on disk: ${svgPath}`);
Expand Down Expand Up @@ -105,7 +105,13 @@ export default declare(({
return {
visitor: {
Program: {
enter({ scope, node }, { file }) {
enter({ scope, node }, { file, opts, filename }) {
if (typeof filename === 'string' && typeof opts.filename !== 'undefined') {
throw new TypeError('the "filename" option may only be provided when transforming code');
}
if (typeof filename === 'undefined' && typeof opts.filename !== 'string') {
throw new TypeError('the "filename" option is required when transforming code');
}
if (!scope.hasBinding('React')) {
const reactImportDeclaration = t.importDeclaration([
t.importDefaultSpecifier(t.identifier('React')),
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/test-import-read-file.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react';
import MySvg from './close.svg';

export default function MyFunctionIcon() {
return <MySvg />;
}

export class MyClassIcon extends React.Component {
render() {
return <MySvg />;
}
}
15 changes: 13 additions & 2 deletions test/sanity.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { transformFile } from '@babel/core';
import { transformFile, transform } from '@babel/core';
import fs from 'fs';
import path from 'path';
import inlineReactSvgPlugin from '../src';
Expand Down Expand Up @@ -71,7 +71,7 @@ if (fs.existsSync(path.resolve('./PACKAGE.JSON'))) {
if (err && err.message.indexOf('match case') !== -1) {
console.log('test/fixtures/test-case-sensitive.jsx', 'Test passed: Expected case sensitive error was thrown');
} else {
throw new Error('Test failed: Expected case sensitive error wasn‘t thrown');
throw new Error('Test failed: Expected case sensitive error wasn‘t thrown, got: ' + err.message);
}
});
} else {
Expand Down Expand Up @@ -122,3 +122,14 @@ transformFile('test/fixtures/test-dynamic-require.jsx', {
if (err) throw err;
console.log('test/fixtures/test-dynamic-require.jsx', result.code);
});

const filename = 'test/fixtures/test-import-read-file.jsx';
transform(fs.readFileSync(filename), {
presets: ['airbnb'],
plugins: [
[inlineReactSvgPlugin, { filename }],
],
}, (err, result) => {
if (err) throw err;
console.log('test/fixtures/test-import-read-file.jsx', result.code);
});