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

Commit

Permalink
fix(index): don't concat options.outputPath and options.publicPath (
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored and michael-ciniawsky committed Feb 19, 2018
1 parent 9ee8332 commit 98bf052
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 25 deletions.
12 changes: 3 additions & 9 deletions src/index.js
Expand Up @@ -10,7 +10,7 @@ export default function loader(content) {

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

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

let url = loaderUtils.interpolateName(this, options.name, {
context,
Expand All @@ -27,9 +27,8 @@ export default function loader(content) {
);
}

const filePath = this.resourcePath;

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

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

url = relativePath + url;
} else if (options.outputPath) {
// support functions as outputPath to generate them dynamically
outputPath = typeof options.outputPath === 'function' ? options.outputPath(url) : options.outputPath + url;

url = outputPath;
} else {
outputPath = url;
outputPath += url;
}

let publicPath = `__webpack_public_path__ + ${JSON.stringify(url)}`;
Expand Down
54 changes: 52 additions & 2 deletions test/options/__snapshots__/outputPath.test.js.snap
@@ -1,5 +1,55 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Options outputPath {Function} 1`] = `"module.exports = __webpack_public_path__ + \\"test/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`;
exports[`Options outputPath {Function} 1`] = `
Object {
"assets": Array [
"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;

exports[`Options outputPath {String} 1`] = `"module.exports = __webpack_public_path__ + \\"/test/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`;
exports[`Options outputPath {Function} with \`publicPath\` {Function} 1`] = `
Object {
"assets": Array [
"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;

exports[`Options outputPath {Function} with \`publicPath\` {String} 1`] = `
Object {
"assets": Array [
"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;

exports[`Options outputPath {String} 1`] = `
Object {
"assets": Array [
"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;

exports[`Options outputPath {String} with \`publicPath\` {Function} 1`] = `
Object {
"assets": Array [
"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;

exports[`Options outputPath {String} with \`publicPath\` {String} 1`] = `
Object {
"assets": Array [
"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;
18 changes: 16 additions & 2 deletions test/options/__snapshots__/publicPath.test.js.snap
@@ -1,5 +1,19 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Options publicPath {Function} 1`] = `"module.exports = \\"test/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`;
exports[`Options publicPath {Function} 1`] = `
Object {
"assets": Array [
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;

exports[`Options publicPath {String} 1`] = `"module.exports = \\"/test/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`;
exports[`Options publicPath {String} 1`] = `
Object {
"assets": Array [
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;
88 changes: 82 additions & 6 deletions test/options/outputPath.test.js
Expand Up @@ -10,15 +10,15 @@ describe('Options', () => {
loader: {
test: /(png|jpg|svg)/,
options: {
outputPath: '/test/',
outputPath: 'output_path/',
},
},
};

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

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

test('{Function}', async () => {
Expand All @@ -27,16 +27,92 @@ describe('Options', () => {
test: /(png|jpg|svg)/,
options: {
outputPath(url) {
return `test/${url}`;
return `output_path/${url}`;
},
},
},
};

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

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

test('{String} with `publicPath` {String}', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
outputPath: 'output_path/',
publicPath: 'public_path/',
},
},
};

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

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

test('{Function} with `publicPath` {String}', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
publicPath: 'public_path/',
outputPath(url) {
return `output_path/${url}`;
},
},
},
};

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

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

test('{String} with `publicPath` {Function}', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
outputPath: 'output_path/',
publicPath(url) {
return `public_path/${url}`;
},
},
},
};

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

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

test('{Function} with `publicPath` {Function}', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
outputPath(url) {
return `output_path/${url}`;
},
publicPath(url) {
return `public_path/${url}`;
},
},
},
};

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

expect({ assets, source }).toMatchSnapshot();
});
});
});
12 changes: 6 additions & 6 deletions test/options/publicPath.test.js
Expand Up @@ -10,15 +10,15 @@ describe('Options', () => {
loader: {
test: /(png|jpg|svg)/,
options: {
publicPath: '/test/',
publicPath: 'public_path/',
},
},
};

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

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

test('{Function}', async () => {
Expand All @@ -27,16 +27,16 @@ describe('Options', () => {
test: /(png|jpg|svg)/,
options: {
publicPath(url) {
return `test/${url}`;
return `public_path/${url}`;
},
},
},
};

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

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

3 comments on commit 98bf052

@tnguyen14
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tnguyen14 you use buggy behavior, so it was fix

@tnguyen14
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Please sign in to comment.