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

Commit

Permalink
feat: introduced "postTransformPublicPath" option
Browse files Browse the repository at this point in the history
removed prefixPublicPathWithWebpackPublicPath and introduced postTransformPublicPath option instead
  • Loading branch information
fabb committed Jul 22, 2019
1 parent e776945 commit 4f990d7
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 90 deletions.
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ export default function loader(content) {
}

publicPath = JSON.stringify(publicPath);
}

if (options.prefixPublicPathWithWebpackPublicPath) {
publicPath = `__webpack_public_path__ + ${publicPath}`;
}
if (options.postTransformPublicPath) {
publicPath = options.postTransformPublicPath(publicPath);
}

if (typeof options.emitFile === 'undefined' || options.emitFile) {
Expand Down
4 changes: 2 additions & 2 deletions src/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
}
]
},
"prefixPublicPathWithWebpackPublicPath": {
"type": "boolean"
"postTransformPublicPath": {
"instanceof": "Function"
},
"context": {
"description": "A custom file context (https://github.com/webpack-contrib/file-loader#context).",
Expand Down
47 changes: 37 additions & 10 deletions test/__snapshots__/publicPath-option.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`when applied with \`publicPath\` and \`prefixPublicPathWithWebpackPublicPath\` options matches snapshot for \`{Function}\` value 1`] = `
exports[`when applied with \`postTransformPublicPath\` option matches snapshot for appending to input parameter value 1`] = `
Object {
"assets": Array [
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = __webpack_public_path__ + \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
"source": "module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\" + \\"/test\\";",
}
`;

exports[`when applied with \`publicPath\` and \`prefixPublicPathWithWebpackPublicPath\` options matches snapshot for \`{Function}\` value and pass \`context\` 1`] = `
exports[`when applied with \`postTransformPublicPath\` option matches snapshot for returned input parameter value without modification 1`] = `
Object {
"assets": Array [
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = __webpack_public_path__ + \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
"source": "module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;

exports[`when applied with \`publicPath\` and \`prefixPublicPathWithWebpackPublicPath\` options matches snapshot for \`{Function}\` value and pass \`resourcePath\` 1`] = `
exports[`when applied with \`publicPath\` and \`postTransformPublicPath\` option \`{Function}\` value matches snapshot for appending to input parameter value 1`] = `
Object {
"assets": Array [
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = __webpack_public_path__ + \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\" + \\"?test=test\\";",
}
`;
exports[`when applied with \`publicPath\` and \`prefixPublicPathWithWebpackPublicPath\` options matches snapshot for \`{String}\` value 1`] = `
exports[`when applied with \`publicPath\` and \`postTransformPublicPath\` option \`{Function}\` value matches snapshot for prefixing with __webpack_public_path__ 1`] = `
Object {
"assets": Array [
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
Expand All @@ -36,16 +36,34 @@ Object {
}
`;
exports[`when applied with \`publicPath\` and \`prefixPublicPathWithWebpackPublicPath\` options matches snapshot for \`{String}\` value as URL 1`] = `
exports[`when applied with \`publicPath\` and \`postTransformPublicPath\` option \`{Function}\` value matches snapshot for prefixing with string 1`] = `
Object {
"assets": Array [
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = \\"path_prefix/\\" + \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;
exports[`when applied with \`publicPath\` and \`postTransformPublicPath\` option \`{Function}\` value matches snapshot for returned input parameter value without modification 1`] = `
Object {
"assets": Array [
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = __webpack_public_path__ + \\"https://cdn.com/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;
exports[`when applied with \`publicPath\` and \`prefixPublicPathWithWebpackPublicPath\` options matches snapshot for \`{String}\` value without trailing slash 1`] = `
exports[`when applied with \`publicPath\` and \`postTransformPublicPath\` option \`{String}\` value matches snapshot for appending to input parameter value 1`] = `
Object {
"assets": Array [
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\" + \\"?test=test\\";",
}
`;
exports[`when applied with \`publicPath\` and \`postTransformPublicPath\` option \`{String}\` value matches snapshot for prefixing with __webpack_public_path__ 1`] = `
Object {
"assets": Array [
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
Expand All @@ -54,6 +72,15 @@ Object {
}
`;
exports[`when applied with \`publicPath\` and \`postTransformPublicPath\` option \`{String}\` value matches snapshot for returned input parameter value without modification 1`] = `
Object {
"assets": Array [
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;
exports[`when applied with \`publicPath\` option matches snapshot for \`{Function}\` value 1`] = `
Object {
"assets": Array [
Expand Down
201 changes: 126 additions & 75 deletions test/publicPath-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,13 @@ describe('when applied with `publicPath` option', () => {
});
});

describe('when applied with `publicPath` and `prefixPublicPathWithWebpackPublicPath` options', () => {
it('matches snapshot for `{String}` value', async () => {
describe('when applied with `postTransformPublicPath` option', () => {
it('matches snapshot for returned input parameter value without modification', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
publicPath: 'public_path/',
prefixPublicPathWithWebpackPublicPath: true,
postTransformPublicPath: (p) => p,
},
},
};
Expand All @@ -147,13 +146,12 @@ describe('when applied with `publicPath` and `prefixPublicPathWithWebpackPublicP
expect({ assets, source }).toMatchSnapshot();
});

