Skip to content
This repository has been archived by the owner on Mar 17, 2021. It is now read-only.

Commit

Permalink
feat: context is now available in outputPath and publicPath (#305)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Dec 20, 2018
1 parent 0d66e64 commit d5eb823
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 4 deletions.
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,20 @@ Specify a filesystem path where the target file(s) will be placed.
loader: 'file-loader',
options: {
name: '[path][name].[ext]',
outputPath: (url, resourcePath) => {
outputPath: (url, resourcePath, context) => {
// `resourcePath` is original absolute path to asset
// `context` is directory where stored asset (`rootContext`) or `context` option

// To get relative path you can use
// const relativePath = path.relative(context, resourcePath);

if (/my-custom-image\.png/.test(resourcePath)) {
return `other_output_path/${url}`
}

if (/images/.test(context)) {
return `image_output_path/${url}`;
}

return `output_path/${url}`;
},
Expand Down Expand Up @@ -224,11 +233,20 @@ Specifies a custom public path for the target file(s).
loader: 'file-loader',
options: {
name: '[path][name].[ext]',
publicPath: (url, resourcePath) {
publicPath: (url, resourcePath, context) => {
// `resourcePath` is original absolute path to asset
// `context` is directory where stored asset (`rootContext`) or `context` option

// To get relative path you can use
// const relativePath = path.relative(context, resourcePath);

if (/my-custom-image\.png/.test(resourcePath)) {
return `other_public_path/${url}`
}

if (/images/.test(context)) {
return `image_output_path/${url}`;
}

return `public_path/${url}`;
}
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function loader(content) {

if (options.outputPath) {
if (typeof options.outputPath === 'function') {
outputPath = options.outputPath(url, this.resourcePath);
outputPath = options.outputPath(url, this.resourcePath, context);
} else {
outputPath = path.posix.join(options.outputPath, url);
}
Expand Down Expand Up @@ -55,7 +55,7 @@ export default function loader(content) {

if (options.publicPath) {
if (typeof options.publicPath === 'function') {
publicPath = options.publicPath(url, this.resourcePath);
publicPath = options.publicPath(url, this.resourcePath, context);
} else if (options.publicPath.endsWith('/')) {
publicPath = options.publicPath + url;
} else {
Expand Down
9 changes: 9 additions & 0 deletions test/__snapshots__/outputPath-option.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ Object {
}
`;

exports[`when applied with \`outputPath\` option matches snapshot for \`{Function}\` value and pass \`context\` 1`] = `
Object {
"assets": Array [
"output_path_func/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = __webpack_public_path__ + \\"output_path_func/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;

exports[`when applied with \`outputPath\` option matches snapshot for \`{Function}\` value and pass \`resourcePath\` 1`] = `
Object {
"assets": Array [
Expand Down
9 changes: 9 additions & 0 deletions test/__snapshots__/publicPath-option.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ Object {
}
`;

exports[`when applied with \`publicPath\` option matches snapshot for \`{Function}\` value and pass \`context\` 1`] = `
Object {
"assets": Array [
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;

exports[`when applied with \`publicPath\` option matches snapshot for \`{Function}\` value and pass \`resourcePath\` 1`] = `
Object {
"assets": Array [
Expand Down
21 changes: 21 additions & 0 deletions test/outputPath-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,25 @@ describe('when applied with `outputPath` option', () => {

expect({ assets, source }).toMatchSnapshot();
});

it('matches snapshot for `{Function}` value and pass `context`', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
outputPath(url, resourcePath, context) {
expect(context).toMatch('fixtures');

return `output_path_func/${url}`;
},
},
},
};

const stats = await webpack('fixture.js', config);
const [module] = stats.toJson().modules;
const { assets, source } = module;

expect({ assets, source }).toMatchSnapshot();
});
});
21 changes: 21 additions & 0 deletions test/publicPath-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,25 @@ describe('when applied with `publicPath` option', () => {

expect({ assets, source }).toMatchSnapshot();
});

it('matches snapshot for `{Function}` value and pass `context`', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
publicPath(url, resourcePath, context) {
expect(context).toMatch('fixtures');

return `public_path/${url}`;
},
},
},
};

const stats = await webpack('fixture.js', config);
const [module] = stats.toJson().modules;
const { assets, source } = module;

expect({ assets, source }).toMatchSnapshot();
});
});

0 comments on commit d5eb823

Please sign in to comment.