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

Commit 4c1ccaa

Browse files
evilebottnawimichael-ciniawsky
authored andcommitted
fix(index): don't append outputPath to the original url (options.outputPath {Function})
1 parent 3b071f5 commit 4c1ccaa

File tree

8 files changed

+155
-28
lines changed

8 files changed

+155
-28
lines changed

src/index.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,15 @@ export default function loader(content) {
1212

1313
const context = options.context || this.rootContext || (this.options && this.options.context);
1414

15-
let url = loaderUtils.interpolateName(this, options.name, {
15+
const url = loaderUtils.interpolateName(this, options.name, {
1616
context,
1717
content,
1818
regExp: options.regExp,
1919
});
2020

21-
let outputPath = '';
22-
23-
if (options.outputPath) {
24-
// support functions as outputPath to generate them dynamically
25-
outputPath = (
26-
typeof options.outputPath === 'function' ? options.outputPath(url) : options.outputPath
27-
);
28-
}
21+
let outputPath = (
22+
typeof options.outputPath === 'function' ? options.outputPath(url) : path.join(options.outputPath || '', url)
23+
);
2924

3025
if (options.useRelativePath) {
3126
const filePath = this.resourcePath;
@@ -41,19 +36,17 @@ export default function loader(content) {
4136
} else {
4237
outputPath = relativePath + url;
4338
}
44-
45-
url = relativePath + url;
46-
} else {
47-
outputPath += url;
4839
}
4940

50-
let publicPath = `__webpack_public_path__ + ${JSON.stringify(url)}`;
41+
let publicPath = null;
5142

5243
if (options.publicPath !== undefined) {
5344
// support functions as publicPath to generate them dynamically
5445
publicPath = JSON.stringify(
5546
typeof options.publicPath === 'function' ? options.publicPath(url) : options.publicPath + url,
5647
);
48+
} else {
49+
publicPath = `__webpack_public_path__ + ${JSON.stringify(outputPath)}`;
5750
}
5851

5952
if (options.emitFile === undefined || options.emitFile) {

test/fixtures/fixture-nested.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* eslint-disable */
2+
import png from './nested/file.png';
3+
4+
export default png;

test/fixtures/nested/file.png

6.62 KB
Loading

test/options/__snapshots__/outputPath.test.js.snap

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,34 @@
33
exports[`Options outputPath {Function} 1`] = `
44
Object {
55
"assets": Array [
6-
"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
6+
"output_path_func/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
77
],
8-
"source": "module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
8+
"source": "module.exports = __webpack_public_path__ + \\"output_path_func/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
9+
}
10+
`;
11+
12+
exports[`Options outputPath {Function} with \`options.name\` 1`] = `
13+
Object {
14+
"assets": Array [
15+
"output_path_func/file.png",
16+
],
17+
"source": "module.exports = __webpack_public_path__ + \\"output_path_func/file.png\\";",
918
}
1019
`;
1120

1221
exports[`Options outputPath {Function} with \`publicPath\` {Function} 1`] = `
1322
Object {
1423
"assets": Array [
15-
"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
24+
"output_path_func/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
1625
],
17-
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
26+
"source": "module.exports = \\"public_path_func/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
1827
}
1928
`;
2029

2130
exports[`Options outputPath {Function} with \`publicPath\` {String} 1`] = `
2231
Object {
2332
"assets": Array [
24-
"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
33+
"output_path_func/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
2534
],
2635
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
2736
}
@@ -32,7 +41,16 @@ Object {
3241
"assets": Array [
3342
"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
3443
],
35-
"source": "module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
44+
"source": "module.exports = __webpack_public_path__ + \\"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
45+
}
46+
`;
47+
48+
exports[`Options outputPath {String} with \`options.name\` 1`] = `
49+
Object {
50+
"assets": Array [
51+
"output_path/file.png",
52+
],
53+
"source": "module.exports = __webpack_public_path__ + \\"output_path/file.png\\";",
3654
}
3755
`;
3856

@@ -41,7 +59,7 @@ Object {
4159
"assets": Array [
4260
"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
4361
],
44-
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
62+
"source": "module.exports = \\"public_path_func/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
4563
}
4664
`;
4765

@@ -53,3 +71,21 @@ Object {
5371
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
5472
}
5573
`;
74+
75+
exports[`Options outputPath {String} with \`publicPath\` {String} without trailing slash 1`] = `
76+
Object {
77+
"assets": Array [
78+
"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
79+
],
80+
"source": "module.exports = \\"public_path9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
81+
}
82+
`;
83+
84+
exports[`Options outputPath {String} without trailing slash 1`] = `
85+
Object {
86+
"assets": Array [
87+
"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
88+
],
89+
"source": "module.exports = __webpack_public_path__ + \\"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
90+
}
91+
`;

test/options/__snapshots__/publicPath.test.js.snap

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,12 @@ Object {
1717
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
1818
}
1919
`;
20+
21+
exports[`Options publicPath {String} without trailing slash 1`] = `
22+
Object {
23+
"assets": Array [
24+
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
25+
],
26+
"source": "module.exports = \\"public_path9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
27+
}
28+
`;

test/options/__snapshots__/useRelativePath.test.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ Object {
2121
exports[`Options useRelativePath \`true\` with absolute \`context\` 1`] = `
2222
Object {
2323
"assets": Array [
24-
"../file-loader/test/fixtures/nested/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
24+
"file-loader/test/fixtures/nested/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
2525
],
26-
"source": "module.exports = __webpack_public_path__ + \\"../file-loader/test/fixtures/nested/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
26+
"source": "module.exports = __webpack_public_path__ + \\"file-loader/test/fixtures/nested/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
2727
}
2828
`;
2929

test/options/outputPath.test.js

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,65 @@ describe('Options', () => {
2121
expect({ assets, source }).toMatchSnapshot();
2222
});
2323

24+
test('{String} without trailing slash', async () => {
25+
const config = {
26+
loader: {
27+
test: /(png|jpg|svg)/,
28+
options: {
29+
outputPath: 'output_path',
30+
},
31+
},
32+
};
33+
34+
const stats = await webpack('fixture.js', config);
35+
const { assets, source } = stats.toJson().modules[1];
36+
37+
expect({ assets, source }).toMatchSnapshot();
38+
});
39+
40+
test('{String} with `options.name`', async () => {
41+
const config = {
42+
loader: {
43+
test: /(png|jpg|svg)/,
44+
options: {
45+
name: '[path][name].[ext]',
46+
outputPath: 'output_path/',
47+
},
48+
},
49+
};
50+
51+
const stats = await webpack('fixture.js', config);
52+
const { assets, source } = stats.toJson().modules[1];
53+
54+
expect({ assets, source }).toMatchSnapshot();
55+
});
56+
2457
test('{Function}', async () => {
2558
const config = {
2659
loader: {
2760
test: /(png|jpg|svg)/,
2861
options: {
2962
outputPath(url) {
30-
return `output_path/${url}`;
63+
return `output_path_func/${url}`;
64+
},
65+
},
66+
},
67+
};
68+
69+
const stats = await webpack('fixture.js', config);
70+
const { assets, source } = stats.toJson().modules[1];
71+
72+
expect({ assets, source }).toMatchSnapshot();
73+
});
74+
75+
test('{Function} with `options.name`', async () => {
76+
const config = {
77+
loader: {
78+
test: /(png|jpg|svg)/,
79+
options: {
80+
name: '[name].[ext]',
81+
outputPath(url) {
82+
return `output_path_func/${url}`;
3183
},
3284
},
3385
},
@@ -56,14 +108,31 @@ describe('Options', () => {
56108
expect({ assets, source }).toMatchSnapshot();
57109
});
58110

111+
test('{String} with `publicPath` {String} without trailing slash', async () => {
112+
const config = {
113+
loader: {
114+
test: /(png|jpg|svg)/,
115+
options: {
116+
outputPath: 'output_path',
117+
publicPath: 'public_path',
118+
},
119+
},
120+
};
121+
122+
const stats = await webpack('fixture.js', config);
123+
const { assets, source } = stats.toJson().modules[1];
124+
125+
expect({ assets, source }).toMatchSnapshot();
126+
});
127+
59128
test('{Function} with `publicPath` {String}', async () => {
60129
const config = {
61130
loader: {
62131
test: /(png|jpg|svg)/,
63132
options: {
64133
publicPath: 'public_path/',
65134
outputPath(url) {
66-
return `output_path/${url}`;
135+
return `output_path_func/${url}`;
67136
},
68137
},
69138
},
@@ -82,7 +151,7 @@ describe('Options', () => {
82151
options: {
83152
outputPath: 'output_path/',
84153
publicPath(url) {
85-
return `public_path/${url}`;
154+
return `public_path_func/${url}`;
86155
},
87156
},
88157
},
@@ -100,10 +169,10 @@ describe('Options', () => {
100169
test: /(png|jpg|svg)/,
101170
options: {
102171
outputPath(url) {
103-
return `output_path/${url}`;
172+
return `output_path_func/${url}`;
104173
},
105174
publicPath(url) {
106-
return `public_path/${url}`;
175+
return `public_path_func/${url}`;
107176
},
108177
},
109178
},

test/options/publicPath.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,22 @@ describe('Options', () => {
2121
expect({ assets, source }).toMatchSnapshot();
2222
});
2323

24+
test('{String} without trailing slash', async () => {
25+
const config = {
26+
loader: {
27+
test: /(png|jpg|svg)/,
28+
options: {
29+
publicPath: 'public_path',
30+
},
31+
},
32+
};
33+
34+
const stats = await webpack('fixture.js', config);
35+
const { assets, source } = stats.toJson().modules[1];
36+
37+
expect({ assets, source }).toMatchSnapshot();
38+
});
39+
2440
test('{Function}', async () => {
2541
const config = {
2642
loader: {

0 commit comments

Comments
 (0)