Skip to content
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

fix: Deno and cloudflare workers deploy #1265

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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: 2 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18.x'
node-version: '20.x'
cache: 'npm'

- run: npm ci
Expand All @@ -30,7 +30,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18.x'
node-version: '20.x'
cache: 'npm'

- run: npm ci
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
strategy:
max-parallel: 5
matrix:
node_version: [ 20.x, 18.x, 16.x, 14.x, 12.x, ]
node_version: [ 20.x, 18.x, 16.x ]
os: [ ubuntu-latest, windows-latest ]

env:
Expand Down
4 changes: 2 additions & 2 deletions .mocharc.js → .mocharc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ module.exports = {
exit: true,
reporter: 'spec',
ui: 'bdd',
require: ['dotenv/config', 'source-map-support/register', './babel-register.js'],
}
require: ['./esbuild-setup.mjs'],
};
28 changes: 0 additions & 28 deletions babel-register.js

This file was deleted.

164 changes: 41 additions & 123 deletions build.mjs
Original file line number Diff line number Diff line change
@@ -1,131 +1,49 @@
/* eslint-disable no-console */
import { exec } from 'node:child_process'
import * as fs from 'node:fs'
import * as fsp from 'node:fs/promises'
import * as path from 'node:path'
import { promisify } from 'node:util'

import * as babel from '@babel/core'
import * as fsWalk from '@nodelib/fs.walk'

const pkg = JSON.parse(fs.readFileSync('package.json').toString())

/**
* @param {'esm'|'cjs'} module
*/
function options(module) {
const plugins = [
[
'@babel/plugin-transform-modules-commonjs',
{
importInterop: 'node',
},
],
['@upleveled/remove-node-prefix'],
[
'replace-import-extension',
{
extMapping: {
'.ts': extMap[module],
'.js': extMap[module],
},
},
],
[
'babel-plugin-transform-replace-expressions',
{
replace: {
'process.env.MINIO_JS_PACKAGE_VERSION': JSON.stringify(pkg.version),
},
},
],
]

return {
sourceMaps: 'inline',
assumptions: {
constantSuper: true,
noIncompleteNsImportDetection: true,
constantReexports: true,
import esbuild from 'esbuild';
import * as fs from 'node:fs/promises';
import * as path from 'node:path';
import { fileURLToPath } from 'url';

// Helper function to get the directory name of the current module
const __dirname = path.dirname(fileURLToPath(import.meta.url));

const pkg = JSON.parse(await fs.readFile('package.json', 'utf8'));

async function build(module) {
console.log(`Building for ${module}...`);

const outDir = module === 'cjs' ? './dist/main/' : './dist/esm/';
const format = module === 'cjs' ? 'cjs' : 'esm';
const extension = module === 'cjs' ? '.js' : '.mjs';

await esbuild.build({
entryPoints: [path.join(__dirname, 'src/minio.js')],
outdir: outDir,
platform: 'node',
target: 'node16',
format: format,
bundle: true,
sourcemap: true,
external: Object.keys(pkg.dependencies || {}),
define: {
'process.env.MINIO_JS_PACKAGE_VERSION': JSON.stringify(pkg.version),
},
plugins: module === 'esm' ? plugins.splice(1) : plugins,
presets: [['@babel/env', { targets: { node: '14' }, modules: false }], ['@babel/preset-typescript']],
}
}

const extMap = { cjs: '.js', esm: '.mjs' }

async function buildFiles({ files, module, outDir }) {
console.log(`building for ${module}`)
await promisify(exec)(`npx tsc --outDir ${outDir}`, { stdio: 'inherit' })
const ext = extMap[module]

const opt = options(module)
for (const file of files) {
if (!file.dirent.isFile()) {
continue
}

const outFilePath = path.join(outDir, path.relative('src/', file.path))
const outDirPath = path.dirname(outFilePath)

await fsp.mkdir(outDirPath, { recursive: true })
const distCodePath = outFilePath.replace(/\.[tj]s$/g, ext)

if (file.path.endsWith('.d.ts')) {
await fsp.copyFile(file.path, outFilePath)
continue
}

try {
const result = await babel.transformAsync(await fsp.readFile(file.path, 'utf-8'), {
filename: file.path,
...opt,
})

await fsp.writeFile(distCodePath, result.code)
} catch (e) {
console.error(`failed to transpile ${file.path}`)
throw e
}
}

for (const file of fsWalk.walkSync(outDir)) {
if (file.dirent.isDirectory()) {
continue
}

if (!file.path.endsWith('.d.ts')) {
continue
}

const fileContent = fs.readFileSync(file.path).toString()

const mts = babel.transformSync(fileContent, {
filename: file.path,
sourceMaps: true,
plugins: [['@babel/plugin-syntax-typescript'], ['replace-import-extension', { extMapping: { '.ts': ext } }]],
})

await fsp.unlink(file.path)

let outFilePath = file.path.slice(0, file.path.length - '.d.ts'.length) + '.d.ts'
if (module === 'esm') {
outFilePath = file.path.slice(0, file.path.length - '.d.ts'.length) + '.d.mts'
}
await fsp.writeFile(outFilePath, mts.code)
}
plugins: [
// Add any esbuild plugins you need here
],
});
}

async function main() {
await fsp.rm('dist', { recursive: true, force: true })

const entries = fsWalk.walkSync('src/')
await fs.rm('dist', { recursive: true, force: true });

// Build for both CommonJS and ES Module formats
await Promise.all([
buildFiles({ files: entries, module: 'cjs', outDir: './dist/main/' }),
buildFiles({ files: entries, module: 'esm', outDir: './dist/esm/' }),
])
build('cjs'),
build('esm'),
]);
}

await main()
main().catch((error) => {
console.error(error);
process.exit(1);
});
11 changes: 11 additions & 0 deletions esbuild-setup.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import dotenv from 'dotenv';
import sourceMapSupport from 'source-map-support';
import esbuildRegister from 'esbuild-register/dist/node';

dotenv.config();
sourceMapSupport.install();

esbuildRegister.register({
target: 'node16',
format: 'cjs',
});
Loading