-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathgulpfile.js
More file actions
153 lines (134 loc) · 6.61 KB
/
Copy pathgulpfile.js
File metadata and controls
153 lines (134 loc) · 6.61 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
var gulp = require('gulp');
// source-map 0.7.x (bundled in gulp-typescript) uses fetch-based wasm loading when
// global `fetch` exists (Node 18+), so we must supply a data: URL for the wasm.
var path = require('path');
try {
var smDir = path.join(require.resolve('gulp-typescript'), '..', '..', 'node_modules', 'source-map');
var wasmBuf = require('fs').readFileSync(path.join(smDir, 'lib', 'mappings.wasm'));
var wasmDataUrl = 'data:application/wasm;base64,' + wasmBuf.toString('base64');
var SourceMapConsumer = require(smDir).SourceMapConsumer;
SourceMapConsumer.initialize({'lib/mappings.wasm': wasmDataUrl});
} catch(e) { /* source-map wasm not needed in this env */ }
var ts = require('gulp-typescript');
var sourcemaps = require('gulp-sourcemaps');
var uglify = require('gulp-uglify');
var Server = require('karma').Server;
var argv = require('yargs').argv;
var gulpif = require('gulp-if');
var tsProject = ts.createProject('tsconfig.json');
var tsProjectMini = ts.createProject('tsconfig.json', { outFile: "./dist/GameAnalytics.min.js" });
var tsProjectDebug = ts.createProject('tsconfig.json', { outFile: "./dist/GameAnalytics.debug.js" });
var tsDeclaration = ts.createProject('tsconfig.json');
var replace = require('gulp-replace');
var concat = require('gulp-concat');
var insert = require('gulp-insert');
gulp.task('build_debug', function() {
var tsResult = tsProjectDebug.src()
.pipe(sourcemaps.init())
.pipe(tsProjectDebug());
return tsResult.js
.pipe(sourcemaps.write())
.pipe(gulp.dest('.'));
});
gulp.task('declaration', function() {
var tsResult = tsDeclaration.src()
.pipe(tsDeclaration());
return tsResult.dts
.pipe(replace('declare module public_enums', 'export module gameanalytics'))
.pipe(replace('declare var GameAnalytics', 'declare var GameAnalyticsCommand'))
.pipe(insert.wrap("", "export declare var GameAnalytics: typeof gameanalytics.GameAnalytics;\n"))
.pipe(insert.wrap("", "export default GameAnalytics;\n"))
.pipe(gulp.dest('.'));
});
gulp.task('bundle_min_js', function() {
return gulp.src(['./vendor/hmac-sha256-min.js', './vendor/enc-base64-min.js'])
.pipe(concat('bundle.min.js'))
.pipe(gulp.dest('./vendor'));
});
gulp.task('bundle_js', function() {
return gulp.src(['./vendor/hmac-sha256.js', './vendor/enc-base64.js'])
.pipe(concat('bundle.js'))
.pipe(gulp.dest('./vendor'));
});
gulp.task('test', function (done) {
new Server({
configFile: __dirname + '/karma.conf.js',
singleRun: true
}, done).start();
});
gulp.task('build_mini', function() {
var tsResult = tsProjectMini.src()
.pipe(replace('GALogger.debugEnabled = true', 'GALogger.debugEnabled = false'))
.pipe(replace('GALogger.d(', '//GALogger.d('))
.pipe(gulpif(argv.nologging, replace('GALogger.', '//GALogger.')))
.pipe(gulpif(argv.nologging, replace('//GALOGGER_START', '/*GALOGGER_START')))
.pipe(gulpif(argv.nologging, replace('//GALOGGER_END', '//GALOGGER_END*/')))
.pipe(gulpif(argv.nologging, replace('import GALogger = gameanalytics.logging.GALogger', '//import GALogger = gameanalytics.logging.GALogger')))
.pipe(tsProjectMini());
return tsResult.js
.pipe(gulp.dest('.'));
});
gulp.task('build_normal', function() {
var tsResult = tsProject.src()
.pipe(replace('GALogger.debugEnabled = true', 'GALogger.debugEnabled = false'))
.pipe(replace('GALogger.d(', '//GALogger.d('))
.pipe(tsProject());
return tsResult.js
.pipe(gulp.dest('.'));
});
var mini = function() {
return gulp.src(['./vendor/bundle.min.js', './dist/GameAnalytics.min.js'])
.pipe(concat('GameAnalytics.min.js'))
.pipe(uglify())
.pipe(insert.wrap("(function(scope){\n", "\nscope.gameanalytics=gameanalytics;\nscope.GameAnalytics=GameAnalytics;\n})(this);\n"))
.pipe(gulp.dest('./dist'));
};
gulp.task('mini', gulp.series(gulp.parallel('bundle_min_js', 'build_mini'), mini));
var normal = function() {
return gulp.src(['./vendor/bundle.min.js', './dist/GameAnalytics.js'])
.pipe(concat('GameAnalytics.js'))
.pipe(uglify())
.pipe(insert.wrap("(function(scope){\n", "\nscope.gameanalytics=gameanalytics;\nscope.GameAnalytics=GameAnalytics;\n})(this);\n"))
.pipe(gulp.dest('./dist'));
};
gulp.task('normal', gulp.series(gulp.parallel('bundle_min_js', 'build_normal'), normal));
var unity = function() {
return gulp.src(['./vendor/bundle.js', './dist/GameAnalytics.js'])
.pipe(concat('GameAnalytics.jspre'))
.pipe(gulp.dest('./dist'));
};
gulp.task('unity', gulp.series(gulp.parallel('bundle_js', 'build_normal'), unity));
var ga_node = function() {
return gulp.src(['./vendor/bundle.js', './dist/GameAnalytics.js'])
.pipe(concat('GameAnalytics.node.js'))
.pipe(insert.wrap("'use strict';\n", "module.exports = gameanalytics;"))
.pipe(gulp.dest('./dist'));
};
gulp.task('ga_node', gulp.series(gulp.parallel('bundle_js', 'build_normal'), ga_node));
var construct = function () {
return gulp.src(['./vendor/bundle.js', './dist/GameAnalytics.js'])
.pipe(concat('GameAnalytics.construct.js'))
.pipe(insert.wrap("'use strict';\n", "globalThis.gameanalytics = gameanalytics;"))
.pipe(gulp.dest('./dist'));
};
gulp.task('construct', gulp.series(gulp.parallel('bundle_js', 'build_normal'), construct));
var debug = function() {
return gulp.src(['./vendor/bundle.min.js', './dist/GameAnalytics.debug.js'])
.pipe(concat('GameAnalytics.debug.js'))
.pipe(insert.wrap("(function(scope){\n", "\nscope.gameanalytics=gameanalytics;\nscope.GameAnalytics=GameAnalytics;\n})(this);\n"))
.pipe(gulp.dest('./dist'));
};
gulp.task('debug', gulp.series(gulp.parallel('bundle_min_js', 'build_debug'), debug));
// P2 — ESM output target
// Appends named ESM exports to the standard bundle so bundlers that understand
// the package.json "exports" → "import" field can consume it as an ES module.
// Not tree-shakeable (the namespace bundle stays monolithic) but enables
// `import GameAnalytics from 'gameanalytics'` and proper bundler resolution.
var esm = function() {
return gulp.src(['./vendor/bundle.min.js', './dist/GameAnalytics.js'])
.pipe(concat('GameAnalytics.esm.js'))
.pipe(insert.wrap("", "\nexport { gameanalytics };\nexport default GameAnalytics;\n"))
.pipe(gulp.dest('./dist'));
};
gulp.task('esm', gulp.series(gulp.parallel('bundle_min_js', 'build_normal'), esm));
gulp.task('default', gulp.series('debug', 'mini', 'unity', 'ga_node', 'construct', 'normal', 'esm', 'declaration'));