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

Commit d5eb823

Browse files
authoredDec 20, 2018
feat: context is now available in outputPath and publicPath (#305)
1 parent 0d66e64 commit d5eb823

6 files changed

+82
-4
lines changed
 

‎README.md

+20-2
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,20 @@ Specify a filesystem path where the target file(s) will be placed.
180180
loader: 'file-loader',
181181
options: {
182182
name: '[path][name].[ext]',
183-
outputPath: (url, resourcePath) => {
183+
outputPath: (url, resourcePath, context) => {
184184
// `resourcePath` is original absolute path to asset
185+
// `context` is directory where stored asset (`rootContext`) or `context` option
186+
187+
// To get relative path you can use
188+
// const relativePath = path.relative(context, resourcePath);
189+
185190
if (/my-custom-image\.png/.test(resourcePath)) {
186191
return `other_output_path/${url}`
187192
}
193+
194+
if (/images/.test(context)) {
195+
return `image_output_path/${url}`;
196+
}
188197

189198
return `output_path/${url}`;
190199
},
@@ -224,11 +233,20 @@ Specifies a custom public path for the target file(s).
224233
loader: 'file-loader',
225234
options: {
226235
name: '[path][name].[ext]',
227-
publicPath: (url, resourcePath) {
236+
publicPath: (url, resourcePath, context) => {
228237
// `resourcePath` is original absolute path to asset
238+
// `context` is directory where stored asset (`rootContext`) or `context` option
239+
240+
// To get relative path you can use
241+
// const relativePath = path.relative(context, resourcePath);
242+
229243
if (/my-custom-image\.png/.test(resourcePath)) {
230244
return `other_public_path/${url}`
231245
}
246+
247+
if (/images/.test(context)) {
248+
return `image_output_path/${url}`;
249+
}
232250

233251
return `public_path/${url}`;
234252
}

‎src/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default function loader(content) {
2222

2323
if (options.outputPath) {
2424
if (typeof options.outputPath === 'function') {
25-
outputPath = options.outputPath(url, this.resourcePath);
25+
outputPath = options.outputPath(url, this.resourcePath, context);
2626
} else {
2727
outputPath = path.posix.join(options.outputPath, url);
2828
}
@@ -55,7 +55,7 @@ export default function loader(content) {
5555

5656
if (options.publicPath) {
5757
if (typeof options.publicPath === 'function') {
58-
publicPath = options.publicPath(url, this.resourcePath);
58+
publicPath = options.publicPath(url, this.resourcePath, context);
5959
} else if (options.publicPath.endsWith('/')) {
6060
publicPath = options.publicPath + url;
6161
} else {

‎test/__snapshots__/outputPath-option.test.js.snap

+9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ Object {
99
}
1010
`;
1111

12+
exports[`when applied with \`outputPath\` option matches snapshot for \`{Function}\` value and pass \`context\` 1`] = `
13+
Object {
14+
"assets": Array [
15+
"output_path_func/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
16+
],
17+
"source": "module.exports = __webpack_public_path__ + \\"output_path_func/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
18+
}
19+
`;
20+
1221
exports[`when applied with \`outputPath\` option matches snapshot for \`{Function}\` value and pass \`resourcePath\` 1`] = `
1322
Object {
1423
"assets": Array [

‎test/__snapshots__/publicPath-option.test.js.snap

+9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ Object {
99
}
1010
`;
1111

12+
exports[`when applied with \`publicPath\` option matches snapshot for \`{Function}\` value and pass \`context\` 1`] = `
13+
Object {
14+
"assets": Array [
15+
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
16+
],
17+
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
18+
}
19+
`;
20+
1221
exports[`when applied with \`publicPath\` option matches snapshot for \`{Function}\` value and pass \`resourcePath\` 1`] = `
1322
Object {
1423
"assets": Array [

‎test/outputPath-option.test.js

+21
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,25 @@ describe('when applied with `outputPath` option', () => {
210210

211211
expect({ assets, source }).toMatchSnapshot();
212212
});
213+
214+
it('matches snapshot for `{Function}` value and pass `context`', async () => {
215+
const config = {
216+
loader: {
217+
test: /(png|jpg|svg)/,
218+
options: {
219+
outputPath(url, resourcePath, context) {
220+
expect(context).toMatch('fixtures');
221+
222+
return `output_path_func/${url}`;
223+
},
224+
},
225+
},
226+
};
227+
228+
const stats = await webpack('fixture.js', config);
229+
const [module] = stats.toJson().modules;
230+
const { assets, source } = module;
231+
232+
expect({ assets, source }).toMatchSnapshot();
233+
});
213234
});

‎test/publicPath-option.test.js

+21
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,25 @@ describe('when applied with `publicPath` option', () => {
9191

9292
expect({ assets, source }).toMatchSnapshot();
9393
});
94+
95+
it('matches snapshot for `{Function}` value and pass `context`', async () => {
96+
const config = {
97+
loader: {
98+
test: /(png|jpg|svg)/,
99+
options: {
100+
publicPath(url, resourcePath, context) {
101+
expect(context).toMatch('fixtures');
102+
103+
return `public_path/${url}`;
104+
},
105+
},
106+
},
107+
};
108+
109+
const stats = await webpack('fixture.js', config);
110+
const [module] = stats.toJson().modules;
111+
const { assets, source } = module;
112+
113+
expect({ assets, source }).toMatchSnapshot();
114+
});
94115
});

0 commit comments

Comments
 (0)
This repository has been archived.