diff --git a/tests/fixtures/hard-source-confighash-dir/config-hash b/tests/fixtures/hard-source-confighash-dir/config-hash new file mode 100644 index 00000000..63d8dbd4 --- /dev/null +++ b/tests/fixtures/hard-source-confighash-dir/config-hash @@ -0,0 +1 @@ +b \ No newline at end of file diff --git a/tests/fixtures/hard-source-confighash-dir/fib/index.js b/tests/fixtures/hard-source-confighash-dir/fib/index.js new file mode 100644 index 00000000..bf0205d4 --- /dev/null +++ b/tests/fixtures/hard-source-confighash-dir/fib/index.js @@ -0,0 +1,3 @@ +module.exports = function(n) { + return n + (n > 0 ? n - 2 : 0); +}; \ No newline at end of file diff --git a/tests/fixtures/hard-source-confighash-dir/index.js b/tests/fixtures/hard-source-confighash-dir/index.js new file mode 100644 index 00000000..8acf0395 --- /dev/null +++ b/tests/fixtures/hard-source-confighash-dir/index.js @@ -0,0 +1,3 @@ +var fib = require('./fib'); + +console.log(fib(3)); diff --git a/tests/fixtures/hard-source-confighash-dir/webpack.config.js b/tests/fixtures/hard-source-confighash-dir/webpack.config.js new file mode 100644 index 00000000..1f08a1e5 --- /dev/null +++ b/tests/fixtures/hard-source-confighash-dir/webpack.config.js @@ -0,0 +1,24 @@ +var fs = require('fs'); + +var HardSourceWebpackPlugin = require('../../..'); + +module.exports = { + context: __dirname, + entry: './index.js', + output: { + path: __dirname + '/tmp', + filename: 'main.js', + }, + plugins: [ + new HardSourceWebpackPlugin({ + cacheDirectory: 'cache/[confighash]', + recordsPath: 'cache/[confighash]/records.json', + configHash: function(config) { + return fs.readFileSync(__dirname + '/config-hash', 'utf8'); + }, + environmentPaths: { + root: __dirname + '/../../..', + }, + }), + ], +}; diff --git a/tests/fixtures/hard-source-confighash/config-hash b/tests/fixtures/hard-source-confighash/config-hash new file mode 100644 index 00000000..63d8dbd4 --- /dev/null +++ b/tests/fixtures/hard-source-confighash/config-hash @@ -0,0 +1 @@ +b \ No newline at end of file diff --git a/tests/fixtures/hard-source-confighash/fib/index.js b/tests/fixtures/hard-source-confighash/fib/index.js new file mode 100644 index 00000000..bf0205d4 --- /dev/null +++ b/tests/fixtures/hard-source-confighash/fib/index.js @@ -0,0 +1,3 @@ +module.exports = function(n) { + return n + (n > 0 ? n - 2 : 0); +}; \ No newline at end of file diff --git a/tests/fixtures/hard-source-confighash/index.js b/tests/fixtures/hard-source-confighash/index.js new file mode 100644 index 00000000..8acf0395 --- /dev/null +++ b/tests/fixtures/hard-source-confighash/index.js @@ -0,0 +1,3 @@ +var fib = require('./fib'); + +console.log(fib(3)); diff --git a/tests/fixtures/hard-source-confighash/webpack.config.js b/tests/fixtures/hard-source-confighash/webpack.config.js new file mode 100644 index 00000000..44a677c8 --- /dev/null +++ b/tests/fixtures/hard-source-confighash/webpack.config.js @@ -0,0 +1,24 @@ +var fs = require('fs'); + +var HardSourceWebpackPlugin = require('../../..'); + +module.exports = { + context: __dirname, + entry: './index.js', + output: { + path: __dirname + '/tmp', + filename: 'main.js', + }, + plugins: [ + new HardSourceWebpackPlugin({ + cacheDirectory: 'cache', + recordsPath: 'cache/records.json', + configHash: function(config) { + return fs.readFileSync(__dirname + '/config-hash', 'utf8'); + }, + environmentPaths: { + root: __dirname + '/../../..', + }, + }), + ], +}; diff --git a/tests/hard-source.js b/tests/hard-source.js new file mode 100644 index 00000000..a58969d5 --- /dev/null +++ b/tests/hard-source.js @@ -0,0 +1,51 @@ +var fs = require('fs'); + +var expect = require('chai').expect; + +var itCompilesChange = require('./util').itCompilesChange; + +describe('hard-source features', function() { + + itCompilesChange('hard-source-confighash', { + 'config-hash': 'a', + 'fib.js': [ + 'module.exports = function(n) {', + ' return n + (n > 0 ? n - 1 : 0);', + '};', + ].join('\n'), + 'fib/index.js': null, + }, { + 'config-hash': 'b', + 'fib.js': null, + 'fib/index.js': [ + 'module.exports = function(n) {', + ' return n + (n > 0 ? n - 2 : 0);', + '};', + ].join('\n'), + }, function(output) { + expect(fs.readdirSync(__dirname + '/fixtures/hard-source-confighash/tmp/cache')) + .to.have.length(6); + }); + + itCompilesChange('hard-source-confighash-dir', { + 'config-hash': 'a', + 'fib.js': [ + 'module.exports = function(n) {', + ' return n + (n > 0 ? n - 1 : 0);', + '};', + ].join('\n'), + 'fib/index.js': null, + }, { + 'config-hash': 'b', + 'fib.js': null, + 'fib/index.js': [ + 'module.exports = function(n) {', + ' return n + (n > 0 ? n - 2 : 0);', + '};', + ].join('\n'), + }, function(output) { + expect(fs.readdirSync(__dirname + '/fixtures/hard-source-confighash-dir/tmp/cache')) + .to.have.length(2); + }); + +}); diff --git a/tests/util/index.js b/tests/util/index.js index e1f4555b..ef80f098 100644 --- a/tests/util/index.js +++ b/tests/util/index.js @@ -1,5 +1,6 @@ var fs = require('fs'); var path = require('path'); +var vm = require('vm'); var expect = require('chai').expect; var MemoryFS = require('memory-fs'); @@ -8,9 +9,26 @@ var Promise = require('bluebird'); var rimraf = require('rimraf'); var webpack = require('webpack'); +function wrapModule(code) { + return '(function(exports, require, module, __filename, __dirname) {' + + code + + '})'; +} + +function callModule(fn, filename) { + var module = {exports: {}}; + fn(module.exports, Object.assign(function(modulename) { + if (/\W/.test(modulename[0])) { + return require(path.join(path.dirname(filename), modulename)); + } + return require(modulename); + }, require), module, filename, path.dirname(filename)); + return module.exports; +} + exports.compile = function(fixturePath) { - var configPath = path.join(__dirname, '..', 'fixtures', fixturePath, 'webpack.config'); - var compiler = webpack(require(configPath)); + var configPath = path.join(__dirname, '..', 'fixtures', fixturePath, 'webpack.config.js'); + var compiler = webpack(callModule(vm.runInThisContext(wrapModule(fs.readFileSync(configPath, 'utf8')), {filename: configPath}), configPath)); var outputfs = compiler.outputFileSystem = new MemoryFS(); var readdir = Promise.promisify(outputfs.readdir, {context: outputfs}); var readFile = Promise.promisify(outputfs.readFile, {context: outputfs});