From ac6770c1a4c996496b05b81a42cce66d9effcaaf Mon Sep 17 00:00:00 2001 From: Jared White Date: Sat, 3 Sep 2022 21:01:18 -0700 Subject: [PATCH 1/3] Update esbuild package to use Ruby version of Ruby2JS --- packages/esbuild-plugin/package.json | 3 +- packages/esbuild-plugin/src/index.js | 66 +++++++++++++++---- .../esbuild-plugin/test/esbuild.config.js | 4 +- packages/esbuild-plugin/test/test_esbuild.js | 30 +++++---- 4 files changed, 75 insertions(+), 28 deletions(-) diff --git a/packages/esbuild-plugin/package.json b/packages/esbuild-plugin/package.json index edaa59db..464c9328 100644 --- a/packages/esbuild-plugin/package.json +++ b/packages/esbuild-plugin/package.json @@ -1,6 +1,6 @@ { "name": "@ruby2js/esbuild-plugin", - "version": "0.0.3", + "version": "1.0.0", "description": "ruby2js plugin for esbuild", "contributors": [ "Jared White", @@ -28,7 +28,6 @@ "access": "public" }, "dependencies": { - "@ruby2js/ruby2js": ">0.0.1", "convert-source-map": "^1.8.0" }, "devDependencies": { diff --git a/packages/esbuild-plugin/src/index.js b/packages/esbuild-plugin/src/index.js index 8db22802..bc807003 100644 --- a/packages/esbuild-plugin/src/index.js +++ b/packages/esbuild-plugin/src/index.js @@ -1,26 +1,70 @@ -const Ruby2JS = require('@ruby2js/ruby2js') +const path = require("path") const convert = require('convert-source-map') -const path = require('path') const fs = require('fs').promises +const { spawn } = require('child_process'); -module.exports = (options = {}) => ({ +const spawnChild = async (source, extraArgs, filepath) => { + const child = spawn('bundle', ['exec', 'ruby2js', '--filepath', filepath, ...extraArgs]) + + child.stdin.write(source) + child.stdin.end() + + let data = ""; + for await (const chunk of child.stdout) { + data += chunk; + } + let error = ""; + for await (const chunk of child.stderr) { + error += chunk; + } + const exitCode = await new Promise((resolve, reject) => { + child.on('close', resolve); + }); + + if (exitCode) { + throw new Error(`subprocess error exit ${exitCode}, ${data} ${error}`); + } + return data; +} + +const ruby2js = (options = {}) => ({ name: 'ruby2js', setup(build) { if (!options.buildFilter) options.buildFilter = /\.js\.rb$/ + let extraArgs = [] + if (typeof options.provideSourceMaps === "undefined") { + options.provideSourceMaps = true + } + if (options.provideSourceMaps) { + extraArgs.push("--sourcemap") + } + if (typeof options.extraArgs !== undefined) { + extraArgs = [...extraArgs, ...options.extraArgs] + } build.onLoad({ filter: options.buildFilter }, async (args) => { const code = await fs.readFile(args.path, 'utf8') - js = Ruby2JS.convert(code, { ...options, file: args.path }) - const output = js.toString() + let js = await spawnChild(code, extraArgs, args.path) - const smap = js.sourcemap - smap.sourcesContent = [code] - smap.sources[0] = path.basename(args.path) + if (options.provideSourceMaps) { + js = JSON.parse(js) + const output = `${js.code}\n` + const smap = js.sourcemap + smap.sourcesContent = [code] + smap.sources[0] = path.basename(args.path) - return { - contents: output + convert.fromObject(smap).toComment(), - loader: 'js' + return { + contents: output + convert.fromObject(smap).toComment(), + loader: 'js' + } + } else { + return { + contents: js, + loader: 'js' + } } }) }, }) + +module.exports = ruby2js diff --git a/packages/esbuild-plugin/test/esbuild.config.js b/packages/esbuild-plugin/test/esbuild.config.js index 95a565df..250514dd 100644 --- a/packages/esbuild-plugin/test/esbuild.config.js +++ b/packages/esbuild-plugin/test/esbuild.config.js @@ -16,7 +16,7 @@ require("esbuild").build({ minify, plugins: [ ruby2js({ - preset: true + extraArgs: ["--preset"] }) ], -}).catch(() => process.exit(1)) \ No newline at end of file +}).catch(() => process.exit(1)) diff --git a/packages/esbuild-plugin/test/test_esbuild.js b/packages/esbuild-plugin/test/test_esbuild.js index 956384ed..54a7d37e 100644 --- a/packages/esbuild-plugin/test/test_esbuild.js +++ b/packages/esbuild-plugin/test/test_esbuild.js @@ -1,24 +1,28 @@ const assert = require('assert') const fs = require('fs').promises -describe('@ruby2js/esbuild-plugin', function() { +function timeout(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); +} + +describe('@ruby2js/esbuild-plugin', function () { this.timeout(5000); - it('runs code through ruby2js', () => { + it('runs code through ruby2js', async () => { require("./esbuild.config.js") - setTimeout(async () => { - const code = await fs.readFile( - "app/assets/builds/application.js", - { encoding: "utf-8"} - ) + await timeout(1000) + + const code = await fs.readFile( + "app/assets/builds/application.js", + { encoding: "utf-8" } + ) - assert.strictEqual( + assert.strictEqual( `(() => { - // main.js.rb - console.log(parseInt("2A", 16)); - })(); - `, code) - }, 1000) + // main.js.rb + console.log(parseInt("2A", 16)); +})(); +`, code) }) }) From 9100f0ef0c9485de7d2cbe05a04865ee227e2936 Mon Sep 17 00:00:00 2001 From: Jared White Date: Sat, 3 Sep 2022 23:30:58 -0700 Subject: [PATCH 2/3] No need to copy Opal-variant of Ruby2JS into esbuild node modules --- Rakefile | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Rakefile b/Rakefile index d69d8900..dbadc2b8 100644 --- a/Rakefile +++ b/Rakefile @@ -19,7 +19,7 @@ namespace :demo do end namespace :packages do - # TODO: add tests and support for Vite and esbuild + # TODO: add tests and support for Vite desc "Build & test the Node version of Ruby2JS plus frontend bundling packages" task :test do @@ -30,9 +30,7 @@ namespace :packages do end Dir.chdir 'packages/esbuild-plugin' do - npm_root = `npm root`.strip sh 'yarn install' unless File.exist? 'yarn.lock' - sh "cp ../ruby2js/ruby2js.js #{npm_root}/@ruby2js/ruby2js/ruby2js.js" sh 'yarn test' end From 53ee28d4eb9139c23b0f6e2b19744d1137a0028d Mon Sep 17 00:00:00 2001 From: Jared White Date: Fri, 9 Sep 2022 14:31:54 -0700 Subject: [PATCH 3/3] Fix bug in esbuild plugin --- packages/esbuild-plugin/src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/esbuild-plugin/src/index.js b/packages/esbuild-plugin/src/index.js index bc807003..c3be01a9 100644 --- a/packages/esbuild-plugin/src/index.js +++ b/packages/esbuild-plugin/src/index.js @@ -39,7 +39,7 @@ const ruby2js = (options = {}) => ({ extraArgs.push("--sourcemap") } if (typeof options.extraArgs !== undefined) { - extraArgs = [...extraArgs, ...options.extraArgs] + extraArgs = [...extraArgs, ...(options.extraArgs || [])] } build.onLoad({ filter: options.buildFilter }, async (args) => {