-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
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
Treat all unknown extensions as resources #667
Comments
Looking at webpack.config.dev.js, I'm wondering: why use url-loader for audio / video, but file-loader for images? In the scenarios I'm most familiar with, you'd usually do the complete opposite of that: you want essential images, like sprite sheets, to load immediately, but sound and video can wait. I think this using file-loader for unknown resources is a good default. |
I didn’t really think it through. If you have better ideas for defaults please submit a PR. |
@gaearon Any word on this? I wanted to use a php file, to ajax from a form. But I have to manually copy it over after the build process since there's no included loader for php |
@iRoachie I think any backend files (like php) should live outside the project folder. The project folder is only meant to be for files related to the front-end client-side app. |
This is a good example of working with backend APIs. It assumes Node but PHP would work similarly. |
How's this similar to PHP? I'm using the php to send and email when the data from the form is posted. The link you provided assumes using Node and using endpoints to access the data. |
By similar I meant that, like in that tutorial, you can put |
@gaearon I messaged you on twitter |
This is interesting. We are having the same issue with the angular-cli. In my opinion, regardless of "how" you treat the file (emitted with file-loader into dist, or converted to js, etc), my opinion is that you want it managed by webpack in some way or another. A catchall unknown-source loader feels like it would be pretty beneficial. I wonder if perhaps leveraging the |
@fson, @gaearon I can take this if we agree on a few assumptions:
|
Let's reopen until follow up comments from #1059 are addressed. |
@gaearon app is broken. When changing regex in default loader per last PR suggestion it captures the |
Some guidance needed to complete PR comments: In eslint config L49, should it not be Add Do we need line 47 at all, or is specifying Validation of default loader: As we could have loader regexes in test, include or exclude, I think validation that works now for most changes is more robust than trying to calculate the default matches. E.g. below naive validation for the default loader that's hard-coded to the current setting - it has false negatives but perhaps that's acceptable. See proposed jest config - not an expert on jest, any advice for a good test?
|
I think relying on the order of loaders to validate them is going to be very brittle. Then instead of breaking the loader config you'll break the validator. If we think forgetting to add file types to the default file loader is going to be an issue, maybe we should just compose the loader config from separate parts like this: var loaders = [
{
test: /\.(js|jsx)$/,
loader: 'babel', ...
},
{
test: /\.css$/,
loader: ...
},
...
];
var defaultLoader = {
exclude: loaders.map((loader) => loader.test),
loader: 'url',
};
// Then in the config:
...
loaders: loaders.concat(defaultLoader), Yes, it does add some indirection, but it's a tradeoff. |
@JSON I was getting started with this approach but then realized this will break when someone adds include/exclude statements to existing or new loaders. In this case, chances are high that the default loader would break and would do so silently. I think the validator idea has the advantage that it serves as a pull linkage - yes it will fail on changes, but most likely failure will be verbose, which will remind the dev to update the loader and/or the validator. |
Unless we take in account each The best thing we can do is keep the config as easy to understand and simple as possible. A check at the end of the file that assumes the first loader is always the catch-all loader and has an I propose we either compose the catch-all rule from the others like I suggested above, or just have big warning comment that you must add new file extensions to the excludes. Maybe @gaearon has something to say because he had the idea about validating the config in the first place. :) |
If it's too hard don't validate. :-) |
Eh - sorry @fson for calling you json above :-)) you can see my brain's loaded with loaders. Agree with lack of simplicity. But lets not auto-build a loader for the reasons you say, I think it would silently fail on next changes. Jest - I'll leave the regex verification up to you in this case if you don't mind. Thanks! |
Turns out our regex in the Jest As a stopgap measure, I reverted the Jest config back to the previous regex which whitelists non-JS/CSS extensions separately, but we still need to find a way to get the same behaviour as our Webpack configuration in Jest. Maybe @cpojer can help us here? We're trying to create a Jest config that matches this Webpack config: module: {
loaders: [
{
exclude: [
/\.html$/,
/\.(js|jsx)$/,
/\.css$/,
/\.json$/
],
loader: 'url',
...
},
{
test: /\.(js|jsx)$/,
include: paths.appSrc,
loader: 'babel',
...
},
{
test: /\.css$/,
loader: 'style!css?importLoaders=1!postcss'
},
{
test: /\.json$/,
loader: 'json'
}
]
}, Files with The first attempt was a regex with a negative lookahead like this: moduleNameMapper: {
'^.+\\.(?!(js|jsx|css|json)$)[^\\.]+$': resolve('config/jest/FileStub.js'),
'^.+\\.css$': resolve('config/jest/CSSStub.js')
} However, this doesn't work because Is it even possible at the moment to replicate this Webpack config in Jest? I think being able to somehow match against the absolute file path would be necessary for that. |
What @cpojer told me:
|
Thanks Dan for sharing my messages to you. Yes, the transform field is meant to be used for this. If you guys are building
etc. |
I’m getting a bit tired of extending our whitelist like in #665.
I think we should have a “catch all” loader that uses
file-loader
for any unknown extensions.This makes sense because it’s easy to explain: “if you import something that isn’t JS or CSS, it becomes a part of the build, you get its filename, and can do whatever with it”.
I think @dralletje planned to look into this.
The text was updated successfully, but these errors were encountered: