Skip to content

Commit

Permalink
node: build options JS banner supported
Browse files Browse the repository at this point in the history
  • Loading branch information
Hideaki Matsunami committed May 27, 2023
1 parent 03644a3 commit 185fe7a
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 12 deletions.
59 changes: 53 additions & 6 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,62 @@
import type { Plugin, PluginBuild } from 'esbuild'
const readline = require('node:readline')
const { Readable } = require('node:stream')
const fs = require('node:fs')
const { generate } = require('gas-entry-generator')

async function countLines(s: string): Promise<number> {
return new Promise(resolve => {
let count = 0
const input = Readable.from([s])
const rl = readline.createInterface({ input })

rl.on('line', () => {
count++
})

rl.on('close', () => {
resolve(count)
})
})
}

async function deleteBanner(code: string, jsBanner: string): Promise<string> {
const input = Readable.from([code])
const bannerLines = await countLines(jsBanner)

return new Promise(resolve => {
let _count = 0
let _code = ''
const rl = readline.createInterface({ input })

rl.on('line', (data: string) => {
_count++
if(_count <= bannerLines) return

_code += `${data}\n`
})

rl.on('close', () => {
resolve(_code)
})
})
}

const GasPlugin: Plugin = {
name: 'gas-plugin',
setup(build: PluginBuild) {
const fs = require('fs')
const { generate } = require('gas-entry-generator')
build.onEnd(async () => {
const esbuildOptions = build.initialOptions
const jsBanner = esbuildOptions.banner?.js
const code = fs.readFileSync(esbuildOptions.outfile, {encoding: 'utf8'});
const gas = generate(code, { comment: true })

build.onEnd(result => {
const outfile = fs.readFileSync(build.initialOptions.outfile, {encoding: 'utf8'});
const gas = generate(outfile, { comment: true })
fs.writeFileSync(build.initialOptions.outfile, 'var global = this;' + '\n' + gas.entryPointFunctions + '\n' + outfile)
if (jsBanner === undefined) {
fs.writeFileSync(esbuildOptions.outfile, 'var global = this;' + '\n' + gas.entryPointFunctions + '\n' + code)
} else {
const bannerDeleted = await deleteBanner(code, jsBanner)
fs.writeFileSync(esbuildOptions.outfile, jsBanner + 'var global = this;' + '\n' + gas.entryPointFunctions + bannerDeleted)
}
})
}
}
Expand Down
70 changes: 64 additions & 6 deletions test/node/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ const fs = require('fs')
const esbuild = require('esbuild')
const { GasPlugin } = require('../../dist');

test.beforeEach(() => {
fs.rmSync('./dist', { force: true, recursive: true })
})

test('declare global functions', async t => {
const outfilePath = 'dist/bundle.js'
test('declare global functions. banner#js is not defined', async t => {
const outfilePath = 'dist/test1.js'

await esbuild.build({
entryPoints: ['../fixtures/main.ts'],
Expand Down Expand Up @@ -54,3 +50,65 @@ function main2() {

t.is(outfile, expected)
});


test('declare global functions. banner#js is defined', async t => {
const outfilePath = 'dist/test2.js'

await esbuild.build({
entryPoints: ['../fixtures/main.ts'],
bundle: true,
outfile: outfilePath,
banner: {
js: `/**
* This is banner
* This is banner
* This is banner
*/
`
},
plugins: [GasPlugin]
})

const outfile = fs.readFileSync(outfilePath, { encoding: 'utf8' })
const expected = `/**
* This is banner
* This is banner
* This is banner
*/
var global = this;
function main1() {
}
function main2() {
}
"use strict";
(() => {
// ../fixtures/util.ts
var add = (n1, n2) => n1 + n2;
var sub = (n1, n2) => n1 - n2;
var util_default = {
add,
sub
};
// ../fixtures/main.ts
var greet = (name) => {
console.log("Hello " + name);
};
var main1 = () => {
greet("mahaker");
console.log(util_default.add(2, 3));
console.log(util_default.sub(0, 5));
};
var main2 = () => {
greet("world!");
console.log(util_default.add(10, 5));
console.log(util_default.sub(10, 5));
};
global.main1 = main1;
global.main2 = main2;
})();
`

t.is(outfile, expected)
});

0 comments on commit 185fe7a

Please sign in to comment.