-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathconcat.js
More file actions
139 lines (125 loc) · 4.53 KB
/
concat.js
File metadata and controls
139 lines (125 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* grunt-contrib-concat
* http://gruntjs.com/
*
* Copyright (c) 2016 "Cowboy" Ben Alman, contributors
* Licensed under the MIT license.
*/
'use strict';
module.exports = function(grunt) {
// Internal lib.
var comment = require('./lib/comment').init(grunt);
var chalk = require('chalk');
var sourcemap = require('./lib/sourcemap').init(grunt);
grunt.registerMultiTask('concat', 'Concatenate files.', function() {
// Merge task-specific and/or target-specific options with these defaults.
var options = this.options({
separator: grunt.util.linefeed,
banner: '',
footer: '',
stripBanners: false,
process: false,
sourceMap: false,
sourceMapName: undefined,
sourceMapStyle: 'embed'
});
// Normalize boolean options that accept options objects.
if (options.stripBanners === true) {
options.stripBanners = {};
}
if (options.process === true) {
options.process = {};
}
// Process banner and footer.
var banner = grunt.template.process(options.banner);
var footer = grunt.template.process(options.footer);
// Set a local variable for whether to build source maps or not.
var sourceMap = options.sourceMap;
// If content is not embedded and it will be modified, either exit or do
// not make the source map.
if (
sourceMap && options.sourceMapStyle === 'link' &&
(options.stripBanners || options.process)
) {
// Warn and exit if --force isn't set.
grunt.warn(
'stripBanners or process option is enabled. ' +
'Set sourceMapStyle option to \'embed\' or \'inline\'.'
);
// --force is set, continue on without the source map.
grunt.log.warn('Skipping creation of source maps.');
// Set sourceMap to false to keep maps from being constructed.
sourceMap = false;
}
var exitEarly = {};
try {
// Iterate over all src-dest file pairs.
this.files.forEach(function(f) {
// Initialize source map objects.
var sourceMapHelper;
if (sourceMap) {
sourceMapHelper = sourcemap.helper(f, options);
sourceMapHelper.add(banner);
}
// Concat banner + specified files + footer.
var src = banner + f.src.filter(function(filepath) {
// Warn on invalid source files (if nonull was set). They will be
// removed if --force is specified.
if (!grunt.file.exists(filepath)) {
grunt.fail.warn('Source file "' + filepath + '" not found.');
if (!grunt.option('force')) {
// See the catch clause below for why we do this.
throw exitEarly;
}
return false;
}
return true;
}).map(function(filepath, i) {
if (grunt.file.isDir(filepath)) {
return;
}
// Read file source.
var src = grunt.file.read(filepath);
// Process files as templates if requested.
if (typeof options.process === 'function') {
src = options.process(src, filepath);
} else if (options.process) {
src = grunt.template.process(src, options.process);
}
// Strip banners if requested.
if (options.stripBanners) {
src = comment.stripBanner(src, options.stripBanners);
}
// Add the lines of this file to our map.
if (sourceMapHelper) {
src = sourceMapHelper.addlines(src, filepath);
if (i < f.src.length - 1) {
sourceMapHelper.add(options.separator);
}
}
return src;
}).join(options.separator) + footer;
if (sourceMapHelper) {
sourceMapHelper.add(footer);
sourceMapHelper.write();
// Add sourceMappingURL to the end.
src += sourceMapHelper.url();
}
// Write the destination file.
grunt.file.write(f.dest, src);
// Print a success message.
grunt.verbose.write('File ' + chalk.cyan(f.dest) + ' created.');
});
} catch (reason) {
if (reason === exitEarly) {
// grunt.fail.warn uses the "exit" library, which doesn't stop the
// process synchronously on Node.js 0.10 on Windows, which could result
// in files still being concatenated even when some are missing. See:
// https://github.com/nodejs/node-v0.x-archive/issues/3584#issuecomment-8034529
// Therefore, we throw a unique reference to emulate exiting early.
return;
}
throw reason;
}
});
};