Proposal: explicit named imports for non-JS/CSS assets
See original GitHub issueProblem
We currently allow you to do this:
import logo from './logo.png';
After getting used to it, you’ll probably be comfortable with this giving you a URL.
But what about other types? For example, what should this return?
import doc from './doc.md';
Markdown source? Compiled HTML? An AST?
What about this?
import Icon from './icon.svg';
Should this give you a link? The SVG content? A React component?
The usual answer is “decide it for yourself in the configuration file”. However, that doesn’t work for CRA so we decided to treat all unknown extensions as URL imports. This is not ideal because in some cases it just doesn’t make any sense, and in others there are advanced (but still relatively common) use cases that aren’t satisfied.
Proposal
What if we allowed to user to pick what they want, from a limited supported subset per filetype?
import { url as logoUrl } from './logo.png';
import { html as docHtml } from './doc.md';
import { ReactComponent as Icon } from './icon.svg';
Named imports are checked by webpack so you’d get a compile error if you use an unsupported one.
Things that are unused will be tree shaken so if you only use e.g. HTML of Markdown files, their source won’t be bundled. Same for SVGs (whether you consume them as raw source, URLs, or React components).
Other zero-configuration tools can also adopt this approach.
Concerns
- What do we do with the default import? Ideally I’d like to forbid it for anything other than JS/CSS because the intent is not clear for asset files (which version do you get?) We could do this with a lint rule.
- If we make the breaking change, how do we update the consumers? We could write a codemod (it should be very simple).
- It would be nice to coordinate this across at least a few other projects (e.g. @ndelangen Storybook). Maybe @KyleAMathews (Gatsby) @devongovett (Parcel) @rauchg (Next) would also be interested? I imagine we’ll need to write a multi-file Webpack loader for this, but I don’t see why other bundlers couldn’t adopt a similar convention.
- Build performance: generating all possible content variations can be too slow. Ideally it would be nice if loaders had information about which import was used.
Thoughts?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:240
- Comments:62 (22 by maintainers)
Top GitHub Comments
@dmwyatt, you can use a babel-plugin-macro such as raw.macro.
File contents will be bundled within
main.[hash].chunk.js
.@gaearon, Is this still under consideration?