-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgulpfile.js
213 lines (187 loc) · 7.19 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
const browserSync = require('browser-sync').create();
const connecthistoryApiFallback = require('connect-history-api-fallback');
const fs = require('fs');
const gulp = require('gulp');
const gulpClean = require('gulp-clean');
const gulpCleanCss = require('gulp-clean-css');
const gulpConcat = require('gulp-concat');
const gulpEsLint = require('gulp-eslint');
const gulpMerge = require('gulp-merge');
const gulpReplace = require('gulp-replace');
const gulpSass = require('gulp-sass');
const gulpSassUnicode = require('gulp-sass-unicode');
const gulpSourceMaps = require('gulp-sourcemaps');
const gulpStyleLint = require('gulp-stylelint');
const gulpUglify = require('gulp-uglify');
const gulpWatch = require('gulp-watch');
const path = require('path');
const pump = require('pump');
const through2 = require('through2');
const webpack = require('webpack');
const webpackStream = require('webpack-stream');
const appVersion = JSON.parse(fs.readFileSync('./package.json')).version;
const webpackBaseConfig = {
devtool: 'inline-source-map',
module: {
loaders: [{
test: /src.*\.js$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: ['es2015'],
plugins: [
['angularjs-annotate', {
explicitOnly: true,
}],
'transform-async-to-generator',
'transform-exponentiation-operator',
'transform-html-import-to-string',
'transform-runtime',
'babel-plugin-ng-component-module',
],
},
}],
},
plugins: [
new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
}),
],
};
gulp.task('build', () => {
gulp.start(['components', 'copy-assets', 'html', 'js-app', 'js-vendor']);
});
gulp.task('build-development', ['clean'], () => {
gulp.start('build');
});
gulp.task('build-release', ['clean', 'configure-webpack-for-release'], () => {
gulp.start('build');
});
gulp.task('clean', () => gulp.src('dist', { read: false }).pipe(gulpClean()));
gulp.task('components', () => {
const baseComponentsPath = 'src/components';
// iterate through sub-directories in src/components
fs.readdirSync(baseComponentsPath)
.filter(file => fs.statSync(path.join(baseComponentsPath, file)).isDirectory())
.forEach((folder) => {
const indexPath = path.join(baseComponentsPath, folder, '/index.js');
if (fs.existsSync(indexPath) === true) {
// merge the result of webpack and our own sass compilation
gulpMerge(
// compile component/index.js with webpack + babel
gulp.src(indexPath)
.pipe(webpackStream(Object.assign({
output: {
filename: `${folder}.js`,
},
}, webpackBaseConfig))),
// compile all component scss files into javascript
gulp.src(path.join(baseComponentsPath, folder, '/**/*.scss'))
.pipe(gulpSass())
.pipe(gulpSassUnicode())
.on('error', (...args) => {
// this prevents gulp from crashing during sass errors
console.error('swallowed sass Error', args);
})
.pipe(gulpConcat('component.css'))
.pipe(gulpReplace('/assets/', `/${appVersion}/assets/`))
.pipe(gulpCleanCss())
.pipe(through2.obj((chunk, enc, cb) => {
const cssJs = `!function(){var e=document.createElement("style");e.type="text/css",e.innerHTML="${chunk.contents.toString().replace(/"/g, '\\"').replace(/\r/g, '').replace(/\n/g, '\\n')}",document.getElementsByTagName("head")[0].appendChild(e)}();`; // eslint-disable-line max-len
chunk.contents = new Buffer(cssJs, 'binary');
cb(null, chunk);
})))
.pipe(gulpConcat(`${folder}.js`))
.pipe(gulp.dest(`./dist/${appVersion}/components`));
}
});
});
gulp.task('configure-webpack-for-release', () => {
// disable source maps
delete webpackBaseConfig.devtool;
});
gulp.task('copy-assets', (cb) => {
pump([
gulp.src('src/assets/**/*'),
gulp.dest(`dist/${appVersion}/assets`),
], cb);
});
gulp.task('develop', ['build-development'], () => {
gulp.start(['watch', 'serve']);
});
gulp.task('html', (cb) => {
pump([
gulp.src(['src/index.html']),
gulpReplace(/\{version\}/g, appVersion),
gulp.dest('dist/'),
], cb);
});
gulp.task('js-app', (cb) => {
const config = Object.assign({
entry: './src/app.js',
output: {
filename: `./dist/${appVersion}/app.min.js`,
},
}, webpackBaseConfig);
webpack(config, () => {
// TODO report errors
cb();
});
});
gulp.task('js-vendor', (cb) => {
pump([
gulp.src([
'node_modules/angular/angular.js',
'node_modules/oclazyload/dist/ocLazyLoad.js',
'node_modules/@uirouter/angularjs/release/angular-ui-router.js',
]),
gulpConcat('vendor.min.js'),
gulpSourceMaps.init(),
gulpUglify(),
gulpSourceMaps.write(),
gulp.dest(`dist/${appVersion}`),
], cb);
});
gulp.task('lint', ['lint-js', 'lint-scss']);
gulp.task('lint-js', () => gulp
.src(['gulpfile.js', './src/**/*.js'])
.pipe(gulpEsLint())
.pipe(gulpEsLint.format())
.pipe(gulpEsLint.failAfterError()));
gulp.task('lint-scss', (cb) => {
pump([
gulp.src(['src/**/*.scss']),
gulpStyleLint({
failAfterError: true,
debug: true,
reporters: [{
formatter: 'string',
console: true,
}],
}),
], cb);
});
gulp.task('serve', () => {
browserSync.init({
files: ['./dist/**/*.*'],
ghostMode: false,
notify: false,
server: {
baseDir: './dist',
middleware: [connecthistoryApiFallback()],
},
snippetOptions: {
blacklist: ['**/*?disable-browsersync'],
},
});
});
gulp.task('watch', () => {
gulpWatch(['src/app.js', 'src/stateConfig.js', 'src/services/**/*.js'], () => gulp.start('js-app'))
.on('error', (...args) => console.log('WATCH ERROR js-app', args)); // eslint-disable-line no-console
gulpWatch(['src/components/**/*', 'src/styleConfig.scss'], () => gulp.start('components'))
.on('error', (...args) => console.log('WATCH ERROR components', args)); // eslint-disable-line no-console
gulpWatch('src/assets/**/*', () => gulp.start('copy-assets'))
.on('error', (...args) => console.log('WATCH ERROR copy-assets', args)); // eslint-disable-line no-console
gulpWatch('src/index.html', () => gulp.start('html'))
.on('error', (...args) => console.log('WATCH ERROR html', args)); // eslint-disable-line no-console
});