Skip to content

Commit

Permalink
support embedding all assets and packaging them into a single html
Browse files Browse the repository at this point in the history
  • Loading branch information
mayswind committed Nov 18, 2018
1 parent 72a6c10 commit 395442f
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 22 deletions.
61 changes: 61 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(/<link rel="stylesheet" href="(css\/[a-zA-Z0-9\-_.]+\.css)">/g, function(match, fileName) {
var content = fs.readFileSync('dist/' + fileName, 'utf8');
return '<style type="text/css">' + content + '</style>';
}))
.pipe($.replace(/<script src="(js\/[a-zA-Z0-9\-_.]+\.js)"><\/script>/g, function(match, fileName) {
var content = fs.readFileSync('dist/' + fileName, 'utf8');
return '<script type="application/javascript">' + content + '</script>';
}))
.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 = '</body>';
var fileNames = fs.readdirSync(langDir, 'utf8');

if (fileNames.length > 0) {
var script = '<script type="application/javascript">' +
'angular.module("ariaNg").config(["ariaNgAssetsCacheServiceProvider",function(e){';

for (var i = 0; i < fileNames.length; i++) {
var fileName = fileNames[i];
var content = fs.readFileSync(langDir + fileName, 'utf8');

var lastPointIndex = fileName.lastIndexOf('.');
var languageName = fileName.substr(0, lastPointIndex);

content = content.replace(/\n/g, '\\n');
content = content.replace(/"/g, '\\"');
script += 'e.setLanguageAsset(\'' + languageName + '\',"' + content + '");';
}

script += '}]);</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');
});
61 changes: 40 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@
<script src="scripts/filters/taskOrderBy.js"></script>
<script src="scripts/filters/taskStatus.js"></script>
<script src="scripts/filters/volume.js"></script>
<script src="scripts/services/ariaNgAssetsCacheService.js"></script>
<script src="scripts/services/ariaNgLanguageLoader.js"></script>
<script src="scripts/services/ariaNgCommonService.js"></script>
<script src="scripts/services/ariaNgNotificationService.js"></script>
Expand Down
56 changes: 56 additions & 0 deletions src/scripts/services/ariaNgAssetsCacheService.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
};
}]);
}());
Loading

0 comments on commit 395442f

Please sign in to comment.