This repository was archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
Copy pathutil.js
321 lines (283 loc) · 10 KB
/
util.js
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
const config = require('./config');
const gulp = require('gulp');
const gutil = require('gulp-util');
const frep = require('gulp-frep');
const fs = require('fs');
const args = require('minimist')(process.argv.slice(2));
const path = require('path');
const rename = require('gulp-rename');
const filter = require('gulp-filter');
const concat = require('gulp-concat');
const series = require('stream-series');
const lazypipe = require('lazypipe');
const glob = require('glob').sync;
const uglify = require('gulp-uglify');
const sass = require('gulp-sass')(require('sass'));
const plumber = require('gulp-plumber');
const ngAnnotate = require('gulp-ng-annotate');
const insert = require('gulp-insert');
const gulpif = require('gulp-if');
const cssnano = require('cssnano');
const gulpPostcss = require('gulp-postcss');
const postcss = require('postcss');
const _ = require('lodash');
const constants = require('./const');
const VERSION = constants.VERSION;
const BUILD_MODE = constants.BUILD_MODE;
const IS_DEV = constants.IS_DEV;
const ROOT = constants.ROOT;
const utils = require('../scripts/gulp-utils.js');
exports.buildJs = buildJs;
exports.autoprefix = utils.autoprefix;
exports.buildModule = buildModule;
exports.filterNonCodeFiles = filterNonCodeFiles;
exports.readModuleArg = readModuleArg;
exports.themeBuildStream = themeBuildStream;
exports.minifyCss = minifyCss;
exports.dedupeCss = dedupeCss;
exports.args = args;
/**
* Builds the entire component library javascript.
*/
function buildJs() {
const jsFiles = config.jsCoreFiles;
config.componentPaths.forEach(component => {
jsFiles.push(path.join(component, '*.js'));
jsFiles.push(path.join(component, '**/*.js'));
});
gutil.log("building js files...");
const jsBuildStream = gulp.src(jsFiles)
.pipe(filterNonCodeFiles())
.pipe(utils.buildNgMaterialDefinition())
.pipe(plumber())
.pipe(ngAnnotate())
.pipe(utils.addJsWrapper(true));
const jsProcess = series(jsBuildStream, themeBuildStream())
.pipe(concat('angular-material.js'))
.pipe(BUILD_MODE.transform())
.pipe(insert.prepend(config.banner))
.pipe(insert.append(';window.ngMaterial={version:{full: "' + VERSION + '"}};'))
.pipe(gulp.dest(config.outputDir))
.pipe(gulpif(!IS_DEV, uglify({output: {comments: 'some'}})))
.pipe(rename({extname: '.min.js'}))
.pipe(gulp.dest(config.outputDir));
return series(jsProcess, deployMaterialMocks());
// Deploy the `angular-material-mocks.js` file to the `dist` directory
function deployMaterialMocks() {
return gulp.src(config.mockFiles)
.pipe(gulp.dest(config.outputDir));
}
}
function minifyCss(extraOptions) {
const options = {
reduceTransforms: false,
svgo: false
};
const preset = {
preset: [
'default',
_.assign(options, extraOptions)
]
};
return gulpPostcss([cssnano(preset)]);
}
/**
* @param {string} module
* @param {{isRelease, minify, useBower}=} opts
*/
function buildModule(module, opts) {
opts = opts || {};
if (module.indexOf(".") < 0) {
module = "material.components." + module;
}
gutil.log('Building ' + module + (opts.isRelease && ' minified' || '') + ' ...');
const name = module.split('.').pop();
utils.copyDemoAssets(name, 'src/components/', 'dist/demos/');
let stream = utils.filesForModule(module)
.pipe(filterNonCodeFiles())
.pipe(filterLayoutAttrFiles())
.pipe(gulpif('*.scss', buildModuleStyles(name)))
.pipe(gulpif('*.js', buildModuleJs(name)));
if (module === 'material.core') {
stream = splitStream(stream);
}
return stream
.pipe(BUILD_MODE.transform())
.pipe(insert.prepend(config.banner))
.pipe(gulpif(opts.minify, buildMin()))
.pipe(gulpif(opts.useBower, buildBower()))
.pipe(gulp.dest(BUILD_MODE.outputDir + name));
function splitStream(stream) {
const js = series(stream, themeBuildStream())
.pipe(filter('**/*.js'))
.pipe(concat('core.js'));
const css = stream
.pipe(filter(['**/*.css', '!**/ie_fixes.css']));
return series(js, css);
}
function buildMin() {
return lazypipe()
.pipe(gulpif, /.css$/, minifyCss(),
uglify({output: {comments: 'some'}})
.on('error', function(e) {
console.log('\x07', e.message);
return this.end();
}
)
)
.pipe(rename, function(path) {
path.extname = path.extname
.replace(/.js$/, '.min.js')
.replace(/.css$/, '.min.css');
})
();
}
function buildBower() {
return lazypipe()
.pipe(utils.buildModuleBower, name, VERSION)();
}
function buildModuleJs(name) {
const patterns = [
{
pattern: /@ngInject/g,
replacement: 'ngInject'
},
{
// Turns `thing.$inject` into `thing['$inject']` in order to prevent
// Closure from stripping it from objects with an @constructor
// annotation.
pattern: /\.\$inject\b/g,
replacement: "['$inject']"
}
];
return lazypipe()
.pipe(plumber)
.pipe(ngAnnotate)
.pipe(frep, patterns)
.pipe(concat, name + '.js')
();
}
/**
* @param {string} name module name. I.e. 'select', 'dialog', etc.
* @returns {*}
*/
function buildModuleStyles(name) {
let files = [];
const inputVariableConsumers = [
'input', 'select', 'checkbox', 'datepicker', 'radioButton', 'switch'
];
config.themeBaseFiles.forEach(function(fileGlob) {
files = files.concat(glob(fileGlob, {cwd: ROOT}));
});
// Handle md-input and md-input-container variables that need to be shared with md-select
// in order to orchestrate identical layouts and alignments. In the future, it may be necessary
// to also use these variables with md-datepicker and md-autocomplete.
if (inputVariableConsumers.includes(name)) {
files = files.concat(glob(config.inputVariables, {cwd: ROOT}));
}
const baseStyles = files.map(function(fileName) {
return fs.readFileSync(fileName, 'utf8').toString();
}).join('\n');
let sassModules;
// Don't add the Sass modules to core since they get automatically included already.
if (name === 'core') {
sassModules = '';
} else {
sassModules = fs.readFileSync(config.scssModules, 'utf8').toString();
}
return lazypipe()
.pipe(insert.prepend, baseStyles)
.pipe(gulpif, /theme.scss/, rename(name + '-default-theme.scss'), concat(name + '.scss'))
// Theme files are suffixed with the `default-theme.scss` string.
// In some cases there are multiple theme SCSS files, which should be concatenated together.
.pipe(gulpif, /default-theme.scss/, concat(name + '-default-theme.scss'))
// We can't prepend these earlier, or they get duplicated in a way that hoistScssAtUseStatements
// can't deduplicate them.
.pipe(insert.prepend, sassModules)
.pipe(utils.hoistScssAtUseStatements)
.pipe(sass)
.pipe(dedupeCss)
.pipe(utils.autoprefix)
(); // Invoke the returning lazypipe function to create our new pipe.
}
}
/**
* @returns {string} module name. i.e. material.components.icon
*/
function readModuleArg() {
const module = args.c ? 'material.components.' + args.c : (args.module || args.m);
if (!module) {
gutil.log('\nProvide a component argument via `-c`:',
'\nExample: -c toast');
gutil.log('\nOr provide a module argument via `--module` or `-m`.',
'\nExample: --module=material.components.toast or -m material.components.dialog');
throw new Error("Unable to read module arguments.");
}
return module;
}
/**
* We are not injecting the layout-attributes selectors into the core module css,
* otherwise we would have the layout-classes and layout-attributes in there.
*/
function filterLayoutAttrFiles() {
return filter(function(file) {
return !/.*layout-attributes\.scss/g.test(file.path);
});
}
function filterNonCodeFiles() {
return filter(function(file) {
return !/demo|module\.json|script\.js|\.spec.js|README/.test(file.path);
});
}
// builds the theming related css and provides it as a JS const for AngularJS
function themeBuildStream() {
// Make a copy so that we don't modify the actual config that is used by other functions
const paths = config.themeBaseFiles.slice(0);
config.componentPaths.forEach(component => paths.push(path.join(component, '*-theme.scss')));
paths.push(config.themeCore);
return gulp.src(paths)
.pipe(concat('default-theme.scss'))
.pipe(utils.hoistScssVariables())
.pipe(sass())
.pipe(dedupeCss())
// The PostCSS orderedValues plugin modifies the theme color expressions.
.pipe(minifyCss({orderedValues: false}))
.pipe(utils.cssToNgConstant('material.core', '$MD_THEME_CSS'));
}
// Removes duplicated CSS properties.
function dedupeCss() {
const prefixRegex = /-(webkit|moz|ms|o)-.+/;
return insert.transform(function(contents) {
// Parse the CSS into an AST.
const parsed = postcss.parse(contents);
// Walk through all the rules, skipping comments, media queries etc.
parsed.walk(function(rule) {
// Skip over any comments, media queries and rules that have less than 2 properties.
if (rule.type !== 'rule' || !rule.nodes || rule.nodes.length < 2) return;
// Walk all of the properties within a rule.
rule.walk(function(prop) {
// Check if there's a similar property that comes after the current one.
const hasDuplicate = validateProp(prop) && _.find(rule.nodes, function(otherProp) {
return prop !== otherProp && prop.prop === otherProp.prop && validateProp(otherProp);
});
// Remove the declaration if it's duplicated.
if (hasDuplicate) {
prop.remove();
gutil.log(gutil.colors.yellow(
'Removed duplicate property: "' +
prop.prop + ': ' + prop.value + '" from "' + rule.selector + '"...'
));
}
});
});
// Turn the AST back into CSS.
return parsed.toResult().css;
});
// Checks if a property is a style declaration and that it
// doesn't contain any vendor prefixes.
function validateProp(prop) {
return prop && prop.type === 'decl' && ![prop.prop, prop.value].some(function(value) {
return value.indexOf('-') > -1 && prefixRegex.test(value);
});
}
}