forked from google/blockly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
1018 lines (941 loc) · 30.4 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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @license
* Copyright 2018 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Gulp script to build Blockly for Node & NPM.
* Run this script by calling "npm install" in this directory.
*/
var gulp = require('gulp');
gulp.shell = require('gulp-shell');
gulp.concat = require('gulp-concat');
gulp.replace = require('gulp-replace');
gulp.rename = require('gulp-rename');
gulp.insert = require('gulp-insert');
gulp.umd = require('gulp-umd');
var path = require('path');
var fs = require('fs');
var rimraf = require('rimraf');
var execSync = require('child_process').execSync;
var through2 = require('through2');
var closureCompiler = require('google-closure-compiler').gulp();
var closureDeps = require('google-closure-deps');
var packageJson = require('./package.json');
var argv = require('yargs').argv;
const upstream_url = "https://github.com/google/blockly.git";
////////////////////////////////////////////////////////////
// Build //
////////////////////////////////////////////////////////////
const licenseRegex = `\\/\\*\\*
\\* @license
\\* (Copyright \\d+ (Google LLC|Massachusetts Institute of Technology))
( \\* All rights reserved.
)? \\* SPDX-License-Identifier: Apache-2.0
\\*\\/`;
/**
* Helper method for stripping the Google's and MIT's Apache Licenses.
*/
function stripApacheLicense() {
// Strip out Google's and MIT's Apache licences.
// Closure Compiler preserves dozens of Apache licences in the Blockly code.
// Remove these if they belong to Google or MIT.
// MIT's permission to do this is logged in Blockly issue #2412.
return gulp.replace(new RegExp(licenseRegex, "g"), '');
}
/**
* Helper method for prepending the auto-generated header text.
*/
function prependHeader() {
return gulp.insert.prepend(`// Do not edit this file; automatically generated by gulp.\n`);
}
/**
* Closure compiler warning groups used to treat warnings as errors.
* For a full list of closure compiler groups, consult:
* https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/DiagnosticGroups.java#L113
*/
var JSCOMP_ERROR = [
'accessControls',
'checkPrototypalTypes',
'checkRegExp',
'checkTypes',
'checkVars',
'conformanceViolations',
'const',
'constantProperty',
'deprecated',
'deprecatedAnnotations',
'duplicateMessage',
'es5Strict',
'externsValidation',
'functionParams',
'globalThis',
'invalidCasts',
'misplacedTypeAnnotation',
'missingGetCssName',
// 'missingOverride',
'missingPolyfill',
'missingProperties',
'missingProvide',
'missingRequire',
'missingReturn',
// 'missingSourcesWarnings',
'moduleLoad',
'msgDescriptions',
'nonStandardJsDocs',
// 'polymer',
// 'reportUnknownTypes',
// 'strictCheckTypes',
// 'strictMissingProperties',
'strictModuleDepCheck',
// 'strictPrimitiveOperators',
'suspiciousCode',
'typeInvalidation',
'undefinedNames',
'undefinedVars',
'underscore',
'unknownDefines',
'unusedLocalVariables',
// 'unusedPrivateMembers',
'useOfGoogBase',
'uselessCode',
'untranspilableFeatures',
'visibility'
];
/**
* Helper method for calling the Closure compiler.
* @param {*} compilerOptions
* @param {boolean=} opt_verbose Optional option for verbose logging
* @param {boolean=} opt_warnings_as_error Optional option for treating warnings
* as errors.
*/
function compile(compilerOptions, opt_verbose, opt_warnings_as_error) {
compilerOptions = compilerOptions || {};
compilerOptions.compilation_level = 'SIMPLE_OPTIMIZATIONS';
compilerOptions.warning_level = opt_verbose ? 'VERBOSE' : 'DEFAULT';
compilerOptions.language_in =
compilerOptions.language_in || 'ECMASCRIPT6_STRICT';
compilerOptions.language_out = 'ECMASCRIPT5_STRICT';
compilerOptions.rewrite_polyfills = false;
compilerOptions.hide_warnings_for = 'node_modules';
if (opt_warnings_as_error) {
compilerOptions.jscomp_error = JSCOMP_ERROR;
}
const platform = ['native', 'java', 'javascript'];
return closureCompiler(compilerOptions, { platform });
}
/**
* Helper method for possibly adding the Closure library into a sources array.
* @param {Array.<string>} srcs
*/
function maybeAddClosureLibrary(srcs) {
if (argv.closureLibrary) {
// If you require Google's Closure library, you can include it in your
// build by adding the --closure-library flag.
// You will also need to include the "google-closure-library" in your list
// of devDependencies.
console.log('Including the google-closure-library in your build.');
if (!fs.existsSync('./node_modules/google-closure-library')) {
throw Error('You must add the google-closure-library to your ' +
'devDependencies in package.json, and run `npm install`.');
}
srcs.push('./node_modules/google-closure-library/closure/goog/**/**/*.js');
}
return srcs;
}
/**
* This task builds Blockly's core files.
* blockly_compressed.js
*/
function buildCompressed(cb) {
const defines = 'Blockly.VERSION="' + packageJson.version + '"';
return gulp.src(maybeAddClosureLibrary(['core/**/**/*.js']), {base: './'})
// Directories in Blockly are used to group similar files together
// but are not used to limit access with @package, instead the
// method means something is internal to Blockly and not a public
// API.
// Flatten all files so they're in the same directory, but ensure that
// files with the same name don't conflict.
.pipe(gulp.rename(function (p) {
var dirname = p.dirname.replace(new RegExp(path.sep, "g"), "-");
p.dirname = "";
p.basename = dirname + "-" + p.basename;
}))
.pipe(stripApacheLicense())
.pipe(compile({
dependency_mode: 'PRUNE',
entry_point: './core-requires.js',
js_output_file: 'blockly_compressed.js',
externs: ['./externs/svg-externs.js', './externs/goog-externs.js'],
define: defines,
language_in:
argv.closureLibrary ? 'ECMASCRIPT_2015' : 'ECMASCRIPT6_STRICT'
}, argv.verbose, argv.strict))
.pipe(prependHeader())
.pipe(gulp.dest('./'));
};
/**
* This task builds the Blockly's built in blocks.
* blocks_compressed.js
*/
function buildBlocks() {
// Add provides used throughout blocks/ in order to be compatible with the
// compiler. Anything added to this list must be removed from the compiled
// result using the remove regex steps below.
const provides = `
goog.provide('Blockly');
goog.provide('Blockly.Blocks');
goog.provide('Blockly.Comment');
goog.provide('Blockly.FieldCheckbox');
goog.provide('Blockly.FieldColour');
goog.provide('Blockly.FieldDropdown');
goog.provide('Blockly.FieldImage');
goog.provide('Blockly.FieldLabel');
goog.provide('Blockly.FieldMultilineInput');
goog.provide('Blockly.FieldNumber');
goog.provide('Blockly.FieldTextInput');
goog.provide('Blockly.FieldVariable');
goog.provide('Blockly.Mutator');
goog.provide('Blockly.Warning');`;
return gulp.src(maybeAddClosureLibrary(['blocks/*.js']), {base: './'})
// Add Blockly.Blocks to be compatible with the compiler.
.pipe(gulp.replace(`goog.provide('Blockly.Constants.Colour');`,
`${provides}goog.provide('Blockly.Constants.Colour');`))
.pipe(stripApacheLicense())
.pipe(compile({
dependency_mode: 'NONE',
externs: ['./externs/goog-externs.js'],
js_output_file: 'blocks_compressed.js'
}, argv.verbose, argv.strict))
.pipe(gulp.replace('\'use strict\';', '\'use strict\';\n\n\n'))
// Remove Blockly.Blocks to be compatible with Blockly.
.pipe(gulp.replace(/var Blockly=\{[^;]*\};\n?/, ''))
// Remove Blockly Fields to be compatible with Blockly.
.pipe(gulp.replace(/Blockly\.Field[^=\(]+=\{[^;]*\};/g, ''))
// Remove Blockly Warning, Comment & Mutator to be compatible with Blockly.
.pipe(gulp.replace(/Blockly\.(Comment|Warning|Mutator)=\{[^;]*\};/g, ''))
.pipe(prependHeader())
.pipe(gulp.dest('./'));
};
/**
* A helper method for building a Blockly code generator.
* @param {string} language Generator language.
* @param {string} namespace Language namespace.
*/
function buildGenerator(language, namespace) {
var provides = `
goog.provide('Blockly.Generator');
goog.provide('Blockly.utils.global');
goog.provide('Blockly.utils.string');`;
return gulp.src([`generators/${language}.js`, `generators/${language}/*.js`], {base: './'})
.pipe(stripApacheLicense())
// Add Blockly.Generator and Blockly.utils.string to be compatible with the compiler.
.pipe(gulp.replace(`goog.provide('Blockly.${namespace}');`,
`${provides}goog.provide('Blockly.${namespace}');`))
.pipe(compile({
dependency_mode: 'NONE',
externs: ['./externs/goog-externs.js'],
js_output_file: `${language}_compressed.js`
}, argv.verbose, argv.strict))
.pipe(gulp.replace('\'use strict\';', '\'use strict\';\n\n\n'))
// Remove Blockly.Generator and Blockly.utils.string to be compatible with Blockly.
.pipe(gulp.replace(/var Blockly=\{[^;]*\};\s*Blockly.utils.global={};\s*Blockly.utils.string={};\n?/, ''))
.pipe(prependHeader())
.pipe(gulp.dest('./'));
};
/**
* This task builds the javascript generator.
* javascript_compressed.js
*/
function buildJavascript() {
return buildGenerator('javascript', 'JavaScript');
};
/**
* This task builds the python generator.
* python_compressed.js
*/
function buildPython() {
return buildGenerator('python', 'Python');
};
/**
* This task builds the php generator.
* php_compressed.js
*/
function buildPHP() {
return buildGenerator('php', 'PHP');
};
/**
* This task builds the lua generator.
* lua_compressed.js
*/
function buildLua() {
return buildGenerator('lua', 'Lua');
};
/**
* This task builds the dart generator:
* dart_compressed.js
*/
function buildDart() {
return buildGenerator('dart', 'Dart');
};
/**
* This tasks builds all the generators:
* javascript_compressed.js
* python_compressed.js
* php_compressed.js
* lua_compressed.js
* dart_compressed.js
*/
const buildGenerators = gulp.parallel(
buildJavascript,
buildPython,
buildPHP,
buildLua,
buildDart
);
/**
* This task builds Blockly's uncompressed file.
* blockly_uncompressed.js
*/
function buildUncompressed() {
const closurePath = argv.closureLibrary ?
'node_modules/google-closure-library/closure/goog' :
'closure/goog';
const header = `// Do not edit this file; automatically generated by gulp.
'use strict';
this.IS_NODE_JS = !!(typeof module !== 'undefined' && module.exports);
this.BLOCKLY_DIR = (function(root) {
if (!root.IS_NODE_JS) {
// Find name of current directory.
var scripts = document.getElementsByTagName('script');
var re = new RegExp('(.+)[\\\/]blockly_(.*)uncompressed\\\.js$');
for (var i = 0, script; script = scripts[i]; i++) {
var match = re.exec(script.src);
if (match) {
return match[1];
}
}
alert('Could not detect Blockly\\'s directory name.');
}
return '';
})(this);
this.BLOCKLY_BOOT = function(root) {
// Execute after Closure has loaded.
`;
const footer = `
delete root.BLOCKLY_DIR;
delete root.BLOCKLY_BOOT;
delete root.IS_NODE_JS;
};
if (this.IS_NODE_JS) {
this.BLOCKLY_BOOT(this);
module.exports = Blockly;
} else {
document.write('<script src="' + this.BLOCKLY_DIR +
'/${closurePath}/base.js"></script>');
document.write('<script>this.BLOCKLY_BOOT(this);</script>');
}
`;
let deps = [];
return gulp.src(maybeAddClosureLibrary(['core/**/**/*.js']))
.pipe(through2.obj((file, _enc, cb) => {
const result = closureDeps.parser.parseFile(file.path);
for (const dep of result.dependencies) {
deps.push(dep);
}
cb(null);
}))
.on('end', () => {
// Update the path to closure for any files that we don't know the full path
// of (parsed from a goog.addDependency call).
for (const dep of deps) {
dep.setClosurePath(closurePath);
}
const addDependency = closureDeps.depFile.getDepFileText(closurePath, deps);
const requires = `goog.addDependency("base.js", [], []);
// Load Blockly.
goog.require('Blockly.requires')
`;
fs.writeFileSync('blockly_uncompressed.js',
header +
addDependency +
requires +
footer);
});
};
/**
* This task builds Blockly's lang files.
* msg/*.js
*/
function buildLangfiles(done) {
// Run js_to_json.py
const jsToJsonCmd = `python ./i18n/js_to_json.py \
--input_file ${path.join('msg', 'messages.js')} \
--output_dir ${path.join('msg', 'json')} \
--quiet`;
execSync(jsToJsonCmd, { stdio: 'inherit' });
// Run create_messages.py
let json_files = fs.readdirSync(path.join('msg', 'json'));
json_files = json_files.filter(file => file.endsWith('json') &&
!(new RegExp(/(keys|synonyms|qqq|constants)\.json$/).test(file)));
json_files = json_files.map(file => path.join('msg', 'json', file));
const createMessagesCmd = `python ./i18n/create_messages.py \
--source_lang_file ${path.join('msg', 'json', 'en.json')} \
--source_synonym_file ${path.join('msg', 'json', 'synonyms.json')} \
--source_constants_file ${path.join('msg', 'json', 'constants.json')} \
--key_file ${path.join('msg', 'json', 'keys.json')} \
--output_dir ${path.join('msg', 'js')} \
--quiet ${json_files.join(' ')}`;
execSync(createMessagesCmd, { stdio: 'inherit' });
done();
};
/**
* This tasks builds Blockly's core files:
* blockly_compressed.js
* blocks_compressed.js
* blockly_uncompressed.js
*/
const buildCore = gulp.parallel(
buildCompressed,
buildBlocks,
buildUncompressed
);
/**
* This task builds all of Blockly:
* blockly_compressed.js
* blocks_compressed.js
* javascript_compressed.js
* python_compressed.js
* php_compressed.js
* lua_compressed.js
* dart_compressed.js
* blockly_uncompressed.js
* msg/json/*.js
*/
const build = gulp.parallel(
buildCore,
buildGenerators,
buildLangfiles
);
////////////////////////////////////////////////////////////
// Typings //
////////////////////////////////////////////////////////////
// Generates the TypeScript definition file (d.ts) for Blockly.
// As well as generating the typings of each of the files under core/ and msg/,
// the script also pulls in a number of part files from typings/parts.
// This includes the header (incl License), additional useful interfaces
// including Blockly Options and Google Closure typings.
function typings() {
const tmpDir = './typings/tmp';
const blocklySrcs = [
"core/",
"core/components",
"core/components/tree",
"core/components/menu",
"core/keyboard_nav",
"core/renderers/common",
"core/renderers/measurables",
"core/theme",
"core/utils",
"msg/"
];
// Clean directory if exists.
if (fs.existsSync(tmpDir)) {
rimraf.sync(tmpDir);
}
fs.mkdirSync(tmpDir);
// Find all files that will be included in the typings file.
let files = [];
blocklySrcs.forEach((src) => {
files = files.concat(fs.readdirSync(src)
.filter(fn => fn.endsWith('.js'))
.map(fn => path.join(src, fn)));
});
// Generate typings file for each file.
files.forEach((file) => {
const typescriptFileName = `${path.join(tmpDir, file)}.d.ts`;
if (file.indexOf('core/msg.js') > -1) {
return;
}
const cmd = `node ./node_modules/typescript-closure-tools/definition-generator/src/main.js ${file} ${typescriptFileName}`;
console.log(`Generating typings for ${file}`);
execSync(cmd, { stdio: 'inherit' });
});
const srcs = [
'typings/parts/blockly-header.d.ts',
'typings/parts/blockly-interfaces.d.ts',
`${tmpDir}/core/**`,
`${tmpDir}/core/components/**`,
`${tmpDir}/core/components/tree/**`,
`${tmpDir}/core/components/menu/**`,
`${tmpDir}/core/keyboard_nav/**`,
`${tmpDir}/core/renderers/common/**`,
`${tmpDir}/core/renderers/measurables/**`,
`${tmpDir}/core/utils/**`,
`${tmpDir}/core/theme/**`,
`${tmpDir}/msg/**`
];
return gulp.src(srcs)
.pipe(gulp.concat('blockly.d.ts'))
.pipe(gulp.dest('typings'))
.on('end', function () {
// Clean up tmp directory.
if (fs.existsSync(tmpDir)) {
rimraf.sync(tmpDir);
}
});
};
////////////////////////////////////////////////////////////
// NPM packaging tasks //
////////////////////////////////////////////////////////////
// The destination path where all the NPM distribution files will go.
const packageDistribution = './dist';
/**
* A helper method for wrapping a file into a Universal Module Definition.
* @param {string} namespace The export namespace.
* @param {Array<Object>} dependencies An array of dependencies to inject.
*/
function packageUMD(namespace, dependencies) {
return gulp.umd({
dependencies: function () { return dependencies; },
namespace: function () { return namespace; },
exports: function () { return namespace; },
template: path.join(__dirname, 'package/templates/umd.template')
});
};
/**
* A helper method for wrapping a file into a CommonJS module for Node.js.
* @param {string} namespace The export namespace.
* @param {Array<Object>} dependencies An array of dependencies to inject.
*/
function packageCommonJS(namespace, dependencies) {
return gulp.umd({
dependencies: function () { return dependencies; },
namespace: function () { return namespace; },
exports: function () { return namespace; },
template: path.join(__dirname, 'package/templates/node.template')
});
};
/**
* This task wraps blockly_compressed.js into a UMD module.
* @example import 'blockly/blockly';
*/
function packageBlockly() {
return gulp.src('blockly_compressed.js')
.pipe(packageUMD('Blockly', []))
.pipe(gulp.rename('blockly.js'))
.pipe(gulp.dest(packageDistribution));
};
/**
* This task wraps blocks_compressed.js into a CommonJS module for Node.js.
* This is an equivelant task to package-blockly but for Node.js.
* @example import 'blockly/blockly-node';
*/
function packageBlocklyNode() {
// Override textToDomDocument, providing a Node.js alternative to DOMParser.
return gulp.src('blockly_compressed.js')
.pipe(gulp.insert.append(`
if (typeof DOMParser !== 'function') {
var DOMParser = require("jsdom/lib/jsdom/living").DOMParser;
var XMLSerializer = require("jsdom/lib/jsdom/living").XMLSerializer;
var doc = Blockly.utils.xml.textToDomDocument(
'<xml xmlns="https://developers.google.com/blockly/xml"></xml>');
Blockly.utils.xml.document = function() {
return doc;
};
}`))
.pipe(packageCommonJS('Blockly', []))
.pipe(gulp.rename('blockly-node.js'))
.pipe(gulp.dest(packageDistribution));
};
/**
* This task wraps blocks_compressed.js into a UMD module.
* @example import 'blockly/blocks';
*/
function packageBlocks() {
return gulp.src('blocks_compressed.js')
.pipe(gulp.insert.prepend(`
Blockly.Blocks={};`))
.pipe(packageUMD('Blockly.Blocks', [{
name: 'Blockly',
amd: './core',
cjs: './core',
}]))
.pipe(gulp.rename('blocks.js'))
.pipe(gulp.dest(packageDistribution));
};
/**
* This task wraps package/index.js into a UMD module.
* We implicitly require the Node entry point in CommonJS environments,
* and the Browser entry point for AMD environments.
* @example import * as Blockly from 'blockly';
*/
function packageIndex() {
return gulp.src('package/index.js')
.pipe(packageUMD('Blockly', [{
name: 'Blockly',
amd: './browser',
cjs: './node',
}]))
.pipe(gulp.rename('index.js'))
.pipe(gulp.dest(packageDistribution));
};
/**
* This task wraps package/browser/index.js into a UMD module.
* By default, the module includes Blockly core and built-in blocks,
* as well as the JavaScript code generator and the English block
* localization files.
* This module is configured (in package.json) to replaces the module
* built by package-node in browser environments.
* @example import * as Blockly from 'blockly/browser';
*/
function packageBrowser() {
return gulp.src('package/browser/index.js')
.pipe(packageUMD('Blockly', [{
name: 'Blockly',
amd: './core-browser',
cjs: './core-browser',
},{
name: 'En',
amd: './msg/en',
cjs: './msg/en',
},{
name: 'BlocklyBlocks',
amd: './blocks',
cjs: './blocks',
},{
name: 'BlocklyJS',
amd: './javascript',
cjs: './javascript',
}]))
.pipe(gulp.rename('browser.js'))
.pipe(gulp.dest(packageDistribution));
};
/**
* This task wraps package/browser/core.js into a UMD module.
* By default, the module includes the Blockly core package and a
* helper method to set the locale.
* This module is configured (in package.json) to replaces the module
* built by package-node-core in browser environments.
* @example import * as Blockly from 'blockly/core';
*/
function packageCore() {
return gulp.src('package/browser/core.js')
.pipe(packageUMD('Blockly', [{
name: 'Blockly',
amd: './blockly',
cjs: './blockly',
}]))
.pipe(gulp.rename('core-browser.js'))
.pipe(gulp.dest(packageDistribution));
};
/**
* This task wraps package/node/index.js into a CommonJS module for Node.js.
* By default, the module includes Blockly core and built-in blocks,
* as well as all the code generators and the English block localization files.
* This module is configured (in package.json) to be replaced by the module
* built by package-browser in browser environments.
* @example import * as Blockly from 'blockly/node';
*/
function packageNode() {
return gulp.src('package/node/index.js')
.pipe(packageCommonJS('Blockly', [{
name: 'Blockly',
cjs: './core',
},{
name: 'En',
cjs: './msg/en',
},{
name: 'BlocklyBlocks',
cjs: './blocks',
},{
name: 'BlocklyJS',
cjs: './javascript',
},{
name: 'BlocklyPython',
cjs: './python',
},{
name: 'BlocklyPHP',
cjs: './php',
},{
name: 'BlocklyLua',
cjs: './lua',
}, {
name: 'BlocklyDart',
cjs: './dart',
}]))
.pipe(gulp.rename('node.js'))
.pipe(gulp.dest(packageDistribution));
};
/**
* This task wraps package/node/core.js into a CommonJS module for Node.js.
* By default, the module includes the Blockly core package for Node.js
* and a helper method to set the locale.
* This module is configured (in package.json) to be replaced by the module
* built by package-core in browser environments.
* @example import * as Blockly from 'blockly/core';
*/
function packageNodeCore() {
return gulp.src('package/node/core.js')
.pipe(packageCommonJS('Blockly', [{
name: 'Blockly',
amd: './blockly-node',
cjs: './blockly-node',
}]))
.pipe(gulp.rename('core.js'))
.pipe(gulp.dest(packageDistribution));
};
/**
* A helper method for packaging a Blockly code generator into a UMD module.
* @param {string} file Source file name.
* @param {string} rename Destination file name.
* @param {string} generator Generator export namespace.
*/
function packageGenerator(file, rename, generator) {
return gulp.src(file)
.pipe(packageUMD(generator, [{
name: 'Blockly',
amd: './core',
cjs: './core',
}]))
.pipe(gulp.rename(rename))
.pipe(gulp.dest(packageDistribution));
};
/**
* This task wraps javascript_compressed.js into a UMD module.
* @example import 'blockly/javascript';
*/
function packageJavascript() {
return packageGenerator('javascript_compressed.js', 'javascript.js', 'Blockly.JavaScript');
};
/**
* This task wraps python_compressed.js into a UMD module.
* @example import 'blockly/python';
*/
function packagePython() {
return packageGenerator('python_compressed.js', 'python.js', 'Blockly.Python');
};
/**
* This task wraps lua_compressed.js into a UMD module.
* @example import 'blockly/lua';
*/
function packageLua() {
return packageGenerator('lua_compressed.js', 'lua.js', 'Blockly.Lua');
};
/**
* This task wraps dart_compressed.js into a UMD module.
* @example import 'blockly/dart';
*/
function packageDart() {
return packageGenerator('dart_compressed.js', 'dart.js', 'Blockly.Dart');
};
/**
* This task wraps php_compressed.js into a UMD module.
* @example import 'blockly/php';
*/
function packagePHP() {
return packageGenerator('php_compressed.js', 'php.js', 'Blockly.PHP');
};
/**
* This task wraps each of the msg/js/* files into a UMD module.
* @example import * as En from 'blockly/msg/en';
*/
function packageLocales() {
// Remove references to goog.provide and goog.require.
return gulp.src('msg/js/*.js')
.pipe(gulp.replace(/goog\.[^\n]+/g, ''))
.pipe(gulp.insert.prepend(`
var Blockly = {};Blockly.Msg={};`))
.pipe(packageUMD('Blockly.Msg', [{
name: 'Blockly',
amd: '../core',
cjs: '../core',
}]))
.pipe(gulp.dest(`${packageDistribution}/msg`));
};
/**
* This task creates a UMD bundle of Blockly which includes the Blockly
* core files, the built-in blocks, the JavaScript code generator and the
* English localization files.
* @example <script src="https://unpkg.com/blockly/blockly.min.js"></script>
*/
function packageUMDBundle() {
var srcs = [
'blockly_compressed.js',
'msg/js/en.js',
'blocks_compressed.js',
'javascript_compressed.js'
];
return gulp.src(srcs)
.pipe(gulp.concat('blockly.min.js'))
.pipe(packageUMD('Blockly', []))
.pipe(gulp.dest(`${packageDistribution}`))
};
/**
* This task copies all the media/* files into the distribution directory.
*/
function packageMedia() {
return gulp.src('./media/*')
.pipe(gulp.dest(`${packageDistribution}/media`));
};
/**
* This task copies the package.json file into the distribution directory.
*/
function packageJSON(cb) {
const json = Object.assign({}, packageJson);
delete json['scripts'];
if (!fs.existsSync(packageDistribution)) {
fs.mkdirSync(packageDistribution);
}
fs.writeFileSync(`${packageDistribution}/package.json`,
JSON.stringify(json, null, 2));
cb();
};
/**
* This task copies the package/README.md file into the distribution directory.
* This file is what developers will see at https://www.npmjs.com/package/blockly.
*/
function packageReadme() {
return gulp.src('./package/README.md')
.pipe(gulp.dest(`${packageDistribution}`));
};
/**
* This task copies the typings/blockly.d.ts TypeScript definition file into the
* distribution directory.
* The bundled declaration file is referenced in package.json in the types property.
*/
function packageDTS() {
return gulp.src('./typings/blockly.d.ts')
.pipe(gulp.dest(`${packageDistribution}`));
};
/**
* This task prepares the NPM distribution files under the /dist directory.
*/
const package = gulp.parallel(
packageIndex,
packageBrowser,
packageNode,
packageCore,
packageNodeCore,
packageBlockly,
packageBlocklyNode,
packageBlocks,
packageJavascript,
packagePython,
packageLua,
packageDart,
packagePHP,
packageLocales,
packageMedia,
packageUMDBundle,
packageJSON,
packageReadme,
packageDTS
);
// Stash current state, check out the named branch, and sync with
// google/blockly.
function syncBranch(branchName) {
return function(done) {
execSync('git stash save -m "Stash for sync"', { stdio: 'inherit' });
execSync('git checkout ' + branchName, { stdio: 'inherit' });
execSync('git pull ' + upstream_url + ' ' + branchName,
{ stdio: 'inherit' });
execSync('git push origin ' + branchName, { stdio: 'inherit' });
done();
}
}
// Stash current state, check out develop, and sync with google/blockly.
function syncDevelop() {
return syncBranch('develop');
};
// Stash current state, check out master, and sync with google/blockly.
function syncMaster() {
return syncBranch('master');
};
// Helper function: get a name for a rebuild branch. Format: rebuild_mm_dd_yyyy.
function getRebuildBranchName() {
var date = new Date();
var mm = date.getMonth() + 1; // Month, 0-11
var dd = date.getDate(); // Day of the month, 1-31
var yyyy = date.getFullYear();
return 'rebuild_' + mm + '_' + dd + '_' + yyyy;
};
// Helper function: get a name for a rebuild branch. Format: rebuild_yyyy_mm.
function getRCBranchName() {
var date = new Date();
var mm = date.getMonth() + 1; // Month, 0-11
var yyyy = date.getFullYear();
return 'rc_' + yyyy + '_' + mm;
};
// Recompile and push to origin.
const recompile = gulp.series(
syncDevelop,
function(done) {
var branchName = getRebuildBranchName();
console.log('make-rebuild-branch: creating branch ' + branchName);
execSync('git checkout -b ' + branchName, { stdio: 'inherit' });
done();
},
build,
typings,
function(done) {
console.log('push-rebuild-branch: committing rebuild');
execSync('git commit -am "Rebuild"', { stdio: 'inherit' });
var branchName = getRebuildBranchName();
execSync('git push origin ' + branchName, { stdio: 'inherit' });
console.log('Branch ' + branchName + ' pushed to GitHub.');
console.log('Next step: create a pull request against develop.');
done();
}
);
// Create and push an RC branch.
// Note that this pushes to google/blockly.
const createRC = gulp.series(
syncDevelop,
function(done) {
var branchName = getRCBranchName();
execSync('git checkout -b ' + branchName, { stdio: 'inherit' });
execSync('git push ' + upstream_url + ' ' + branchName,
{ stdio: 'inherit' });
execSync('git checkout -b gh-pages');
execSync('git push ' + upstream_url + ' gh-pages');
done();
},
);
// See https://docs.npmjs.com/cli/version.
const preversion = gulp.series(
syncMaster,
function(done) {
// Create a branch named bump_version for the bump and rebuild.
execSync('git checkout -b bump_version', { stdio: 'inherit' });
done();
},
);
// See https://docs.npmjs.com/cli/version
function postversion(done) {
// Push both the branch and tag to google/blockly.
execSync('git push ' + upstream_url + ' bump_version',
{ stdio: 'inherit' });
var tagName = 'v' + packageJson.version;
execSync('git push ' + upstream_url + ' ' + tagName,
{ stdio: 'inherit' });
done();
};