Skip to content

Commit

Permalink
Merge pull request #209 from mzgoddard/environment-hash-lock-default
Browse files Browse the repository at this point in the history
Default to package-lock.json and yarn.lock for environmentHash
  • Loading branch information
mzgoddard authored Dec 11, 2017
2 parents 6efdb26 + 1b56ed8 commit d3ffdc1
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 98 deletions.
33 changes: 0 additions & 33 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('');
Expand Down
74 changes: 47 additions & 27 deletions lib/env-hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/hard-source-packageyarnlock-hash/env-hash
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
b
3 changes: 3 additions & 0 deletions tests/fixtures/hard-source-packageyarnlock-hash/fib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function(n) {
return n + (n > 0 ? n - 2 : 0);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
cacheDirectory: "cache",
recordsPath: "cache/records.json",
environmentHash: {
root: __dirname,
},
}
3 changes: 3 additions & 0 deletions tests/fixtures/hard-source-packageyarnlock-hash/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var fib = require('./fib');

console.log(fib(3));
3 changes: 3 additions & 0 deletions tests/fixtures/hard-source-packageyarnlock-hash/loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function(source) {
return require('fs').readFileSync('./vendor/lib1.js') + source;
};

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("b");
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("b");
22 changes: 22 additions & 0 deletions tests/fixtures/hard-source-packageyarnlock-hash/webpack.config.js
Original file line number Diff line number Diff line change
@@ -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),
],
};
1 change: 1 addition & 0 deletions tests/fixtures/hard-source-packageyarnlock-hash/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
b
98 changes: 60 additions & 38 deletions tests/hard-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",',
Expand All @@ -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",',
Expand Down Expand Up @@ -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',
});

});

0 comments on commit d3ffdc1

Please sign in to comment.