Skip to content

build cjs and esm with Vite #1745

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions .cspell.json

This file was deleted.

7 changes: 3 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
lib
esm
umd
/out
*.tsbuildinfo

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

# Icon must end with two \r
Icon
Icon


# Thumbnails
._*
Expand Down
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "./node_modules/@biomejs/biome/configuration_schema.json",
"organizeImports": { "enabled": true },
"files": {
"ignore": ["./lib", "./esm", "./umd", "./coverage"]
"ignore": ["./out", "./coverage"]
},
"linter": {
"enabled": true
Expand Down
29 changes: 14 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,28 @@
"version": "9.0.0",
"type": "module",
"description": "parse Scrapbox notation to JavaScript Object",
"files": ["lib", "esm", "umd"],
"main": "./lib/index.js",
"module": "./esm/index.js",
"types": "./lib/index.d.ts",
"unpkg": "./umd/scrapbox-parser.umd.cjs",
"files": ["out"],
"main": "./out/index.cjs",
"module": "./out/index.mjs",
"types": "./out/types/index.d.ts",
"unpkg": "./out/scrapbox-parser.umd.js",
"exports": {
"import": "./esm/index.js",
"require": "./lib/index.js",
"node": "./esm/index.js",
"default": "./lib/index.js"
"import": "./out/index.mjs",
"require": "./out/index.cjs",
"node": "./out/index.mjs",
"default": "./out/index.cjs"
},
"scripts": {
"prebuild": "node -e 'fs.rmSync(`out`, {recursive:true, force:true})'",
"build": "run-p build:*",
"build:cjs": "tsc -p ./tsconfig.cjs.json",
"build:esm": "tsc -p ./tsconfig.esm.json",
"build:umd": "vite build",
"prepare": "run-s clean build",
"clean": "node -e '[`lib`, `esm`, `umd`, `.cjs.tsbuildinfo`, `.esm.tsbuildinfo`].forEach(path => { fs.rmSync(path, {recursive:true, force:true}) })'",
"build:types": "tsc -p ./tsconfig.json",
"build:vite": "vite build",
"prepare": "npm run build",
"test": "vitest run --coverage",
"test:update": "vitest run --updateSnapshot --no-cache",
"lint": "run-p lint:*",
"lint:biome": "biome check .",
"lint:tsc": "tsc -p ./tsconfig.eslint.json",
"lint:tsc": "tsc -p ./tsconfig.lint.json",
"format": "biome check --write ."
},
"repository": {
Expand Down
9 changes: 0 additions & 9 deletions tsconfig.cjs.json

This file was deleted.

9 changes: 0 additions & 9 deletions tsconfig.esm.json

This file was deleted.

16 changes: 8 additions & 8 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
/* Visit https://aka.ms/tsconfig to read more about this file */

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

/* Modules */
"module": "commonjs" /* Specify what module code is generated. */,
"module": "Preserve" /* Specify what module code is generated. */,
// "rootDir": "./", /* Specify the root folder within your source files. */
"moduleResolution": "Node" /* Specify how TypeScript looks up a file from a given module specifier. */,
"moduleResolution": "Bundler" /* Specify how TypeScript looks up a file from a given module specifier. */,
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
// "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
"allowImportingTsExtensions": true /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */,
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
Expand All @@ -51,7 +51,7 @@
/* Emit */
"declaration": true /* Generate .d.ts files from TypeScript and JavaScript files in your project. */,
"declarationMap": true /* Create sourcemaps for d.ts files. */,
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
"emitDeclarationOnly": true /* Only output d.ts files and not JavaScript files. */,
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
// "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. */
Expand All @@ -69,11 +69,11 @@
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
"declarationDir": "./out" /* Specify the output directory for generated declaration files. */,

/* Interop Constraints */
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
// "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. */
"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. */,
// "isolatedDeclarations": true, /* Require sufficient annotation on exports so other tools can trivially generate declaration files. */
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
Expand Down
File renamed without changes.
42 changes: 25 additions & 17 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
/// <reference types="vitest" />
import { resolve } from "node:path";
import { defineConfig } from "vite";
import { defineConfig, type LibraryFormats } from "vite";

const formats = ["es", "cjs", "umd"] satisfies LibraryFormats[];

export default defineConfig({
build: {
emptyOutDir: false,
outDir: "umd",
lib: {
formats: ["umd"],
entry: resolve(__dirname, "src/index.ts"),
name: "ScrapboxParser",
fileName: "scrapbox-parser",
},
},
test: {
include: ["**/tests/**/*.test.ts"],
coverage: {
include: ["src/**/*.ts"],
},
},
build: {
emptyOutDir: false,
outDir: "out",
lib: {
formats,
entry: resolve(__dirname, "src/index.ts"),
fileName: (format) =>
((
{
cjs: "index.cjs",
es: "index.mjs",
umd: "scrapbox-parser.umd.js",
} satisfies Record<(typeof formats)[number], string>
)[format]),
},
},
test: {
include: ["**/tests/**/*.test.ts"],
coverage: {
include: ["src/**/*.ts"],
},
},
});
Loading