-
-
Notifications
You must be signed in to change notification settings - Fork 254
feat(index): add a new parameter to publicPath
(options.publicPath
)
#242
Conversation
This change makes the publicPath function to return the original path of the resource making possible to build custom or relatives paths.
Claudiomar seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. |
This is a exemple how to use this funcionality //... other webpack config
loader: "file-loader",
options: {
name: "[name].[ext]?[hash]",
outputPath: "img/",
publicPath: ( file , resource ) => {
// resource can be 'src/templates/pages/firstPath/secondPath/index.pug';
const basePath = resource.split('src/').pop(); //get the base of resource
const compareBaseName = path.basename(basePath, '.pug');
const pagesFullPath = basePath.match(/(?:(?=pages\/)(?:.*)[(?=\/)])/).shift();
const isRoot = pagesFullPath.match(/[^(?:pages)].+$/);
if (basePath == 'css/style.scss'){
return `../${file}`;
}else {
if( compareBaseName == "index" ) {
return !isRoot ? `assets/${file}` : `${path.relative(pagesFullPath,'pages/')}/assets/${file}`;
} else {
return `${path.relative(`${pagesFullPath}/anotherPath`,'pages/')}/assets/${file}`;
}
}
},
// .. at the end it returns '../../assets/img/my-img.png'; PS: using the file-loader parameter 'useRelativePath' didn't resolve my relative path. |
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.
Please add tests
publicPath
(options.publicPath
)
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.
Can you please elaborate more on this change and why it is needed?
Test added |
Sometimes it is necessary to create relative paths for the generated files, but 'useRelativePath' options, is based on the structure where the file is being processed, but should be based on the new structure generated by the bundle. If my generating structure has paths other than the generated one, there will be path conflicts. With this new change the developer can create the own relative path for the generated files. Example :
Using 'useRelativePath' in this example. the relative path generated will be '../../../', but i need '../', |
Could you try to provide a simpler example then the one you posted in #242 (comment) please ? I'm not really in favor of using |
I created a repository with a project that uses this functionality. there's have two npm script , start and start:error. PS: You will not pass the entire object this._module.issuer , only the ressorce (this._module.issuer.ressource) |
Hello, any news on that ? |
@@ -58,7 +58,7 @@ export default function loader(content) { | |||
if (options.publicPath !== undefined) { | |||
// support functions as publicPath to generate them dynamically | |||
publicPath = JSON.stringify( | |||
typeof options.publicPath === 'function' ? options.publicPath(url) : options.publicPath + url, | |||
typeof options.publicPath === 'function' ? options.publicPath(url, this._module.issuer.resource) : options.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.
Better send second params this
and you can inside you code what you need, it is universal solution.
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.
The proposal is to know what is the path of the resource that called the loader. With this information any developer will get to know the level of the tree of the origin file and adapt the output path according.
Closing in favor of #166, see #242 (comment) Feel free to reopen and implement the logic in #166 within this PR :) |
This change makes the publicPath function to return the original path of the resource making possible to build custom or relatives paths.