mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-04-29 05:15:38 +08:00
fix: don't inline css in csp mode.
Also add `angular-csp.css` to the resulting build.
This commit is contained in:
@@ -3,6 +3,7 @@ var shell = require('shelljs');
|
||||
var grunt = require('grunt');
|
||||
var spawn = require('child_process').spawn;
|
||||
var version;
|
||||
var CSP_CSS_HEADER = '/* Include this file in your html if you are using the CSP mode. */\n\n';
|
||||
|
||||
module.exports = {
|
||||
|
||||
@@ -70,12 +71,20 @@ module.exports = {
|
||||
|
||||
|
||||
addStyle: function(src, styles, minify){
|
||||
styles = styles.map(processCSS.bind(this)).join('\n');
|
||||
src += styles;
|
||||
return src;
|
||||
styles = styles.reduce(processCSS.bind(this), {
|
||||
js: [src],
|
||||
css: []
|
||||
});
|
||||
return {
|
||||
js: styles.js.join('\n'),
|
||||
css: styles.css.join('\n')
|
||||
};
|
||||
|
||||
function processCSS(state, file) {
|
||||
var css = fs.readFileSync(file).toString(),
|
||||
js;
|
||||
state.css.push(css);
|
||||
|
||||
function processCSS(file){
|
||||
var css = fs.readFileSync(file).toString();
|
||||
if(minify){
|
||||
css = css
|
||||
.replace(/\r?\n/g, '')
|
||||
@@ -91,7 +100,10 @@ module.exports = {
|
||||
.replace(/\\/g, '\\\\')
|
||||
.replace(/'/g, "\\'")
|
||||
.replace(/\r?\n/g, '\\n');
|
||||
return "!angular.$$csp() && angular.element(document).find('head').prepend('<style type=\"text/css\">" + css + "</style>');";
|
||||
js = "!angular.$$csp() && angular.element(document).find('head').prepend('<style type=\"text/css\">" + css + "</style>');";
|
||||
state.js.push(js);
|
||||
|
||||
return state;
|
||||
}
|
||||
},
|
||||
|
||||
@@ -100,7 +112,7 @@ module.exports = {
|
||||
var processed = src
|
||||
.replace(/"NG_VERSION_FULL"/g, NG_VERSION.full)
|
||||
.replace(/"NG_VERSION_MAJOR"/, NG_VERSION.major)
|
||||
.replace(/"NG_VERSION_MINOR"/, NG_VERSION.minor)
|
||||
.replace(/"NG_VERSION_ MINOR"/, NG_VERSION.minor)
|
||||
.replace(/"NG_VERSION_DOT"/, NG_VERSION.dot)
|
||||
.replace(/"NG_VERSION_CDN"/, NG_VERSION.cdn)
|
||||
.replace(/"NG_VERSION_CODENAME"/, NG_VERSION.codename);
|
||||
@@ -112,17 +124,28 @@ module.exports = {
|
||||
build: function(config, fn){
|
||||
var files = grunt.file.expand(config.src);
|
||||
var styles = config.styles;
|
||||
var processedStyles;
|
||||
//concat
|
||||
var src = files.map(function(filepath){
|
||||
var src = files.map(function(filepath) {
|
||||
return grunt.file.read(filepath);
|
||||
}).join(grunt.util.normalizelf('\n'));
|
||||
//process
|
||||
var processed = this.process(src, grunt.config('NG_VERSION'), config.strict);
|
||||
if (styles) processed = this.addStyle(processed, styles.css, styles.minify);
|
||||
if (styles) {
|
||||
processedStyles = this.addStyle(processed, styles.css, styles.minify);
|
||||
processed = processedStyles.js;
|
||||
if (config.styles.generateCspCssFile) {
|
||||
grunt.file.write(removeSuffix(config.dest) + '-csp.css', CSP_CSS_HEADER + processedStyles.css);
|
||||
}
|
||||
}
|
||||
//write
|
||||
grunt.file.write(config.dest, processed);
|
||||
grunt.log.ok('File ' + config.dest + ' created.');
|
||||
fn();
|
||||
|
||||
function removeSuffix(fileName) {
|
||||
return fileName.replace(/\.js$/, '');
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user