Skip to content

Commit

Permalink
Remove through2 in favor of Node.js stream.Transform (#16519)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed May 21, 2024
1 parent 3b1a3c0 commit 125ba9a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 54 deletions.
92 changes: 50 additions & 42 deletions Gulpfile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import fs from "fs";
import { cpus } from "os";
import { createRequire } from "module";
import { fileURLToPath } from "url";
import { Transform as TransformStream } from "stream";
import { callbackify } from "util";
import plumber from "gulp-plumber";
import through from "through2";
import colors from "picocolors";
import gulp from "gulp";
import { rollup } from "rollup";
Expand Down Expand Up @@ -110,15 +111,18 @@ function generateHelpers(generator, dest, filename, message) {
.src(".", { base: monorepoRoot })
.pipe(errorsLogger())
.pipe(
through.obj(async (file, enc, callback) => {
const { default: generateCode } = await import(generator);

file.path = filename;
file.contents = Buffer.from(
await formatCode(await generateCode(filename), dest + file.path)
);
log(`${colors.green("✔")} Generated ${message}`);
callback(null, file);
new TransformStream({
objectMode: true,
transform: callbackify(async file => {
const { default: generateCode } = await import(generator);

file.path = filename;
file.contents = Buffer.from(
await formatCode(await generateCode(filename), dest + file.path)
);
log(`${colors.green("✔")} Generated ${message}`);
return file;
}),
})
)
.pipe(gulp.dest(dest, { mode: 0o644 }));
Expand Down Expand Up @@ -178,40 +182,44 @@ function generateStandalone() {
return gulp
.src(babelStandalonePluginConfigGlob, { base: monorepoRoot })
.pipe(
through.obj(async (file, enc, callback) => {
log("Generating @babel/standalone files");
const pluginConfig = JSON.parse(file.contents);
let imports = `import makeNoopPlugin from "../make-noop-plugin.ts";`;
let exportDecls = "";
let exportsList = "";
let allList = "";

for (const plugin of pluginConfig.noopPlugins) {
const camelPlugin = kebabToCamel(plugin);
exportDecls += `${camelPlugin} = makeNoopPlugin(),`;
allList += `"${plugin}": ${camelPlugin},`;
}
new TransformStream({
objectMode: true,
transform: callbackify(async file => {
log("Generating @babel/standalone files");
const pluginConfig = JSON.parse(file.contents);
let imports = `import makeNoopPlugin from "../make-noop-plugin.ts";`;
let exportDecls = "";
let exportsList = "";
let allList = "";

for (const plugin of pluginConfig.noopPlugins) {
const camelPlugin = kebabToCamel(plugin);
exportDecls += `${camelPlugin} = makeNoopPlugin(),`;
allList += `"${plugin}": ${camelPlugin},`;
}

for (const plugin of pluginConfig.externalPlugins) {
const camelPlugin = kebabToCamel(plugin);
imports += `import ${camelPlugin} from "@babel/plugin-${plugin}";`;
exportsList += `${camelPlugin},`;
allList += `"${plugin}": ${camelPlugin},`;
}
for (const plugin of pluginConfig.externalPlugins) {
const camelPlugin = kebabToCamel(plugin);
imports += `import ${camelPlugin} from "@babel/plugin-${plugin}";`;
exportsList += `${camelPlugin},`;
allList += `"${plugin}": ${camelPlugin},`;
}

const fileContents = `/*
* This file is auto-generated! Do not modify it directly.
* To re-generate run 'yarn gulp generate-standalone'
*/
${imports}
export const ${exportDecls.slice(0, -1)};
export {${exportsList}};
export const all: { [k: string]: any } = {${allList}};`;
file.path = "plugins.ts";
file.contents = Buffer.from(
await formatCode(fileContents, dest + file.path)
);
callback(null, file);
const fileContents = `/*
* This file is auto-generated! Do not modify it directly.
* To re-generate run 'yarn gulp generate-standalone'
*/
${imports}
export const ${exportDecls.slice(0, -1)};
export {${exportsList}};
export const all: { [k: string]: any } = {${allList}};`;
file.path = "plugins.ts";
file.contents = Buffer.from(
await formatCode(fileContents, dest + file.path)
);

return file;
}),
})
)
.pipe(gulp.dest(dest));
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
"semver": "^6.3.1",
"shelljs": "^0.8.5",
"test262-stream": "^1.4.0",
"through2": "^4.0.0",
"typescript": "^5.4.5",
"typescript-eslint": "^7.8.0"
},
Expand Down
12 changes: 1 addition & 11 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7170,7 +7170,6 @@ __metadata:
semver: "npm:^6.3.1"
shelljs: "npm:^0.8.5"
test262-stream: "npm:^1.4.0"
through2: "npm:^4.0.0"
typescript: "npm:^5.4.5"
typescript-eslint: "npm:^7.8.0"
languageName: unknown
Expand Down Expand Up @@ -14713,7 +14712,7 @@ __metadata:
languageName: node
linkType: hard

"readable-stream@npm:3, readable-stream@npm:^3.6.0":
"readable-stream@npm:^3.6.0":
version: 3.6.2
resolution: "readable-stream@npm:3.6.2"
dependencies:
Expand Down Expand Up @@ -16415,15 +16414,6 @@ __metadata:
languageName: node
linkType: hard

"through2@npm:^4.0.0":
version: 4.0.2
resolution: "through2@npm:4.0.2"
dependencies:
readable-stream: "npm:3"
checksum: 10/72c246233d9a989bbebeb6b698ef0b7b9064cb1c47930f79b25d87b6c867e075432811f69b7b2ac8da00ca308191c507bdab913944be8019ac43b036ce88f6ba
languageName: node
linkType: hard

"through@npm:>=2.2.7 <3":
version: 2.3.8
resolution: "through@npm:2.3.8"
Expand Down

0 comments on commit 125ba9a

Please sign in to comment.