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

Commit 54fa5a3

Browse files
fix(index): handle protocol URL's correctly (options.publicPath) (#253)
1 parent c0785d1 commit 54fa5a3

File tree

3 files changed

+57
-17
lines changed

3 files changed

+57
-17
lines changed

src/index.js

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,28 @@ export default function loader(content) {
1818
regExp: options.regExp,
1919
});
2020

21-
let outputPath = (
22-
typeof options.outputPath === 'function' ? options.outputPath(url) : path.join(options.outputPath || '', url)
23-
);
21+
let outputPath = url;
22+
23+
if (options.outputPath) {
24+
if (typeof options.outputPath === 'function') {
25+
outputPath = options.outputPath(url);
26+
} else {
27+
outputPath = path.join(options.outputPath, url);
28+
}
29+
}
2430

2531
if (options.useRelativePath) {
2632
const filePath = this.resourcePath;
27-
const issuerContext = context || (this._module && this._module.issuer
28-
&& this._module.issuer.context);
2933

30-
const relativeUrl = issuerContext && path.relative(issuerContext, filePath).split(path.sep).join('/');
34+
const issuerContext = context || (
35+
this._module &&
36+
this._module.issuer &&
37+
this._module.issuer.context
38+
);
39+
40+
const relativeUrl = issuerContext && path.relative(issuerContext, filePath)
41+
.split(path.sep)
42+
.join('/');
3143

3244
const relativePath = relativeUrl && `${path.dirname(relativeUrl)}/`;
3345
// eslint-disable-next-line no-bitwise
@@ -38,15 +50,18 @@ export default function loader(content) {
3850
}
3951
}
4052

41-
let publicPath = null;
53+
let publicPath = `__webpack_public_path__ + ${JSON.stringify(outputPath)}`;
4254

43-
if (options.publicPath !== undefined) {
44-
// support functions as publicPath to generate them dynamically
45-
publicPath = JSON.stringify(
46-
typeof options.publicPath === 'function' ? options.publicPath(url) : path.join(options.publicPath || '', url),
47-
);
48-
} else {
49-
publicPath = `__webpack_public_path__ + ${JSON.stringify(outputPath)}`;
55+
if (options.publicPath) {
56+
if (typeof options.publicPath === 'function') {
57+
publicPath = options.publicPath(url);
58+
} else if (options.publicPath.endsWith('/')) {
59+
publicPath = options.publicPath + url;
60+
} else {
61+
publicPath = `${options.publicPath}/${url}`;
62+
}
63+
64+
publicPath = JSON.stringify(publicPath);
5065
}
5166

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

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,16 @@ Object {
99
}
1010
`;
1111

12-
exports[`Options publicPath {String} 1`] = `
12+
exports[`Options publicPath {String} - URL 1`] = `
13+
Object {
14+
"assets": Array [
15+
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
16+
],
17+
"source": "module.exports = \\"https://cdn.com/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
18+
}
19+
`;
20+
21+
exports[`Options publicPath {String} - Without trailing slash 1`] = `
1322
Object {
1423
"assets": Array [
1524
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
@@ -18,7 +27,7 @@ Object {
1827
}
1928
`;
2029

21-
exports[`Options publicPath {String} without trailing slash 1`] = `
30+
exports[`Options publicPath {String} 1`] = `
2231
Object {
2332
"assets": Array [
2433
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",

test/options/publicPath.test.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('Options', () => {
2121
expect({ assets, source }).toMatchSnapshot();
2222
});
2323

24-
test('{String} without trailing slash', async () => {
24+
test('{String} - Without trailing slash', async () => {
2525
const config = {
2626
loader: {
2727
test: /(png|jpg|svg)/,
@@ -37,6 +37,22 @@ describe('Options', () => {
3737
expect({ assets, source }).toMatchSnapshot();
3838
});
3939

40+
test('{String} - URL', async () => {
41+
const config = {
42+
loader: {
43+
test: /(png|jpg|svg)/,
44+
options: {
45+
publicPath: 'https://cdn.com/',
46+
},
47+
},
48+
};
49+
50+
const stats = await webpack('fixture.js', config);
51+
const { assets, source } = stats.toJson().modules[1];
52+
53+
expect({ assets, source }).toMatchSnapshot();
54+
});
55+
4056
test('{Function}', async () => {
4157
const config = {
4258
loader: {

0 commit comments

Comments
 (0)