Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GPT-432] Package server webpack #346

Merged
merged 43 commits into from
Jan 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
b56da0a
init webpack bundler
Jan 9, 2017
f39644b
memory-fs only for compiler.outputFileSystem
Jan 9, 2017
d7be7b5
initial test
Jan 10, 2017
7faf403
passing down compiler errors
Jan 10, 2017
8ffe4cf
testing compiler errors
Jan 10, 2017
de55e78
moved integration test to /integration
Jan 10, 2017
abb5f83
drop rimraf -> fs-extra
Jan 10, 2017
e268992
babel-loader && preset-env
Jan 10, 2017
18e6ce7
integration test wip
Jan 10, 2017
99b4b84
bundler options
Jan 10, 2017
5dc0060
es6 example integration test
Jan 10, 2017
f5d80b3
cleanup
Jan 10, 2017
8a71702
webpack1
Jan 11, 2017
a1db055
uglify and optimize plugins
Jan 11, 2017
c0d9c6b
added config module
Jan 11, 2017
d80bfa2
libraryTarget=commonjs2, externals=regex, added json-loader & integra…
Jan 11, 2017
c24d039
wrap while/do/for;; loops with an iterator limit
Jan 11, 2017
4a17d9a
structure/internaAPIi refactor
Jan 12, 2017
d0d1574
fix webpack error handling propagation
Jan 12, 2017
46af301
handling relative paths from npm module requires
Jan 12, 2017
2aeeb80
integration tests
Jan 12, 2017
3ca88c7
clean legacy code
Jan 13, 2017
7e1cbdc
unit tests
Jan 13, 2017
af9b87f
resolveLoader
Jan 16, 2017
89aafd3
tests
Jan 17, 2017
1bb52c7
initial test
Jan 10, 2017
fd0c275
testing compiler errors
Jan 10, 2017
17ef3b3
moved integration test to /integration
Jan 10, 2017
afdb51e
drop rimraf -> fs-extra
Jan 10, 2017
c312a54
integration test wip
Jan 10, 2017
d97575c
es6 example integration test
Jan 10, 2017
494a8b1
cleanup
Jan 10, 2017
0b491bb
added config module
Jan 11, 2017
4939afd
libraryTarget=commonjs2, externals=regex, added json-loader & integra…
Jan 11, 2017
818f58a
wrap while/do/for;; loops with an iterator limit
Jan 11, 2017
8944a47
structure/internaAPIi refactor
Jan 12, 2017
9b3fefe
integration tests
Jan 12, 2017
366200e
unit tests
Jan 13, 2017
19477b3
resolveLoader
Jan 16, 2017
2a9cd22
added target node for the webpack config
Jan 18, 2017
41944ce
tests cleanup
Jan 18, 2017
dbf4714
rebase
Jan 18, 2017
34dc1ce
review-fixes
Jan 20, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 34 additions & 26 deletions npm-shrinkwrap.json

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

10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,21 @@
"accept-language-parser": "1.1.2",
"async": "1.5.2",
"aws-sdk": "2.6.11",
"babel-core": "6.21.0",
"babel-loader": "6.2.10",
"babel-preset-env": "1.1.8",
"clean-css": "3.4.18",
"colors": "1.1.2",
"dependency-graph": "0.5.0",
"detective": "4.3.1",
"express": "3.21.2",
"falafel": "1.2.0",
"falafel-loader": "0.0.3",
"form-data": "0.1.4",
"fs-extra": "0.30.0",
"handlebars": "4.0.5",
"jade": "1.11.0",
"json-loader": "0.5.4",
"memory-fs": "0.4.1",
"minimal-request": "2.2.0",
"multer": "0.1.4",
"nice-cache": "0.0.5",
Expand All @@ -78,6 +83,7 @@
"targz": "1.0.1",
"uglify-js": "2.6.4",
"underscore": "1.8.3",
"watch": "0.19.1"
"watch": "0.19.1",
"webpack": "1.14.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth upgrading to 2.0 since the new hot sauce has been released... Maybe too soon?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's out of beta I 👍 on this, unless it requires a significant amount of work so that we want to keep it to the next PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and thats great news 🎉
Although, until we finally drop ourself support for node 0.10&co I will suggest we stick to v1. I'll prepare a v2 upgrade on a separate PR, what you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* External Dependencies handler for webpack
* Returns an array with handlers to indicates dependencies that should not be
* bundled by webPack but instead remain requested by the resulting bundle.
* For more info http://webpack.github.io/docs/configuration.html#externals
*
*/
'use strict';
var format = require('stringformat');
var _ = require('underscore');
var strings = require('../../../../../resources');


module.exports = function externalDependenciesHandlers(dependencies){
var deps = dependencies || {};

var missingExternalDependecy = function(dep, dependencies) {
return !_.contains(_.keys(dependencies), dep);
};

return [
function(context, req, callback) {
if (/^[a-z@][a-z\-\/0-9]+$/i.test(req)) {
var dependencyName = req;
if (/\//g.test(dependencyName)) {
dependencyName = dependencyName.substring(0, dependencyName.indexOf('/'));
}
if (missingExternalDependecy(dependencyName, deps)) {
return callback(new Error(format(strings.errors.cli.SERVERJS_DEPENDENCY_NOT_DECLARED, JSON.stringify(dependencyName))));
}
}
callback();
},
/^[a-z@][a-z\-\/0-9]+$/i
];
};
63 changes: 63 additions & 0 deletions src/cli/domain/package-server-script/bundle/config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*jshint camelcase:false */
'use strict';

var webpack = require('webpack');
var path = require('path');
var wrapLoops = require('./wrapLoops');
var externalDependenciesHandlers = require('./externalDependenciesHandlers');

module.exports = function webpackConfigGenerator(params){
return {
entry: params.dataPath,
target: 'node',
output: {
path: '/build',
filename: params.fileName,
libraryTarget: 'commonjs2',
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be be setting target: 'node', here? (see https://webpack.github.io/docs/configuration.html#target)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch!

externals: externalDependenciesHandlers(params.dependencies),
module: {
loaders: [
{
test: /\.json$/,
exclude: /node_modules/,
loader: 'json-loader'
},
{
test: /\.js?$/,
exclude: /node_modules/,
loaders: [
'falafel-loader',
'babel-loader?' + JSON.stringify({
cacheDirectory: true,
'presets': [
[require.resolve('babel-preset-env'), {
'targets': {
'node': 4
}
}]
]
})
],
}
]
},
plugins: [
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin({
compressor: {
warnings: false,
screw_ie8: true
},
sourceMap: false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for not having source-maps?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment we are focusing on having a production grade-output only. Not sure we wanted to have sourceMaps outputted now. What you guys recon?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need sourcemaps on the server.js. Definitely will when we'll use this for client-side stuff, but that's not this PR's scope

Copy link
Contributor

@chriscartlidge chriscartlidge Jan 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest it's really useful when debugging production issues. The times that I've had to step through minified code and back reference that we the uncompiled stuff has been a lot.

Maybe it shouldn't hold up this PR but it'd be a great addition IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with @chriscartlidge , I think that once we'll have the whole es6 server+client bundle workflow in place, we should definitely add those in.

}),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production')
})
],
falafel: wrapLoops,
resolveLoader: {
root: path.resolve(__dirname, '../../../../../../node_modules')
}
};
};
27 changes: 27 additions & 0 deletions src/cli/domain/package-server-script/bundle/config/wrapLoops.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';


var CONST_MAX_ITERATIONS = require('../../../../../resources/settings').maxLoopIterations;

module.exports = function wrapLoops(node){
var loopKeywords = ['WhileStatement', 'ForStatement', 'DoWhileStatement'];

if(loopKeywords.indexOf(node.type) > -1){
node.update(
'var __ITER = ' + CONST_MAX_ITERATIONS + ';'
+ node.source()
);
}

if(!node.parent){
return;
}

if(loopKeywords.indexOf(node.parent.type) > -1 && node.type === 'BlockStatement'){
node.update('{ if(__ITER <=0){ throw new Error("Loop exceeded maximum '
+ 'allowed iterations"); } '
+ node.source().substr(1).slice(0, -1)
+ ' __ITER--; }'
);
}
};
40 changes: 40 additions & 0 deletions src/cli/domain/package-server-script/bundle/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*jshint camelcase:false */
'use strict';
var webpackConfig = require('./config');
var console = require('console');
var MemoryFS = require('memory-fs');
var webpack = require('webpack');

var memoryFs = new MemoryFS();

module.exports = function bundle(params, callBack) {
var config = webpackConfig(params);
var compiler = webpack(config);
compiler.outputFileSystem = memoryFs;

compiler.run(function(error, stats){
var softError;
var warning;

// handleFatalError
if (error) {
return callBack(error);
}

var info = stats.toJson();
// handleSoftErrors
if (stats.hasErrors()) {
softError = info.errors.toString();
return callBack(softError);
}
// handleWarnings
if (stats.hasWarnings()) {
warning = info.warnings.toString();
}

console.log(stats.toString(params.webpack.stats));

var serverContentBundled = memoryFs.readFileSync('/build/server.js', 'UTF8');
callBack(warning, serverContentBundled);
});
};
18 changes: 0 additions & 18 deletions src/cli/domain/package-server-script/compress.js

This file was deleted.

47 changes: 0 additions & 47 deletions src/cli/domain/package-server-script/getLocalDependencies.js

This file was deleted.

21 changes: 0 additions & 21 deletions src/cli/domain/package-server-script/getSandBoxedJs/index.js

This file was deleted.

Loading