88 * @fileoverview Gulp script to build Blockly for Node & NPM.
99 */
1010
11- const gulp = require ( 'gulp' ) ;
12- gulp . replace = require ( 'gulp-replace' ) ;
13- gulp . rename = require ( 'gulp-rename' ) ;
14- gulp . sourcemaps = require ( 'gulp-sourcemaps' ) ;
11+ import * as gulp from 'gulp' ;
12+ import replace from 'gulp-replace' ;
13+ import rename from 'gulp-rename' ;
14+ import sourcemaps from 'gulp-sourcemaps' ;
1515
16- const path = require ( 'path' ) ;
17- const fs = require ( 'fs' ) ;
18- const fsPromises = require ( 'fs/promises' ) ;
19- const { exec, execSync} = require ( 'child_process' ) ;
16+ import * as path from 'path' ;
17+ import * as fs from 'fs' ;
18+ import * as fsPromises from 'fs/promises' ;
19+ import { exec , execSync } from 'child_process' ;
2020
21- const { globSync} = require ( 'glob' ) ;
22- const closureCompiler = require ( 'google-closure-compiler' ) . gulp ( ) ;
23- const argv = require ( 'yargs' ) . argv ;
24- const { rimraf} = require ( 'rimraf' ) ;
21+ import { globSync } from 'glob' ;
22+ // For v20250609.0.0 and later:
23+ // import {gulp as closureCompiler} from 'google-closure-compiler';
24+ import ClosureCompiler from 'google-closure-compiler' ;
25+ import yargs from 'yargs' ;
26+ import { hideBin } from 'yargs/helpers' ;
27+ import { rimraf } from 'rimraf' ;
2528
26- const { BUILD_DIR , LANG_BUILD_DIR , RELEASE_DIR , TSC_OUTPUT_DIR , TYPINGS_BUILD_DIR } = require ( './config' ) ;
27- const { getPackageJson} = require ( './helper_tasks' ) ;
29+ import { BUILD_DIR , LANG_BUILD_DIR , RELEASE_DIR , TSC_OUTPUT_DIR , TYPINGS_BUILD_DIR } from './config.mjs' ;
30+ import { getPackageJson } from './helper_tasks.mjs' ;
2831
29- const { posixPath, quote} = require ( '../helpers' ) ;
32+ import { posixPath , quote } from '../helpers.js' ;
33+
34+ const closureCompiler = ClosureCompiler . gulp ( ) ;
35+
36+ const argv = yargs ( hideBin ( process . argv ) ) . parse ( ) ;
3037
3138////////////////////////////////////////////////////////////
3239// Build //
@@ -182,7 +189,7 @@ function stripApacheLicense() {
182189 // Closure Compiler preserves dozens of Apache licences in the Blockly code.
183190 // Remove these if they belong to Google or MIT.
184191 // MIT's permission to do this is logged in Blockly issue #2412.
185- return gulp . replace ( new RegExp ( licenseRegex , 'g' ) , '\n\n\n\n' ) ;
192+ return replace ( new RegExp ( licenseRegex , 'g' ) , '\n\n\n\n' ) ;
186193 // Replace with the same number of lines so that source-maps are not affected.
187194}
188195
@@ -306,7 +313,7 @@ const JSCOMP_OFF = [
306313 * Builds Blockly as a JS program, by running tsc on all the files in
307314 * the core directory.
308315 */
309- function buildJavaScript ( done ) {
316+ export function tsc ( done ) {
310317 execSync (
311318 `tsc -outDir "${ TSC_OUTPUT_DIR } " -declarationDir "${ TYPINGS_BUILD_DIR } "` ,
312319 { stdio : 'inherit' } ) ;
@@ -318,7 +325,7 @@ function buildJavaScript(done) {
318325 * This task regenerates msg/json/en.js and msg/json/qqq.js from
319326 * msg/messages.js.
320327 */
321- function generateMessages ( done ) {
328+ export function messages ( done ) {
322329 // Run js_to_json.py
323330 const jsToJsonCmd = `${ PYTHON } scripts/i18n/js_to_json.py \
324331 --input_file ${ path . join ( 'msg' , 'messages.js' ) } \
@@ -573,10 +580,10 @@ function buildCompiled() {
573580 // Fire up compilation pipline.
574581 return gulp . src ( chunkOptions . js , { base : './' } )
575582 . pipe ( stripApacheLicense ( ) )
576- . pipe ( gulp . sourcemaps . init ( ) )
583+ . pipe ( sourcemaps . init ( ) )
577584 . pipe ( compile ( options ) )
578- . pipe ( gulp . rename ( { suffix : COMPILED_SUFFIX } ) )
579- . pipe ( gulp . sourcemaps . write ( '.' ) )
585+ . pipe ( rename ( { suffix : COMPILED_SUFFIX } ) )
586+ . pipe ( sourcemaps . write ( '.' ) )
580587 . pipe ( gulp . dest ( RELEASE_DIR ) ) ;
581588}
582589
@@ -668,7 +675,7 @@ async function buildLangfileShims() {
668675 // (We have to do it this way because messages.js is a script and
669676 // not a CJS module with exports.)
670677 globalThis . Blockly = { Msg : { } } ;
671- require ( '../../msg/messages.js' ) ;
678+ await import ( '../../msg/messages.js' ) ;
672679 const exportedNames = Object . keys ( globalThis . Blockly . Msg ) ;
673680 delete globalThis . Blockly ;
674681
@@ -689,12 +696,14 @@ ${exportedNames.map((name) => ` ${name},`).join('\n')}
689696}
690697
691698/**
692- * This task builds Blockly core, blocks and generators together and uses
693- * Closure Compiler's ADVANCED_COMPILATION mode.
699+ * This task uses Closure Compiler's ADVANCED_COMPILATION mode to
700+ * compile together Blockly core, blocks and generators with a simple
701+ * test app; the purpose is to verify that Blockly is compatible with
702+ * the ADVANCED_COMPILATION mode.
694703 *
695704 * Prerequisite: buildJavaScript.
696705 */
697- function buildAdvancedCompilationTest ( ) {
706+ function compileAdvancedCompilationTest ( ) {
698707 // If main_compressed.js exists (from a previous run) delete it so that
699708 // a later browser-based test won't check it should the compile fail.
700709 try {
@@ -718,17 +727,17 @@ function buildAdvancedCompilationTest() {
718727 } ;
719728 return gulp . src ( srcs , { base : './' } )
720729 . pipe ( stripApacheLicense ( ) )
721- . pipe ( gulp . sourcemaps . init ( ) )
730+ . pipe ( sourcemaps . init ( ) )
722731 . pipe ( compile ( options ) )
723- . pipe ( gulp . sourcemaps . write (
732+ . pipe ( sourcemaps . write (
724733 '.' , { includeContent : false , sourceRoot : '../../' } ) )
725734 . pipe ( gulp . dest ( './tests/compile/' ) ) ;
726735}
727736
728737/**
729738 * This task cleans the build directory (by deleting it).
730739 */
731- function cleanBuildDir ( ) {
740+ export function cleanBuildDir ( ) {
732741 // Sanity check.
733742 if ( BUILD_DIR === '.' || BUILD_DIR === '/' ) {
734743 return Promise . reject ( `Refusing to rm -rf ${ BUILD_DIR } ` ) ;
@@ -737,16 +746,13 @@ function cleanBuildDir() {
737746}
738747
739748// Main sequence targets. Each should invoke any immediate prerequisite(s).
740- exports . cleanBuildDir = cleanBuildDir ;
741- exports . langfiles = gulp . parallel ( buildLangfiles , buildLangfileShims ) ;
742- exports . tsc = buildJavaScript ;
743- exports . minify = gulp . series ( exports . tsc , buildCompiled , buildShims ) ;
744- exports . build = gulp . parallel ( exports . minify , exports . langfiles ) ;
749+ // function cleanBuildDir, above
750+ export const langfiles = gulp . parallel ( buildLangfiles , buildLangfileShims ) ;
751+ export const minify = gulp . series ( tsc , buildCompiled , buildShims ) ;
752+ // function tsc, above
753+ export const build = gulp . parallel ( minify , langfiles ) ;
745754
746755// Manually-invokable targets, with prerequisites where required.
747- exports . messages = generateMessages ; // Generate msg/json/en.json et al.
748- exports . buildAdvancedCompilationTest =
749- gulp . series ( exports . tsc , buildAdvancedCompilationTest ) ;
750-
751- // Targets intended only for invocation by scripts; may omit prerequisites.
752- exports . onlyBuildAdvancedCompilationTest = buildAdvancedCompilationTest ;
756+ // function messages, above
757+ export const buildAdvancedCompilationTest =
758+ gulp . series ( tsc , compileAdvancedCompilationTest ) ;
0 commit comments