|
| 1 | +var should = require("should"); |
| 2 | +var path = require("path"); |
| 3 | +var fs = require("fs"); |
| 4 | + |
| 5 | +var NodeEnvironmentPlugin = require("../lib/node/NodeEnvironmentPlugin"); |
| 6 | +var Compiler = require("../lib/Compiler"); |
| 7 | +var WebpackOptionsApply = require("../lib/WebpackOptionsApply"); |
| 8 | +var WebpackOptionsDefaulter = require("../lib/WebpackOptionsDefaulter"); |
| 9 | + |
| 10 | +describe("Compiler (caching)", function() { |
| 11 | + this.timeout(10000); |
| 12 | + |
| 13 | + function compile(entry, options, callback) { |
| 14 | + new WebpackOptionsDefaulter().process(options); |
| 15 | + options.entry = entry; |
| 16 | + options.context = path.join(__dirname, "fixtures"); |
| 17 | + options.output.path = ""; |
| 18 | + options.output.filename = "bundle.js"; |
| 19 | + options.output.pathinfo = true; |
| 20 | + var logs = { |
| 21 | + mkdirp: [], |
| 22 | + writeFile: [], |
| 23 | + }; |
| 24 | + |
| 25 | + var c = new Compiler(); |
| 26 | + c.options = new WebpackOptionsApply().process(options, c); |
| 27 | + new NodeEnvironmentPlugin().apply(c); |
| 28 | + var files = {}; |
| 29 | + c.outputFileSystem = { |
| 30 | + join: path.join.bind(path), |
| 31 | + mkdirp: function(path, callback) { |
| 32 | + logs.mkdirp.push(path); |
| 33 | + callback(); |
| 34 | + }, |
| 35 | + writeFile: function(name, content, callback) { |
| 36 | + logs.writeFile.push(name, content); |
| 37 | + files[name] = content.toString("utf-8"); |
| 38 | + callback(); |
| 39 | + } |
| 40 | + }; |
| 41 | + c.plugin("compilation", function(compilation) { |
| 42 | + compilation.bail = true; |
| 43 | + }); |
| 44 | + |
| 45 | + var compilerIteration = 1; |
| 46 | + |
| 47 | + function runCompiler(callback) { |
| 48 | + c.run(function(err, stats) { |
| 49 | + if(err) throw err; |
| 50 | + should.strictEqual(typeof stats, "object"); |
| 51 | + stats = stats.toJson({ |
| 52 | + modules: true, |
| 53 | + reasons: true |
| 54 | + }); |
| 55 | + should.strictEqual(typeof stats, "object"); |
| 56 | + stats.should.have.property("errors"); |
| 57 | + Array.isArray(stats.errors).should.be.ok; |
| 58 | + if(stats.errors.length > 0) { |
| 59 | + stats.errors[0].should.be.instanceOf(Error); |
| 60 | + throw stats.errors[0]; |
| 61 | + } |
| 62 | + stats.logs = logs; |
| 63 | + callback(stats, files, compilerIteration++); |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + var postCompileCallbackStack = []; |
| 68 | + |
| 69 | + function addAfterCompileCallback(callback) { |
| 70 | + postCompileCallbackStack.push(callback); |
| 71 | + } |
| 72 | + |
| 73 | + c.plugin("after-compile", function(stats, callback) { |
| 74 | + |
| 75 | + if(postCompileCallbackStack.length > 0) { |
| 76 | + postCompileCallbackStack.shift().apply(this, arguments); |
| 77 | + } |
| 78 | + |
| 79 | + callback() |
| 80 | + }) |
| 81 | + |
| 82 | + runCompiler(callback); |
| 83 | + |
| 84 | + return { |
| 85 | + compilerInstance: c, |
| 86 | + runAgain: runCompiler, |
| 87 | + addAfterCompileCallback: addAfterCompileCallback |
| 88 | + }; |
| 89 | + } |
| 90 | + |
| 91 | + var tempFixturePath = path.join(__dirname, "fixtures", "temp-cache-fixture"); |
| 92 | + var aFilepath = path.join(tempFixturePath, 'a.js'); |
| 93 | + var cFilepath = path.join(tempFixturePath, 'c.js'); |
| 94 | + |
| 95 | + after(function() { |
| 96 | + try { |
| 97 | + if(fs.statSync(tempFixturePath)) { |
| 98 | + fs.unlinkSync(aFilepath); |
| 99 | + fs.unlinkSync(cFilepath); |
| 100 | + fs.rmdirSync(tempFixturePath); |
| 101 | + } |
| 102 | + } catch(e) { |
| 103 | + if(e.code !== 'ENOENT') { |
| 104 | + throw e; |
| 105 | + } |
| 106 | + } |
| 107 | + }) |
| 108 | + |
| 109 | + function createTempFixture() { |
| 110 | + |
| 111 | + // Remove previous copy if present |
| 112 | + try { |
| 113 | + if(fs.statSync(tempFixturePath)) { |
| 114 | + fs.unlinkSync(aFilepath); |
| 115 | + fs.unlinkSync(cFilepath); |
| 116 | + fs.rmdirSync(tempFixturePath); |
| 117 | + } |
| 118 | + } catch(e) { |
| 119 | + if(e.code !== 'ENOENT') { |
| 120 | + throw e; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + // Copy over file since we'll be modifying some of them |
| 125 | + fs.mkdirSync(tempFixturePath); |
| 126 | + fs.createReadStream(path.join(__dirname, "fixtures", 'a.js')).pipe(fs.createWriteStream(aFilepath)); |
| 127 | + fs.createReadStream(path.join(__dirname, "fixtures", 'c.js')).pipe(fs.createWriteStream(cFilepath)); |
| 128 | + |
| 129 | + return { |
| 130 | + rootPath: tempFixturePath, |
| 131 | + aFilepath: aFilepath, |
| 132 | + cFilepath: cFilepath |
| 133 | + }; |
| 134 | + } |
| 135 | + |
| 136 | + it("should cache single file (with manual 1s wait) ", function(done) { |
| 137 | + |
| 138 | + var options = {}; |
| 139 | + var tempFixture = createTempFixture(); |
| 140 | + |
| 141 | + var helper = compile("./temp-cache-fixture/c", options, function(stats, files) { |
| 142 | + |
| 143 | + // Not cached the first time |
| 144 | + stats.assets[0].name.should.be.exactly('bundle.js'); |
| 145 | + stats.assets[0].emitted.should.be.exactly(true); |
| 146 | + |
| 147 | + helper.runAgain(function(stats, files, iteration) { |
| 148 | + |
| 149 | + // Cached the second run |
| 150 | + stats.assets[0].name.should.be.exactly('bundle.js'); |
| 151 | + stats.assets[0].emitted.should.be.exactly(false); |
| 152 | + |
| 153 | + var aContent = fs.readFileSync(tempFixture.aFilepath).toString().replace('This is a', 'This is a MODIFIED'); |
| 154 | + |
| 155 | + setTimeout(function() { |
| 156 | + fs.writeFileSync(tempFixture.aFilepath, aContent); |
| 157 | + |
| 158 | + helper.runAgain(function(stats, files, iteration) { |
| 159 | + |
| 160 | + // Cached the third run |
| 161 | + stats.assets[0].name.should.be.exactly('bundle.js'); |
| 162 | + stats.assets[0].emitted.should.be.exactly(true); |
| 163 | + |
| 164 | + done(); |
| 165 | + }); |
| 166 | + |
| 167 | + }, 1100); |
| 168 | + }); |
| 169 | + }); |
| 170 | + }); |
| 171 | + |
| 172 | + it("should cache single file (even with no timeout) ", function(done) { |
| 173 | + |
| 174 | + var options = {}; |
| 175 | + var tempFixture = createTempFixture(); |
| 176 | + |
| 177 | + var helper = compile("./temp-cache-fixture/c", options, function(stats, files) { |
| 178 | + |
| 179 | + // Not cached the first time |
| 180 | + stats.assets[0].name.should.be.exactly('bundle.js'); |
| 181 | + stats.assets[0].emitted.should.be.exactly(true); |
| 182 | + |
| 183 | + helper.runAgain(function(stats, files, iteration) { |
| 184 | + |
| 185 | + // Cached the second run |
| 186 | + stats.assets[0].name.should.be.exactly('bundle.js'); |
| 187 | + stats.assets[0].emitted.should.be.exactly(false); |
| 188 | + |
| 189 | + files['bundle.js'].should.containEql('"This is a"'); |
| 190 | + |
| 191 | + var aContent = fs.readFileSync(tempFixture.aFilepath).toString().replace('This is a', 'This is a MODIFIED'); |
| 192 | + |
| 193 | + setTimeout(function() { |
| 194 | + fs.writeFileSync(tempFixture.aFilepath, aContent); |
| 195 | + |
| 196 | + helper.runAgain(function(stats, files, iteration) { |
| 197 | + |
| 198 | + // Cached the third run |
| 199 | + stats.assets[0].name.should.be.exactly('bundle.js'); |
| 200 | + stats.assets[0].emitted.should.be.exactly(true); |
| 201 | + |
| 202 | + files['bundle.js'].should.containEql('"This is a MODIFIED"'); |
| 203 | + |
| 204 | + done(); |
| 205 | + }); |
| 206 | + }, 1100); |
| 207 | + }); |
| 208 | + }); |
| 209 | + }); |
| 210 | + |
| 211 | + it("should only build when modified (with manual 1s wait)", function(done) { |
| 212 | + |
| 213 | + var options = {}; |
| 214 | + var tempFixture = createTempFixture(); |
| 215 | + |
| 216 | + var helper = compile("./temp-cache-fixture/c", options, function(stats, files) { |
| 217 | + |
| 218 | + // Built the first time |
| 219 | + stats.modules[0].name.should.containEql('a.js'); |
| 220 | + stats.modules[0].built.should.be.exactly(true, 'a.js should have been built'); |
| 221 | + |
| 222 | + stats.modules[1].name.should.containEql('c.js'); |
| 223 | + stats.modules[1].built.should.be.exactly(true, 'c.js should have been built'); |
| 224 | + |
| 225 | + helper.runAgain(function(stats, files, iteration) { |
| 226 | + |
| 227 | + // Not built when cached the second run |
| 228 | + stats.modules[0].name.should.containEql('a.js'); |
| 229 | + stats.modules[0].built.should.be.exactly(false, 'a.js should not have built'); |
| 230 | + |
| 231 | + stats.modules[1].name.should.containEql('c.js'); |
| 232 | + stats.modules[1].built.should.be.exactly(false, 'c.js should not have built'); |
| 233 | + |
| 234 | + var aContent = fs.readFileSync(tempFixture.aFilepath).toString().replace('This is a', 'This is a MODIFIED'); |
| 235 | + |
| 236 | + fs.writeFileSync(tempFixture.aFilepath, aContent); |
| 237 | + |
| 238 | + helper.runAgain(function(stats, files, iteration) { |
| 239 | + |
| 240 | + // And only a.js built after it was modified |
| 241 | + stats.modules[0].name.should.containEql('a.js'); |
| 242 | + stats.modules[0].built.should.be.exactly(true, 'a.js should have been built'); |
| 243 | + |
| 244 | + stats.modules[1].name.should.containEql('c.js'); |
| 245 | + stats.modules[1].built.should.be.exactly(false, 'c.js should not have built'); |
| 246 | + |
| 247 | + done(); |
| 248 | + }); |
| 249 | + }); |
| 250 | + }); |
| 251 | + }); |
| 252 | + |
| 253 | + it("should only build when modified (even with no timeout)", function(done) { |
| 254 | + |
| 255 | + var options = {}; |
| 256 | + var tempFixture = createTempFixture(); |
| 257 | + |
| 258 | + var helper = compile("./temp-cache-fixture/c", options, function(stats, files) { |
| 259 | + |
| 260 | + // Built the first time |
| 261 | + stats.modules[0].name.should.containEql('a.js'); |
| 262 | + stats.modules[0].built.should.be.exactly(true, 'a.js should have been built'); |
| 263 | + |
| 264 | + stats.modules[1].name.should.containEql('c.js'); |
| 265 | + stats.modules[1].built.should.be.exactly(true, 'c.js should have been built'); |
| 266 | + |
| 267 | + helper.runAgain(function(stats, files, iteration) { |
| 268 | + |
| 269 | + // Not built when cached the second run |
| 270 | + stats.modules[0].name.should.containEql('a.js'); |
| 271 | + stats.modules[0].built.should.be.exactly(false, 'a.js should not have built'); |
| 272 | + |
| 273 | + stats.modules[1].name.should.containEql('c.js'); |
| 274 | + stats.modules[1].built.should.be.exactly(false, 'c.js should not have built'); |
| 275 | + |
| 276 | + var aContent = fs.readFileSync(tempFixture.aFilepath).toString().replace('This is a', 'This is a MODIFIED'); |
| 277 | + |
| 278 | + fs.writeFileSync(tempFixture.aFilepath, aContent); |
| 279 | + |
| 280 | + helper.runAgain(function(stats, files, iteration) { |
| 281 | + |
| 282 | + // And only a.js built after it was modified |
| 283 | + stats.modules[0].name.should.containEql('a.js'); |
| 284 | + stats.modules[0].built.should.be.exactly(true, 'a.js should have been built'); |
| 285 | + |
| 286 | + stats.modules[1].name.should.containEql('c.js'); |
| 287 | + stats.modules[1].built.should.be.exactly(false, 'c.js should not have built'); |
| 288 | + |
| 289 | + done(); |
| 290 | + }); |
| 291 | + }); |
| 292 | + }); |
| 293 | + }); |
| 294 | + |
| 295 | +}); |
0 commit comments