Skip to content

Commit

Permalink
build!: pure esm
Browse files Browse the repository at this point in the history
  • Loading branch information
unicornware committed Sep 28, 2022
1 parent eba6975 commit f28c85a
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 1,074 deletions.
2 changes: 1 addition & 1 deletion .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"patches/",
"yarn.lock"
],
"ignoreRegExpList": [],
"ignoreRegExpList": ["/from\\s+(['\"]).*\\1/"],
"ignoreWords": [],
"language": "en-US",
"patterns": [],
Expand Down
3 changes: 1 addition & 2 deletions .dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ larsgw
lcov
lintstagedrc
micnncim
mkdist
mkbuild
nocheck
npmrc
nums
Expand All @@ -40,7 +40,6 @@ preid
safecrlf
syncer
tsnode
tspaths
vates
vitest
vsicons
Expand Down
18 changes: 0 additions & 18 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,6 @@ const config = {
rules: {
'unicorn/no-process-exit': 0
}
},
{
files: ['src/index.ts'],
rules: {
/**
* mkdist converts `exports.default` to `module.exports = _default`.
*
* `exports.default` statements are only output if `export default ...`
* is used for default exports.
*
* this means that `export { default } from '...'` should **not** be
* used where default exports should be supported, as with the package
* entry point.
*
* @see https://github.com/unjs/mkdist/blob/v0.3.13/src/loaders/js.ts#L40
*/
'unicorn/prefer-export-from': 0
}
}
]
}
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

