remove inline fallback code for CSP

This commit is contained in:
Kyle Spearrin 2017-06-17 10:08:47 -04:00
parent 00007c20a7
commit ce1352cb9f
4 changed files with 49 additions and 12 deletions

View File

@ -65,7 +65,12 @@ gulp.task('clean:lib', function (cb) {
gulp.task('clean', ['clean:js', 'clean:css', 'clean:lib', 'dist:clean']); gulp.task('clean', ['clean:js', 'clean:css', 'clean:lib', 'dist:clean']);
gulp.task('min:js', ['clean:js'], function () { gulp.task('min:js', ['clean:js'], function () {
return gulp.src([paths.js, '!' + paths.minJs], { base: '.' }) return gulp.src(
[
paths.js,
'!' + paths.minJs,
'!' + paths.webroot + 'js/fallback*.js'
], { base: '.' })
.pipe(concat(paths.concatJsDest)) .pipe(concat(paths.concatJsDest))
.pipe(uglify()) .pipe(uglify())
.pipe(gulp.dest('.')); .pipe(gulp.dest('.'));
@ -356,6 +361,19 @@ gulp.task('dist:js:app', function () {
.pipe(gulp.dest('.')); .pipe(gulp.dest('.'));
}); });
gulp.task('dist:js:fallback', function () {
var mainStream = gulp
.src([
paths.webroot + 'js/fallback*.js'
]);
merge(mainStream, config())
.pipe(preprocess({ context: { cacheTag: randomString } }))
.pipe(uglify())
.pipe(rename({ suffix: '.min' }))
.pipe(gulp.dest(paths.dist + 'js'));
});
gulp.task('dist:js:lib', function () { gulp.task('dist:js:lib', function () {
return gulp return gulp
.src([ .src([
@ -383,7 +401,7 @@ gulp.task('dist:preprocess', function () {
gulp.task('dist', ['build'], function (cb) { gulp.task('dist', ['build'], function (cb) {
return runSequence( return runSequence(
'dist:clean', 'dist:clean',
['dist:move', 'dist:css', 'dist:js:app', 'dist:js:lib'], ['dist:move', 'dist:css', 'dist:js:app', 'dist:js:lib', 'dist:js:fallback'],
'dist:preprocess', 'dist:preprocess',
cb); cb);
}); });

View File

@ -43,12 +43,9 @@
<script src="https://js.stripe.com/v2/"></script> <script src="https://js.stripe.com/v2/"></script>
<!-- @if true !> <!-- @if true !>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" /> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<meta name="x-stylesheet-test-bs" content="" class="invisible" />
<script>!function(a,b,c){var d,e=document,f=e.getElementsByTagName('SCRIPT'),g=f[f.length-1].previousElementSibling,h=e.defaultView&&e.defaultView.getComputedStyle?e.defaultView.getComputedStyle(g):g.currentStyle;if(h&&h[a]!==b)for(d=0;d<c.length;d++)e.write('<link rel="stylesheet" href="'+c[d]+'"/>')}('visibility','hidden',['lib\/bootstrap\/css\/bootstrap.min.css?v=<!-- @echo cacheTag !>']);</script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" /> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" />
<meta name="x-stylesheet-test-fa" content="" class="fa" /> <meta name="x-stylesheet-test" content="" class="fa invisible" />
<script>!function(a,b,c){var d,e=document,f=e.getElementsByTagName('SCRIPT'),g=f[f.length-1].previousElementSibling,h=e.defaultView&&e.defaultView.getComputedStyle?e.defaultView.getComputedStyle(g):g.currentStyle;if(h&&h[a]!==b)for(d=0;d<c.length;d++)e.write('<link rel="stylesheet" href="'+c[d]+'"/>')}('font-family','FontAwesome',['lib\/font-awesome\/css\/font-awesome.min.css?v=<!-- @echo cacheTag !>']);</script> <script src="js/fallback-styles.min.js?v=<!-- @echo cacheTag !>"></script>
<link rel="stylesheet" href="css/vault.min.css?v=<!-- @echo cacheTag !>" /> <link rel="stylesheet" href="css/vault.min.css?v=<!-- @echo cacheTag !>" />
<!-- @endif --> <!-- @endif -->
@ -66,13 +63,9 @@
<!-- @if true !> <!-- @if true !>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script>(window.jQuery||document.write('<script src="lib\/jquery\/jquery.min.js?v=<!-- @echo cacheTag !>"><\/script>'));</script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script>((window.jQuery&&window.jQuery.fn&&window.jQuery.fn.modal)||document.write('<script src="lib\/bootstrap\/js\/bootstrap.min.js?v=<!-- @echo cacheTag !>"><\/script>'));</script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.3/angular.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.3/angular.min.js"></script>
<script>(window.angular||document.write('<script src="lib\/angular\/angular.min.js?v=<!-- @echo cacheTag !>"><\/script>'));</script> <script src="js/fallback-scripts.min.js?v=<!-- @echo cacheTag !>"></script>
<script src="js/lib.min.js?v=<!-- @echo cacheTag !>"></script> <script src="js/lib.min.js?v=<!-- @echo cacheTag !>"></script>
<script src="js/bw.min.js?v=<!-- @echo cacheTag !>"></script> <script src="js/bw.min.js?v=<!-- @echo cacheTag !>"></script>

View File

@ -0,0 +1,9 @@
function loadScriptIfMissing(condition, path) {
if (!condition) {
document.write('<script src="' + path + '?v=' + cacheTag + '"><\/script>');
}
}
loadScriptIfMissing(window.jQuery, 'lib\/jquery\/jquery.min.js');
loadScriptIfMissing(window.jQuery && window.jQuery.fn && window.jQuery.fn.modal, 'lib\/bootstrap\/js\/bootstrap.min.js');
loadScriptIfMissing(window.angular, 'lib\/angular\/angular.min.js');

17
src/js/fallback-styles.js Normal file
View File

@ -0,0 +1,17 @@
var cacheTag = '/* @echo cacheTag */' || '';
function loadStylesheetIfMissing(property, value, paths) {
var scripts = document.getElementsByTagName('SCRIPT'),
siblings = scripts[scripts.length - 1].previousElementSibling,
meta = document.defaultView && document.defaultView.getComputedStyle ?
document.defaultView.getComputedStyle(siblings) : siblings.currentStyle;
if (meta && meta[property] !== value) {
for (var i = 0; i < paths.length; i++) {
document.write('<link rel="stylesheet" href="' + paths[i] + '?v=' + cacheTag + '" />');
}
}
}
loadStylesheetIfMissing('visibility', 'hidden', ['lib\/bootstrap\/css\/bootstrap.min.css']);
loadStylesheetIfMissing('fontFamily', 'FontAwesome', ['lib\/font-awesome\/css\/font-awesome.min.css']);