Skip to content

Commit 5a6e803

Browse files
committed
build(esm)!: pure esm
Signed-off-by: Lexus Drumgold <[email protected]>
1 parent 686010e commit 5a6e803

File tree

4 files changed

+112
-18
lines changed

4 files changed

+112
-18
lines changed

.attw.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"color": true,
33
"emoji": true,
44
"format": "ascii",
5-
"ignoreRules": [],
5+
"ignoreRules": ["cjs-resolves-to-esm"],
66
"summary": true
77
}

README.md

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# tutils
22

3-
[![github release](https://img.shields.io/github/v/release/flex-development/tutils.svg?include_prereleases&sort=semver)](https://github.com/flex-development/tutils/releases/latest)
3+
[![github release](https://img.shields.io/github/v/release/flex-development/tutils.svg?include_prereleases)](https://github.com/flex-development/tutils/releases/latest)
44
[![npm](https://img.shields.io/npm/v/@flex-development/tutils.svg)](https://npmjs.com/package/@flex-development/tutils)
55
[![codecov](https://codecov.io/gh/flex-development/tutils/branch/main/graph/badge.svg?token=ZT857Z0ITA)](https://codecov.io/gh/flex-development/tutils)
6-
[![module type: cjs+esm](https://img.shields.io/badge/module%20type-cjs%2Besm-brightgreen)](https://github.com/voxpelli/badges-cjs-esm)
6+
[![module type: esm](https://img.shields.io/badge/module%20type-esm-brightgreen)](https://github.com/voxpelli/badges-cjs-esm)
77
[![license](https://img.shields.io/github/license/flex-development/tutils.svg)](LICENSE.md)
88
[![conventional commits](https://img.shields.io/badge/-conventional%20commits-fe5196?logo=conventional-commits&logoColor=ffffff)](https://conventionalcommits.org/)
99
[![typescript](https://img.shields.io/badge/-typescript-3178c6?logo=typescript&logoColor=ffffff)](https://typescriptlang.org/)
@@ -30,6 +30,8 @@ This package contains a set of [TypeScript][1]-friendly utilities.
3030

3131
## Install
3232

33+
This package is [ESM only][2].
34+
3335
```sh
3436
yarn add @flex-development/tutils
3537
```
@@ -53,9 +55,13 @@ yarn add @flex-development/tutils@flex-development/tutils
5355

5456
## Related
5557

56-
- [`pkg-types`][2] - TypeScript definitions for `package.json`
57-
- [`tsconfig-types`][3] - TypeScript definitions for `tsconfig.json`
58+
- [`esm-types`][3] - TypeScript definitions for [ECMAScript modules][4]
59+
- [`pkg-types`][5] - TypeScript definitions for `package.json`
60+
- [`tsconfig-types`][6] - TypeScript definitions for `tsconfig.json`
5861

5962
[1]: https://typescriptlang.org/
60-
[2]: https://github.com/flex-development/pkg-types
61-
[3]: https://github.com/flex-development/tsconfig-types
63+
[2]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
64+
[3]: https://github.com/flex-development/esm-types
65+
[4]: https://nodejs.org/api/esm.html
66+
[5]: https://github.com/flex-development/pkg-types
67+
[6]: https://github.com/flex-development/tsconfig-types

build.config.ts

+97-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44
* @see https://github.com/flex-development/mkbuild
55
*/
66

7-
import { defineBuildConfig, type Config } from '@flex-development/mkbuild'
7+
import {
8+
defineBuildConfig,
9+
type Config,
10+
type OutputMetadata
11+
} from '@flex-development/mkbuild'
12+
import pathe from '@flex-development/pathe'
13+
import type { BuildResult, OutputFile, PluginBuild } from 'esbuild'
814
import pkg from './package.json' assert { type: 'json' }
915
import tsconfig from './tsconfig.build.json' assert { type: 'json' }
1016

@@ -15,9 +21,96 @@ import tsconfig from './tsconfig.build.json' assert { type: 'json' }
1521
*/
1622
const config: Config = defineBuildConfig({
1723
charset: 'utf8',
18-
entries: [{ format: 'esm' }, { format: 'cjs' }],
19-
sourcemap: true,
20-
sourcesContent: false,
24+
entries: [
25+
{ dts: 'only' },
26+
{ dts: false, pattern: ['**/index.ts', 'enums/*'] },
27+
{
28+
dts: false,
29+
pattern: ['!utils/*.options.ts', 'index.ts', 'utils/*'],
30+
sourceRoot: pathe.join(pkg.homepage, 'tree', pkg.tagPrefix + pkg.version),
31+
sourcemap: true,
32+
sourcesContent: true
33+
}
34+
],
35+
minifySyntax: true,
36+
plugins: [
37+
{
38+
/**
39+
* Plugin name.
40+
*
41+
* @const {string} name
42+
*/
43+
name: 'fix-sourcemaps',
44+
45+
/**
46+
* Makes sourcemap files relative to [`sourceRoot`][1].
47+
*
48+
* [1]: https://esbuild.github.io/api/#source-root
49+
* [2]: https://esbuild.github.io/plugins
50+
*
51+
* @see https://github.com/evanw/esbuild/issues/2218
52+
*
53+
* @param {PluginBuild} build - [esbuild plugin api][2]
54+
* @param {PluginBuild['onEnd']} build.onEnd - Build end callback
55+
* @return {void} Nothing when complete
56+
*/
57+
setup({ initialOptions, onEnd }: PluginBuild): void {
58+
const { absWorkingDir = process.cwd() } = initialOptions
59+
60+
return void onEnd((result: BuildResult): void => {
61+
/**
62+
* Output file objects.
63+
*
64+
* @const {OutputFile[]} outputFiles
65+
*/
66+
const outputFiles: OutputFile[] = []
67+
68+
for (const output of result.outputFiles!) {
69+
if (output.path.endsWith('.map')) {
70+
/**
71+
* Relative path to output file sourcemap is for.
72+
*
73+
* **Note**: Relative to {@linkcode absWorkingDir}.
74+
*
75+
* @const {string} outfile
76+
*/
77+
const outfile: string = output.path
78+
.replace(absWorkingDir, '')
79+
.replace(/^\//, '')
80+
.replace(/\.map$/, '')
81+
82+
/**
83+
* Output metadata for {@linkcode outfile}.
84+
*
85+
* @const {OutputMetadata} metadata
86+
*/
87+
const metadata: OutputMetadata =
88+
result.metafile!.outputs[outfile]!
89+
90+
/**
91+
* Parsed sourcemap object.
92+
*
93+
* @const {{ sources: string[] }}
94+
*/
95+
const map: { sources: string[] } = JSON.parse(output.text)
96+
97+
// reset sources to outfile entry point
98+
map.sources = [metadata.entryPoint!]
99+
100+
// redefine outfile text
101+
Object.defineProperty(output, 'text', {
102+
get: (): string => JSON.stringify(map, null, 2)
103+
})
104+
}
105+
106+
outputFiles.push(output)
107+
}
108+
109+
return void (result.outputFiles = outputFiles)
110+
})
111+
}
112+
}
113+
],
21114
target: [
22115
pkg.engines.node.replace(/^\D+/, 'node'),
23116
tsconfig.compilerOptions.target

package.json

+2-7
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,16 @@
3030
"type": "module",
3131
"files": [
3232
"CHANGELOG.md",
33-
"dist",
34-
"src"
33+
"dist"
3534
],
3635
"exports": {
37-
".": {
38-
"import": "./dist/index.mjs",
39-
"require": "./dist/index.cjs"
40-
},
36+
".": "./dist/index.mjs",
4137
"./package.json": "./package.json"
4238
},
4339
"imports": {
4440
"#src": "./src/index.ts",
4541
"#src/*": "./src/*.ts"
4642
},
47-
"main": "./dist/index.cjs",
4843
"module": "./dist/index.mjs",
4944
"types": "./dist/index.d.mts",
5045
"scripts": {

0 commit comments

Comments
 (0)