Skip to content

Commit

Permalink
make multiple assets to the same filename a warning
Browse files Browse the repository at this point in the history
check source content to when comparing assets for warning
  • Loading branch information
sokra committed Sep 13, 2019
1 parent c5450b4 commit e41fab0
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 4 deletions.
33 changes: 29 additions & 4 deletions lib/Compilation.js
Expand Up @@ -37,11 +37,11 @@ const compareLocations = require("./compareLocations");
const { Logger, LogType } = require("./logging/Logger");
const ErrorHelpers = require("./ErrorHelpers");
const buildChunkGraph = require("./buildChunkGraph");
const WebpackError = require("./WebpackError");

/** @typedef {import("./Module")} Module */
/** @typedef {import("./Compiler")} Compiler */
/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("./WebpackError")} WebpackError */
/** @typedef {import("./DependenciesBlockVariable")} DependenciesBlockVariable */
/** @typedef {import("./dependencies/SingleEntryDependency")} SingleEntryDependency */
/** @typedef {import("./dependencies/MultiEntryDependency")} MultiEntryDependency */
Expand Down Expand Up @@ -221,6 +221,25 @@ const addAllToSet = (set, otherSet) => {
}
};

/**
* @param {Source} a a source
* @param {Source} b another source
* @returns {boolean} true, when both sources are equal
*/
const isSourceEqual = (a, b) => {
if (a === b) return true;
// TODO webpack 5: check .buffer() instead, it's called anyway during emit
/** @type {Buffer|string} */
let aSource = a.source();
/** @type {Buffer|string} */
let bSource = b.source();
if (aSource === bSource) return true;
if (typeof aSource === "string" && typeof bSource === "string") return false;
if (!Buffer.isBuffer(aSource)) aSource = Buffer.from(aSource, "utf-8");
if (!Buffer.isBuffer(bSource)) bSource = Buffer.from(bSource, "utf-8");
return aSource.equals(bSource);
};

class Compilation extends Tapable {
/**
* Creates an instance of Compilation.
Expand Down Expand Up @@ -1990,10 +2009,16 @@ class Compilation extends Tapable {
*/
emitAsset(file, source, assetInfo = {}) {
if (this.assets[file]) {

This comment has been minimized.

Copy link
@alexander-akait

alexander-akait Sep 13, 2019

Member

In theory plugin can emit one.js and one.js?foo=bar and no errors will be for this case, but i think it should be warning (error in webpack@5)

if (this.assets[file] !== source) {
throw new Error(
`Conflict: Multiple assets emit to the same filename ${file}`
if (!isSourceEqual(this.assets[file], source)) {
// TODO webpack 5: make this an error instead
this.warnings.push(
new WebpackError(
`Conflict: Multiple assets emit different content to the same filename ${file}`
)
);
this.assets[file] = source;
this.assetsInfo.set(file, assetInfo);
return;
}
const oldInfo = this.assetsInfo.get(file);
this.assetsInfo.set(file, Object.assign({}, oldInfo, assetInfo));
Expand Down
4 changes: 4 additions & 0 deletions test/configCases/emit-asset/different-source/index.js
@@ -0,0 +1,4 @@
it("should compile without warnings", () => {
require("./test1.txt");
require("./test2.txt");
});
1 change: 1 addition & 0 deletions test/configCases/emit-asset/different-source/test1.txt
@@ -0,0 +1 @@
Hello World
1 change: 1 addition & 0 deletions test/configCases/emit-asset/different-source/test2.txt
@@ -0,0 +1 @@
Something else
7 changes: 7 additions & 0 deletions test/configCases/emit-asset/different-source/warnings.js
@@ -0,0 +1,7 @@
module.exports = [
[
/Conflict/,
/Multiple assets emit different content to the same filename/,
/same-name\.txt/
]
];
15 changes: 15 additions & 0 deletions test/configCases/emit-asset/different-source/webpack.config.js
@@ -0,0 +1,15 @@
module.exports = {
module: {
rules: [
{
test: /\.txt$/,
use: {
loader: "file-loader",
options: {
name: "same-name.txt"
}
}
}
]
}
};
4 changes: 4 additions & 0 deletions test/configCases/emit-asset/equal-source/index.js
@@ -0,0 +1,4 @@
it("should compile without warnings", () => {
require("./test1.txt");
require("./test2.txt");
});
1 change: 1 addition & 0 deletions test/configCases/emit-asset/equal-source/test1.txt
@@ -0,0 +1 @@
Hello World
1 change: 1 addition & 0 deletions test/configCases/emit-asset/equal-source/test2.txt
@@ -0,0 +1 @@
Hello World
15 changes: 15 additions & 0 deletions test/configCases/emit-asset/equal-source/webpack.config.js
@@ -0,0 +1,15 @@
module.exports = {
module: {
rules: [
{
test: /\.txt$/,
use: {
loader: "file-loader",
options: {
name: "same-name.txt"
}
}
}
]
}
};

0 comments on commit e41fab0

Please sign in to comment.