-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
234 lines (212 loc) · 6.75 KB
/
gulpfile.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
const gulp = require('gulp');
const gutil = require('gulp-util');
const path = require('path');
const del = require('del');
const runSequence = require('run-sequence');
const gitRev = require('git-rev-sync');
const fs = require('fs');
const webpack = require('webpack');
const autoprefixer = require('autoprefixer');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const AppCachePlugin = require('appcache-webpack-plugin');
const WebpackDevServer = require('webpack-dev-server');
const configFile = require('./config.js');
// Default settings are for dev, run 'prod-build-config' to change for production
const filename = '[name].[hash]';
const APP_BASE = path.resolve(__dirname, 'src');
const DIST_BASE = path.resolve(__dirname, 'web');
const WEBPACK_ENTRY = `webpack-dev-server/client?http://localhost:${configFile.devServerPort}`;
const momentjsLocales = fs
.readdirSync(path.resolve(APP_BASE, 'services/translations'))
.filter(file => file.endsWith('.json'))
.map(file => `${file.slice(0, -5)}.js`)
.join('|');
const config = {
indexGlobalVars: {
HASH: gitRev.long(),
BRANCH: gitRev.branch(),
VERSION: require('./package').version
},
entry: {
app: [WEBPACK_ENTRY, path.resolve(APP_BASE, 'index.js')]
},
output: {
path: DIST_BASE,
publicPath: '/',
filename: `${filename}.js`,
chunkFilename: `${filename}.js`,
pathinfo: true
},
devtool: 'eval',
module: {
loaders: [
{
// Our own JS files via babel
test: /\.js$/,
loader: 'babel?' +
'presets[]=es2015,' +
'plugins[]=transform-es2015-modules-commonjs,' +
'plugins[]=transform-runtime,' +
'cacheDirectory!eslint',
include: [APP_BASE]
},
{
test: /\.(png|jpe?g|gif|svg)(\?\S*)?$/,
loader: 'file'
},
{
test: /\.(woff|woff2|ttf|eot)(\?\S*)?$/,
loader: 'url'
},
{
test: /\.(html|md)$/,
loader: 'raw'
},
{
include: /\.json$/,
loader: 'json'
},
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract('style', 'css?sourceMap!sass?sourceMap,indentedSyntax=false')
},
{
test: /\.sass$/,
loader: ExtractTextPlugin.extract('style', 'css?sourceMap!sass?sourceMap,indentedSyntax=true')
},
// Libraries
{
test: /ngclipboard.js$/,
loader: 'imports?Clipboard=clipboard'
},
{
test: /angular-localForage.js$/,
loader: 'imports?this=>{angular: angular}'
},
{
test: /ngClip.js$/,
loader: 'imports?ZeroClipboard'
}
]
},
resolve: {
alias: {
'ng-clip': 'ng-clip/src/ngClip',
'app': APP_BASE
}
},
plugins: [
new ExtractTextPlugin(`${filename}.css`),
new HtmlWebpackPlugin({
filename: 'index.html',
template: `!!ejs!${path.resolve(APP_BASE, 'index_template.html')}`,
inject: false,
favicon: path.resolve(APP_BASE, 'images/favicon.png')
}),
// Only use english locale
new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, new RegExp(momentjsLocales))
],
node: {
console: false,
global: true,
process: true,
Buffer: false,
setImmediate: false
},
postcss: [
autoprefixer({
browsers: ['last 2 version']
})
],
debug: true,
jshint: require('./package.json').jshintConfig,
jscs: require('./package.json').jscsConfig
};
let compiler;
gulp.task('clean', () => del([DIST_BASE]));
gulp.task('dev-build-config', () => {
config.plugins.push(
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('development')
},
__UHCGG_API_URL__: JSON.stringify(configFile.api.development)
})
);
});
gulp.task('prod-build-config', () => {
// Remove webpack entry
config.entry.app.splice(0, 1);
// Production flags
config.bail = true;
config.debug = false;
config.output.pathinfo = false;
// Full source map
config.devtool = 'source-map';
// Production plugins
config.plugins.push(
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production')
},
__UHCGG_API_URL__: JSON.stringify(configFile.api.production)
}),
new webpack.NoErrorsPlugin(),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.UglifyJsPlugin(),
new webpack.optimize.OccurenceOrderPlugin(),
new AppCachePlugin({
network: ['*'],
// Exclude manifest + map files + changelog md
exclude: [/\.appcache$/, /\.map$/, /\.md$/],
output: 'manifest.appcache'
})
);
gutil.log('Switched to production build configuration');
});
gulp.task('webpack:init', () => {
compiler = webpack(config);
gutil.log('Created webpack compiler');
});
gulp.task('webpack:init-dev', done => {
runSequence('dev-build-config', 'webpack:init', done);
});
gulp.task('webpack:init-prod', done => {
runSequence('prod-build-config', 'webpack:init', done);
});
gulp.task('webpack:prod', ['webpack:init-prod'], done => {
compiler.run((err, stats) => {
if (err) {
throw new gutil.PluginError('webpack:prod', err);
}
gutil.log('[webpack:prod]', stats.toString({
colors: true
}));
done();
});
});
gulp.task('webpack:dev', ['webpack:init-dev'], done => {
new WebpackDevServer(compiler, {
publicPath: config.output.publicPath,
stats: {
colors: true
},
contentBase: APP_BASE,
port: configFile.devServerPort
}).listen(configFile.devServerPort, 'localhost', err => {
if (err) {
throw new gutil.PluginError('webpack-dev-server', err);
}
done();
gutil.log(
'[webpack-dev-server]',
`http://localhost:${configFile.devServerPort}/webpack-dev-server/index.html`,
`http://localhost:${configFile.devServerPort}/index.html`
);
});
});
gulp.task('build', done => {
runSequence('clean', 'webpack:prod', done);
});
gulp.task('dev', ['webpack:dev']);