Skip to content

Commit

Permalink
feat: replace ts-node by tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
fpasquet committed Aug 14, 2023
1 parent feb3b54 commit 38a63fa
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 186 deletions.
27 changes: 4 additions & 23 deletions bin/build.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,22 @@
import { cpSync } from 'node:fs';
import { resolve } from 'node:path';
import { build as buildVite } from 'vite';
import { createServer as createViteServer } from 'vite';

import { ASSETS_DIR, OUT_DIR, OUT_PUBLIC_DIR } from '@/app-paths';
import { writeJsonDataFiles } from '@/helpers/contentHelper';

const BASE_URL = process.env.BASE_URL || '/';
const MODE = process.env.NODE_ENV || 'production';
const ROOT_DIR = process.cwd();
const ASSETS_DIR = resolve(ROOT_DIR, '_assets');
const OUT_DIR = resolve(ROOT_DIR, 'dist');
const OUT_PUBLIC_DIR = resolve(OUT_DIR, 'public');

const args = process.argv.slice(2).reduce<Record<string, string | number | boolean>>((currentArgs, currentArg) => {
const [key, value] = currentArg.replace('--', '').split('=');
currentArgs[key] = value;
return currentArgs;
}, {});

const writeJsonDataFilesAndFeedFile = async (): Promise<void> => {
const vite = await createViteServer({
server: { middlewareMode: true },
base: BASE_URL,
appType: 'custom',
});

try {
const { writeJsonDataFiles } = await vite.ssrLoadModule('/src/helpers/contentHelper.ts');
writeJsonDataFiles();
} catch (e) {
console.error(e);
} finally {
vite.close();
}
};

