-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
200 lines (176 loc) · 5.9 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
'use strict';
require('dotenv').config({ silent: true });
const mountPath = process.env.MOUNT_PATH || '';
const gulp = require('gulp');
const plumber = require('gulp-plumber');
const runSequence = require('run-sequence');
const del = require('del');
const eslint = require('gulp-eslint');
const crisper = require('gulp-crisper');
const gulpif = require('gulp-if');
const vulcanize = require('gulp-vulcanize');
const babel = require('gulp-babel');
const htmlreplace = require('gulp-html-replace');
const replace = require('gulp-replace');
const sourcemaps = require('gulp-sourcemaps');
const rename = require('gulp-rename');
const inject = require('gulp-inject');
const historyApiFallback = require('connect-history-api-fallback');
const browserSync = require('browser-sync').create();
const reload = browserSync.reload;
// Utility functions
let ISDISTMODE = false;
gulp.task('default', ['dist'], () => {
});
// clean the bild direcories
gulp.task('clean', () => {
return del(['.transpiled', 'dist']);
});
// create a web server with live reload
gulp.task('serve', ['transpile'], () => {
browserSync.init({
server: {
baseDir: '.transpiled',
routes: {
'/bower_components': 'bower_components',
},
middleware: [historyApiFallback()],
},
notify: false,
});
gulp.watch('app/**/*.{html, js}', ['transpile', reload]);
});
// create a web server with live reload and linting
gulp.task('serve:lint', ['transpile', 'lint'], () => {
browserSync.init({
server: {
baseDir: '.transpiled',
routes: {
'/bower_components': 'bower_components',
},
middleware: [historyApiFallback()],
},
notify: false,
});
gulp.watch('app/**/*.{html, js}', ['transpile', 'lint', reload]);
});
// tranpile javascript
gulp.task('transpile', ['copyFiles'], () => {
gulp.src('app/index.html')
.pipe(gulp.dest('./.transpiled'));
return gulp.src(['app/**/*.html', '!app/bower_components/**/*', '!app/index.html'])
.pipe(gulpif(!ISDISTMODE, plumber()))
.pipe(crisper({
scriptInHead: false, // true is default
onlySplit: false,
}))
.pipe(gulpif(!ISDISTMODE, sourcemaps.init()))
.pipe(babel({
plugins: ['transform-object-assign', 'es6-promise'],
presets: ['es2015'],
only: '*.js',
}))
.pipe(gulpif(!ISDISTMODE, sourcemaps.write('.')))
.pipe(gulpif(!ISDISTMODE, plumber.stop()))
.pipe(gulp.dest('./.transpiled'));
});
// lint using AirBnB's rules
gulp.task('lint', () => {
const src = ['app/**/*.{html,js}', '!app/test/**/*'];
if (ISDISTMODE) {
src.push('gulpfile.js');
}
return gulp.src(src)
.pipe(eslint())
.pipe(eslint.format())
.pipe(gulpif(ISDISTMODE, eslint.failAfterError()));
});
gulp.task('dist', () => {
ISDISTMODE = true;
runSequence('clean', 'lint', 'transpile', 'injectDinamicImports', 'vulcanize', 'copyFiles');
});
// optimize polymer compomponents
gulp.task('vulcanize', () => {
return gulp.src('./.transpiled/elements/elements.html')
.pipe(vulcanize({
abspath: '',
stripComments: true,
inlineCss: true,
inlineScripts: true,
addedImports: [
'../bower_components/import-tinymce/import-tinymce.html',
],
}))
.pipe(gulp.dest('./dist/elements/'));
});
gulp.task('copyFiles', ['copyAssets'], () => {
const destination = ISDISTMODE ? 'dist' : '.transpiled';
if (ISDISTMODE) {
// change paths
gulp.src('.transpiled/index.html')
.pipe(htmlreplace({
baseurl: `<base href="${mountPath}/">`,
mountpath: `<qea-main-app id="mainApp" mount-path="${mountPath}"></qea-main-app>`,
webcomponents: './bower_components/webcomponentsjs/webcomponents-lite.js',
}))
.pipe(gulp.dest(`${destination}`));
// copy necessary bower components
gulp.src(
['bower_components/{webcomponentsjs,import-tinymce,tinymce,ace-element}/**/*']
).pipe(gulp.dest('./dist/bower_components/'));
}
// copy styles
gulp.src('app/styles/**/*')
.pipe(gulp.dest(`${destination}/styles/`));
// copy script
gulp.src('node_modules/redux/dist/redux.min.js')
.pipe(gulp.dest(`${destination}/scripts/`));
// copy images
return gulp.src('app/images/**/*')
.pipe(gulp.dest(`${destination}/images/`));
});
gulp.task('copyAssets', () => {
const destination = ISDISTMODE ? 'dist' : '.transpiled';
if (ISDISTMODE) {
// change image paths
gulp.src('./dist/elements/elements.html')
.pipe(replace('../../images/', '../images/'))
.pipe(replace(/="[^\s]*\/?assets\//g, '="../assets/'))
.pipe(replace(/\/\/ <replace:start>[\s\S]*\/\/ <replace:stop>/g,
'this._renderEditor(qid, editorType, this.query.eid);'))
.pipe(gulp.dest('./dist/elements/'));
gulp.src('app/elements/**/assets/**/*')
.pipe(rename((path) => {
path.dirname = path.dirname.match(/\/assets.*/)[0] || 'assets';
}))
.pipe(gulp.dest(`${destination}/`));
} else {
gulp.src('app/elements/**/assets/**/*')
.pipe(gulp.dest(`${destination}/elements/`));
}
gulp.src('app/scripts/**/*')
.pipe(gulp.dest('dist/scripts/'))
.pipe(gulp.dest('.transpiled/scripts/'));
// copy images
return gulp.src('app/images/**/*')
.pipe(gulp.dest(`${destination}/images/`));
});
gulp.task('injectDinamicImports', () => {
return gulp.src('.transpiled/elements/elements.html')
.pipe(inject(gulp.src('.transpiled/elements/asq-*-editor/asq-*-editor.html',
{ read: false }), { relative: true }))
.pipe(gulp.dest('./.transpiled/elements'));
});
gulp.task('electron', ['transpile'], () => {
gulp.src('.transpiled/index.html')
.pipe(htmlreplace({
mountpath: `<qea-main-app id="mainApp"
mount-path="${mountPath}" is-electron></qea-main-app>`,
electronRequire: `<script> window.nodeRequire = require;
delete window.require;delete window.exports;
delete window.module;</script>`,
baseurl: '',
}))
.pipe(gulp.dest('.transpiled'));
return;
});