Skip to content

Commit 9424deb

Browse files
authored
build: Refactor gulpfiles from CJS to ESM (#9149)
* refactor(build): Rename "package" gulp task (but not npm script) to "pack" This is to avoid an issue due to "package" being a reserved word in JavaScript, and therefore not a valid export identifier. * refactor(build): Convert gulpfile.js from CJS to ESM. * refactor(build): Convert scripts/gulpfiles/*.js from CJS to ESM * fix(build): Fix eslint warning for @license tag in gulpfile.mjs * chore(build): Remove unused imports * fix(build): Fix incorrect import of gulp-gzip * fix(build): Fix incorrect sourcemaps import reference
1 parent 6a04d0e commit 9424deb

14 files changed

+251
-238
lines changed

eslint.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ export default [
184184
files: [
185185
'eslint.config.mjs',
186186
'.prettierrc.js',
187-
'gulpfile.js',
187+
'gulpfile.mjs',
188188
'scripts/helpers.js',
189189
'tests/mocha/.mocharc.js',
190190
'tests/migration/validate-renamings.mjs',

gulpfile.js

Lines changed: 0 additions & 54 deletions
This file was deleted.

gulpfile.mjs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* @license
3+
* Copyright 2018 Google LLC
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/**
8+
* @fileoverview Gulp script to build Blockly for Node & NPM.
9+
* Run this script by calling "npm install" in this directory.
10+
*/
11+
/* eslint-env node */
12+
13+
// Needed to prevent prettier from munging exports order, due to
14+
// https://github.com/simonhaenisch/prettier-plugin-organize-imports/issues/146
15+
// - but has the unfortunate side effect of suppressing ordering of
16+
// imports too:
17+
//
18+
// organize-imports-ignore
19+
20+
import {parallel} from 'gulp';
21+
import {
22+
deployDemos,
23+
deployDemosBeta,
24+
prepareDemos,
25+
} from './scripts/gulpfiles/appengine_tasks.mjs';
26+
import {
27+
build,
28+
buildAdvancedCompilationTest,
29+
cleanBuildDir,
30+
langfiles,
31+
messages,
32+
minify,
33+
tsc,
34+
} from './scripts/gulpfiles/build_tasks.mjs';
35+
import {docs} from './scripts/gulpfiles/docs_tasks.mjs';
36+
import {
37+
createRC,
38+
syncDevelop,
39+
syncMaster,
40+
updateGithubPages,
41+
} from './scripts/gulpfiles/git_tasks.mjs';
42+
import {cleanReleaseDir, pack} from './scripts/gulpfiles/package_tasks.mjs';
43+
import {
44+
publish,
45+
publishBeta,
46+
recompile,
47+
} from './scripts/gulpfiles/release_tasks.mjs';
48+
import {generators, test} from './scripts/gulpfiles/test_tasks.mjs';
49+
50+
const clean = parallel(cleanBuildDir, cleanReleaseDir);
51+
52+
// Default target if gulp invoked without specifying.
53+
export default build;
54+
55+
// Main sequence targets. They already invoke prerequisites. Listed
56+
// in typical order of invocation, and strictly listing prerequisites
57+
// before dependants.
58+
//
59+
// prettier-ignore
60+
export {
61+
langfiles,
62+
tsc,
63+
minify,
64+
build,
65+
pack, // Formerly package.
66+
publishBeta,
67+
publish,
68+
prepareDemos,
69+
deployDemosBeta,
70+
deployDemos,
71+
updateGithubPages as gitUpdateGithubPages,
72+
}
73+
74+
// Manually-invokable targets that also invoke prerequisites where
75+
// required.
76+
//
77+
// prettier-ignore
78+
export {
79+
messages, // Generate msg/json/en.json et al.
80+
clean,
81+
test,
82+
generators as testGenerators,
83+
buildAdvancedCompilationTest,
84+
createRC as gitCreateRC,
85+
docs,
86+
}
87+
88+
// Legacy targets, to be deleted.
89+
//
90+
// prettier-ignore
91+
export {
92+
recompile,
93+
syncDevelop as gitSyncDevelop,
94+
syncMaster as gitSyncMaster,
95+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"lint:fix": "eslint . --fix",
3434
"langfiles": "gulp langfiles",
3535
"minify": "gulp minify",
36-
"package": "gulp package",
36+
"package": "gulp pack",
3737
"postinstall": "patch-package",
3838
"prepareDemos": "gulp prepareDemos",
3939
"publish": "npm ci && gulp publish",

scripts/gulpfiles/appengine_tasks.js renamed to scripts/gulpfiles/appengine_tasks.mjs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88
* @fileoverview Gulp script to deploy Blockly demos on appengine.
99
*/
1010

11-
const gulp = require('gulp');
11+
import * as gulp from 'gulp';
1212

13-
const fs = require('fs');
14-
const path = require('path');
15-
const execSync = require('child_process').execSync;
16-
const buildTasks = require('./build_tasks.js');
17-
const packageTasks = require('./package_tasks.js');
18-
const {rimraf} = require('rimraf');
13+
import * as fs from 'fs';
14+
import * as path from 'path';
15+
import {execSync} from 'child_process';
16+
import * as buildTasks from './build_tasks.mjs';
17+
import {getPackageJson} from './helper_tasks.mjs';
18+
import * as packageTasks from './package_tasks.mjs';
19+
import {rimraf} from 'rimraf';
1920

20-
const packageJson = require('../../package.json');
2121
const demoTmpDir = '../_deploy';
2222
const demoStaticTmpDir = '../_deploy/static';
2323

@@ -123,7 +123,7 @@ function deployToAndClean(demoVersion) {
123123
*/
124124
function getDemosVersion() {
125125
// Replace all '.' with '-' e.g. 9-3-3-beta-2
126-
return packageJson.version.replace(/\./g, '-');
126+
return getPackageJson().version.replace(/\./g, '-');
127127
}
128128

129129
/**
@@ -162,7 +162,7 @@ function deployBetaAndClean(done) {
162162
*
163163
* Prerequisites (invoked): clean, build
164164
*/
165-
const prepareDemos = gulp.series(
165+
export const prepareDemos = gulp.series(
166166
prepareDeployDir,
167167
gulp.parallel(
168168
gulp.series(
@@ -180,16 +180,9 @@ const prepareDemos = gulp.series(
180180
/**
181181
* Deploys demos.
182182
*/
183-
const deployDemos = gulp.series(prepareDemos, deployAndClean);
183+
export const deployDemos = gulp.series(prepareDemos, deployAndClean);
184184

185185
/**
186186
* Deploys beta version of demos (version appended with -beta).
187187
*/
188-
const deployDemosBeta = gulp.series(prepareDemos, deployBetaAndClean);
189-
190-
module.exports = {
191-
// Main sequence targets. Each should invoke any immediate prerequisite(s).
192-
deployDemos: deployDemos,
193-
deployDemosBeta: deployDemosBeta,
194-
prepareDemos: prepareDemos
195-
};
188+
export const deployDemosBeta = gulp.series(prepareDemos, deployBetaAndClean);

scripts/gulpfiles/build_tasks.js renamed to scripts/gulpfiles/build_tasks.mjs

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,32 @@
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

Comments
 (0)