-
-
Notifications
You must be signed in to change notification settings - Fork 256
Ability to specify custom public file name for resources #81
Conversation
: config.publicPath + url | ||
typeof config.publicPath === "function" | ||
? config.publicPath(url) | ||
: publicUrl |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is going to behave as intended. publicUrl
isn't defined here, it was initialized only inside the if
branch of the if...else
. Shouldn't this be the original code (config.publicPath + url
)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ya you're right, this was a typo.
If I understand you correctly, you're not trying to modify/replace the As far as I can tell, this is more or less the idea of #57 that added var config = {
outputPath: false,
publicPath: false,
name: "[hash].[ext]"
};
// options takes precedence over config
Object.keys(options).forEach(function(attr) {
config[attr] = options[attr];
});
// query takes precedence over config and options
Object.keys(query).forEach(function(attr) {
config[attr] = query[attr];
});
var url = loaderUtils.interpolateName(this, config.name, {
context: config.context || this.options.context,
content: content,
regExp: config.regExp
});
var outputPath = url;
var publicPath = "__webpack_public_path__ + " + JSON.stringify(url);
if (config.outputPath) {
// support functions as outputPath to generate them dynamically
outputPath = JSON.stringify(
typeof config.outputPath === "function"
? config.outputPath(url)
: config.outputPath + url
);
}
if (config.publicPath) {
// support functions as publicPath to generate them dynamically
publicPath = JSON.stringify(
typeof config.publicPath === "function"
? config.publicPath(url)
: config.publicPath + url
);
}
if (query.emitFile === undefined || query.emitFile) {
this.emitFile(outputPath, content);
}
return "module.exports = " + publicPath + ";"; |
This should allow you to configure the loader like this and I think it would also make it easier to reason about the behavior. loader: "file-loader?name=[name].[ext]&publicPath=assets/foo/&outputPath=app/images/" |
Ya ok, that works too. I've updated the code to go with your pattern. Thanks for your input. |
Cool 👍 One other thing: I'd add a |
Why's that? Wouldn't its lack of presence evaluate to the same thing? |
It makes the code a bit more readable. You can look at the config object and know right away that there's a default It's not really crucial in this small of a file, but it's generally a good practice. |
@mobitar described a feature that does exactly what I need. Being able to specify a I am using Play! that has its own little asset controller that handles the typical aggressive caching using hashes in file names. However, the implementation is a bit weird. It does not only require a hash in the file name, but also a .md5 file containing the hash to determine if the requested hash is correct (I assume they don't want to actually create a digest on requests). This file creation is all handled by play's asset pipeline that creates those files on distribution and would also affect the files coming out of webpack. Because of this, I want to webpack to emit files without a hash (as it will be created later by play), but have the public path containing said hashes. However, it seems that this PR changed the original code so that having a public file name that differs from the emitted file name is no longer possible. I think by having |
Why is it taking so long for this PR to be merged? I got another case where this is a must: On Symfony2 projects you add public files to
but you access them trough a link:
Which is rooted in the /web folder outside the root.
or, supporting different namings
The same will be done on images and Extracted CSS generated from SASS/Stylus |
@DiegoYungh, because everyone is very busy. After the webpack v2 release I'm probably going to maintain this library. Meanwhile, if anyone could test this PR and verifies it works, that would be appreciated and will result in a quicker merge. @mobitar, could you sign the CLA? |
@SpaceK33z done |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I tested it and verified it worked.
- It seems like you changed the permissions of the
index.js
file from 644 to 755. Could you change that back? - Could you document this in the README?
After this I'll merge :).
@mobitar please add to your git config to ignore fileMode. |
@DiegoYungh @SpaceK33z done. |
@jskrzypek Do you think there is value in having the result of publicPath/outputPath functions interpolated by the loader-utils? In the example below I believe I need the path information from the name property (incoming url) to know if I need to replace a part of it, however when creating the public path I just want to do as follows.
Perhaps there is another way to do this. When publishing the output in my case, shared the assets need to go on the root/assets/images/x.png, while tool assets need to go under a variable directory name on the root, ie. root/toolA/assets/images/x.png, root/toolB/assets/images/x.png etc. These image files actually exist on disk as root/tools/toolA/assets/images/x.png, root/tools/toolB/assets/images/x.png, but all the tools are moved to the root when published. |
@derekdon @jskrzypek As stated in my comment from 15 Aug 2016 I have the same requirement. I currently help myself with a fork but would really like to see this in the official codebase. |
Is this possible right now, I mean specify an output folder relative to the entry? |
file-loader places files in the directory specified by the
name
query parameter, i.e:will place the file in the directory app/images, and also references the file using a URL of this same path, i.e
This doesn't make sense since the location you place the resource isn't necessarily the same as how you'd want it to be accessed.
output.publicPath
works when you just want to prefix the name with a directory, likeassets
, but if how you want the file to be accessed is entirely different from the combination of publicPath + name, you're out of luck.This pull request allows you to specific a public name for resources in the query params:
This will place the file in the directory
app/images
, but the URL the file will be accessed by isassets/foo
.