This repository has been archived by the owner on Sep 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
gulpfile.js
163 lines (147 loc) · 5.65 KB
/
gulpfile.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
/**
* @license
* Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at
* http://polymer.github.io/LICENSE.txt
* The complete set of authors may be found at
* http://polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at
* http://polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at
* http://polymer.github.io/PATENTS.txt
*/
'use strict';
const depcheck = require('depcheck');
const fs = require('fs-extra');
const gulp = require('gulp');
const mergeStream = require('merge-stream');
const mocha = require('gulp-spawn-mocha');
const path = require('path');
const runSeq = require('run-sequence');
const stream = require('stream');
const tslint = require('gulp-tslint');
const typescript = require('gulp-typescript');
const babelCore = require('@babel/core');
const sourcemaps = require('gulp-sourcemaps');
const babelPresetMinify =
require('babel-preset-minify')({}, {simplifyComparisons: false});
const tsProject = typescript.createProject(
'tsconfig.json', {typescript: require('typescript')});
gulp.task('lint', ['tslint', 'depcheck']);
gulp.task('clean', (done) => {
fs.remove(path.join(__dirname, 'lib'), done);
});
gulp.task('build', (done) => {
runSeq('clean', ['compile', 'gen-babel-helpers', 'minify-requirejs'], done);
});
gulp.task('compile', () => {
const tsResult = gulp.src(['src/**/*.ts', 'custom_typings/**/*.ts'])
.pipe(sourcemaps.init())
.pipe(tsProject(typescript.reporter.fullReporter()));
return mergeStream(tsResult.js.pipe(sourcemaps.write('../lib')), tsResult.dts)
.pipe(gulp.dest('lib'));
});
gulp.task('test', ['build'], function() {
return gulp.src('lib/test/**/*_test.js', {read: false}).pipe(mocha({
ui: 'tdd',
reporter: 'spec',
}))
});
gulp.task('tslint', function() {
return gulp.src('src/**/*.ts')
.pipe(tslint({configuration: 'tslint.json', formatter: 'verbose'}))
.pipe(tslint.report())
});
gulp.task('depcheck', function() {
return depcheck(__dirname, {
ignoreMatches: [
// "@types/*" dependencies are type declarations that are
// automatically loaded by TypeScript during build. depcheck can't
// detect this so we ignore them here.
'@types/*',
// Also it can't yet parse files that use async iteration.
// TODO(rictic): remove these
'mz',
'multipipe',
'polymer-bundler',
'parse5',
'dom5',
'@babel/traverse',
'stream',
'html-minifier',
]
})
.then((result) => {
let invalidFiles = Object.keys(result.invalidFiles) || [];
let invalidJsFiles = invalidFiles.filter((f) => f.endsWith('.js'));
if (invalidJsFiles.length > 0) {
throw new Error(`Invalid files: ${invalidJsFiles}`);
}
if (result.dependencies.length) {
throw new Error(`Unused dependencies: ${result.dependencies}`);
}
});
});
/*
* There doesn't seem to be documentation on what helpers are available, or
* which helpers are required for which transforms. The
* source is here:
* https://github.com/babel/babel/blob/6.x/packages/babel-helpers/src/helpers.js
*
* This list is an educated guess at the helpers needed for our transform set
* of ES2015 - modules. When we switch to loose mode we should update the list.
*
* All helpers are listed here, with some commented out, so it's clear what
* we've excluded.
*/
const babelHelperWhitelist = [
'typeof', // Symbol support, for IE11
// 'jsx', // we don't support JSX
// 'asyncIterator', // async-iterators are not in ES2015
// 'asyncGenerator', // async-iterators are not in ES2015
// 'asyncGeneratorDelegate', // async-iterators are not in ES2015
// 'asyncToGenerator', // async functions are not in ES2015
'classCallCheck',
'createClass',
'defineEnumerableProperties',
'defaults', // used to make `obj.__proto__ = bar` work
'defineProperty',
'extends', // used when setting __proto__
'get', // needed for class compilation
'inherits', // used for es6 class inheritance
'instanceof',
// 'interopRequireDefault', // for modules support
// 'interopRequireWildcard', // for modules support
'newArrowCheck', // confirms that `this` is correct inside arrow function
// body
'objectDestructuringEmpty',
'objectWithoutProperties',
'possibleConstructorReturn', // can we exclude with loose?
// 'selfGlobal', // not needed. `global` is not ES2015
'set', // difficult to tell if needed
'slicedToArray',
// 'slicedToArrayLoose',
'taggedTemplateLiteral',
// 'taggedTemplateLiteralLoose',
'temporalRef', // not needed in loose?
'temporalUndefined',
'toArray',
'toConsumableArray',
];
gulp.task('gen-babel-helpers', () => {
const helpersCode = babelCore.buildExternalHelpers(babelHelperWhitelist);
const minified =
babelCore.transform(helpersCode, {presets: [babelPresetMinify]}).code;
fs.mkdirpSync('./lib/');
fs.writeFileSync('./lib/babel-helpers.min.js', minified, {encoding: 'utf-8'});
});
gulp.task('minify-requirejs', () => {
const requireJsPath =
path.join(path.dirname(require.resolve('requirejs')), '..', 'require.js');
const requireJsCode = fs.readFileSync(requireJsPath, 'utf-8');
const minified =
babelCore.transform(requireJsCode, {presets: [babelPresetMinify]}).code;
fs.mkdirpSync('./lib/');
fs.writeFileSync('./lib/requirejs.min.js', minified, {encoding: 'utf-8'});
});