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

Commit

Permalink
fix(index): handle protocol URL's correctly (options.publicPath) (#253
Browse files Browse the repository at this point in the history
)
  • Loading branch information
michael-ciniawsky committed Feb 21, 2018
1 parent c0785d1 commit 54fa5a3
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 17 deletions.
43 changes: 29 additions & 14 deletions src/index.js
Expand Up @@ -18,16 +18,28 @@ export default function loader(content) {
regExp: options.regExp,
});

let outputPath = (
typeof options.outputPath === 'function' ? options.outputPath(url) : path.join(options.outputPath || '', url)
);
let outputPath = url;

if (options.outputPath) {
if (typeof options.outputPath === 'function') {
outputPath = options.outputPath(url);
} else {
outputPath = path.join(options.outputPath, url);
}
}

if (options.useRelativePath) {
const filePath = this.resourcePath;
const issuerContext = context || (this._module && this._module.issuer
&& this._module.issuer.context);

const relativeUrl = issuerContext && path.relative(issuerContext, filePath).split(path.sep).join('/');
const issuerContext = context || (
this._module &&
this._module.issuer &&
this._module.issuer.context
);

const relativeUrl = issuerContext && path.relative(issuerContext, filePath)
.split(path.sep)
.join('/');

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

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

if (options.publicPath !== undefined) {
// support functions as publicPath to generate them dynamically
publicPath = JSON.stringify(
typeof options.publicPath === 'function' ? options.publicPath(url) : path.join(options.publicPath || '', url),
);
} else {
publicPath = `__webpack_public_path__ + ${JSON.stringify(outputPath)}`;
if (options.publicPath) {

This comment has been minimized.

Copy link
@Cap32

Cap32 Mar 5, 2018

if (options.publicPath !== undefined) is better than if (options.publicPath) because user may set an empty string to override webpack config

if (typeof options.publicPath === 'function') {
publicPath = options.publicPath(url);
} else if (options.publicPath.endsWith('/')) {
publicPath = options.publicPath + url;
} else {
publicPath = `${options.publicPath}/${url}`;
}

publicPath = JSON.stringify(publicPath);
}

if (options.emitFile === undefined || options.emitFile) {
Expand Down
13 changes: 11 additions & 2 deletions test/options/__snapshots__/publicPath.test.js.snap
Expand Up @@ -9,7 +9,16 @@ Object {
}
`;

exports[`Options publicPath {String} 1`] = `
exports[`Options publicPath {String} - URL 1`] = `
Object {
"assets": Array [
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = \\"https://cdn.com/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;

exports[`Options publicPath {String} - Without trailing slash 1`] = `
Object {
"assets": Array [
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
Expand All @@ -18,7 +27,7 @@ Object {
}
`;

exports[`Options publicPath {String} without trailing slash 1`] = `
exports[`Options publicPath {String} 1`] = `
Object {
"assets": Array [
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
Expand Down
18 changes: 17 additions & 1 deletion test/options/publicPath.test.js
Expand Up @@ -21,7 +21,7 @@ describe('Options', () => {
expect({ assets, source }).toMatchSnapshot();
});

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

test('{String} - URL', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
publicPath: 'https://cdn.com/',
},
},
};

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

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

test('{Function}', async () => {
const config = {
loader: {
Expand Down

0 comments on commit 54fa5a3

Please sign in to comment.