@temir.ra/create-ts-lib 0.7.6 → 0.8.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 +6 -0
- package/README.md +39 -19
- package/buildinfo.txt +1 -1
- package/package.json +1 -1
- package/template/README.md +2 -0
- package/template/package.json +10 -2
- package/template/src/constants.ts +3 -0
- package/template/tsconfig.build.json +21 -0
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -143,12 +143,12 @@ The generated package is pre-configured with `build:bundle` only. See [TSC Compi
|
|
|
143
143
|
// convenience script to run the build steps in sequence
|
|
144
144
|
"build": "bun run build:bundle && bun run build:tsc && bun run build:cli-bundle",
|
|
145
145
|
|
|
146
|
-
// compiles the library to declaration files and ESM JavaScript in dist/
|
|
147
|
-
"build:tsc": "tsc --project tsconfig.build.json",
|
|
148
|
-
|
|
149
146
|
// bundles the library into ESM and IIFE formats for distribution
|
|
150
147
|
"build:bundle": "bun run scripts/build-bundle.ts",
|
|
151
148
|
|
|
149
|
+
// compiles the library to declaration files and ESM JavaScript in dist/
|
|
150
|
+
"build:tsc": "tsc --project tsconfig.build.json",
|
|
151
|
+
|
|
152
152
|
// bundles the CLI into a single file for distribution; requires the "bin" field to be set to the bundled output
|
|
153
153
|
"build:cli-bundle": "bun build src/cli.ts --entry-naming \"[dir]/[name].bundle.[ext]\" --outdir dist --target node --format esm --minify --sourcemap=external"
|
|
154
154
|
|
|
@@ -239,7 +239,7 @@ See [`create-workspace` README](https://www.npmjs.com/package/@temir.ra/create-w
|
|
|
239
239
|
}
|
|
240
240
|
```
|
|
241
241
|
|
|
242
|
-
Exports condition `import` may point to the compiled output instead of the bundled output `./dist/index.bundle.js`:
|
|
242
|
+
Exports condition `import` may point to the compiled output instead of the bundled output `./dist/index.bundle.js` if `"emitDeclarationOnly": false`:
|
|
243
243
|
|
|
244
244
|
```json
|
|
245
245
|
{
|
|
@@ -295,7 +295,7 @@ https://your-own-cdn.com/<LIB_NAME>/index.js
|
|
|
295
295
|
|
|
296
296
|
### Bundled - absorbed into the consumer's output
|
|
297
297
|
|
|
298
|
-
The consumer's bundler absorbs the library into their own output. Module identity is destroyed - `import.meta.url` now points to the consumer's bundle, not the library's. However, **the path relationship can be preserved by convention**: if the consumer copies the library's assets into their
|
|
298
|
+
The consumer's bundler absorbs the library into their own output. Module identity is destroyed - `import.meta.url` now points to the consumer's bundle, not the library's. However, **the path relationship can be preserved by convention**: if the consumer copies the library's assets into their output directory at the same relative path the library expects, `import.meta.url` resolution continues to work correctly.
|
|
299
299
|
|
|
300
300
|
### Contract
|
|
301
301
|
|
|
@@ -312,7 +312,7 @@ If the library has runtime assets, the following contract applies:
|
|
|
312
312
|
1. **Copy the assets** - see [When the library is bundled by the consumer](#when-the-library-is-bundled-by-the-consumer) below.
|
|
313
313
|
2. **Ensure the assets are served**
|
|
314
314
|
- if the consumer is a web server, ensure the copied assets are served as static files
|
|
315
|
-
- if the consumer is a bundler, ensure the copied assets are included in the
|
|
315
|
+
- if the consumer is a bundler, ensure the copied assets are included in the output directory
|
|
316
316
|
|
|
317
317
|
#### Scoped assets directory convention
|
|
318
318
|
|
|
@@ -325,35 +325,55 @@ assets/
|
|
|
325
325
|
└── ...
|
|
326
326
|
```
|
|
327
327
|
|
|
328
|
-
This also prevents naming collisions when multiple libraries are bundled into the same consumer app. Each library's assets live under their own namespace; no library can shadow another's files.
|
|
329
|
-
|
|
330
328
|
Add `assets/` to the `files` field in `package.json` so that the assets are included in the published package:
|
|
331
329
|
|
|
332
330
|
```json
|
|
331
|
+
{
|
|
332
|
+
// ... ,
|
|
333
333
|
"files": [
|
|
334
|
-
|
|
335
|
-
"
|
|
336
|
-
"buildinfo.txt",
|
|
337
|
-
"assets"
|
|
334
|
+
// ... ,
|
|
335
|
+
"assets/"
|
|
338
336
|
],
|
|
337
|
+
// ... ,
|
|
338
|
+
}
|
|
339
339
|
```
|
|
340
340
|
|
|
341
341
|
#### Accessing assets
|
|
342
342
|
|
|
343
|
-
Construct asset URLs directly from `import.meta.url
|
|
343
|
+
Construct asset URLs directly from `import.meta.url`:
|
|
344
344
|
|
|
345
345
|
```typescript
|
|
346
346
|
const packageUrl = new URL('../', import.meta.url);
|
|
347
347
|
const assetsUrl = new URL('assets/<@SCOPE>/<LIB_NAME>/', packageUrl);
|
|
348
348
|
|
|
349
|
+
// for `--target browser` and `--target node` (isomorphic)
|
|
350
|
+
const assetUrl = new URL('<ASSET>', assetsUrl);
|
|
351
|
+
const asset = await fetch(assetUrl).then(response => response.body);
|
|
349
352
|
|
|
350
|
-
// for
|
|
351
|
-
const asset = await fetch(new URL('<ASSET>', assetsUrl)).then(r => r.json());
|
|
352
|
-
|
|
353
|
-
// for --target node
|
|
353
|
+
// for `--target node` only
|
|
354
354
|
import { readFile } from 'fs/promises';
|
|
355
355
|
import { fileURLToPath } from 'url';
|
|
356
|
-
const
|
|
356
|
+
const assetUrl = new URL('<ASSET>', assetsUrl);
|
|
357
|
+
const assetPath = fileURLToPath(assetUrl);
|
|
358
|
+
const asset = await readFile(assetPath);
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
The generated `./src/constants.ts` scaffolds the isomorphic approach and `./constants` is configured as an additional entry point in `package.json` for direct imports:
|
|
362
|
+
|
|
363
|
+
```json
|
|
364
|
+
{
|
|
365
|
+
// ... ,
|
|
366
|
+
"exports": {
|
|
367
|
+
// ... ,
|
|
368
|
+
"./constants": {
|
|
369
|
+
"entrypoint": "./src/constants.ts",
|
|
370
|
+
"types": "./dist/constants.d.ts",
|
|
371
|
+
"browser": "./dist/constants.bundle.js",
|
|
372
|
+
"import": "./dist/constants.bundle.js"
|
|
373
|
+
}
|
|
374
|
+
},
|
|
375
|
+
// ... ,
|
|
376
|
+
}
|
|
357
377
|
```
|
|
358
378
|
|
|
359
379
|
#### README statement for library consumers
|
|
@@ -361,7 +381,7 @@ const asset = JSON.parse(await readFile(fileURLToPath(new URL('<ASSET>', assetsU
|
|
|
361
381
|
```markdown
|
|
362
382
|
## Asset resolution
|
|
363
383
|
|
|
364
|
-
This library resolves assets at runtime using `import.meta.url`. If you
|
|
384
|
+
This library resolves assets at runtime using `import.meta.url`. If you include this library into your bundle, copy `node_modules/<@SCOPE>/<LIB_NAME>/assets/<@SCOPE>/<LIB_NAME>/` to your output directory as `assets/<@SCOPE>/<LIB_NAME>/`.
|
|
365
385
|
```
|
|
366
386
|
|
|
367
387
|
#### When the library is bundled by the consumer
|
package/buildinfo.txt
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.
|
|
1
|
+
0.8.0+545e022
|
package/package.json
CHANGED
package/template/README.md
CHANGED
package/template/package.json
CHANGED
|
@@ -17,8 +17,15 @@
|
|
|
17
17
|
"exports": {
|
|
18
18
|
".": {
|
|
19
19
|
"entrypoint": "./src/index.ts",
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
20
21
|
"browser": "./dist/index.bundle.js",
|
|
21
22
|
"import": "./dist/index.bundle.js"
|
|
23
|
+
},
|
|
24
|
+
"./constants": {
|
|
25
|
+
"entrypoint": "./src/constants.ts",
|
|
26
|
+
"types": "./dist/constants.d.ts",
|
|
27
|
+
"browser": "./dist/constants.bundle.js",
|
|
28
|
+
"import": "./dist/constants.bundle.js"
|
|
22
29
|
}
|
|
23
30
|
},
|
|
24
31
|
"imports": {
|
|
@@ -41,8 +48,9 @@
|
|
|
41
48
|
"clean": "bun run clean:dist && bun run clean:tsbuildinfo",
|
|
42
49
|
"tests": "bun test",
|
|
43
50
|
"prebuild": "bun run buildinfo",
|
|
44
|
-
"build": "bun run build:bundle",
|
|
45
|
-
"build:bundle": "bun run scripts/build-bundle.ts"
|
|
51
|
+
"build": "bun run build:bundle && bun run build:tsc",
|
|
52
|
+
"build:bundle": "bun run scripts/build-bundle.ts",
|
|
53
|
+
"build:tsc": "tsc --project tsconfig.build.json"
|
|
46
54
|
},
|
|
47
55
|
"devDependencies": {
|
|
48
56
|
"@types/bun": "latest",
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "./tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"rootDir": "./src/",
|
|
5
|
+
"module": "nodenext",
|
|
6
|
+
"moduleResolution": "nodenext",
|
|
7
|
+
"declaration": true,
|
|
8
|
+
"declarationMap": true,
|
|
9
|
+
"emitDeclarationOnly": true,
|
|
10
|
+
"outDir": "./dist/"
|
|
11
|
+
},
|
|
12
|
+
"include": [
|
|
13
|
+
"src/**/*.ts"
|
|
14
|
+
],
|
|
15
|
+
"exclude": [
|
|
16
|
+
"node_modules/",
|
|
17
|
+
"dist/",
|
|
18
|
+
"tests/",
|
|
19
|
+
"scripts/"
|
|
20
|
+
]
|
|
21
|
+
}
|