[![conventional commits](https://img.shields.io/badge/conventional%20commits-1.0.0-yellow.svg)](https://conventionalcommits.org)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
[![module type: esm](https://img.shields.io/badge/module%20type-esm-brightgreen)](https://github.com/voxpelli/badges-cjs-esm)
[![npm](https://img.shields.io/npm/v/@flex-development/dist-tag.svg)](https://npmjs.com/package/@flex-development/dist-tag)
[![license](https://img.shields.io/github/license/flex-development/dist-tag.svg)](LICENSE.md)
[![typescript](https://badgen.net/badge/-/typescript?color=2a72bc&icon=typescript&label)](https://typescriptlang.org)
Expand Down
146 changes: 20 additions & 126 deletions build.config.ts
Original file line number Diff line number Diff line change
@@ -1,135 +1,29 @@
/**
* @file Unbuild Config
* @module config/unbuild
* @see https://github.com/unjs/unbuild#configuration
* @file Build Config
* @module config/build
* @see https://github.com/flex-development/mkbuild
*/

import type { MkdistOptions } from 'mkdist'
import path from 'node:path'
import { applyChanges } from 'resolve-tspaths/dist/steps/applyChanges'
import { computeAliases } from 'resolve-tspaths/dist/steps/computeAliases'
import { generateChanges } from 'resolve-tspaths/dist/steps/generateChanges'
import { getFilesToProcess } from 'resolve-tspaths/dist/steps/getFilesToProcess'
import { loadTSConfig } from 'resolve-tspaths/dist/steps/loadTSConfig'
import type { Alias, Change, TextChange } from 'resolve-tspaths/dist/types'
import {
defineBuildConfig,
type BuildConfig,
type BuildContext,
type BuildOptions,
type MkdistBuildEntry
} from 'unbuild'
import pkg from './package.json'
import { defineBuildConfig, type Config } from '@flex-development/mkbuild'
import tsconfig from './tsconfig.build.json' assert { type: 'json' }

/** @const {BuildConfig} config - Build config */
const config: BuildConfig = defineBuildConfig({
devDependencies: Object.keys(pkg.devDependencies),
/**
* Build configuration options.
*
* @const {Config} config
*/
const config: Config = defineBuildConfig({
entries: [
{
builder: 'mkdist',
declaration: true,
ext: 'cjs',
format: 'cjs',
input: 'src/'
},
{
builder: 'mkdist',
declaration: true,
ext: 'mjs',
format: 'esm',
input: 'src/'
},
{ builder: 'mkdist', declaration: true, input: 'src/' },
{ builder: 'rollup', declaration: false, input: 'src/cli' }
{ declaration: true, ignore: ['cli.ts'] },
{ bundle: true, minify: true, source: 'src/cli.ts' }
],
hooks: {
/**
* Transforms path aliases found in build files.
*
* @param {BuildContext} ctx - Build context
* @param {BuildOptions} ctx.options - Build options
* @return {void} Nothing when complete
*/
'mkdist:done'({ options }: BuildContext): void {
const { outDir, rootDir } = options

try {
const { paths = {} } = loadTSConfig(`${rootDir}/tsconfig.json`).options

/** @const {string[]} files - Build files to process */
const files: string[] = getFilesToProcess(outDir, [
'cjs',
'd.cts',
'd.mts',
'd.ts',
'mjs'
])

/** @const {Alias[]} aliases - Path alias objects */
const aliases: Alias[] = computeAliases(rootDir, paths)

/**
* Changes to apply to build files.
*
* @const {Change[]} changes
*/
const changes: Change[] = generateChanges(files, aliases, {
outPath: outDir,
srcPath: path.resolve('src')
}).map(({ file, text, ...rest }) => {
/** @const {string} ext - {@link file} extension */
const ext: string = path.extname(file)

/**
* Text changes applied to {@link file}.
*
* Each change in {@link rest.changes} will be updated to include the
* current file extension, {@link ext}.
*
* @const {TextChange[]} changes
*/
const changes: TextChange[] = (rest.changes ?? []).map(chg => ({
...chg,
modified: !/node_modules/.test(path.resolve(file, chg.modified))
? chg.modified + ext
: chg.modified
}))

// make sure file extensions are included in path transformations
for (const { modified } of changes) {
text = text.replace(modified.replace(ext, ''), modified)
}

return { changes, file, text }
})

return void applyChanges(changes)
} catch (e: unknown) {
console.error(e instanceof Error ? e.message : e)
}
},
/**
* Updates `mkdist` build options.
*
* @see https://github.com/unjs/mkdist#-usage
*
* @param {BuildContext} _ - Build context
* @param {MkdistBuildEntry} __ - `mkdist` build entry
* @param {MkdistOptions} options - `mkdist` build options
* @return {void} Nothing when complete
*/
'mkdist:entry:options'(_, __, options: MkdistOptions): void {
options.pattern = ['**', '!**/{__mocks__,__snapshots__,__tests__}/**']
}
},
rollup: {
cjsBridge: true,
commonjs: { extensions: ['.cjs', '.js'] },
emitCJS: true,
esbuild: { logLevel: 'info', minify: true, target: 'esnext' },
inlineDependencies: true,
json: { compact: true },
resolve: {}
esbuild: {
platform: 'node',
sourcemap: 'external',
sourcesContent: false,
target: [tsconfig.compilerOptions.target, 'node14'],
treeShaking: true,
tsconfig: 'tsconfig.build.json'
}
})

Expand Down
40 changes: 15 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,25 @@
"dist",
"src"
],
"bin": {
"dist-tag": "./dist/cli.cjs",
"dist-tag-esm": "./dist/cli.mjs"
},
"bin": "./dist/cli.mjs",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
},
"./flags": {
"types": "./dist/flags.d.ts",
"import": "./dist/flags.mjs",
"require": "./dist/flags.cjs"
},
"./options": {
"types": "./dist/options.d.ts",
"import": "./dist/options.mjs",
"require": "./dist/options.cjs"
},
".": "./dist/index.mjs",
"./flags": "./dist/flags.mjs",
"./options": "./dist/options.mjs",
"./package.json": "./package.json"
},
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"types": "./dist/index.d.mts",
"typesVersions": {
"*": {
"*": [
"./dist/*.d.mts",
"./dist/index.d.mts"
]
}
},
"scripts": {
"build": "unbuild",
"build": "mkbuild",
"check:ci": "yarn dedupe --check && yarn check:format && yarn check:lint && yarn check:spelling && yarn check:types && yarn check:types:build && yarn test:cov && NODE_ENV=production yarn pack -o %s-%v.tgz && yarn clean:pack",
"check:format": "prettier --check .",
"check:lint": "eslint --exit-on-fatal-error --ext cjs,cts,gql,json,jsonc,md,mjs,ts,yml --max-warnings 0 .",
Expand Down Expand Up @@ -90,6 +82,7 @@
"@commitlint/config-conventional": "17.0.3",
"@commitlint/types": "17.0.0",
"@faker-js/faker": "7.4.0",
"@flex-development/mkbuild": "git@github.com:flex-development/mkbuild.git",
"@flex-development/tutils": "5.0.0-dev.1",
"@graphql-eslint/eslint-plugin": "3.10.7",
"@jsdevtools/chai-exec": "2.1.1",
Expand Down Expand Up @@ -141,14 +134,12 @@
"prettier": "2.7.1",
"prettier-plugin-sh": "0.12.8",
"pretty-format": "28.1.3",
"resolve-tspaths": "0.7.4",
"sade": "1.8.1",
"trash-cli": "5.0.0",
"ts-dedent": "2.2.0",
"ts-node": "10.9.1",
"tsconfig-paths": "4.1.0",
"typescript": "4.8.2",
"unbuild": "0.8.8",
"version-bump-prompt": "6.1.0",
"vite-tsconfig-paths": "3.5.0",
"vitest": "0.21.1",
Expand All @@ -158,7 +149,6 @@
"resolutions": {
"@ardatan/sync-fetch": "larsgw/sync-fetch#head=worker_threads",
"graphql-config": "patch:graphql-config@npm%3A4.3.5#patches/graphql-config+4.3.5.dev.patch",
"mkdist": "patch:mkdist@npm:0.3.13#patches/mkdist-npm-0.3.13-c41cf41c68.patch",
"typescript": "4.8.2",
"vite": "3.1.3"
},
Expand Down
39 changes: 0 additions & 39 deletions patches/mkdist-npm-0.3.13-c41cf41c68.patch

This file was deleted.

35 changes: 2 additions & 33 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,13 @@
*/

import type mri from 'mri'
import path from 'node:path'
import sade from 'sade'
import pkg from '../package.json' assert { type: 'json' }
import type Flags from './flags'
import lookup from './node'

/* c8 ignore start */

/**
* CLI filename.
*
* @const {string} filename
*/
const filename: string = process.argv[1] ?? ''

/**
* CLI program name.
*
* @var {string} name
*/
let name: string = 'dist-tag'

// update cli program name if running esm-compatible cli
if (/dist-tag-esm/.test(filename) || path.extname(filename) === '.mjs') {
name = 'dist-tag-esm'
}

/**
* CLI program.
*
* @see https://github.com/lukeed/sade#single-command-mode
*
* @const {sade.Sade} program
*/
const program: sade.Sade = sade([name, '[target]'].join(' '))

program
.version(pkg.version) /* c8 ignore stop */
sade(`${pkg.name.replace(/.*\//, '')} [target]`)
.version(pkg.version)
.describe(pkg.description)
.example('2.0.0')
.example('2.0.0-alpha.1')
Expand Down
4 changes: 1 addition & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
* @module dist-tag
*/

import lookup from './node'

export type { default as Flags } from './flags'
export { default } from './node'
export type { default as Options } from './options'
export default lookup

0 comments on commit f28c85a

Please sign in to comment.