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

Commit 98bf052

Browse files
evilebottnawimichael-ciniawsky
authored andcommittedFeb 19, 2018
fix(index): don't concat options.outputPath and options.publicPath (#246)
1 parent 9ee8332 commit 98bf052

File tree

5 files changed

+159
-25
lines changed

5 files changed

+159
-25
lines changed
 

‎src/index.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default function loader(content) {
1010

1111
validateOptions(schema, options, 'File Loader');
1212

13-
const context = options.context || this.rootContext || this.options && this.options.context
13+
const context = options.context || this.rootContext || (this.options && this.options.context);
1414

1515
let url = loaderUtils.interpolateName(this, options.name, {
1616
context,
@@ -27,9 +27,8 @@ export default function loader(content) {
2727
);
2828
}
2929

30-
const filePath = this.resourcePath;
31-
3230
if (options.useRelativePath) {
31+
const filePath = this.resourcePath;
3332
const issuerContext = (this._module && this._module.issuer
3433
&& this._module.issuer.context) || context;
3534

@@ -44,13 +43,8 @@ export default function loader(content) {
4443
}
4544

4645
url = relativePath + url;
47-
} else if (options.outputPath) {
48-
// support functions as outputPath to generate them dynamically
49-
outputPath = typeof options.outputPath === 'function' ? options.outputPath(url) : options.outputPath + url;
50-
51-
url = outputPath;
5246
} else {
53-
outputPath = url;
47+
outputPath += url;
5448
}
5549

5650
let publicPath = `__webpack_public_path__ + ${JSON.stringify(url)}`;
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,55 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Options outputPath {Function} 1`] = `"module.exports = __webpack_public_path__ + \\"test/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`;
3+
exports[`Options outputPath {Function} 1`] = `
4+
Object {
5+
"assets": Array [
6+
"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
7+
],
8+
"source": "module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
9+
}
10+
`;
411

5-
exports[`Options outputPath {String} 1`] = `"module.exports = __webpack_public_path__ + \\"/test/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`;
12+
exports[`Options outputPath {Function} with \`publicPath\` {Function} 1`] = `
13+
Object {
14+
"assets": Array [
15+
"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
16+
],
17+
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
18+
}
19+
`;
20+
21+
exports[`Options outputPath {Function} with \`publicPath\` {String} 1`] = `
22+
Object {
23+
"assets": Array [
24+
"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
25+
],
26+
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
27+
}
28+
`;
29+
30+
exports[`Options outputPath {String} 1`] = `
31+
Object {
32+
"assets": Array [
33+
"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
34+
],
35+
"source": "module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
36+
}
37+
`;
38+
39+
exports[`Options outputPath {String} with \`publicPath\` {Function} 1`] = `
40+
Object {
41+
"assets": Array [
42+
"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
43+
],
44+
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
45+
}
46+
`;
47+
48+
exports[`Options outputPath {String} with \`publicPath\` {String} 1`] = `
49+
Object {
50+
"assets": Array [
51+
"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
52+
],
53+
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
54+
}
55+
`;
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Options publicPath {Function} 1`] = `"module.exports = \\"test/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`;
3+
exports[`Options publicPath {Function} 1`] = `
4+
Object {
5+
"assets": Array [
6+
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
7+
],
8+
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
9+
}
10+
`;
411

5-
exports[`Options publicPath {String} 1`] = `"module.exports = \\"/test/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`;
12+
exports[`Options publicPath {String} 1`] = `
13+
Object {
14+
"assets": Array [
15+
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
16+
],
17+
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
18+
}
19+
`;

‎test/options/outputPath.test.js

+82-6
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ describe('Options', () => {
1010
loader: {
1111
test: /(png|jpg|svg)/,
1212
options: {
13-
outputPath: '/test/',
13+
outputPath: 'output_path/',
1414
},
1515
},
1616
};
1717

1818
const stats = await webpack('fixture.js', config);
19-
const { source } = stats.toJson().modules[1];
19+
const { assets, source } = stats.toJson().modules[1];
2020

21-
expect(source).toMatchSnapshot();
21+
expect({ assets, source }).toMatchSnapshot();
2222
});
2323

2424
test('{Function}', async () => {
@@ -27,16 +27,92 @@ describe('Options', () => {
2727
test: /(png|jpg|svg)/,
2828
options: {
2929
outputPath(url) {
30-
return `test/${url}`;
30+
return `output_path/${url}`;
3131
},
3232
},
3333
},
3434
};
3535

3636
const stats = await webpack('fixture.js', config);
37-
const { source } = stats.toJson().modules[1];
37+
const { assets, source } = stats.toJson().modules[1];
3838

39-
expect(source).toMatchSnapshot();
39+
expect({ assets, source }).toMatchSnapshot();
40+
});
41+
42+
test('{String} with `publicPath` {String}', async () => {
43+
const config = {
44+
loader: {
45+
test: /(png|jpg|svg)/,
46+
options: {
47+
outputPath: 'output_path/',
48+
publicPath: 'public_path/',
49+
},
50+
},
51+
};
52+
53+
const stats = await webpack('fixture.js', config);
54+
const { assets, source } = stats.toJson().modules[1];
55+
56+
expect({ assets, source }).toMatchSnapshot();
57+
});
58+
59+
test('{Function} with `publicPath` {String}', async () => {
60+
const config = {
61+
loader: {
62+
test: /(png|jpg|svg)/,
63+
options: {
64+
publicPath: 'public_path/',
65+
outputPath(url) {
66+
return `output_path/${url}`;
67+
},
68+
},
69+
},
70+
};
71+
72+
const stats = await webpack('fixture.js', config);
73+
const { assets, source } = stats.toJson().modules[1];
74+
75+
expect({ assets, source }).toMatchSnapshot();
76+
});
77+
78+
test('{String} with `publicPath` {Function}', async () => {
79+
const config = {
80+
loader: {
81+
test: /(png|jpg|svg)/,
82+
options: {
83+
outputPath: 'output_path/',
84+
publicPath(url) {
85+
return `public_path/${url}`;
86+
},
87+
},
88+
},
89+
};
90+
91+
const stats = await webpack('fixture.js', config);
92+
const { assets, source } = stats.toJson().modules[1];
93+
94+
expect({ assets, source }).toMatchSnapshot();
95+
});
96+
97+
test('{Function} with `publicPath` {Function}', async () => {
98+
const config = {
99+
loader: {
100+
test: /(png|jpg|svg)/,
101+
options: {
102+
outputPath(url) {
103+
return `output_path/${url}`;
104+
},
105+
publicPath(url) {
106+
return `public_path/${url}`;
107+
},
108+
},
109+
},
110+
};
111+
112+
const stats = await webpack('fixture.js', config);
113+
const { assets, source } = stats.toJson().modules[1];
114+
115+
expect({ assets, source }).toMatchSnapshot();
40116
});
41117
});
42118
});

‎test/options/publicPath.test.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ describe('Options', () => {
1010
loader: {
1111
test: /(png|jpg|svg)/,
1212
options: {
13-
publicPath: '/test/',
13+
publicPath: 'public_path/',
1414
},
1515
},
1616
};
1717

1818
const stats = await webpack('fixture.js', config);
19-
const { source } = stats.toJson().modules[1];
19+
const { assets, source } = stats.toJson().modules[1];
2020

21-
expect(source).toMatchSnapshot();
21+
expect({ assets, source }).toMatchSnapshot();
2222
});
2323

2424
test('{Function}', async () => {
@@ -27,16 +27,16 @@ describe('Options', () => {
2727
test: /(png|jpg|svg)/,
2828
options: {
2929
publicPath(url) {
30-
return `test/${url}`;
30+
return `public_path/${url}`;
3131
},
3232
},
3333
},
3434
};
3535

3636
const stats = await webpack('fixture.js', config);
37-
const { source } = stats.toJson().modules[1];
37+
const { assets, source } = stats.toJson().modules[1];
3838

39-
expect(source).toMatchSnapshot();
39+
expect({ assets, source }).toMatchSnapshot();
4040
});
4141
});
4242
});

3 commit comments

Comments
 (3)

tnguyen14 commented on Aug 23, 2018

@tnguyen14

This was a breaking change in behavior. The application I'm working on relied on this behavior, and updating from 0.11.2 to 2.0.0 broke our setup, and it took me 5 hours to figure out what the problem was. I had to had 2 versions of this module, and do a bunch of console.log in the module's source code, in order to figure out the problem. I wish this had been released as a major new version, instead of a bug fix.

alexander-akait commented on Aug 24, 2018

@alexander-akait
Member

@tnguyen14 you use buggy behavior, so it was fix

tnguyen14 commented on Aug 24, 2018

@tnguyen14

@evilebottnawi The idea behind a breaking version number in semver, is that it represents a breaking/ backward-incompatible change, whether that change is a result of a new feature or a bug fix.

This repository has been archived.