Skip to content

esbuild: use Ruby platform #183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
3 changes: 1 addition & 2 deletions packages/esbuild-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ruby2js/esbuild-plugin",
"version": "0.0.3",
"version": "1.0.0",
"description": "ruby2js plugin for esbuild",
"contributors": [
"Jared White",
Expand Down Expand Up @@ -28,7 +28,6 @@
"access": "public"
},
"dependencies": {
"@ruby2js/ruby2js": ">0.0.1",
"convert-source-map": "^1.8.0"
},
"devDependencies": {
Expand Down
66 changes: 55 additions & 11 deletions packages/esbuild-plugin/src/index.js
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions packages/esbuild-plugin/test/esbuild.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require("esbuild").build({
minify,
plugins: [
ruby2js({
preset: true
extraArgs: ["--preset"]
})
],
}).catch(() => process.exit(1))
}).catch(() => process.exit(1))
30 changes: 17 additions & 13 deletions packages/esbuild-plugin/test/test_esbuild.js
Original file line number Diff line number Diff line change
@@ -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)
})
})