Skip to content

Commit

Permalink
chore(build): fix webpack
Browse files Browse the repository at this point in the history
  • Loading branch information
fzn0x committed Jun 17, 2024
1 parent 8ebdb7c commit fe3a18d
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 60 deletions.
2 changes: 1 addition & 1 deletion browser/hyperfetch-browser.min.js

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

2 changes: 1 addition & 1 deletion browser/hyperfetch-browser.min.js.map

Large diffs are not rendered by default.

92 changes: 38 additions & 54 deletions build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,92 +5,76 @@
Copyright (c) 2022 Taishi Naritomi
*/

import { exec } from "child_process";
import fs from "fs";
import path from "path";
import arg from "arg";
import { build } from "esbuild";
import type { Plugin, PluginBuild, BuildOptions } from "esbuild";
import glob from "glob";
import { exec } from 'child_process'
import fs from 'fs'
import path from 'path'
import arg from 'arg'
import { build } from 'esbuild'
import type { Plugin, PluginBuild, BuildOptions } from 'esbuild'
import { glob } from 'glob'

const args = arg({
"--watch": Boolean,
});
'--watch': Boolean,
})

const isWatch = args["--watch"] || false;
const isWatch = args['--watch'] || false

const entryPoints = glob.sync("./src/**/*.ts", {
ignore: [
"./src/**/*.test.ts",
"./src/mod.ts",
"./src/middleware.ts",
"./src/deno/**/*.ts",
],
});
const entryPoints = glob.sync('./src/**/*.ts', {
ignore: ['./src/**/*.test.ts', './src/mod.ts', './src/deno/**/*.ts'],
})

/*
This plugin is inspired by the following.
https://github.com/evanw/esbuild/issues/622#issuecomment-769462611
*/
const addExtension = (
extension: string = ".js",
fileExtension: string = ".ts"
): Plugin => ({
name: "add-extension",
const addExtension = (extension: string = '.js', fileExtension: string = '.ts'): Plugin => ({
name: 'add-extension',
setup(build: PluginBuild) {
build.onResolve({ filter: /.*/ }, (args) => {
if (args.importer) {
const p = path.join(args.resolveDir, args.path);
let tsPath = `${p}${fileExtension}`;
const p = path.join(args.resolveDir, args.path)
let tsPath = `${p}${fileExtension}`

let importPath = "";
let importPath = ''
if (fs.existsSync(tsPath)) {
importPath = args.path + extension;
importPath = args.path + extension
} else {
tsPath = path.join(
args.resolveDir,
args.path,
`index${fileExtension}`
);
tsPath = path.join(args.resolveDir, args.path, `index${fileExtension}`)
if (fs.existsSync(tsPath)) {
importPath = `${args.path}/index${extension}`;
importPath = `${args.path}/index${extension}`
}
}
return { path: importPath, external: true };
return { path: importPath, external: true }
}
});
})
},
});
})

const commonOptions: BuildOptions = {
watch: isWatch,
entryPoints,
logLevel: "info",
platform: "node",
};
logLevel: 'info',
platform: 'node',
}

const cjsBuild = () =>
build({
...commonOptions,
outbase: "./src",
outdir: "./dist/cjs",
format: "cjs",
});
outbase: './src',
outdir: './dist/cjs',
format: 'cjs',
})

const esmBuild = () =>
build({
...commonOptions,
bundle: true,
outbase: "./src",
outdir: "./dist",
format: "esm",
plugins: [addExtension(".js")],
});
outbase: './src',
outdir: './dist',
format: 'esm',
plugins: [addExtension('.js')],
})

Promise.all([esmBuild(), cjsBuild()]);
Promise.all([esmBuild(), cjsBuild()])

