Webpack Config is stripping out Crypto
- OS?
Mac OSX El Capitan - Versions.
angular-cli: local (v1.0.0-beta.10, branch: webpack)
node: 4.4.7
os: darwin x64 - Repro steps.
I've pulled down the Angular-cli project and am building it locally, so I am using the new webpack version.
The project that I am building uses Crypto libraries (via a dependency of a dependency ...).
When I was running
ng buildI was seeing the dependency built like this:
/* WEBPACK VAR INJECTION */(function(Buffer) {'use strict';
var crypto = __webpack_require__(147);
var BufferUtil = __webpack_require__(21);
var $ = __webpack_require__(15);
var Hash = module.exports;
Hash.sha1 = function(buf) {
$.checkArgument(BufferUtil.isBuffer(buf));
return crypto.createHash('sha1').update(buf).digest();
};
...
And then when I looked at module 147 (I.E. the crypto module) I was seeing nothing there:
/***/ },
/* 147 */
/***/ function(module, exports, __webpack_require__) {
/***/ },
After a while of searching I took a look in the Angular-CLI code and saw that part of the webpack config (addon/ng2/models/webpack-build-common.ts) was this:
node: {
global: 'window',
crypto: 'empty',
module: false,
clearImmediate: false,
setImmediate: false
}
When I remove crypto: 'empty' everything builds correctly with the crypto module getting pulled in.
So my question is, why have you specified that crypto should be empty? Is there any way to fix this more permanently?
Also affected here.
Tried to use gencryption which has a dependency to crypto. The lib calls createHash(...) which leads to Uncaught TypeError: crypto.createHash is not a function.
Could this be an option on .angular.cli? I am affected too, and the solution already exists, and already exists on webpack using crypto-browserify. I see no reason to not allow that webpack functionality in angular-cli, even if it's set by default.
Closing with https://github.com/angular/angular-cli/issues/1548#issuecomment-286151056 as the reasoning. For apps that need server-side crypto while using universal, please follow https://github.com/angular/angular-cli/issues/1050.
Is there a workaround for this problem other than forking Angular CLI?
My very dirty workaround: create fixAngularCrypto.js
var fs = require('fs')
console.log('running postinstall script to enable crypto')
fs.readFile('./node_modules/@angular/cli/models/webpack-configs/common.js', 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
var result = data.replace(/crypto: \'empty\',/g, 'crypto: true,');
fs.writeFile('./node_modules/@angular/cli/models/webpack-configs/common.js', result, 'utf8', function (err) {
if (err) return console.log(err);
});
});
add the following line in the script section of package.json:
"postinstall":"node fixAngularCrypto.js"
run npm install
Thanks @blascone. Your workaround solves my problem.
This workaround should be modified for the new version of @angular/cli I think. -> Changing "common.js" to "browser.js" works for me.
thanks @blascone for the workaround. Dirty indeed but functional at least.
Hoping that a clean and proper solution will be find to allow enabling crypto on angular-cli with webpack.
Maybe once we get possibility of overriding default webpack config with a custom one ?
@blascone. Looks like your solution has been nullified in AngularJs 5!
@Sydwell yes. but you can execute ng eject and customize your local webpack.config.ts
@froid911 How would I go about customizing my webpack.config to allow node's crypto? I'm new to webpack.
@web-snake just change the crypto parameter on node to true
node: {
global: 'window',
crypto: true, <---- from 'empty' to true
module: false,
clearImmediate: false,
setImmediate: false
}
@froid1911 where do I change this parameter in an angular-cli project ? I don't find any webpack.config.ts file ...
@GrandSchtroumpf you have to execute ng eject. Afterwards you'll see the file in your project directory.
Full steps for others coming to the thread:
ng eject
// Output: Ejection was successful.
// Open webpack.config.js
// Change these lines
"node": {
"fs": "empty",
"global": true,
"crypto": true, <---- from 'empty' to true
"tls": "empty",
"net": "empty",
"process": true,
"module": false,
"clearImmediate": false,
"setImmediate": false
},
I have modified the crypto: true in webpack.config.js. Still facing the same issue
@Srinivas72 I am facing the same problem with angular you got any way out?
Yes was able to fix this.
create one javascript file in your project root folder.
add this code in to that.
var fs = require('fs'); console.log('running postinstall script to enable crypto'); fs.readFile('./node_modules/@angular/cli/models/webpack-configs/browser.js', 'utf8', function (err,data) { if (err) { return console.log(err); } var result = data.replace(/crypto: 'empty',/g, 'crypto: true,');
fs.writeFile('./node_modules/@angular/cli/models/webpack-configs/browser.js', result, 'utf8', function (err) { if (err) return console.log(err); }); });
in package.json
add "postinstall": "node yourfilename.js"
run npm install
it will resolve your issue.
@Srinivas72 thanks :) I was keeping the "postinstall" parameter at the wrong place. its working
@Srinivas72 Not working for me at least.