it('matches snapshot for `{String}` value without trailing slash', async () => {
it('matches snapshot for appending to input parameter value', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
publicPath: 'public_path',
prefixPublicPathWithWebpackPublicPath: true,
postTransformPublicPath: (p) => `${p} + "/test"`,
},
},
};
Expand All @@ -164,87 +162,140 @@ describe('when applied with `publicPath` and `prefixPublicPathWithWebpackPublicP

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

// notice that this case will produce invalid urls if __webpack_public_path__ is set to an absolute url
it('matches snapshot for `{String}` value as URL', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
publicPath: 'https://cdn.com/',
prefixPublicPathWithWebpackPublicPath: true,
describe('when applied with `publicPath` and `postTransformPublicPath` option', () => {
describe('`{String}` value', () => {
it('matches snapshot for returned input parameter value without modification', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
publicPath: 'public_path/',
postTransformPublicPath: (p) => p,
},
},
},
};

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

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

it('matches snapshot for `{Function}` value', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
publicPath(url) {
return `public_path/${url}`;
};

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

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

it('matches snapshot for appending to input parameter value', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
publicPath: 'public_path/',
postTransformPublicPath: (p) => `${p} + "?test=test"`,
},
prefixPublicPathWithWebpackPublicPath: true,
},
},
};

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

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

it('matches snapshot for `{Function}` value and pass `resourcePath`', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
publicPath(url, resourcePath) {
expect(resourcePath).toMatch('file.png');

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

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

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

it('matches snapshot for prefixing with __webpack_public_path__', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
publicPath: 'public_path/',
postTransformPublicPath: (p) => `__webpack_public_path__ + ${p}`,
},
prefixPublicPathWithWebpackPublicPath: true,
},
},
};
};

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

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

it('matches snapshot for `{Function}` value and pass `context`', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
publicPath(url, resourcePath, context) {
expect(context).toMatch('fixtures');

return `public_path/${url}`;
describe('`{Function}` value', () => {
it('matches snapshot for returned input parameter value without modification', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
publicPath(url) {
return `public_path/${url}`;
},
postTransformPublicPath: (p) => p,
},
prefixPublicPathWithWebpackPublicPath: true,
},
},
};
};

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

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

it('matches snapshot for appending to input parameter value', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
publicPath(url) {
return `public_path/${url}`;
},
postTransformPublicPath: (p) => `${p} + "?test=test"`,
},
},
};

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

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

it('matches snapshot for prefixing with string', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
publicPath: 'public_path/',
postTransformPublicPath: (p) => `"path_prefix/" + ${p}`,
},
},
};

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

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

it('matches snapshot for prefixing with __webpack_public_path__', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
publicPath: 'public_path/',
postTransformPublicPath: (p) => `__webpack_public_path__ + ${p}`,
},
},
};

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

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

0 comments on commit 4f990d7

Please sign in to comment.