-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
executable file
·181 lines (161 loc) · 5.98 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
Done:
- Reorder hook initialization process. Webpack is created in the configure() function
but the compilation only occure in the initialize(cb) function, the cb() in the called
when the first compilation is finished.
- use spinner like webpack template when compiling :) / optionnal
- better display of the compilation stats.
Todo:
- maybe delete the "// Merge default options" line 74 and following, options are correct in
the webpack template.
- use defaults and configKey (this.logPrefix, timeout, watching, ...)
- move this todo elsewhere
-
*/
const webpack = require('webpack');
const merge = require('webpack-merge');
const ora = require('ora');
module.exports = function (sails) {
// Sails hook specification
const hook = {
defaults: {
__configKey__: {
_hookTimeout: 40000, // wait 40 seconds before timing out
logPrefix: 'sails-hook-webpack-vue:',
}
},
// to only call the initialize callback once ... in development when watching
cbCalled: false,
// to uniformize the logging emit by this hook
logPrefix: '',
configure() {
let webpackConfig = sails.config[this.configKey];
this.logPrefix = webpackConfig.logPrefix;
// Validate hook configuration
if (!webpackConfig || !webpackConfig.options) {
sails.log.warn(this.logPrefix, 'No webpack options have been defined.');
sails.log.warn(this.logPrefix, 'Please configure your config/webpack.js file.');
return {};
}
const environment = process.env.NODE_ENV || 'development';
const host = sails.getHost() || 'localhost';
const port = sails.config.port || 1337;
// Webpack options
let options = webpackConfig.options;
// Merge environment-specific options
if (webpackConfig[environment]) {
options = merge(options, webpackConfig[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
sails.log.debug(this.logPrefix, 'Creating webpack compiler ...');
hook.compiler = webpack(options);
// Registrating dev and hot Middleware for development
if (environment === 'development') {
const historyFallback = require('connect-history-api-fallback');
const webpackDev = require('webpack-dev-middleware');
const webpackHot = require('webpack-hot-middleware');
// disabling logging, we already handle logging in compiler callback and displayStats
let config = {
hot: merge({
quiet: true,
log: (message) => { config.hot.quiet || sails.log(message) }
}, webpackConfig.middlewares ? webpackConfig.middlewares.hot : {}),
dev: merge({
quiet: false,
stats: {
colors: true,
children: false,
cachedAssets: false,
chunks: true,
chunkModules: false,
errorDetails: false,
assets: false,
version: false
},
log: text => { text.split('\n').forEach(line => sails.log.info(line)) },
warn: text => { text.split('\n').forEach(line => sails.log.warn(line)) },
error: text => { text.split('\n').forEach(line => sails.log.error(line)) }
}, webpackConfig.middlewares ? webpackConfig.middlewares.dev : {})
};
sails.log.debug(this.logPrefix, 'Configuring development middlewares ...');
sails.config.http.middleware.webpackHot = webpackHot(hook.compiler, config.hot);
sails.config.http.middleware.webpackDev = webpackDev(hook.compiler, config.dev);
// insert middlewares after the after middleware or sails.router by default
let afterMiddleware = webpackConfig.middlewares.after || 'router';
let index = sails.config.http.middleware.order.findIndex(el => el === afterMiddleware ? el : undefined);
sails.config.http.middleware.order.splice(index+1, 0, 'webpackHot', 'webpackDev');
if(webpackConfig.middlewares.history) {
sails.config.http.middleware.historyFallback = historyFallback();
sails.config.http.middleware.order.splice(index+1, 0, 'historyFallback');
}
}
},
initialize(cb) {
if (process.env.NODE_ENV === 'development') {
const spinner = ora('Building for development...').start();
process.nextTick(() => {
if(!hook.cbCalled) {
spinner.succeed('Compilation done, watching for change...');
hook.cbCalled = true;
cb();
}
});
}
else if(process.env.NODE_ENV === 'testing') {
sails.log.info(this.logPrefix, 'No need for webpack during server-side tests.');
process.nextTick(() => {
if(!hook.cbCalled) {
hook.cbCalled = true;
cb();
}
});
}
else {
const spinner = ora('Building for production...').start();
hook.compiler.run((err, stats) => {
if(!hook.cbCalled) {
spinner.succeed("Compilation done.");
hook.cbCalled = true;
cb();
}
hook.displayStats(err, stats);
});
}
},
displayStats(err, stats) {
if(err) {
sails.log.error(hook.logPrefix, err);
return;
}
stats.toString({
colors: true,
children: false,
cachedAssets: false,
chunks: true,
chunkModules: false,
errorDetails: false,
assets: false,
version: true
// chunkModules: true
}).split('\n').forEach(line => sails.log.info(line));
}
};
return hook;
};