Skip to content

Commit

Permalink
Better mutli-runtime support
Browse files Browse the repository at this point in the history
  • Loading branch information
corrideat committed Apr 14, 2023
1 parent 2b520d4 commit 2d45515
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 16 deletions.
92 changes: 82 additions & 10 deletions esbuild.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,100 @@
*/

import esbuild from 'esbuild';
import nodePath from 'node:path';

await esbuild.build({
const buildOptionsBase = {
entryPoints: ['./src/index.ts'],
outdir: 'dist',
bundle: true,
minify: true,
format: 'cjs',
entryNames: '[name]',
platform: 'node',
external: ['esbuild'],
};

const formats = ['cjs', 'esm'];

const filterListeners = (includeListeners, excludeListeners) => ({
name: 'filterListeners',
setup(build) {
build.onLoad(
{
filter: /./,
namespace: 'filterListeners',
},
() => ({
contents:
'const x = () => {throw Error("Unsupported listener in this build")}; export default x;',
loader: 'js',
}),
);

build.onResolve(
{
filter: /./,
namespace: 'file',
},
({ path, resolveDir }) => {
const resolvedPath = nodePath.resolve(resolveDir, path);

if (!/\/src\/listeners\/[^/]+$/.test(resolvedPath)) return;

const baseName = nodePath.basename(path);

if (
(!excludeListeners ||
!excludeListeners.includes(baseName)) &&
(!includeListeners || includeListeners.includes(baseName))
) {
return;
}

return {
external: false,
namespace: 'filterListeners',
path: path,
};
},
);
},
});

await esbuild.build({
entryPoints: ['./src/index.ts'],
outdir: 'dist',
bundle: true,
minify: true,
await Promise.resolve(
formats.map((format) => {
return esbuild.build({
...buildOptionsBase,
format,
outExtension: {
'.js': format === 'esm' ? '.mjs' : '.js',
},
plugins: [
filterListeners(undefined, ['deno', 'cloudflare-workers']),
],
});
}),
);

await Promise.resolve(
formats.map((format) => {
return esbuild.build({
...buildOptionsBase,
format,
entryNames: 'cloudflare-workers',
outExtension: {
'.js': format === 'esm' ? '.mjs' : '.js',
},
plugins: [filterListeners(['cloudflare-workers', 'dynamic'])],
});
}),
);

esbuild.build({
...buildOptionsBase,
entryNames: 'deno',
format: 'esm',
entryNames: '[name]',
platform: 'node',
external: ['esbuild'],
outExtension: {
'.js': '.mjs',
},
plugins: [filterListeners(['deno', 'dynamic'])],
});
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
{
"name": "@exact-realty/routemate",
"version": "1.0.0",
"version": "1.0.1",
"description": "",
"dist": "dist/index.js",
"main": "dist/index.js",
"module": "./dist/index.mjs",
"exports": {
"import": "./dist/index.mjs",
"require": "./dist/index.js"
".": {
"deno": "./dist/deno.mjs",
"cloudflare-workers": {
"import": "./dist/cloudflare-workers.mjs",
"require": "./dist/cloudflare-workers.mjs"
},
"import": "./dist/index.mjs",
"require": "./dist/index.js"
},
"./cloudflare-workers": {
"import": "./dist/cloudflare-workers.mjs",
"require": "./dist/cloudflare-workers.mjs"
}
},
"files": [
"dist/**/*"
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export {
default as ResponseError_,
handleResponseError as handleResponseError,
} from './ResponseError';
export * as default from './server';
export { default, Router } from './server';
export type {
TErrorHandler,
TListen,
Expand Down
File renamed without changes.

0 comments on commit 2d45515

Please sign in to comment.