exec(
`tsc ${
isWatch ? "-w" : ""
} --emitDeclarationOnly --declaration --project tsconfig.build.json`
);
exec(`tsc ${isWatch ? '-w' : ''} --emitDeclarationOnly --declaration --project tsconfig.build.json`)
Binary file modified bun.lockb
Binary file not shown.
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
"version": "0.1.0",
"description": "Supertiny (4kB minified & 0 dependencies) and strong-typed HTTP client for Deno, Bun, Node.js, Cloudflare Workers and Browsers.",
"main": "./dist/index.js",
"repository": "fzn0x/hypf",
"repository": {
"type": "git",
"url": "https://github.com/fzn0x/hypf.git"
},
"type": "module",
"files": [
"dist"
Expand Down Expand Up @@ -69,12 +72,17 @@
"data-retrieval"
],
"license": "MIT",
"publishConfig": {
"registry": "https://registry.npmjs.org"
},
"devDependencies": {
"@hono/eslint-config": "^0.0.6",
"@types/node": "^20.11.26",
"arg": "^5.0.2",
"esbuild": "^0.15.12",
"esbuild-register": "^3.5.0",
"eslint": "8.55.0",
"glob": "^10.4.1",
"prettier": "^3.3.2",
"rimraf": "^5.0.5",
"ts-loader": "^9.5.1",
Expand Down
13 changes: 10 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
# bun ./bun.lockb --hash: 6234A78B511F5B19-ad96a3354cb89bdb-7C67F0662DC70DB6-2b96fa65a6996414
# bun ./bun.lockb --hash: 7F993CDF47004981-e5cf8c7e206dc094-D45253AFE7E2C313-dd3310c26dc4927c


"@discoveryjs/json-ext@^0.5.0":
Expand Down Expand Up @@ -1302,7 +1302,7 @@ esbuild@^0.15.12:
"@esbuild/linux-loong64" "0.15.18"
esbuild-linux-mips64le "0.15.18"

esbuild@^0.21.3, esbuild@~0.21.4:
"esbuild@>=0.12 <1", esbuild@^0.21.3, esbuild@~0.21.4:
version "0.21.5"
resolved "https://registry.npmjs.org/esbuild/-/esbuild-0.21.5.tgz"
integrity sha512-mg3OPMV4hXywwpoDxu3Qda5xCKQi+vCTZq8S9J/EpkhB2HzKXq4SNFZE3+NK93JYxc8VMSep+lOUSC/RVKaBqw==
Expand Down Expand Up @@ -1411,6 +1411,13 @@ esbuild-openbsd-64@0.15.18:
resolved "https://registry.npmjs.org/esbuild-openbsd-64/-/esbuild-openbsd-64-0.15.18.tgz"
integrity sha512-yK5NCcH31Uae076AyQAXeJzt/vxIo9+omZRKj1pauhk3ITuADzuOx5N2fdHrAKPxN+zH3w96uFKlY7yIn490xQ==

esbuild-register@^3.5.0:
version "3.5.0"
resolved "https://registry.npmjs.org/esbuild-register/-/esbuild-register-3.5.0.tgz"
integrity sha512-+4G/XmakeBAsvJuDugJvtyF1x+XJT4FMocynNpxrvEBViirpfUn2PgNpCHedfWhF4WokNsO/OvMKrmJOIJsI5A==
dependencies:
debug "^4.3.4"

esbuild-sunos-64@0.15.18:
version "0.15.18"
resolved "https://registry.npmjs.org/esbuild-sunos-64/-/esbuild-sunos-64-0.15.18.tgz"
Expand Down Expand Up @@ -1923,7 +1930,7 @@ glob@^7.1.3:
once "^1.3.0"
path-is-absolute "^1.0.0"

glob@^10.3.7:
glob@^10.3.7, glob@^10.4.1:
version "10.4.1"
resolved "https://registry.npmjs.org/glob/-/glob-10.4.1.tgz"
integrity sha512-2jelhlq3E4ho74ZyVLN03oKdAZVUa6UDZzFLVH1H7dnoax+y9qyaq8zBkfDIggjniU19z0wU18y16jMB2eyVIw==
Expand Down

0 comments on commit fe3a18d

Please sign in to comment.