mod-build 3.6.21 → 3.6.22-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,7 +10,7 @@ module.exports = function(gulp, gulpPlugins, siteSettings, siteData) {
10
10
  if (siteData.isQSPage) {
11
11
  sequenceOpts = ['clean', 'grab-cdn', 'grab-shared-components', 'grab-shared-scripts', 'templates', 'styles', 'grab-theme-json', 'combine-files', 'grab-images', 'copy-json', 'js-lint', 'html-min', 'css-min', 'js-min', 'img-min', 'extras', 'brand', 'copy-files-to-build-path', 'cache-bust', 'grab-tooltips-json', 'build-stat'];
12
12
  } else {
13
- sequenceOpts = ['clean', 'grab-cdn', 'grab-shared-components', 'grab-shared-scripts', 'templates', 'styles', 'combine-files', 'grab-images', 'js-lint', 'html-min', 'css-min', 'js-min', 'img-min', 'extras', 'brand', 'cache-bust', 'grab-tooltips-json', 'build-stat'];
13
+ sequenceOpts = ['clean', 'grab-cdn', 'grab-shared-components', 'grab-shared-scripts', 'templates', 'styles', 'combine-files', 'grab-images','grab-global-images', 'js-lint', 'html-min', 'css-min', 'js-min', 'img-min', 'extras', 'brand', 'cache-bust', 'grab-tooltips-json', 'build-stat'];
14
14
  }
15
15
  } else {
16
16
  sequenceOpts = ['clean', 'grab-cdn', 'grab-shared-components', 'grab-shared-scripts', 'templates', 'styles', 'js-lint', 'html-min', 'css-min', 'js-min', 'img-min', 'extras', 'brand', 'cache-bust', 'build-stat'];
@@ -0,0 +1,82 @@
1
+ var fs = require('fs');
2
+ var request = require('request');
3
+ var source = require('vinyl-source-stream');
4
+ var replace = require('gulp-replace');
5
+
6
+ function streamToDestination(gulp, siteSettings, url, fileName) {
7
+ return new Promise(resolve => { // eslint-disable-line no-undef
8
+ request(url)
9
+ .on('response', resp => {
10
+ if (resp.statusCode !== 200 && resp.statusCode !== 206) {
11
+ throw new Error(`${resp.statusCode} Error while fetching ${fileName}`);
12
+ } else {
13
+ replaceImgSrc(gulp, siteSettings, fileName);
14
+ }
15
+ })
16
+ .pipe(source(fileName))
17
+ .pipe(gulp.dest(`${siteSettings.srcFolder}/resources/images/`))
18
+ .on('finish', resolve);
19
+ });
20
+ }
21
+
22
+ function replaceImgSrc(gulp, siteSettings, fileName) {
23
+ const fileRegex = new RegExp(`(https?:\/\/.*${fileName})`, 'gm');
24
+
25
+ return gulp.src(`${siteSettings.tmpFolder}/index.html`)
26
+ .pipe(replace(fileRegex, '/resources/images/' + fileName))
27
+ .pipe(gulp.dest('.'));
28
+ }
29
+
30
+ const getTags = (string) => {
31
+ const regExs = [/<img.*?src="(.*?)"[^\>]+>/g, /<use.*?href="(.*?)".*?>/g, /<source.*?srcset="(.*?)".*?>/g, /url\('#{\$path}.*?'\)/g];
32
+ let tags = [];
33
+
34
+ regExs.forEach((regEx) => {
35
+ const match = string.match(regEx);
36
+
37
+ if (match) {
38
+ tags = [...tags, ...match];
39
+ }
40
+ });
41
+
42
+ return tags;
43
+ };
44
+
45
+ const getUrls = (tags) => {
46
+ const urls = [];
47
+
48
+ tags.forEach((tag) => {
49
+ if (tag) {
50
+ let url = tag;
51
+ url = url.match(/(https?:\/\/[^\s]+)/gim);
52
+ if (url) {
53
+ url = url[0];
54
+ url = url.replace(/"/g, "")
55
+ urls.push(url)
56
+ }
57
+ }
58
+ });
59
+
60
+ return [...new Set(urls)]; // eslint-disable-line no-undef
61
+ };
62
+
63
+ const parseImageSources = (gulp, siteSettings) => {
64
+ const text = fs.readFileSync(`${siteSettings.tmpFolder}/index.html`, 'utf8');
65
+ const textMatches = getTags(text);
66
+ return getUrls(textMatches);
67
+ };
68
+
69
+ module.exports = function(gulp, siteSettings, siteData) {
70
+ return function() {
71
+ const { isWhiteLabel } = siteData;
72
+ const urls = parseImageSources(gulp, siteSettings);
73
+ const ouputPath = isWhiteLabel ? 'images/' : 'temp/assets/images/';
74
+ const urlsPromises = urls.map((url) => {
75
+ const fileName = url.slice(url.lastIndexOf('/') + 1);
76
+ const relativePath = ouputPath + url.replace(fileName, '');
77
+ return streamToDestination(gulp, siteSettings, url, fileName, relativePath);
78
+ });
79
+
80
+ return Promise.all(urlsPromises); // eslint-disable-line no-undef
81
+ };
82
+ };
@@ -346,6 +346,18 @@ module.exports = function() {
346
346
 
347
347
  return require(opts.gulpTasksFolderPath + '/grab-images')(opts.gulp, opts.gulpSettings, opts.siteData);
348
348
  }
349
+ },
350
+
351
+ // Get global images
352
+ 'grab-global-images': {
353
+ subtasks: [],
354
+ func: function(opts) {
355
+ if ('undefined' === typeof opts.gulpTasksFolderPath) {
356
+ opts.gulpTasksFolderPath = '.';
357
+ }
358
+
359
+ return require(opts.gulpTasksFolderPath + '/grab-global-images')(opts.gulp, opts.gulpSettings, opts.siteData);
360
+ }
349
361
  },
350
362
 
351
363
  // Copy json files to dist
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mod-build",
3
- "version": "3.6.21",
3
+ "version": "3.6.22-beta.1",
4
4
  "description": "Share components for S3 sites.",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"