I have a node module that uses modules that depend on fs like making use of fs.readFileSync etc. I cannot manually remove those dependencies since are at 2nd or 3rd level of the package dependencies having level 0 my root module.
Given that,
I have to run those modules in the browser, so I have tried both browserify and webpack.
My webpack configuration is the following
var path = require('path');
var webpack = require("webpack");
module.exports = {
entry: './pgapi',
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js'
},
module: {
noParse: /node_modules\/json-schema\/lib\/validate\.js/,
loaders: [
{ test: /\.json$/, loader: 'json-loader' }
, {
test: /\.js$/,
loader: "transform?brfs"
}
]
},
resolve: {
extensions: ['', '.webpack.js', '.web.js', '.js']
},
node: {
console: true,
fs: 'empty',
net: 'empty',
tls: 'empty'
},
plugins: [
//new webpack.optimize.UglifyJsPlugin(),
//new webpack.optimize.DedupePlugin(),
new webpack.DefinePlugin({
__DEV__: true
})
]
};
and I'm using browserify as alternative with the brfs transform to try to take care of the fs.readFileSync etc.
After building the bundle.js I run babel since when in Safari there is no complete compliance to ES5 strict mode and also it take cares of the arrow operator and of let and const that are not supported as well using this script:
var fs = require('fs')
var args = process.argv.slice(2);
var code=fs.readFileSync(args[0])
var res=require("babel-core").transform(code, {
plugins: [
"transform-es2015-arrow-functions",
"transform-es2015-constants",
"transform-es2015-block-scoping"]
});
console.log(res.code)
So I do like
$ webpack or $ browserify mymodule.js -o dist/bundle.js -t brfs
and then on dist/bundle.js
$ node babelify.js dist/bundle.js > out.js.
What happens then is a ReferenceError: Can't find variable: fs (see here)
that seems to be due to the fact that the brsf transform does works on static expression eval so that it does not understand variables in fs.readFileSync(varname).
I'm using MacGap in Xcode test the WebView client code on El Capitan.
How to get rid of this at build time?