onlybuild 1.2.1 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # Changelog
2
2
 
3
+ ## [v1.4.0](https://github.com/neogeek/onlybuild/tree/v1.4.0) - (2024-05-08)
4
+
5
+ [Full Changelog](https://github.com/neogeek/onlybuild/compare/v1.3.0...v1.4.0)
6
+
7
+ - [feat] Switch typescript work without a flag. [#18](https://github.com/neogeek/onlybuild/pull/18)
8
+ - [feat] TSX/React.js Support [#17](https://github.com/neogeek/onlybuild/pull/17)
9
+ - [hotfix] Fixed TypeScript logic to not override JavaScript functionality. [#16](https://github.com/neogeek/onlybuild/pull/16)
10
+
11
+ ## [v1.3.0](https://github.com/neogeek/onlybuild/tree/v1.3.0) - (2024-05-08)
12
+
13
+ [Full Changelog](https://github.com/neogeek/onlybuild/compare/v1.2.1...v1.3.0)
14
+
15
+ - [hotfix] Updated packages. [#15](https://github.com/neogeek/onlybuild/pull/15)
16
+ - [feat] Added TypeScript support. (experimental) [#14](https://github.com/neogeek/onlybuild/pull/14)
17
+ - [hotfix] Added experimental code coverage flag to test command. [#13](https://github.com/neogeek/onlybuild/pull/13)
18
+ - [feat] Split build and copy logic into separate methods. [#12](https://github.com/neogeek/onlybuild/pull/12)
19
+
3
20
  ## [v1.2.1](https://github.com/neogeek/onlybuild/tree/v1.2.1) - (2024-05-07)
4
21
 
5
22
  [Full Changelog](https://github.com/neogeek/onlybuild/compare/v1.2.0...v1.2.1)
package/README.md CHANGED
@@ -32,6 +32,8 @@
32
32
  - [Getting Started](#getting-started)
33
33
  - [File Structure](#file-structure)
34
34
  - [Ignore Files](#ignore-files)
35
+ - [React](#react)
36
+ - [TypeScript](#typescript)
35
37
  - [Formatting Files](#formatting-files)
36
38
  - [Watching For Changes](#watching-for-changes)
37
39
  - [Local Server](#local-server)
@@ -262,6 +264,43 @@ screenshot.png
262
264
  LICENSE
263
265
  ```
264
266
 
267
+ ## React
268
+
269
+ If you want to use [React.js](https://react.dev/), instead of <code>&#96;html&#96;</code> string templates, you can do that by using `react-dom/server` in a `.jsx` or `.tsx` file.
270
+
271
+ ```javascript
272
+ import React from 'react';
273
+ import { renderToString } from 'react-dom/server';
274
+
275
+ function Hello() {
276
+ return <h1>Hello, React!</h1>;
277
+ }
278
+
279
+ export default renderToString(<Hello />);
280
+ ```
281
+
282
+ In order for `.jsx` or `.tsx` files to work property you will need to add `"type": "module"` to your `package.json`.
283
+
284
+ ```json
285
+ {
286
+ ...
287
+ "type": "module",
288
+ ...
289
+ }
290
+ ```
291
+
292
+ ## TypeScript
293
+
294
+ In order for TypeScript files to work property you will need to add `"type": "module"` to your `package.json`.
295
+
296
+ ```json
297
+ {
298
+ ...
299
+ "type": "module",
300
+ ...
301
+ }
302
+ ```
303
+
265
304
  ## Formatting Files
266
305
 
267
306
  If you want to reformat the HTML files in the build directory, you can use [Prettier](https://prettier.io/) after the build completes.
@@ -327,6 +366,7 @@ Serving the files once the build is complete is easy using the NPM package [http
327
366
  1. [External API](./examples/external-api/) - Output data fetched from an external API.
328
367
  1. [<code>&#96;html&#96;</code> String Template](./examples/html-string-template/) - Use the <code>&#96;html&#96;</code> string template utility to add syntax highlighting to HTML.
329
368
  1. [Includes](./examples/includes/) - An example that uses reusable includes for building multiple pages.
369
+ 1. [React.js](./examples/react.js/) - An example using React.js to render HTML.
330
370
 
331
371
  ## Benchmark
332
372
 
@@ -1,3 +1,4 @@
1
1
  #!/usr/bin/env node --no-warnings
2
2
  import 'dotenv/config';
3
+ import 'tsx/esm';
3
4
  export {};
package/dist/bin/index.js CHANGED
@@ -1,8 +1,9 @@
1
1
  #!/usr/bin/env node --no-warnings
2
2
  import 'dotenv/config';
3
+ import 'tsx/esm';
3
4
  import { globby } from 'globby';
4
5
  import parseCmdArgs from 'parse-cmd-args';
5
- import { buildFiles } from '../src/build.js';
6
+ import { buildFiles, writeFiles } from '../src/build.js';
6
7
  import { copyFiles } from '../src/copy.js';
7
8
  import pkg from '../package.json' assert { type: 'json' };
8
9
  const args = parseCmdArgs(null, {
@@ -30,14 +31,25 @@ const [buildDir = 'build/'] = [args.flags['--out'], args.flags['-o']]
30
31
  const [ignoreFile = '.onlyignore'] = [args.flags['--ignore'], args.flags['-i']]
31
32
  .filter(flag => typeof flag === 'string')
32
33
  .map(String);
33
- await buildFiles(await globby(['**/*.mjs', '!_*/**/*', '!node_modules/', `!${buildDir}`], {
34
+ const filesToBuild = await globby([
35
+ '**/*.mjs',
36
+ '**/*.jsx',
37
+ '**/*.ts',
38
+ '**/*.tsx',
39
+ '!_*/**/*',
40
+ '!node_modules/',
41
+ `!${buildDir}`
42
+ ].filter(Boolean), {
34
43
  gitignore: false,
35
44
  ignoreFiles: [ignoreFile],
36
45
  cwd: args.inputs[0]
37
- }), buildDir);
38
- await copyFiles(await globby([
46
+ });
47
+ const filesToCopy = await globby([
39
48
  '**/*',
40
49
  '!**/*.mjs',
50
+ '!**/*.jsx',
51
+ '!**/*.ts',
52
+ '!**/*.tsx',
41
53
  '!_*/**/*',
42
54
  '!package.json',
43
55
  '!package-lock.json',
@@ -47,4 +59,6 @@ await copyFiles(await globby([
47
59
  gitignore: false,
48
60
  ignoreFiles: [ignoreFile],
49
61
  cwd: args.inputs[0]
50
- }), buildDir);
62
+ });
63
+ await writeFiles(await buildFiles(filesToBuild), buildDir);
64
+ await copyFiles(filesToCopy, buildDir);
package/dist/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "onlybuild",
3
3
  "description": "A zero-config cli for building static websites.",
4
- "version": "1.2.1",
4
+ "version": "1.4.0",
5
5
  "engines": {
6
6
  "node": ">=20.x"
7
7
  },
@@ -19,15 +19,15 @@
19
19
  "dependencies": {
20
20
  "dotenv": "16.4.5",
21
21
  "globby": "14.0.1",
22
- "parse-cmd-args": "5.0.2"
22
+ "parse-cmd-args": "5.0.2",
23
+ "tsx": "4.9.3"
23
24
  },
24
25
  "devDependencies": {
25
- "@types/node": "20.12.10",
26
- "tsx": "4.9.3",
26
+ "@types/node": "20.12.11",
27
27
  "typescript": "5.4.5"
28
28
  },
29
29
  "scripts": {
30
- "test": "node --import tsx --test ./src/*.test.ts",
30
+ "test": "node --import tsx --test --experimental-test-coverage ./src/*.test.ts",
31
31
  "build": "rm -rf dist/ && tsc && chmod +x ./dist/bin/index.js",
32
32
  "prepare": "npm run build"
33
33
  },
@@ -1,22 +1,43 @@
1
1
  /**
2
- * Write the file to the build directory while creating the directory, recursively, if it doesn't exist.
2
+ * Calculates the output path of the file based on the original input path.
3
3
  *
4
4
  * @param {string} path
5
- * @param {string} buildDir
6
- * @param {string} contents
7
5
  */
8
- export declare const writeFileAndMakeDir: (path: string, buildDir: string, contents: string) => Promise<void>;
6
+ export declare const calculateOutputPathFromInputPath: (path: string) => string;
9
7
  /**
10
- * Import the default export of a JavaScript file and check that the default export is a string before writing the contents to a build file.
8
+ * Returns the default export if it is a string.
11
9
  *
12
10
  * @param {string} path
13
- * @param {string} buildDir
11
+ * @returns {Promise<{ path: string; contents: string }>}
14
12
  */
15
- export declare const buildFile: (path: string, buildDir: string) => Promise<void>;
13
+ export declare const buildFile: (path: string) => Promise<{
14
+ path: string;
15
+ contents: string;
16
+ }>;
16
17
  /**
17
- * Iterate over all the file paths and write them to the build directory.
18
+ * Iterate over all file paths and returns the default export for each file if it is a string.
18
19
  *
19
20
  * @param {string[]} paths
21
+ * @returns {Promise<{ path: string; contents: string }[]>}
22
+ */
23
+ export declare const buildFiles: (paths: string[]) => Promise<{
24
+ path: string;
25
+ contents: string;
26
+ }[]>;
27
+ /**
28
+ * Writes the contents of a file to a path, creating parent directories as needed.
29
+ *
30
+ * @param {string} path
31
+ * @param {string} contents
32
+ */
33
+ export declare const writeFileAndMakeDir: (path: string, contents: string) => Promise<void>;
34
+ /**
35
+ * Write files to the build directory.
36
+ *
37
+ * @param {{ path: string; contents: string }[]} files,
20
38
  * @param {string} buildDir
21
39
  */
22
- export declare const buildFiles: (paths: string[], buildDir: string) => Promise<void>;
40
+ export declare const writeFiles: (files: {
41
+ path: string;
42
+ contents: string;
43
+ }[], buildDir: string) => Promise<void>;
package/dist/src/build.js CHANGED
@@ -1,38 +1,52 @@
1
1
  import { writeFile, mkdir } from 'node:fs/promises';
2
- import { basename, dirname, join, resolve } from 'node:path';
2
+ import { dirname, join, parse, resolve } from 'node:path';
3
3
  /**
4
- * Write the file to the build directory while creating the directory, recursively, if it doesn't exist.
4
+ * Calculates the output path of the file based on the original input path.
5
5
  *
6
6
  * @param {string} path
7
- * @param {string} buildDir
8
- * @param {string} contents
9
7
  */
10
- export const writeFileAndMakeDir = async (path, buildDir, contents) => {
11
- const filename = basename(path, '.mjs');
12
- const directory = filename === 'index'
13
- ? join(buildDir, dirname(path))
14
- : join(buildDir, dirname(path), filename);
15
- await mkdir(directory, { recursive: true });
16
- await writeFile(join(directory, 'index.html'), contents);
8
+ export const calculateOutputPathFromInputPath = (path) => {
9
+ const filename = parse(path).name;
10
+ const directory = filename === 'index' ? dirname(path) : join(dirname(path), filename);
11
+ return join(directory, 'index.html');
17
12
  };
18
13
  /**
19
- * Import the default export of a JavaScript file and check that the default export is a string before writing the contents to a build file.
14
+ * Returns the default export if it is a string.
20
15
  *
21
16
  * @param {string} path
22
- * @param {string} buildDir
17
+ * @returns {Promise<{ path: string; contents: string }>}
23
18
  */
24
- export const buildFile = async (path, buildDir) => {
19
+ export const buildFile = async (path) => {
25
20
  const contents = (await import(resolve(path))).default;
26
- if (typeof contents === 'string') {
27
- await writeFileAndMakeDir(path, buildDir, contents);
28
- }
21
+ return { path, contents: typeof contents === 'string' ? contents : '' };
29
22
  };
30
23
  /**
31
- * Iterate over all the file paths and write them to the build directory.
24
+ * Iterate over all file paths and returns the default export for each file if it is a string.
32
25
  *
33
26
  * @param {string[]} paths
27
+ * @returns {Promise<{ path: string; contents: string }[]>}
28
+ */
29
+ export const buildFiles = async (paths) => {
30
+ return (await Promise.all(paths.map(async (path) => await buildFile(path)))).filter(({ contents }) => contents !== '');
31
+ };
32
+ /**
33
+ * Writes the contents of a file to a path, creating parent directories as needed.
34
+ *
35
+ * @param {string} path
36
+ * @param {string} contents
37
+ */
38
+ export const writeFileAndMakeDir = async (path, contents) => {
39
+ await mkdir(dirname(path), { recursive: true });
40
+ await writeFile(path, contents);
41
+ };
42
+ /**
43
+ * Write files to the build directory.
44
+ *
45
+ * @param {{ path: string; contents: string }[]} files,
34
46
  * @param {string} buildDir
35
47
  */
36
- export const buildFiles = async (paths, buildDir) => {
37
- await Promise.all(paths.map(async (path) => await buildFile(path, buildDir)));
48
+ export const writeFiles = async (files, buildDir) => {
49
+ await Promise.all(files.map(async (file) => {
50
+ await writeFileAndMakeDir(join(buildDir, calculateOutputPathFromInputPath(file.path)), file.contents);
51
+ }));
38
52
  };
@@ -1,12 +1,12 @@
1
1
  /**
2
- * Copy the file to the build directory while creating the directory, recursively, if it doesn't exist.
2
+ * Copies a file from one path to another, creating parent directories as needed.
3
3
  *
4
- * @param {string} path
5
- * @param {string} buildDir
4
+ * @param {string} src
5
+ * @param {string} dest
6
6
  */
7
- export declare const copyFileAndMakeDir: (path: string, buildDir: string) => Promise<void>;
7
+ export declare const copyFileAndMakeDir: (src: string, dest: string) => Promise<void>;
8
8
  /**
9
- * Iterate over all the file paths and copy them to the build directory.
9
+ * Iterate over all file paths and copy them to the build directory.
10
10
  *
11
11
  * @param {string[]} paths
12
12
  * @param {string} buildDir
package/dist/src/copy.js CHANGED
@@ -1,21 +1,21 @@
1
1
  import { copyFile, mkdir } from 'node:fs/promises';
2
2
  import { dirname, join } from 'node:path';
3
3
  /**
4
- * Copy the file to the build directory while creating the directory, recursively, if it doesn't exist.
4
+ * Copies a file from one path to another, creating parent directories as needed.
5
5
  *
6
- * @param {string} path
7
- * @param {string} buildDir
6
+ * @param {string} src
7
+ * @param {string} dest
8
8
  */
9
- export const copyFileAndMakeDir = async (path, buildDir) => {
10
- await mkdir(join(buildDir, dirname(path)), { recursive: true });
11
- await copyFile(path, join(buildDir, path));
9
+ export const copyFileAndMakeDir = async (src, dest) => {
10
+ await mkdir(dirname(dest), { recursive: true });
11
+ await copyFile(src, dest);
12
12
  };
13
13
  /**
14
- * Iterate over all the file paths and copy them to the build directory.
14
+ * Iterate over all file paths and copy them to the build directory.
15
15
  *
16
16
  * @param {string[]} paths
17
17
  * @param {string} buildDir
18
18
  */
19
19
  export const copyFiles = async (paths, buildDir) => {
20
- await Promise.all(paths.map(async (path) => await copyFileAndMakeDir(path, buildDir)));
20
+ await Promise.all(paths.map(async (path) => await copyFileAndMakeDir(path, join(buildDir, path))));
21
21
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "onlybuild",
3
3
  "description": "A zero-config cli for building static websites.",
4
- "version": "1.2.1",
4
+ "version": "1.4.0",
5
5
  "engines": {
6
6
  "node": ">=20.x"
7
7
  },
@@ -19,15 +19,15 @@
19
19
  "dependencies": {
20
20
  "dotenv": "16.4.5",
21
21
  "globby": "14.0.1",
22
- "parse-cmd-args": "5.0.2"
22
+ "parse-cmd-args": "5.0.2",
23
+ "tsx": "4.9.3"
23
24
  },
24
25
  "devDependencies": {
25
- "@types/node": "20.12.10",
26
- "tsx": "4.9.3",
26
+ "@types/node": "20.12.11",
27
27
  "typescript": "5.4.5"
28
28
  },
29
29
  "scripts": {
30
- "test": "node --import tsx --test ./src/*.test.ts",
30
+ "test": "node --import tsx --test --experimental-test-coverage ./src/*.test.ts",
31
31
  "build": "rm -rf dist/ && tsc && chmod +x ./dist/bin/index.js",
32
32
  "prepare": "npm run build"
33
33
  },