|
6 | 6 | 'use strict'; |
7 | 7 |
|
8 | 8 | import * as es from 'event-stream'; |
| 9 | +import * as fs from 'fs'; |
9 | 10 | import * as gulp from 'gulp'; |
10 | 11 | import * as concat from 'gulp-concat'; |
11 | 12 | import * as minifyCSS from 'gulp-cssnano'; |
@@ -159,6 +160,10 @@ export interface IOptimizeTaskOpts { |
159 | 160 | * (emit bundleInfo.json file) |
160 | 161 | */ |
161 | 162 | bundleInfo: boolean; |
| 163 | + /** |
| 164 | + * replace calls to `registerAndGetAmdImageURL` with data uris |
| 165 | + */ |
| 166 | + inlineAmdImages: boolean; |
162 | 167 | /** |
163 | 168 | * (out folder name) |
164 | 169 | */ |
@@ -192,6 +197,14 @@ export function optimizeTask(opts: IOptimizeTaskOpts): () => NodeJS.ReadWriteStr |
192 | 197 | bundle.bundle(entryPoints, loaderConfig, function (err, result) { |
193 | 198 | if (err || !result) { return bundlesStream.emit('error', JSON.stringify(err)); } |
194 | 199 |
|
| 200 | + if (opts.inlineAmdImages) { |
| 201 | + try { |
| 202 | + result = inlineAmdImages(src, result); |
| 203 | + } catch (err) { |
| 204 | + return bundlesStream.emit('error', JSON.stringify(err)); |
| 205 | + } |
| 206 | + } |
| 207 | + |
195 | 208 | toBundleStream(src, bundledFileHeader, result.files).pipe(bundlesStream); |
196 | 209 |
|
197 | 210 | // Remove css inlined resources |
@@ -236,6 +249,42 @@ export function optimizeTask(opts: IOptimizeTaskOpts): () => NodeJS.ReadWriteStr |
236 | 249 | }; |
237 | 250 | } |
238 | 251 |
|
| 252 | +function inlineAmdImages(src: string, result: bundle.IBundleResult): bundle.IBundleResult { |
| 253 | + for (const outputFile of result.files) { |
| 254 | + for (const sourceFile of outputFile.sources) { |
| 255 | + if (sourceFile.path && /\.js$/.test(sourceFile.path)) { |
| 256 | + sourceFile.contents = sourceFile.contents.replace(/\([^.]+\.registerAndGetAmdImageURL\(([^)]+)\)\)/g, (_, m0) => { |
| 257 | + let imagePath = m0; |
| 258 | + // remove `` or '' |
| 259 | + if ((imagePath.charAt(0) === '`' && imagePath.charAt(imagePath.length - 1) === '`') |
| 260 | + || (imagePath.charAt(0) === '\'' && imagePath.charAt(imagePath.length - 1) === '\'')) { |
| 261 | + imagePath = imagePath.substr(1, imagePath.length - 2); |
| 262 | + } |
| 263 | + if (!/\.(png|svg)$/.test(imagePath)) { |
| 264 | + console.log(`original: ${_}`); |
| 265 | + return _; |
| 266 | + } |
| 267 | + const repoLocation = path.join(src, imagePath); |
| 268 | + const absoluteLocation = path.join(REPO_ROOT_PATH, repoLocation); |
| 269 | + if (!fs.existsSync(absoluteLocation)) { |
| 270 | + const message = `Invalid amd image url in file ${sourceFile.path}: ${imagePath}`; |
| 271 | + console.log(message); |
| 272 | + throw new Error(message); |
| 273 | + } |
| 274 | + const fileContents = fs.readFileSync(absoluteLocation); |
| 275 | + const mime = /\.svg$/.test(imagePath) ? 'image/svg+xml' : 'image/png'; |
| 276 | + |
| 277 | + // Mark the file as inlined so we don't ship it by itself |
| 278 | + result.cssInlinedResources.push(repoLocation); |
| 279 | + |
| 280 | + return `("data:${mime};base64,${fileContents.toString('base64')}")`; |
| 281 | + }); |
| 282 | + } |
| 283 | + } |
| 284 | + } |
| 285 | + return result; |
| 286 | +} |
| 287 | + |
239 | 288 | declare class FileWithCopyright extends VinylFile { |
240 | 289 | public __hasOurCopyright: boolean; |
241 | 290 | } |
@@ -278,9 +327,6 @@ function uglifyWithCopyrights(): NodeJS.ReadWriteStream { |
278 | 327 | const output = input |
279 | 328 | .pipe(flatmap((stream, f) => { |
280 | 329 | return stream.pipe(minify({ |
281 | | - compress: { |
282 | | - hoist_funs: true // required due to https://github.com/microsoft/vscode/issues/80202 |
283 | | - }, |
284 | 330 | output: { |
285 | 331 | comments: preserveComments(<FileWithCopyright>f), |
286 | 332 | max_line_len: 1024 |
|
0 commit comments