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

Named asset import for SVG files #3907

Merged
merged 9 commits into from
Feb 2, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
57 changes: 57 additions & 0 deletions packages/babel-plugin-named-asset-import/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'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]) {
const addLoader = loaderMap[ext];

path.replaceWithMultiple(
path.node.specifiers.map(specifier => {
const localName = specifier.local.name;

if (t.isImportDefaultSpecifier(specifier)) {
const newDefaultImport = t.importDeclaration(
[t.importDefaultSpecifier(t.identifier(localName))],
t.stringLiteral(sourcePath)
);

visited.add(newDefaultImport);
return newDefaultImport;
}

const newImport = t.importDeclaration(
[
t.importSpecifier(
t.identifier(localName),
t.identifier(specifier.imported.name)
),
],
t.stringLiteral(
addLoader(sourcePath, specifier.imported.name, localName)
)
);

visited.add(newImport);
return newImport;
})
);
}
},
},
};
}

module.exports = namedAssetImportPlugin;
17 changes: 17 additions & 0 deletions packages/babel-plugin-named-asset-import/package.json
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"
],
"dependencies": {
"@babel/core": "7.0.0-beta.38"
Copy link
Contributor

Choose a reason for hiding this comment

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

Why? Maybe this should be a peer dependency?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I originally wrote this plugin outside create-react-app so I needed this. I can change it to a peer dependency.

}
}
8 changes: 8 additions & 0 deletions packages/babel-preset-react-app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ module.exports = function(api, opts) {
regenerator: true,
},
],
[
Copy link
Contributor

Choose a reason for hiding this comment

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

This should probably have an opt-out flag in options. Just like the flow flag we now have.

require('babel-plugin-named-asset-import'),
Copy link
Contributor

Choose a reason for hiding this comment

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

This shouldn't be enabled in the test environment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was just about to ask for suggestions on how to fix these tests. 😀

I was trying to find a way to use Jest's moduleNameMapper to do something with the webpack loader strings but I will try just disabling the plugin in the test environment.

Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think about moving this to webpack configs instead of keeping it in the preset? That would make more sense to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I know we talked about that before and I tried it but I couldn't get it to work. For some reason when I move this to the webpack config it strips out functions. In this case the svg key would be present in the loaderMap object but it would just be an empty object. So the ReactComponent key would be removed presumably because its value is a function.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you push a commit that tries to implement it, even if it doesn’t work? I think we need to figure out what breaks there. Fine to do as another PR against your existing PR if you prefer so

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually try rebasing. We don’t write babelrc anymore (afaik) since monorepos landed. So it doesn’t get serialized on eject and thus it may be solved now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I merged the next branch (with the monorepo stuff) into this branch a couple of commits ago. That should have the same effect right? You're saying I should try moving this to the webpack config again and see if it works now?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes

{
loaderMap: {
svg: filename => `-!svg-react-loader!${filename}`,
},
Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, maybe this should be in webpack Babel config and not in our preset? Since it’s directly related to webpack specifically. This also would make a peer dependency on the loader unnecessary (it currently would be).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, either works. The only nice thing about keeping it here is that we don't have to duplicate it in the dev and prod webpack configs.

Copy link
Contributor

Choose a reason for hiding this comment

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

We can solve it in other ways (e.g. by actually starting to modularizing configs a little bit). Doesn't have to be solved here.

},
],
isEnvProduction && [
// Remove PropTypes from production build
require('babel-plugin-transform-react-remove-prop-types').default,
Expand Down
1 change: 1 addition & 0 deletions packages/babel-preset-react-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"@babel/preset-flow": "7.0.0-beta.38",
"@babel/preset-react": "7.0.0-beta.38",
"babel-plugin-macros": "2.0.0",
"babel-plugin-named-asset-import": "^0.1.0",
"babel-plugin-transform-dynamic-import": "2.0.0",
"babel-plugin-transform-react-remove-prop-types": "0.4.12"
}
Expand Down
25 changes: 0 additions & 25 deletions packages/react-scripts/config/webpack.config.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,31 +264,6 @@ module.exports = {
},
],
},
// Allows you to use two kinds of imports for SVG:
// import logoUrl from './logo.svg'; gives you the URL.
// import { ReactComponent as Logo } from './logo.svg'; gives you a component.
{
test: /\.svg$/,
use: [
{
loader: require.resolve('babel-loader'),
options: {
// @remove-on-eject-begin
babelrc: false,
presets: [require.resolve('babel-preset-react-app')],
// @remove-on-eject-end
cacheDirectory: true,
},
},
require.resolve('svgr/webpack'),
{
loader: require.resolve('file-loader'),
options: {
name: 'static/media/[name].[hash:8].[ext]',
},
},
],
},
// "file" loader makes sure those assets get served by WebpackDevServer.
// When you `import` an asset, you get its (virtual) filename.
// In production, they would get copied to the `build` folder.
Expand Down
25 changes: 0 additions & 25 deletions packages/react-scripts/config/webpack.config.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,31 +306,6 @@ module.exports = {
),
// Note: this won't work without `new ExtractTextPlugin()` in `plugins`.
},
// Allows you to use two kinds of imports for SVG:
// import logoUrl from './logo.svg'; gives you the URL.
// import { ReactComponent as Logo } from './logo.svg'; gives you a component.
{
test: /\.svg$/,
use: [
{
loader: require.resolve('babel-loader'),
options: {
// @remove-on-eject-begin
babelrc: false,
presets: [require.resolve('babel-preset-react-app')],
// @remove-on-eject-end
cacheDirectory: true,
},
},
require.resolve('svgr/webpack'),
{
loader: require.resolve('file-loader'),
options: {
name: 'static/media/[name].[hash:8].[ext]',
},
},
],
},
// "file" loader makes sure assets end up in the `build` folder.
// When you `import` an asset, you get its filename.
// This loader doesn't use a "test" so it will catch all modules
Expand Down
2 changes: 1 addition & 1 deletion packages/react-scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"raf": "3.4.0",
"react-dev-utils": "^5.0.0",
"style-loader": "0.19.1",
"svgr": "1.6.0",
"svg-react-loader": "^0.4.5",
"sw-precache-webpack-plugin": "0.11.4",
"thread-loader": "1.1.2",
"uglifyjs-webpack-plugin": "1.1.6",
Expand Down