Skip to content

Commit 1b243c2

Browse files
authored
Merge pull request #1745 from progfay/build-vite
build cjs and esm with Vite
2 parents f5ae870 + 89f94bf commit 1b243c2

File tree

9 files changed

+51
-81
lines changed

9 files changed

+51
-81
lines changed

.cspell.json

Lines changed: 0 additions & 18 deletions
This file was deleted.

.gitignore

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
lib
2-
esm
3-
umd
1+
/out
42
*.tsbuildinfo
53

64
### https://raw.github.com/github/gitignore/4a8e0a151becd5ccbb83e4aca6e6c195f3d506fd/Global/macOS.gitignore
@@ -11,7 +9,8 @@ umd
119
.LSOverride
1210

1311
# Icon must end with two \r
14-
Icon
12+
Icon
13+
1514

1615
# Thumbnails
1716
._*

biome.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"$schema": "./node_modules/@biomejs/biome/configuration_schema.json",
33
"organizeImports": { "enabled": true },
44
"files": {
5-
"ignore": ["./lib", "./esm", "./umd", "./coverage"]
5+
"ignore": ["./out", "./coverage"]
66
},
77
"linter": {
88
"enabled": true

package.json

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,28 @@
33
"version": "9.0.0",
44
"type": "module",
55
"description": "parse Scrapbox notation to JavaScript Object",
6-
"files": ["lib", "esm", "umd"],
7-
"main": "./lib/index.js",
8-
"module": "./esm/index.js",
9-
"types": "./lib/index.d.ts",
10-
"unpkg": "./umd/scrapbox-parser.umd.cjs",
6+
"files": ["out"],
7+
"main": "./out/index.cjs",
8+
"module": "./out/index.mjs",
9+
"types": "./out/types/index.d.ts",
10+
"unpkg": "./out/scrapbox-parser.umd.js",
1111
"exports": {
12-
"import": "./esm/index.js",
13-
"require": "./lib/index.js",
14-
"node": "./esm/index.js",
15-
"default": "./lib/index.js"
12+
"import": "./out/index.mjs",
13+
"require": "./out/index.cjs",
14+
"node": "./out/index.mjs",
15+
"default": "./out/index.cjs"
1616
},
1717
"scripts": {
18+
"prebuild": "node -e 'fs.rmSync(`out`, {recursive:true, force:true})'",
1819
"build": "run-p build:*",
19-
"build:cjs": "tsc -p ./tsconfig.cjs.json",
20-
"build:esm": "tsc -p ./tsconfig.esm.json",
21-
"build:umd": "vite build",
22-
"prepare": "run-s clean build",
23-
"clean": "node -e '[`lib`, `esm`, `umd`, `.cjs.tsbuildinfo`, `.esm.tsbuildinfo`].forEach(path => { fs.rmSync(path, {recursive:true, force:true}) })'",
20+
"build:types": "tsc -p ./tsconfig.json",
21+
"build:vite": "vite build",
22+
"prepare": "npm run build",
2423
"test": "vitest run --coverage",
2524
"test:update": "vitest run --updateSnapshot --no-cache",
2625
"lint": "run-p lint:*",
2726
"lint:biome": "biome check .",
28-
"lint:tsc": "tsc -p ./tsconfig.eslint.json",
27+
"lint:tsc": "tsc -p ./tsconfig.lint.json",
2928
"format": "biome check --write ."
3029
},
3130
"repository": {

tsconfig.cjs.json

Lines changed: 0 additions & 9 deletions
This file was deleted.

tsconfig.esm.json

Lines changed: 0 additions & 9 deletions
This file was deleted.

tsconfig.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
/* Visit https://aka.ms/tsconfig to read more about this file */
44

55
/* Projects */
6-
"incremental": true /* Save .tsbuildinfo files to allow for incremental compilation of projects. */,
7-
"composite": true /* Enable constraints that allow a TypeScript project to be used with project references. */,
6+
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
7+
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
88
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
99
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
1010
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
@@ -25,17 +25,17 @@
2525
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
2626

2727
/* Modules */
28-
"module": "commonjs" /* Specify what module code is generated. */,
28+
"module": "Preserve" /* Specify what module code is generated. */,
2929
// "rootDir": "./", /* Specify the root folder within your source files. */
30-
"moduleResolution": "Node" /* Specify how TypeScript looks up a file from a given module specifier. */,
30+
"moduleResolution": "Bundler" /* Specify how TypeScript looks up a file from a given module specifier. */,
3131
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
3232
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
3333
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
3434
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
3535
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
3636
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
3737
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
38-
// "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
38+
"allowImportingTsExtensions": true /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */,
3939
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
4040
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
4141
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
@@ -51,7 +51,7 @@
5151
/* Emit */
5252
"declaration": true /* Generate .d.ts files from TypeScript and JavaScript files in your project. */,
5353
"declarationMap": true /* Create sourcemaps for d.ts files. */,
54-
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
54+
"emitDeclarationOnly": true /* Only output d.ts files and not JavaScript files. */,
5555
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
5656
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
5757
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
@@ -69,11 +69,11 @@
6969
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
7070
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
7171
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
72-
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
72+
"declarationDir": "./out" /* Specify the output directory for generated declaration files. */,
7373

7474
/* Interop Constraints */
7575
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
76-
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
76+
"verbatimModuleSyntax": true /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */,
7777
// "isolatedDeclarations": true, /* Require sufficient annotation on exports so other tools can trivially generate declaration files. */
7878
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
7979
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
File renamed without changes.

vite.config.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
11
/// <reference types="vitest" />
22
import { resolve } from "node:path";
3-
import { defineConfig } from "vite";
3+
import { defineConfig, type LibraryFormats } from "vite";
4+
5+
const formats = ["es", "cjs", "umd"] satisfies LibraryFormats[];
46

57
export default defineConfig({
6-
build: {
7-
emptyOutDir: false,
8-
outDir: "umd",
9-
lib: {
10-
formats: ["umd"],
11-
entry: resolve(__dirname, "src/index.ts"),
12-
name: "ScrapboxParser",
13-
fileName: "scrapbox-parser",
14-
},
15-
},
16-
test: {
17-
include: ["**/tests/**/*.test.ts"],
18-
coverage: {
19-
include: ["src/**/*.ts"],
20-
},
21-
},
8+
build: {
9+
emptyOutDir: false,
10+
outDir: "out",
11+
lib: {
12+
formats,
13+
entry: resolve(__dirname, "src/index.ts"),
14+
fileName: (format) =>
15+
((
16+
{
17+
cjs: "index.cjs",
18+
es: "index.mjs",
19+
umd: "scrapbox-parser.umd.js",
20+
} satisfies Record<(typeof formats)[number], string>
21+
)[format]),
22+
},
23+
},
24+
test: {
25+
include: ["**/tests/**/*.test.ts"],
26+
coverage: {
27+
include: ["src/**/*.ts"],
28+
},
29+
},
2230
});

0 commit comments

Comments
 (0)