diff options
Diffstat (limited to 'gulpfile.babel.js')
-rw-r--r-- | gulpfile.babel.js | 181 |
1 files changed, 100 insertions, 81 deletions
diff --git a/gulpfile.babel.js b/gulpfile.babel.js index 06e995d07..dda198c18 100644 --- a/gulpfile.babel.js +++ b/gulpfile.babel.js | |||
@@ -1,7 +1,12 @@ | |||
1 | /* eslint max-len: 0 */ | 1 | /* eslint max-len: 0 */ |
2 | import gulp from 'gulp'; | 2 | import gulp from 'gulp'; |
3 | import gulpIf from 'gulp-if'; | ||
3 | import babel from 'gulp-babel'; | 4 | import babel from 'gulp-babel'; |
4 | import sass from 'gulp-sass'; | 5 | import sass from 'gulp-sass'; |
6 | import csso from 'gulp-csso'; | ||
7 | import terser from 'terser'; | ||
8 | import composer from 'gulp-uglify/composer'; | ||
9 | import htmlMin from 'gulp-htmlmin'; | ||
5 | import server from 'gulp-server-livereload'; | 10 | import server from 'gulp-server-livereload'; |
6 | import { exec } from 'child_process'; | 11 | import { exec } from 'child_process'; |
7 | import dotenv from 'dotenv'; | 12 | import dotenv from 'dotenv'; |
@@ -17,17 +22,32 @@ import * as rawStyleConfig from './src/theme/default/legacy.js'; | |||
17 | 22 | ||
18 | dotenv.config(); | 23 | dotenv.config(); |
19 | 24 | ||
25 | const uglify = composer(terser, console); | ||
26 | |||
20 | const styleConfig = Object.keys(rawStyleConfig).map((key) => { | 27 | const styleConfig = Object.keys(rawStyleConfig).map((key) => { |
21 | const isHex = /^#[0-9A-F]{6}$/i.test(rawStyleConfig[key]); | 28 | const isHex = /^#[0-9A-F]{6}$/i.test(rawStyleConfig[key]); |
22 | return ({ [`$raw_${kebabCase(key)}`]: isHex ? hexRgb(rawStyleConfig[key], { format: 'array' }).splice(0, 3).join(',') : rawStyleConfig[key] }); | 29 | return { |
30 | [`$raw_${kebabCase(key)}`]: isHex | ||
31 | ? hexRgb(rawStyleConfig[key], { format: 'array' }) | ||
32 | .splice(0, 3) | ||
33 | .join(',') | ||
34 | : rawStyleConfig[key], | ||
35 | }; | ||
23 | }); | 36 | }); |
24 | 37 | ||
25 | const paths = { | 38 | const paths = { |
26 | src: 'src', | 39 | src: 'src', |
27 | dest: 'build', | 40 | dest: 'build', |
28 | tmp: '.tmp', | 41 | tmp: '.tmp', |
29 | dictionaries: 'dictionaries', | ||
30 | package: `out/${config.version}`, | 42 | package: `out/${config.version}`, |
43 | recipes: { | ||
44 | src: 'recipes/*.tar.gz', | ||
45 | dest: 'build/recipes/', | ||
46 | }, | ||
47 | recipeInfo: { | ||
48 | src: 'recipes/*.json', | ||
49 | dest: 'build/recipes/', | ||
50 | }, | ||
31 | html: { | 51 | html: { |
32 | src: 'src/**/*.html', | 52 | src: 'src/**/*.html', |
33 | dest: 'build/', | 53 | dest: 'build/', |
@@ -58,18 +78,22 @@ const paths = { | |||
58 | 78 | ||
59 | function _shell(cmd, cb) { | 79 | function _shell(cmd, cb) { |
60 | console.log('executing', cmd); | 80 | console.log('executing', cmd); |
61 | exec(cmd, { | 81 | exec( |
62 | cwd: paths.dest, | 82 | cmd, |
63 | }, (error, stdout, stderr) => { | 83 | { |
64 | if (error) { | 84 | cwd: paths.dest, |
65 | console.error(`exec error: ${error}`); | 85 | }, |
66 | return; | 86 | (error, stdout, stderr) => { |
67 | } | 87 | if (error) { |
68 | console.log(`stdout: ${stdout}`); | 88 | console.error(`exec error: ${error}`); |
69 | console.log(`stderr: ${stderr}`); | 89 | return; |
70 | 90 | } | |
71 | cb(); | 91 | console.log(`stdout: ${stdout}`); |
72 | }); | 92 | console.log(`stderr: ${stderr}`); |
93 | |||
94 | cb(); | ||
95 | }, | ||
96 | ); | ||
73 | } | 97 | } |
74 | 98 | ||
75 | const clean = (done) => { | 99 | const clean = (done) => { |
@@ -81,60 +105,74 @@ const clean = (done) => { | |||
81 | export { clean }; | 105 | export { clean }; |
82 | 106 | ||
83 | export function mvSrc() { | 107 | export function mvSrc() { |
84 | return gulp.src( | 108 | return gulp |
85 | [ | 109 | .src( |
86 | `${paths.src}/*`, | 110 | [ |
87 | `${paths.src}/*/**`, | 111 | `${paths.src}/*`, |
88 | `!${paths.scripts.watch[1]}`, | 112 | `${paths.src}/*/**`, |
89 | `!${paths.src}/styles/**`, | 113 | `!${paths.scripts.watch[1]}`, |
90 | `!${paths.src}/**/*.js`, | 114 | `!${paths.src}/styles/**`, |
91 | ], { since: gulp.lastRun(mvSrc) }, | 115 | `!${paths.src}/**/*.js`, |
92 | ) | 116 | ], |
117 | { since: gulp.lastRun(mvSrc) }, | ||
118 | ) | ||
93 | .pipe(gulp.dest(paths.dest)); | 119 | .pipe(gulp.dest(paths.dest)); |
94 | } | 120 | } |
95 | 121 | ||
96 | export function mvPackageJson() { | 122 | export function mvPackageJson() { |
97 | return gulp.src( | 123 | return gulp.src(['./package.json']).pipe(gulp.dest(paths.dest)); |
98 | [ | ||
99 | './package.json', | ||
100 | ], | ||
101 | ) | ||
102 | .pipe(gulp.dest(paths.dest)); | ||
103 | } | 124 | } |
104 | 125 | ||
105 | export function mvLernaPackages() { | 126 | export function mvLernaPackages() { |
106 | return gulp.src( | 127 | return gulp.src(['packages/**']).pipe(gulp.dest(`${paths.dest}/packages`)); |
107 | [ | ||
108 | 'packages/**', | ||
109 | ], | ||
110 | ) | ||
111 | .pipe(gulp.dest(`${paths.dest}/packages`)); | ||
112 | } | 128 | } |
113 | 129 | ||
114 | export function html() { | 130 | export function html() { |
115 | return gulp.src(paths.html.src, { since: gulp.lastRun(html) }) | 131 | return gulp |
132 | .src(paths.html.src, { since: gulp.lastRun(html) }) | ||
133 | .pipe(gulpIf(process.env.NODE_ENV !== 'development', htmlMin({ // Only minify in production to speed up dev builds | ||
134 | collapseWhitespace: true, | ||
135 | removeComments: true | ||
136 | }))) | ||
116 | .pipe(gulp.dest(paths.html.dest)); | 137 | .pipe(gulp.dest(paths.html.dest)); |
117 | } | 138 | } |
118 | 139 | ||
119 | export function styles() { | 140 | export function styles() { |
120 | return gulp.src(paths.styles.src) | 141 | return gulp |
121 | .pipe(sassVariables(Object.assign({ | 142 | .src(paths.styles.src) |
122 | $env: process.env.NODE_ENV === 'development' ? 'development' : 'production', | 143 | .pipe( |
123 | }, ...styleConfig))) | 144 | sassVariables( |
124 | .pipe(sass({ | 145 | Object.assign( |
125 | includePaths: [ | 146 | { |
126 | './node_modules', | 147 | $env: |
127 | '../node_modules', | 148 | process.env.NODE_ENV === 'development' |
128 | ], | 149 | ? 'development' |
129 | }).on('error', sass.logError)) | 150 | : 'production', |
151 | }, | ||
152 | ...styleConfig, | ||
153 | ), | ||
154 | ), | ||
155 | ) | ||
156 | .pipe( | ||
157 | sass({ | ||
158 | includePaths: ['./node_modules', '../node_modules'], | ||
159 | }).on('error', sass.logError), | ||
160 | ) | ||
161 | .pipe((gulpIf(process.env.NODE_ENV !== 'development', csso({ // Only minify in production to speed up dev builds | ||
162 | restructure: false, // Don't restructure CSS, otherwise it will break the styles | ||
163 | })))) | ||
130 | .pipe(gulp.dest(paths.styles.dest)); | 164 | .pipe(gulp.dest(paths.styles.dest)); |
131 | } | 165 | } |
132 | 166 | ||
133 | export function scripts() { | 167 | export function scripts() { |
134 | return gulp.src(paths.scripts.src, { since: gulp.lastRun(scripts) }) | 168 | return gulp |
135 | .pipe(babel({ | 169 | .src(paths.scripts.src, { since: gulp.lastRun(scripts) }) |
136 | comments: false, | 170 | .pipe( |
137 | })) | 171 | babel({ |
172 | comments: false, | ||
173 | }), | ||
174 | ) | ||
175 | .pipe(gulpIf(process.env.NODE_ENV !== 'development', uglify())) // Only uglify in production to speed up dev builds | ||
138 | .pipe(gulp.dest(paths.scripts.dest)); | 176 | .pipe(gulp.dest(paths.scripts.dest)); |
139 | } | 177 | } |
140 | 178 | ||
@@ -142,51 +180,32 @@ export function watch() { | |||
142 | gulp.watch(paths.packages.watch, mvLernaPackages); | 180 | gulp.watch(paths.packages.watch, mvLernaPackages); |
143 | gulp.watch(paths.styles.watch, styles); | 181 | gulp.watch(paths.styles.watch, styles); |
144 | 182 | ||
145 | gulp.watch([ | 183 | gulp.watch([paths.src, `${paths.scripts.src}`, `${paths.styles.src}`], mvSrc); |
146 | paths.src, | ||
147 | `${paths.scripts.src}`, | ||
148 | `${paths.styles.src}`, | ||
149 | ], mvSrc); | ||
150 | 184 | ||
151 | gulp.watch(paths.scripts.watch, scripts); | 185 | gulp.watch(paths.scripts.watch, scripts); |
152 | } | 186 | } |
153 | 187 | ||
154 | export function webserver() { | 188 | export function webserver() { |
155 | gulp.src([ | 189 | gulp.src([paths.dest]).pipe( |
156 | paths.dest, | 190 | server({ |
157 | ]) | ||
158 | .pipe(server({ | ||
159 | livereload: true, | 191 | livereload: true, |
160 | })); | 192 | }), |
193 | ); | ||
161 | } | 194 | } |
162 | 195 | ||
163 | export function dictionaries(done) { | 196 | export function recipes() { |
164 | const { SPELLCHECKER_LOCALES } = require('./build/i18n/languages'); | 197 | return gulp.src(paths.recipes.src, { since: gulp.lastRun(recipes) }) |
165 | 198 | .pipe(gulp.dest(paths.recipes.dest)); | |
166 | let packages = ''; | ||
167 | Object.keys(SPELLCHECKER_LOCALES).forEach((key) => { packages = `${packages} hunspell-dict-${key}`; }); | ||
168 | |||
169 | _shell(`npm install --prefix ${path.join(__dirname, 'temp')} ${packages}`, () => { | ||
170 | moveSync( | ||
171 | path.join(__dirname, 'temp', 'node_modules'), | ||
172 | path.join(__dirname, 'build', paths.dictionaries), | ||
173 | ); | ||
174 | |||
175 | removeSync(path.join(__dirname, 'temp')); | ||
176 | |||
177 | done(); | ||
178 | }); | ||
179 | } | 199 | } |
180 | 200 | export function recipeInfo() { | |
181 | export function sign(done) { | 201 | return gulp.src(paths.recipeInfo.src, { since: gulp.lastRun(recipeInfo) }) |
182 | _shell(`codesign --verbose=4 --deep --strict --force --sign "${process.env.SIGNING_IDENTITY}" "${__dirname}/node_modules/electron/dist/Electron.app"`, done); | 202 | .pipe(gulp.dest(paths.recipeInfo.dest)); |
183 | } | 203 | } |
184 | 204 | ||
185 | const build = gulp.series( | 205 | const build = gulp.series( |
186 | clean, | 206 | clean, |
187 | gulp.parallel(mvSrc, mvPackageJson, mvLernaPackages), | 207 | gulp.parallel(mvSrc, mvPackageJson, mvLernaPackages), |
188 | gulp.parallel(html, scripts, styles), | 208 | gulp.parallel(html, scripts, styles, recipes, recipeInfo), |
189 | dictionaries, | ||
190 | ); | 209 | ); |
191 | export { build }; | 210 | export { build }; |
192 | 211 | ||