diff --git a/index.js b/index.js index 4e79bd1f..8a9acfed 100644 --- a/index.js +++ b/index.js @@ -328,40 +328,7 @@ HardSourceWebpackPlugin.prototype.apply = function(compiler) { } var environmentHasher = null; - if (typeof options.environmentPaths !== 'undefined') { - loggerCore.error( - { - id: 'environment-paths-deprecated' - }, - 'environmentPaths is deprecated, please use environmentHash. ' + - 'environmentHash accepts the same options.' - ); - if (options.environmentPaths === false) { - environmentHasher = function() { - return Promise.resolve(''); - }; - } - else if (typeof options.environmentPaths === 'string') { - environmentHasher = function() { - return Promise.resolve(options.environmentPaths); - }; - } - else { - environmentHasher = function() { - return envHash(options.environmentPaths); - }; - } - } if (typeof options.environmentHash !== 'undefined') { - if (environmentHasher) { - loggerCore.warn( - { - id: 'environment-paths-and-hash-defined' - }, - 'environmentHash is a new option replacing environmentPaths. Please ' + - 'use only environmentHash.' - ); - } if (options.environmentHash === false) { environmentHasher = function() { return Promise.resolve(''); diff --git a/lib/env-hash.js b/lib/env-hash.js index ec481781..1fe6df41 100644 --- a/lib/env-hash.js +++ b/lib/env-hash.js @@ -48,34 +48,54 @@ function flatten(items) { module.exports = function(options) { options = options || {}; var root = options.root || process.cwd(); - var files = options.files || ['package.json']; - var directories = options.directories || ['node_modules']; + var files = options.files; + var directories = options.directories; - return Promise.all([ - hashFiles(root, files), - Promise.all(directories.map(function(dir) { - return readdir(path.join(root, dir)) - .then(function(items) { - return Promise.all(items.map(function(item) { - return stat(path.join(root, dir, item)) - .then(function(stat) { - if (stat.isDirectory()) { - return hashFiles(path.join(root, dir, item), files); - } - if (stat.isFile()) { - return hashFile(path.join(root, dir, item)) - .then(function(hash) {return hash ? [hash] : hash;}); - } - }) - .catch(function() {console.error(arguments)}); - })) - .then(function(hashes) {return hashes.filter(Boolean);}); - }) - .catch(function() {}) - .then(flatten); - })) - .then(flatten), - ]) + var hashDefaults = Promise.resolve(); + if (!files && !directories) { + hashDefaults = hashFiles(root, ['package-lock.json', 'yarn.lock']); + } + + return hashDefaults + .then(function(_defaults) { + if (_defaults && _defaults.length > 0) { + return [_defaults]; + } + else { + if (!files) { + files = ['package.json']; + } + if (!directories) { + directories = ['node_modules']; + } + } + + return Promise.all([ + hashFiles(root, files), + Promise.all(directories.map(function(dir) { + return readdir(path.join(root, dir)) + .then(function(items) { + return Promise.all(items.map(function(item) { + return stat(path.join(root, dir, item)) + .then(function(stat) { + if (stat.isDirectory()) { + return hashFiles(path.join(root, dir, item), files); + } + if (stat.isFile()) { + return hashFile(path.join(root, dir, item)) + .then(function(hash) {return hash ? [hash] : hash;}); + } + }) + .catch(function() {console.error(arguments)}); + })) + .then(function(hashes) {return hashes.filter(Boolean);}); + }) + .catch(function() {}) + .then(flatten); + })) + .then(flatten), + ]); + }) .then(flatten) .then(function(items) { items.forEach(function(item) { diff --git a/tests/fixtures/hard-source-packageyarnlock-hash/env-hash b/tests/fixtures/hard-source-packageyarnlock-hash/env-hash new file mode 100644 index 00000000..63d8dbd4 --- /dev/null +++ b/tests/fixtures/hard-source-packageyarnlock-hash/env-hash @@ -0,0 +1 @@ +b \ No newline at end of file diff --git a/tests/fixtures/hard-source-packageyarnlock-hash/fib/index.js b/tests/fixtures/hard-source-packageyarnlock-hash/fib/index.js new file mode 100644 index 00000000..bf0205d4 --- /dev/null +++ b/tests/fixtures/hard-source-packageyarnlock-hash/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-packageyarnlock-hash/hard-source-config.js b/tests/fixtures/hard-source-packageyarnlock-hash/hard-source-config.js new file mode 100644 index 00000000..b55d6b3c --- /dev/null +++ b/tests/fixtures/hard-source-packageyarnlock-hash/hard-source-config.js @@ -0,0 +1,7 @@ +{ + cacheDirectory: "cache", + recordsPath: "cache/records.json", + environmentHash: { + root: __dirname, + }, +} \ No newline at end of file diff --git a/tests/fixtures/hard-source-packageyarnlock-hash/index.js b/tests/fixtures/hard-source-packageyarnlock-hash/index.js new file mode 100644 index 00000000..8acf0395 --- /dev/null +++ b/tests/fixtures/hard-source-packageyarnlock-hash/index.js @@ -0,0 +1,3 @@ +var fib = require('./fib'); + +console.log(fib(3)); diff --git a/tests/fixtures/hard-source-packageyarnlock-hash/loader.js b/tests/fixtures/hard-source-packageyarnlock-hash/loader.js new file mode 100644 index 00000000..693807a3 --- /dev/null +++ b/tests/fixtures/hard-source-packageyarnlock-hash/loader.js @@ -0,0 +1,3 @@ +module.exports = function(source) { + return require('fs').readFileSync('./vendor/lib1.js') + source; +}; diff --git a/tests/fixtures/hard-source-packageyarnlock-hash/package-lock.json b/tests/fixtures/hard-source-packageyarnlock-hash/package-lock.json new file mode 100644 index 00000000..3410062b --- /dev/null +++ b/tests/fixtures/hard-source-packageyarnlock-hash/package-lock.json @@ -0,0 +1 @@ +c \ No newline at end of file diff --git a/tests/fixtures/hard-source-packageyarnlock-hash/vendor/lib1.js b/tests/fixtures/hard-source-packageyarnlock-hash/vendor/lib1.js new file mode 100644 index 00000000..6d012e7f --- /dev/null +++ b/tests/fixtures/hard-source-packageyarnlock-hash/vendor/lib1.js @@ -0,0 +1 @@ +console.log("b"); diff --git a/tests/fixtures/hard-source-packageyarnlock-hash/vendor/lib2.js b/tests/fixtures/hard-source-packageyarnlock-hash/vendor/lib2.js new file mode 100644 index 00000000..6d012e7f --- /dev/null +++ b/tests/fixtures/hard-source-packageyarnlock-hash/vendor/lib2.js @@ -0,0 +1 @@ +console.log("b"); diff --git a/tests/fixtures/hard-source-packageyarnlock-hash/webpack.config.js b/tests/fixtures/hard-source-packageyarnlock-hash/webpack.config.js new file mode 100644 index 00000000..3e829a81 --- /dev/null +++ b/tests/fixtures/hard-source-packageyarnlock-hash/webpack.config.js @@ -0,0 +1,22 @@ +var fs = require('fs'); + +var HardSourceWebpackPlugin = require('../../..'); + +var hardSourceConfig = eval( + '(function() { return (' + + require('fs') + .readFileSync(__dirname + '/hard-source-config.js', 'utf8') + + '); })' +)(); + +module.exports = { + context: __dirname, + entry: './loader.js!./index.js', + output: { + path: __dirname + '/tmp', + filename: 'main.js', + }, + plugins: [ + new HardSourceWebpackPlugin(hardSourceConfig), + ], +}; diff --git a/tests/fixtures/hard-source-packageyarnlock-hash/yarn.lock b/tests/fixtures/hard-source-packageyarnlock-hash/yarn.lock new file mode 100644 index 00000000..63d8dbd4 --- /dev/null +++ b/tests/fixtures/hard-source-packageyarnlock-hash/yarn.lock @@ -0,0 +1 @@ +b \ No newline at end of file diff --git a/tests/hard-source.js b/tests/hard-source.js index f54dc2ad..454d7fff 100644 --- a/tests/hard-source.js +++ b/tests/hard-source.js @@ -143,20 +143,6 @@ describe('hard-source features', function() { }); } - itCompilesEnvironmentHashDisabled('paths-false', [ - '{', - ' cacheDirectory: "cache",', - ' recordsPath: "cache/records.json",', - ' environmentPaths: false,', - '}', - ], [ - '{', - ' cacheDirectory: "cache",', - ' recordsPath: "cache/records.json",', - ' environmentPaths: false,', - '}', - ]); - itCompilesEnvironmentHashDisabled('false', [ '{', ' cacheDirectory: "cache",', @@ -171,30 +157,6 @@ describe('hard-source features', function() { '}', ]); - itCompilesEnvironmentHash('paths-envhash', [ - '{', - ' cacheDirectory: "cache",', - ' recordsPath: "cache/records.json",', - ' environmentPaths: {', - ' root: __dirname,', - ' directories: ["vendor"],', - ' files: [],', - ' },', - '}', - ]); - - itCompilesEnvironmentHash('paths-envhash-files', [ - '{', - ' cacheDirectory: "cache",', - ' recordsPath: "cache/records.json",', - ' environmentPaths: {', - ' root: __dirname,', - ' directories: ["vendor"],', - ' files: ["env-hash"],', - ' },', - '}', - ]); - itCompilesEnvironmentHash('string', [ '{', ' cacheDirectory: "cache",', @@ -258,4 +220,64 @@ describe('hard-source features', function() { '}', ]); + var _packageYarnLockHashConfig = [ + '{', + ' cacheDirectory: "cache",', + ' recordsPath: "cache/records.json",', + ' environmentHash: {', + ' root: __dirname,', + ' },', + '}', + ]; + + function itCompilesPackageYarnLockHash(key, files1, files2) { + itCompiles('compiles hard-source-packageyarnlock-hash ' + key + ' with fresh cache', 'hard-source-packageyarnlock-hash', function() { + return writeFiles('hard-source-packageyarnlock-hash', Object.assign({ + 'hard-source-config.js': _packageYarnLockHashConfig.join('\n'), + }, files1)); + }, function() { + return writeFiles('hard-source-packageyarnlock-hash', Object.assign({ + 'hard-source-config.js': _packageYarnLockHashConfig.join('\n'), + }, files2)) + .then(function() { + return fs.readFileSync(__dirname + '/fixtures/hard-source-packageyarnlock-hash/tmp/cache/stamp', 'utf8'); + }); + }, function(output) { + var stamp = fs.readFileSync(__dirname + '/fixtures/hard-source-packageyarnlock-hash/tmp/cache/stamp', 'utf8'); + expect(stamp).to.not.equal(output.setup2); + }); + } + + itCompilesPackageYarnLockHash('package-lock', { + 'package-lock.json': 'a', + 'yarn.lock': null, + }, { + 'package-lock.json': 'b', + 'yarn.lock': null, + }); + + itCompilesPackageYarnLockHash('yarn-lock', { + 'package-lock.json': null, + 'yarn.lock': 'a', + }, { + 'package-lock.json': null, + 'yarn.lock': 'b', + }); + + itCompilesPackageYarnLockHash('package-yarn-lock', { + 'package-lock.json': 'a', + 'yarn.lock': 'b', + }, { + 'package-lock.json': 'a', + 'yarn.lock': 'c', + }); + + itCompilesPackageYarnLockHash('package-yarn-lock-2', { + 'package-lock.json': 'a', + 'yarn.lock': 'b', + }, { + 'package-lock.json': 'c', + 'yarn.lock': 'b', + }); + });