From 395442fd0a3ea31d448286ac0a2eca3fab8bcc10 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Sun, 18 Nov 2018 12:19:21 +0800 Subject: [PATCH] support embedding all assets and packaging them into a single html --- gulpfile.js | 61 +++++++++++++++++++ package-lock.json | 61 ++++++++++++------- src/index.html | 1 + .../services/ariaNgAssetsCacheService.js | 56 +++++++++++++++++ src/scripts/services/ariaNgLanguageLoader.js | 10 ++- 5 files changed, 167 insertions(+), 22 deletions(-) create mode 100644 src/scripts/services/ariaNgAssetsCacheService.js diff --git a/gulpfile.js b/gulpfile.js index 81f914b..857b81c 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -2,6 +2,7 @@ var gulp = require('gulp'); var gulpLoadPlugins = require('gulp-load-plugins'); var browserSync = require('browser-sync'); var del = require('del'); +var fs = require('fs'); var $ = gulpLoadPlugins(); var reload = browserSync.reload; @@ -149,6 +150,66 @@ gulp.task('info', function () { gulp.task('build', $.sequence('lint', 'html', 'langs', 'images', 'fonts', 'manifest', 'extras', 'info')); +gulp.task('bundle-assets', function () { + return gulp.src('dist/index.html') + .pipe($.replace(//g, function(match, fileName) { + var content = fs.readFileSync('dist/' + fileName, 'utf8'); + return ''; + })) + .pipe($.replace(/'; + })) + .pipe($.replace(/url\(\.\.\/(fonts\/[a-zA-Z0-9\-]+\.woff)(\?[a-zA-Z0-9\-_=.]+)?\)/g, function(match, fileName) { + if (!fs.existsSync('dist/' + fileName)) { + return match; + } + + var contentBuffer = fs.readFileSync('dist/' + fileName); + var contentBase64 = contentBuffer.toString('base64'); + return 'url(data:application/x-font-woff;base64,' + contentBase64 + ')'; + })) + .pipe($.replace(/<\/body>/g, function(match) { + var langDir = 'src/langs/'; + var result = ''; + var fileNames = fs.readdirSync(langDir, 'utf8'); + + if (fileNames.length > 0) { + var script = ''; + result = script + result; + } + + return result; + })) + .pipe($.replace(/<[a-z]+( [a-z\-]+="[a-zA-Z0-9\- ]+")* [a-z\-]+="((favicon.ico)|(favicon.png)|(tileicon.png)|(touchicon.png))"\/?>/g, '')) + .pipe(gulp.dest('dist')); +}); + +gulp.task('bundle-extras', function () { + return gulp.src([ + 'LICENSE' + ]).pipe(gulp.dest('dist')); +}); + +gulp.task('bundle-clean', del.bind(null, ['dist/css', 'dist/js', 'dist/fonts'])); + +gulp.task('build-bundle', $.sequence('lint', 'html', 'images', 'fonts', 'bundle-assets', 'bundle-extras', 'bundle-clean', 'info')); + gulp.task('default', ['clean'], function () { gulp.start('build'); }); diff --git a/package-lock.json b/package-lock.json index 3319966..e624733 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3406,7 +3406,8 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "aproba": { "version": "1.2.0", @@ -3427,12 +3428,14 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, + "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -3447,17 +3450,20 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "core-util-is": { "version": "1.0.2", @@ -3574,7 +3580,8 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "ini": { "version": "1.3.5", @@ -3586,6 +3593,7 @@ "version": "1.0.0", "bundled": true, "dev": true, + "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -3600,6 +3608,7 @@ "version": "3.0.4", "bundled": true, "dev": true, + "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -3607,12 +3616,14 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "minipass": { "version": "2.2.4", "bundled": true, "dev": true, + "optional": true, "requires": { "safe-buffer": "^5.1.1", "yallist": "^3.0.0" @@ -3631,6 +3642,7 @@ "version": "0.5.1", "bundled": true, "dev": true, + "optional": true, "requires": { "minimist": "0.0.8" } @@ -3711,7 +3723,8 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "object-assign": { "version": "4.1.1", @@ -3723,6 +3736,7 @@ "version": "1.4.0", "bundled": true, "dev": true, + "optional": true, "requires": { "wrappy": "1" } @@ -3808,7 +3822,8 @@ "safe-buffer": { "version": "5.1.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "safer-buffer": { "version": "2.1.2", @@ -3844,6 +3859,7 @@ "version": "1.0.2", "bundled": true, "dev": true, + "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -3863,6 +3879,7 @@ "version": "3.0.1", "bundled": true, "dev": true, + "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -3906,12 +3923,14 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "yallist": { "version": "3.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true } } }, @@ -4042,7 +4061,7 @@ }, "readable-stream": { "version": "1.0.34", - "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.0.34.tgz", + "resolved": "http://registry.npmjs.org/readable-stream/-/readable-stream-1.0.34.tgz", "integrity": "sha1-Elgg40vIQtLyqq+v5MKRbuMsFXw=", "dev": true, "requires": { @@ -4060,7 +4079,7 @@ }, "through2": { "version": "0.6.5", - "resolved": "https://registry.npmjs.org/through2/-/through2-0.6.5.tgz", + "resolved": "http://registry.npmjs.org/through2/-/through2-0.6.5.tgz", "integrity": "sha1-QaucZ7KdVyCQcUEOHXp6lozTrUg=", "dev": true, "requires": { @@ -4072,7 +4091,7 @@ }, "glob-watcher": { "version": "0.0.6", - "resolved": "https://registry.npmjs.org/glob-watcher/-/glob-watcher-0.0.6.tgz", + "resolved": "http://registry.npmjs.org/glob-watcher/-/glob-watcher-0.0.6.tgz", "integrity": "sha1-uVtKjfdLOcgymLDAXJeLTZo7cQs=", "dev": true, "requires": { @@ -4155,7 +4174,7 @@ }, "graceful-fs": { "version": "1.2.3", - "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-1.2.3.tgz", + "resolved": "http://registry.npmjs.org/graceful-fs/-/graceful-fs-1.2.3.tgz", "integrity": "sha1-FaSAaldUfLLS2/J/QuiajDRRs2Q=", "dev": true }, @@ -4167,7 +4186,7 @@ }, "lodash": { "version": "1.0.2", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-1.0.2.tgz", + "resolved": "http://registry.npmjs.org/lodash/-/lodash-1.0.2.tgz", "integrity": "sha1-j1dWDIO1n8JwvT1WG2kAQ0MOJVE=", "dev": true }, @@ -4200,7 +4219,7 @@ }, "gulp": { "version": "3.9.1", - "resolved": "https://registry.npmjs.org/gulp/-/gulp-3.9.1.tgz", + "resolved": "http://registry.npmjs.org/gulp/-/gulp-3.9.1.tgz", "integrity": "sha1-VxzkWSjdQK9lFPxAEYZgFsE4RbQ=", "dev": true, "requires": { @@ -4221,7 +4240,7 @@ "dependencies": { "semver": { "version": "4.3.6", - "resolved": "https://registry.npmjs.org/semver/-/semver-4.3.6.tgz", + "resolved": "http://registry.npmjs.org/semver/-/semver-4.3.6.tgz", "integrity": "sha1-MAvG4OhjdPe6YQaLWx7NV/xlMto=", "dev": true } @@ -10858,7 +10877,7 @@ }, "graceful-fs": { "version": "3.0.11", - "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-3.0.11.tgz", + "resolved": "http://registry.npmjs.org/graceful-fs/-/graceful-fs-3.0.11.tgz", "integrity": "sha1-dhPHeKGv6mLyXGMKCG1/Osu92Bg=", "dev": true, "requires": { @@ -10873,7 +10892,7 @@ }, "readable-stream": { "version": "1.0.34", - "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.0.34.tgz", + "resolved": "http://registry.npmjs.org/readable-stream/-/readable-stream-1.0.34.tgz", "integrity": "sha1-Elgg40vIQtLyqq+v5MKRbuMsFXw=", "dev": true, "requires": { @@ -10901,7 +10920,7 @@ }, "through2": { "version": "0.6.5", - "resolved": "https://registry.npmjs.org/through2/-/through2-0.6.5.tgz", + "resolved": "http://registry.npmjs.org/through2/-/through2-0.6.5.tgz", "integrity": "sha1-QaucZ7KdVyCQcUEOHXp6lozTrUg=", "dev": true, "requires": { diff --git a/src/index.html b/src/index.html index c6a86a6..2623abd 100644 --- a/src/index.html +++ b/src/index.html @@ -388,6 +388,7 @@ + diff --git a/src/scripts/services/ariaNgAssetsCacheService.js b/src/scripts/services/ariaNgAssetsCacheService.js new file mode 100644 index 0000000..0c7fbe0 --- /dev/null +++ b/src/scripts/services/ariaNgAssetsCacheService.js @@ -0,0 +1,56 @@ +(function () { + 'use strict'; + + angular.module('ariaNg').provider('ariaNgAssetsCacheService', [function () { + var assetsRoot = {}; + var languageAssetsPrefix = 'languages.'; + + var getAsset = function (path) { + var parts = path.split('.'), + result = assetsRoot; + + for (var i = 0; i < parts.length; i++) { + if (angular.isUndefined(result[parts[i]])) { + return null; + } + + result = result[parts[i]]; + } + + return result; + }; + + var setAsset = function (path, value) { + var parts = path.split('.'), + result = assetsRoot; + + for (var i = 0; i < parts.length - 1; i++) { + if (angular.isUndefined(result[parts[i]])) { + result[parts[i]] = {}; + } + + result = result[parts[i]]; + } + + result[parts[parts.length - 1]] = value; + }; + + this.getLanguageAsset = function (languageName) { + return getAsset(languageAssetsPrefix + languageName); + }; + + this.setLanguageAsset = function (languageName, languageContent) { + setAsset(languageAssetsPrefix + languageName, languageContent); + }; + + this.$get = function () { + var that = this; + + return { + getLanguageAsset: function (languageName) { + return that.getLanguageAsset(languageName); + } + } + }; + }]); +}()); diff --git a/src/scripts/services/ariaNgLanguageLoader.js b/src/scripts/services/ariaNgLanguageLoader.js index 3a408fd..19b4a78 100644 --- a/src/scripts/services/ariaNgLanguageLoader.js +++ b/src/scripts/services/ariaNgLanguageLoader.js @@ -1,7 +1,7 @@ (function () { 'use strict'; - angular.module('ariaNg').factory('ariaNgLanguageLoader', ['$http', '$q', 'ariaNgConstants', 'ariaNgLanguages', 'ariaNgNotificationService', 'ariaNgLogService', 'ariaNgStorageService', function ($http, $q, ariaNgConstants, ariaNgLanguages, ariaNgNotificationService, ariaNgLogService, ariaNgStorageService) { + angular.module('ariaNg').factory('ariaNgLanguageLoader', ['$http', '$q', 'ariaNgConstants', 'ariaNgLanguages', 'ariaNgAssetsCacheService', 'ariaNgNotificationService', 'ariaNgLogService', 'ariaNgStorageService', function ($http, $q, ariaNgConstants, ariaNgLanguages, ariaNgAssetsCacheService, ariaNgNotificationService, ariaNgLogService, ariaNgStorageService) { var getKeyValuePair = function (line) { for (var i = 0; i < line.length; i++) { if (i > 0 && line.charAt(i - 1) !== '\\' && line.charAt(i) === '=') { @@ -96,6 +96,14 @@ deferred.resolve(languageResource); } + if (ariaNgAssetsCacheService.getLanguageAsset(options.key)) { + var languageObject = getLanguageObject(ariaNgAssetsCacheService.getLanguageAsset(options.key)); + ariaNgStorageService.set(languageKey, languageObject); + deferred.resolve(languageObject); + + return deferred.promise; + } + var languagePath = ariaNgConstants.languagePath + '/' + options.key + ariaNgConstants.languageFileExtension; $http({