const build = async (): Promise<void> => {
cpSync(ASSETS_DIR, resolve(OUT_PUBLIC_DIR, 'imgs'), { recursive: true });
await writeJsonDataFilesAndFeedFile();
await writeJsonDataFiles();

if (args.ssr) {
await buildVite({
Expand Down
2 changes: 1 addition & 1 deletion bin/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const dev = async (): Promise<void> => {
},
});

await vite.ssrLoadModule('/src/server.ts');
await vite.ssrLoadModule('/src/server');
};

dev();
29 changes: 6 additions & 23 deletions bin/indexationAlgolia.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
import { createServer as createViteServer } from 'vite';
import { indexationAlglolia } from '@/helpers/indexationAlgoliaHelper';

const indexationAlglolia = async (): Promise<void> => {
const vite = await createViteServer({
server: { middlewareMode: true },
appType: 'custom',
});

try {
const { indexationAlglolia } = await vite.ssrLoadModule('/src/helpers/indexationAlgoliaHelper.ts');

await indexationAlglolia({
appId: process.env.ALGOLIA_APP_ID as string,
apiIndexingKey: process.env.ALGOLIA_API_INDEXING_KEY as string,
index: process.env.ALGOLIA_INDEX as string,
});
} catch (e) {
console.log(e);
} finally {
vite.close();
}
};

indexationAlglolia();
indexationAlglolia({
appId: process.env.ALGOLIA_APP_ID as string,
apiIndexingKey: process.env.ALGOLIA_API_INDEXING_KEY as string,
index: process.env.ALGOLIA_INDEX as string,
});
16 changes: 3 additions & 13 deletions bin/validateMarkdown.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
import { createServer as createViteServer } from 'vite';
import { MarkdownInvalidError, validateMarkdown } from '@/helpers/markdownHelper';

import { MarkdownInvalidError } from '../src/helpers/markdownHelper';
import { getArgs } from './binHelper';

(async (): Promise<void> => {
const args = getArgs<{ ci: boolean }>();

const vite = await createViteServer({
server: { middlewareMode: true },
appType: 'custom',
});

try {
const { validateMarkdown } = await vite.ssrLoadModule('/src/helpers/markdownHelper.ts');
validateMarkdown();
vite.close();
} catch (e) {
vite.close();

const markdownInvalidError = e as MarkdownInvalidError;
} catch (error) {
const markdownInvalidError = error as MarkdownInvalidError;
if (args.ci) {
console.log(`::set-output name=filePath::${markdownInvalidError.markdownFilePathRelative}`);
console.log(`::set-output name=reason::${markdownInvalidError.reason}`);
Expand Down
15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
"type": "module",
"scripts": {
"postinstall": "husky install",
"ts-node": "ts-node -r dotenv/config",
"validate-markdown": "yarn ts-node bin/validateMarkdown",
"indexation:algolia": "yarn ts-node bin/indexationAlgolia",
"build": "yarn ts-node bin/build",
"prerender": "yarn build && yarn ts-node bin/prerender",
"start:dev": "yarn ts-node bin/dev",
"validate-markdown": "tsx bin/validateMarkdown",
"indexation:algolia": "tsx bin/indexationAlgolia",
"build": "tsx bin/build",
"prerender": "yarn build && tsx bin/prerender",
"start:dev": "tsx bin/dev",
"start:prod": "NODE_ENV=production node dist/server",
"start:static": "npx serv --path dist/public",
"lint:es": "eslint --ext .ts,.tsx src bin",
Expand Down Expand Up @@ -107,13 +106,13 @@
"storybook": "^7.1.0",
"stylelint": "^15.2.0",
"stylelint-config-standard-scss": "^7.0.1",
"ts-node": "^10.9.1",
"typescript": "^4.7.4",
"unified": "^10.1.2",
"vite": "^4.4.5",
"vite-tsconfig-paths": "^4.2.0",
"vitest": "^0.33.0",
"zod": "^3.21.4",
"zod-validation-error": "^1.3.1"
"zod-validation-error": "^1.3.1",
"tsx": "^3.12.7"
}
}
2 changes: 2 additions & 0 deletions src/app-paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ export const MARKDOWN_FILE_PATHS = globSync([`${POSTS_DIR}/**/*.md`, `${AUTHORS_
export const PUBLIC_DIR = resolve(ROOT_DIR, 'public');
export const IMGS_DIR = resolve(PUBLIC_DIR, 'imgs');
export const DATA_DIR = resolve(PUBLIC_DIR, 'data');
export const OUT_DIR = resolve(ROOT_DIR, 'dist');
export const OUT_PUBLIC_DIR = resolve(OUT_DIR, 'public');
4 changes: 2 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getEnv } from '@/helpers/getEnvHelper';

export const IS_SSR = import.meta.env.SSR;
export const IS_PRERENDER = import.meta.env.MODE === 'prerender';
export const IS_SSR = import.meta.env?.SSR ?? false;
export const IS_PRERENDER = import.meta.env?.MODE === 'prerender' ?? false;
export const BASE_URL = getEnv<string>('BASE_URL') || '/';
export const AUTHORIZED_LANGUAGES = ['fr', 'en'] as const;
export const DEFAULT_LANGUAGE = 'fr';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useHead, useLink, useMeta, useScript } from 'hoofd';
import { useLink, useMeta, useScript } from 'hoofd';
import React, { useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { matchPath, useLocation } from 'react-router-dom';
Expand All @@ -18,23 +18,9 @@ export const useLayoutTemplateContainer = (): Omit<LayoutTemplateProps, 'childre
const footer = useFooterContainer();
const isHomePage = Boolean(matchPath(PATHS.ROOT, location.pathname));

useHead({
metas: [
{
name: 'google-site-verification',
content: GOOGLE_SITE_VERIFICATION,
},
{
name: 'apple-mobile-web-app-title',
content: 'Blog Eleven Labs',
},
{
name: 'theme-color',
content: themeColor,
},
],
language: i18n.language,
});
useMeta({ name: 'google-site-verification', content: GOOGLE_SITE_VERIFICATION });
useMeta({ name: 'apple-mobile-web-app-title', content: 'Blog Eleven Labs' });
useMeta({ name: 'theme-color', content: themeColor });
useMeta({ property: 'og:locale', content: i18n.language });
useMeta({ property: 'og:site_name', content: 'Blog Eleven Labs' });
useMeta({ property: 'og:url', content: location.pathname + location.search });
Expand Down
1 change: 1 addition & 0 deletions src/entry-server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const render = async (options: RenderOptions): Promise<string> => {
);

const staticPayload = dispatcher.toStatic();

const html = ReactDOMServer.renderToString(
<React.StrictMode>
<HtmlTemplate
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/getEnvHelper.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const getEnv = function <T = string>(key: string): T {
return import.meta.env[key] ?? undefined;
return import.meta?.env ? import.meta.env[key] : undefined;
};
6 changes: 1 addition & 5 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,5 @@
"types": ["vitest/globals", "@types/gtag.js"]
},
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }],
"ts-node": {
"esm": true,
"experimentalSpecifierResolution": "node"
}
"references": [{ "path": "./tsconfig.node.json" }]
}
2 changes: 1 addition & 1 deletion tsconfig.node.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"resolveJsonModule": true,
"downlevelIteration": true
},
"include": ["vite.config.ts", "plugins"]
"include": ["vite.config.ts"]
}
2 changes: 0 additions & 2 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import replace from '@rollup/plugin-replace';
import react from '@vitejs/plugin-react';
import { visualizer } from 'rollup-plugin-visualizer';
import { defineConfig } from 'vite';
import tsconfigPaths from 'vite-tsconfig-paths';

// https://vitejs.dev/config/
export default defineConfig(({ mode, ssrBuild }) => ({
plugins: [
react(),
Expand Down
Loading

0 comments on commit 38a63fa

Please sign in to comment.