minimum working test

This commit is contained in:
Travis Matthews 2017-04-10 22:18:05 -04:00
parent 392b0c3e0f
commit 1ed40e4bfc
9 changed files with 61 additions and 13 deletions

View File

@ -29,6 +29,45 @@ function wrapComment(str) {
*[N] the N-th match obtained from matching the current file name against options.regExp
*/
const REGEXP_HASH = /\[hash:?(\d+)?\]/gi,
REGEXP_CHUNKHASH = /\[chunkhash(?::(\d+))?\]/gi,
REGEXP_NAME = /\[name\]/gi,
REGEXP_ID = /\[id\]/gi,
REGEXP_FILE = /\[file\]/gi,
REGEXP_QUERY = /\[query\]/gi;
// withHashLength, getReplacer from lib/TemplatedPathPlugin.js
const withHashLength = (replacer, handlerFn) => {
return function(_, hashLength) {
const length = hashLength && parseInt(hashLength, 10);
if(length && handlerFn) {
return handlerFn(length);
}
const hash = replacer.apply(this, arguments);
return length ? hash.slice(0, length) : hash;
};
};
const getReplacer = (value, allowEmpty) => {
return function(match) {
// last argument in replacer is the entire input string
const input = arguments[arguments.length - 1];
if(value === null || value === undefined) {
if(!allowEmpty) throw new Error(`Path variable ${match} not implemented in this context: ${input}`);
return "";
} else {
return `${value}`;
}
};
};
const interpolate = (banner, file, hash, chunkHash) => {
return banner
.replace(REGEXP_HASH, withHashLength(getReplacer(hash)))
}
class BannerPlugin {
constructor(options) {
if(arguments.length > 1)
@ -42,21 +81,19 @@ class BannerPlugin {
}
apply(compiler) {
const options = this.options;
const banner = this.banner;
const options = this.options,
banner = this.banner;
compiler.plugin("compilation", (compilation) => {
console.log('compilation: ', JSON.stringify(compilation));
compilation.plugin("optimize-chunk-assets", (chunks, callback) => {
chunks.forEach((chunk) => {
if(options.entryOnly && !chunk.isInitial()) return;
chunk.files
.filter(ModuleFilenameHelpers.matchObject.bind(undefined, options))
.forEach((file) => {
return compilation.assets[file] = new ConcatSource(
banner, "\n", compilation.assets[file]
)
const chunkHash = chunk.renderedHash || chunk.hash
const comment = interpolate(banner, file, compilation.hash, chunkHash)
return compilation.assets[file] = new ConcatSource(comment, "\n", compilation.assets[file])
});
});
callback();

View File

@ -125,7 +125,10 @@ describe("ConfigTestCases", () => {
}
}
// give a free pass to compilation that generated an error
if(!jsonStats.errors.length && filesCount !== optionsArr.length) return done(new Error("Should have found at least one bundle file per webpack config"));
if(!jsonStats.errors.length && filesCount !== optionsArr.length) {
const msg = "Should have found at least one bundle file per webpack config" + filesCount + " " + " " + optionsArr.length
return done(new Error());
}
if(exportedTests < filesCount) return done(new Error("No tests exported by test case"));
process.nextTick(done);
});

View File

@ -1,11 +1,13 @@
it("should interpolate file hash in bundle0 chunk", function() {
it("should interpolate file hash in bundle0 chunk", function(done) {
var fs = require("fs");
var source = fs
.readFileSync(__filename, "utf-8")
.split("\n")
.slice(0,1)[0];
//debugger
source.should.not.containEql("hash");
done()
});
require.include("./test.js");

View File

@ -0,0 +1,7 @@
var fs = require("fs");
module.exports = {
findBundle: function(i, options) {
return "./banner.js";
}
};

View File

@ -5,7 +5,7 @@ module.exports = {
__filename: false
},
entry: {
bundle0: ["./index.js"],
banner: ["./index.js"],
vendors: ["./vendors.js"]
},
output: {
@ -13,8 +13,7 @@ module.exports = {
},
plugins: [
new webpack.BannerPlugin({
banner: "[hash]",
exclude: ["vendors.js"]
banner: "[hash]"
})
]
};

View File

@ -13,7 +13,7 @@ module.exports = {
},
plugins: [
new webpack.BannerPlugin({
banner: "[hash]",
banner: "This is a value",
exclude: ["vendors.js"]
})
]