Skip to content

Commit

Permalink
Various failed attempts to fix things
Browse files Browse the repository at this point in the history
  • Loading branch information
dcramer committed Mar 13, 2024
1 parent 9b78165 commit 501f799
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 2 deletions.
7 changes: 7 additions & 0 deletions apps/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
"test:watch": "dotenv -e .env.test -- vitest --no-threads",
"test:ci": "dotenv -e .env.test -- vitest --no-threads --coverage --reporter=junit --reporter=default --outputFile=junit.xml"
},
"exports": {
"./*.ts": "./src/*.ts",
"./*": [
"./src/*.ts",
"./src/*/index.ts"
]
},
"dependencies": {
"@faker-js/faker": "^8.4.1",
"@fastify/cors": "^8.5.0",
Expand Down
10 changes: 10 additions & 0 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
"start": "cross-env NODE_ENV=production node ./index.js",
"typecheck": "tsc --noEmit --incremental"
},
"exports": {
"./*": [
"./app/*",
"./app/*.ts",
"./app/*.tsx",
"./app/*/index.ts",
"./app/*/index.tsx"
]
},
"dependencies": {
"@fontsource/raleway": "^5.0.17",
"@headlessui/react": "^1.7.18",
Expand Down Expand Up @@ -48,6 +57,7 @@
"dotenv": "^16.4.5",
"execa": "^8.0.1",
"express": "^4.18.3",
"fast-glob": "^3.3.2",
"framer-motion": "^10.18.0",
"get-port": "^7.0.0",
"isbot": "latest",
Expand Down
2 changes: 1 addition & 1 deletion apps/web/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ async function getBuild() {
? await vite.ssrLoadModule("virtual:remix/server-build")
: // @ts-ignore this should exist before running the server
// but it may not exist just yet.
await import("#build/server/index.js");
await import("../build/server/index.js");
// not sure how to make this happy 🤷‍♂️
return build as unknown as ServerBuild;
}
Expand Down
3 changes: 3 additions & 0 deletions apps/web/support/build-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fsExtra from "fs-extra";
import { globSync } from "glob";
import path from "path";
import { fileURLToPath } from "url";
import tsPaths from "./esbuild-tspaths";

const pkg = fsExtra.readJsonSync(
path.join(process.cwd(), "../../package.json"),
Expand Down Expand Up @@ -38,13 +39,15 @@ console.log("building...");

esbuild
.build({
// bundle: true,
entryPoints: entries,
outdir: here("../server-build"),
target: [`node${pkg.engines.node}`],
platform: "node",
sourcemap: true,
format: "esm",
logLevel: "info",
plugins: [tsPaths(here("../tsconfig.json"))],
})
.catch((error: unknown) => {
console.error(error);
Expand Down
113 changes: 113 additions & 0 deletions apps/web/support/esbuild-tspaths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// MIT License

// Copyright (c) 2024 David Cramer
// Copyright (c) 2022 Frank Leng

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

// Based on esbuild-ts-paths, but updated to handle `extends` in tsconfig.

import fg from "fast-glob";
import fs from "fs";
import { resolve } from "path";

const normalizePath =
process.platform === "win32" ? require("normalize-path") : (x: string) => x;

function stripJsonComments(data: string) {
return data.replace(
/\\"|"(?:\\"|[^"])*"|(\/\/.*|\/\*[\s\S]*?\*\/)/g,
(m, g) => (g ? "" : m),
);
}

function escapeRegExp(value: string) {
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
}

function fixExpression(value: string) {
// replace globs with regexp matcher
return value.replace("\\*", ".*");
}

async function collectPaths(tsconfigPath: string) {
// returns file:// so we gotta strip prefix
const absTsconfigPath = import.meta.resolve(tsconfigPath).substring(6);

console.debug(`Importing tsconfig: ${tsconfigPath}`);
let rawTsconfig = fs.readFileSync(absTsconfigPath, "utf8");
rawTsconfig = stripJsonComments(rawTsconfig);
const data = JSON.parse(rawTsconfig);

if (data.compilerOptions.paths) {
return data.compilerOptions.paths;
}

if (data.extends) {
return await collectPaths(data.extends);
}

return {};
}

export default (tsconfigPath = "./tsconfig.json") => {
return {
name: "@peated/esbuild-tspaths",

async setup(build: any) {
const paths = await collectPaths(tsconfigPath);
const pathKeys = Object.keys(paths);

const filterRe = new RegExp(
`^(${pathKeys.map(escapeRegExp).map(fixExpression).join("|")})`,
);

build.onResolve({ filter: filterRe }, async ({ path }: any) => {
const pathKey = pathKeys.find((pkey) =>
new RegExp(`^${pkey}`).test(path),
);

if (!pathKey) throw new Error("Unable to identify path");

const [pathDir] = pathKey.split("*");
let file = path.replace(pathDir, "");
if (file === path) {
// if importing from root of alias
file = "";
}

for (const dir of paths[pathKey]) {
const fileDir = normalizePath(
resolve(process.cwd(), dir).replace("*", file),
);

let [matchedFile] = fg.sync(`${fileDir}.*`);
if (!matchedFile) {
const [matchIndexFile] = fg.sync(`${fileDir}/index.*`);
matchedFile = matchIndexFile;
}
if (matchedFile) {
return { path: matchedFile };
}
}
return { path };
});
},
};
};
7 changes: 7 additions & 0 deletions apps/worker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
"test:ci": "dotenv -e .env.test -- vitest --no-threads --coverage --reporter=junit --reporter=default --outputFile=junit.xml",
"typecheck": "tsc --noEmit --incremental"
},
"exports": {
"./*.ts": "./src/*.ts",
"./*": [
"./src/*.ts",
"./src/*/index.ts"
]
},
"dependencies": {
"@peated/server": "workspace:*",
"@peated/tsconfig": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
},
"packageManager": "[email protected]+sha256.2d0363bb6c314daa67087ef07743eea1ba2e2d360c835e8fec6b5575e4ed9484",
"volta": {
"node": "20.11.0",
"node": "20.11.1",
"pnpm": "8.14.3"
},
"simple-git-hooks": {
Expand Down
7 changes: 7 additions & 0 deletions packages/design/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
"index.ts",
"tailwind.config.ts"
],
"exports": {
"./*.ts": "./*.ts",
"./*": [
"./*.ts",
"./*/index.ts"
]
},
"dependencies": {
"@peated/tsconfig": "workspace:*",
"@tailwindcss/forms": "^0.5.7",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 501f799

Please sign in to comment.