This repository has been archived by the owner on Jan 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathgulpfile.js
91 lines (78 loc) · 2.63 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
'use strict';
const path = require('path');
const fs = require('fs');
const gulp = require('gulp');
const insert = require('gulp-insert');
const filter = require('gulp-filter');
const clean = require('gulp-clean');
const parsePath = require('parse-filepath');
const _ = require('lodash');
const config = require('./config.json');
const THREE_PATH = 'node_modules/three';
const EXAMPLES_PATH = 'examples/js';
/**
* Copy license file
*/
gulp.task('licence', () => {
return gulp.src(`${THREE_PATH}/LICENSE`)
.pipe(gulp.dest('.'));
});
/**
* Copy examples JS files without UMD loader
*/
gulp.task('build-no-umd', () => {
const paths = _.map(config.noUMD, path => `${THREE_PATH}/${EXAMPLES_PATH}/${path}`);
return gulp.src(paths, {base: `${THREE_PATH}/${EXAMPLES_PATH}`})
.pipe(gulp.dest(EXAMPLES_PATH));
});
/**
* Copy examples JS files with UMD loader
*/
gulp.task('build-umd', () => {
function normalize(path) {
return path.replace(/\\/g, '/');
}
const paths = [`${THREE_PATH}/${EXAMPLES_PATH}/**/*.js`]
.concat(_.map(config.ignore, path => `!${THREE_PATH}/${EXAMPLES_PATH}/${path}`))
.concat(_.map(config.noUMD, path => `!${THREE_PATH}/${EXAMPLES_PATH}/${path}`));
return gulp.src(paths, {base: `${THREE_PATH}/${EXAMPLES_PATH}`})
.pipe(insert.prepend(file => {
return `(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define('three.${parsePath(file.path).stem}', ['three'], factory);
}
else if ('undefined' !== typeof exports && 'undefined' !== typeof module) {
module.exports = factory(require('three'));
}
else {
factory(root.THREE);
}
}(this, function(THREE) {
`;
}))
.pipe(insert.append(file => {
const globals = _(config.globals)
.filter((vars, path) => normalize(file.path).includes(path))
.flatten()
.map(global => `THREE.${global} = ${global};`)
.join(`\n`);
return globals ? `\n${globals}\n` : '';
}))
.pipe(insert.append(() => {
return `}));`;
}))
.pipe(gulp.dest(EXAMPLES_PATH));
});
/**
* Remove old files
*/
gulp.task('clean', () => {
return gulp.src(`${EXAMPLES_PATH}/**/*.js`, {read: false, base: EXAMPLES_PATH})
.pipe(filter(file => {
const filePath = path.relative(__dirname, file.path);
return !fs.existsSync(`${THREE_PATH}/${filePath}`);
}))
.pipe(clean());
});
gulp.task('build', gulp.parallel('build-umd', 'build-no-umd'));
gulp.task('default', gulp.series('licence', 'build', 'clean'));