-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Named asset import for SVG files (#3907)
* Add named asset import for svg files via babel plugin and webpack loader. * Fix failing e2e test * Switched to svgr loader * Updated SVG component test * Disable named asset import plugin in test environment * Added tests for including SVG in CSS * Update tests * Moved babel plugin config into webpack config
- Loading branch information
Showing
11 changed files
with
152 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
'use strict'; | ||
|
||
const { extname } = require('path'); | ||
|
||
function namedAssetImportPlugin({ types: t }) { | ||
const visited = new WeakSet(); | ||
|
||
return { | ||
visitor: { | ||
ImportDeclaration(path, { opts: { loaderMap } }) { | ||
const sourcePath = path.node.source.value; | ||
const ext = extname(sourcePath).substr(1); | ||
|
||
if (visited.has(path.node) || sourcePath.indexOf('!') !== -1) { | ||
return; | ||
} | ||
|
||
if (loaderMap[ext]) { | ||
path.replaceWithMultiple( | ||
path.node.specifiers.map(specifier => { | ||
if (t.isImportDefaultSpecifier(specifier)) { | ||
const newDefaultImport = t.importDeclaration( | ||
[ | ||
t.importDefaultSpecifier( | ||
t.identifier(specifier.local.name) | ||
), | ||
], | ||
t.stringLiteral(sourcePath) | ||
); | ||
|
||
visited.add(newDefaultImport); | ||
return newDefaultImport; | ||
} | ||
|
||
const newImport = t.importDeclaration( | ||
[ | ||
t.importSpecifier( | ||
t.identifier(specifier.local.name), | ||
t.identifier(specifier.imported.name) | ||
), | ||
], | ||
t.stringLiteral( | ||
loaderMap[ext][specifier.imported.name] | ||
? loaderMap[ext][specifier.imported.name].replace( | ||
/\[path\]/, | ||
sourcePath | ||
) | ||
: sourcePath | ||
) | ||
); | ||
|
||
visited.add(newImport); | ||
return newImport; | ||
}) | ||
); | ||
} | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
module.exports = namedAssetImportPlugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"name": "babel-plugin-named-asset-import", | ||
"version": "0.1.0", | ||
"description": "Babel plugin for named asset imports in Create React App", | ||
"repository": "facebookincubator/create-react-app", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/facebookincubator/create-react-app/issues" | ||
}, | ||
"main": "index.js", | ||
"files": [ | ||
"index.js" | ||
], | ||
"peerDependencies": { | ||
"@babel/core": "7.0.0-beta.38" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
packages/react-scripts/fixtures/kitchensink/src/features/webpack/SvgInCss.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import React from 'react'; | ||
import './assets/svg.css'; | ||
|
||
export default () => <div id="feature-svg-in-css" />; |
10 changes: 10 additions & 0 deletions
10
packages/react-scripts/fixtures/kitchensink/src/features/webpack/SvgInCss.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import SvgInCss from './SvgInCss'; | ||
|
||
describe('svg in css', () => { | ||
it('renders without crashing', () => { | ||
const div = document.createElement('div'); | ||
ReactDOM.render(<SvgInCss />, div); | ||
}); | ||
}); |
3 changes: 3 additions & 0 deletions
3
packages/react-scripts/fixtures/kitchensink/src/features/webpack/assets/svg.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#feature-svg-in-css { | ||
background-image: url("./logo.svg"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters