-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (88 loc) · 3.29 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const webpack = require('webpack');
const merge = require('webpack-merge');
module.exports = function (sails) {
// Validate hook configuration
if (!sails.config.webpack || !sails.config.webpack.options) {
sails.log.warn('sails-hook-webpack2: No webpack options have been defined.');
sails.log.warn('sails-hook-webpack2: Please configure your config/webpack.js file.');
return {};
}
// Sails hook specification
const hook = {
emitReady: false,
afterBuild(err, stats) {
if (err) return sails.log.error('sails-hook-webpack2: Build error:\n', err);
// Emit events
if (!this.emitReady) {
sails.emit('hook:sails-hook-webpack2:compiler-ready', {});
this.emitReady = true;
}
sails.emit('hook:sails-hook-webpack2:after-build', stats);
// Display information, errors and warnings
if (stats.compilation.warnings && stats.compilation.warnings.length > 0) {
stats.compilation.warnings.forEach(warning => sails.log.warn('sails-hook-webpack2:', warning.message));
}
if (stats.compilation.errors && stats.compilation.errors.length > 0) {
stats.compilation.errors.forEach(error => sails.log.error('sails-hook-webpack2:', error.message));
}
sails.log.info('sails-hook-webpack2: Build complete. Hash: ' + stats.hash + ', Time: ' + (stats.endTime - stats.startTime) + 'ms');
}
};
const environment = process.env.NODE_ENV || 'development';
const host = sails.getHost() || 'localhost';
const port = sails.config.port || 1337;
// Webpack options
let options = sails.config.webpack.options;
// Merge environment-specific options
if (sails.config.webpack[environment]) {
options = merge(options, sails.config.webpack[environment]);
}
// Merge default options
options = merge(options, {
plugins: [
// User environment variables
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify(environment),
'SAILS_HOST': JSON.stringify(host),
'SAILS_PORT': JSON.stringify(port)
}
})
],
performance: {
hints: false
},
stats: 'errors-only'
});
// Create webpack compiler
hook.compiler = webpack(options, (err, stats) => {
if (err) {
sails.log.error('sails-hook-webpack2: Configuration error:\n', err);
return {};
}
sails.log.info('sails-hook-webpack2: Webpack configured successfully.');
if (environment === 'production') {
sails.log.info('sails-hook-webpack2: Building for production...');
hook.compiler.run(hook.afterBuild.bind(hook));
}
else {
sails.log.info('sails-hook-webpack2: Watching for changes...');
hook.compiler.watch(sails.config.webpack.watch, hook.afterBuild.bind(hook));
}
});
// Start webpack-dev-server
if (environment !== 'production' && sails.config.webpack.server) {
const WebpackDevServer = require('webpack-dev-server');
// Webpack-dev-server configuration
let config = {
hot: true,
port: 3000
};
Object.assign(config, sails.config.webpack.server);
// Listen on specific port
hook.server = new WebpackDevServer(hook.compiler, config);
hook.server.listen(config.port);
sails.log.info('sails-hook-webpack2: Server listening on http://' + host + ':' + config.port);
}
return hook;
};