@temir.ra/create-template 0.1.1 → 0.1.3

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 CHANGED
@@ -1,5 +1,13 @@
1
1
  # Version 0
2
2
 
3
+ ## 0.1.3
4
+
5
+ 1. Removed `scripts/dev.ts`.
6
+
7
+ ## 0.1.2
8
+
9
+ 1. Updated `package.json` from `create-ts-lib` template.
10
+
3
11
  ## 0.1.1
4
12
 
5
13
  1. Added clarifications to `README.md`.
package/buildinfo.txt CHANGED
@@ -1 +1 @@
1
- 0.1.1+e250ca3
1
+ 0.1.3+90b72ed
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@temir.ra/create-template",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Template package to create new template packages",
5
5
  "author": "temir.ra",
6
6
  "license": "MIT",
@@ -20,6 +20,9 @@
20
20
  },
21
21
  "bin": "./dist/cli.bundle.js",
22
22
  "files": [
23
+ "scripts/buildinfo.ts",
24
+ "scripts/build-bundle.ts",
25
+ "scripts/cdn-rewrite-map.json",
23
26
  "dist",
24
27
  "CHANGELOG.md",
25
28
  "buildinfo.txt",
@@ -33,7 +36,6 @@
33
36
  "tests": "bun test",
34
37
  "typecheck": "tsc --noEmit",
35
38
  "reinstall": "rm -rf node_modules && rm -f bun.lock && bun pm cache rm && bun install && bunx tsc --version",
36
- "dev": "bun run buildinfo && bun run --watch scripts/dev.ts",
37
39
  "prebuild": "bun run buildinfo",
38
40
  "build": "bun run build:cli-bundle",
39
41
  "build:cli-bundle": "bun build src/cli.ts --entry-naming \"[dir]/[name].bundle.[ext]\" --outdir dist --target node --format esm --minify --sourcemap=external"
@@ -0,0 +1,161 @@
1
+ import { readFileSync } from 'fs';
2
+ import { join } from 'path';
3
+
4
+ import CDN_REWRITE_MAP from './cdn-rewrite-map.json';
5
+
6
+
7
+ interface ExportConditions {
8
+ [key: string]: string | undefined;
9
+ entrypoint?: string;
10
+ types?: string;
11
+ browser?: string;
12
+ import?: string;
13
+ }
14
+
15
+ interface DependencyMap {
16
+ [packageName: string]: string;
17
+ }
18
+
19
+ interface PackageManifest {
20
+ version: string;
21
+ exports?: Record<string, ExportConditions>;
22
+ dependencies?: DependencyMap;
23
+ devDependencies?: DependencyMap;
24
+ peerDependencies?: DependencyMap;
25
+ }
26
+
27
+ function getManifest(packageIdentifier?: string): PackageManifest {
28
+
29
+ const manifestPath = packageIdentifier
30
+ ? join('node_modules', packageIdentifier, 'package.json')
31
+ : 'package.json';
32
+
33
+ let manifest: PackageManifest;
34
+ try {
35
+ manifest = JSON.parse(readFileSync(manifestPath, 'utf-8'));
36
+ } catch {
37
+ if (packageIdentifier)
38
+ throw new Error(`[scripts/build-bundle.ts] Could not read manifest for '${packageIdentifier}' at '${manifestPath}'.`);
39
+ else
40
+ throw new Error(`[scripts/build-bundle.ts] Could not read package manifest at '${manifestPath}'.`);
41
+ }
42
+
43
+ return manifest;
44
+
45
+ }
46
+
47
+ function getManifestEntrypoints(packageManifest: PackageManifest): string[] {
48
+
49
+ const exports = packageManifest.exports;
50
+ if (!exports)
51
+ throw new Error(`[scripts/build-bundle.ts] No 'exports' field found in the given package manifest.`);
52
+
53
+ const entrypoints = Object.entries(exports)
54
+ .map(([key, conditions]) => {
55
+ if (!conditions.entrypoint)
56
+ throw new Error(`[scripts/build-bundle.ts] Export '${key}' does not have an 'entrypoint' condition.`);
57
+ return conditions.entrypoint;
58
+ });
59
+
60
+ return entrypoints;
61
+
62
+ }
63
+
64
+ function getPackageVersion(manifest: PackageManifest, packageIdentifier?: string): string {
65
+
66
+ let version: string | undefined;
67
+ if (packageIdentifier) {
68
+
69
+ const dependencies = {
70
+ ...manifest.dependencies,
71
+ ...manifest.devDependencies,
72
+ ...manifest.peerDependencies,
73
+ };
74
+
75
+ version = dependencies[packageIdentifier];
76
+ if (!version)
77
+ throw new Error(`[scripts/build-bundle.ts] Package '${packageIdentifier}' is not listed in dependencies.`);
78
+
79
+ }
80
+ else {
81
+ if (!manifest.version)
82
+ throw new Error('[scripts/build-bundle.ts] Package manifest does not contain a version field.');
83
+ version = manifest.version;
84
+ }
85
+
86
+ return version;
87
+
88
+ }
89
+
90
+ function resolveCdnUrl(importSpecifier: string, urlTemplate: string): string {
91
+ const manifest = getManifest();
92
+ const version = getPackageVersion(manifest, importSpecifier);
93
+ return urlTemplate.replace('<VERSION>', version);
94
+ }
95
+
96
+ const cdnRewritePlugin = {
97
+ name: 'cdn-rewrite',
98
+ setup(build: any) {
99
+
100
+ const resolved = new Map<string, string>();
101
+ for (const [importSpecifier, urlTemplate] of Object.entries(CDN_REWRITE_MAP) as [string, string][]) {
102
+ const url = resolveCdnUrl(importSpecifier, urlTemplate);
103
+ resolved.set(importSpecifier, url);
104
+ console.log(`[cdn-rewrite] '${importSpecifier}' → '${url}'`);
105
+ }
106
+
107
+ build.onResolve({ filter: /\*/ }, (args: any) => {
108
+ const url = resolved.get(args.path);
109
+ if (url) return { path: url, external: true };
110
+ });
111
+
112
+ },
113
+ };
114
+
115
+
116
+ const entrypoints = getManifestEntrypoints(getManifest());
117
+ console.log('[scripts/build-bundle.ts] Entrypoints:', entrypoints);
118
+
119
+ let buildResult;
120
+
121
+ console.log('[scripts/build-bundle.ts] Starting ESM bundle build...');
122
+ buildResult = await Bun.build({
123
+ entrypoints,
124
+ outdir: 'dist',
125
+ naming: '[dir]/[name].bundle.[ext]',
126
+ target: 'browser',
127
+ format: 'esm',
128
+ minify: true,
129
+ sourcemap: 'external',
130
+ plugins: [cdnRewritePlugin],
131
+ });
132
+
133
+ if (!buildResult.success) {
134
+ console.error('[scripts/build-bundle.ts] Build failed:');
135
+ for (const message of buildResult.logs) {
136
+ console.error(message);
137
+ }
138
+ process.exit(1);
139
+ }
140
+ console.log('[scripts/build-bundle.ts] ESM bundle build completed successfully.');
141
+
142
+ console.log('[scripts/build-bundle.ts] Starting IIFE bundle build...');
143
+ buildResult = await Bun.build({
144
+ entrypoints,
145
+ outdir: 'dist',
146
+ naming: '[dir]/[name].iife.[ext]',
147
+ target: 'browser',
148
+ format: 'iife',
149
+ minify: true,
150
+ sourcemap: 'external',
151
+ plugins: [cdnRewritePlugin],
152
+ });
153
+
154
+ if (!buildResult.success) {
155
+ console.error('[scripts/build-bundle.ts] Build failed:');
156
+ for (const message of buildResult.logs) {
157
+ console.error(message);
158
+ }
159
+ process.exit(1);
160
+ }
161
+ console.log('[scripts/build-bundle.ts] IIFE bundle build completed successfully.');
@@ -0,0 +1,25 @@
1
+ import { execSync } from 'child_process';
2
+ import { readFileSync, writeFileSync } from 'fs';
3
+
4
+
5
+ const BUILD_INFO_FILE = 'buildinfo.txt';
6
+ const GIT_COMMAND = 'git rev-parse --short HEAD';
7
+
8
+ const version: string = JSON.parse(readFileSync('package.json', 'utf-8')).version;
9
+
10
+ let gitHash = '';
11
+ try {
12
+ gitHash = execSync(GIT_COMMAND, { stdio: ['ignore', 'pipe', 'ignore'] })
13
+ .toString()
14
+ .trim();
15
+ } catch { }
16
+
17
+ const buildinfo = gitHash
18
+ ? version.includes('+')
19
+ ? `${version}.${gitHash}`
20
+ : `${version}+${gitHash}`
21
+ : version;
22
+
23
+ writeFileSync(BUILD_INFO_FILE, buildinfo.trim(), 'utf-8');
24
+
25
+ console.log(`'${BUILD_INFO_FILE}' has been updated with build info: ${buildinfo}`);
@@ -0,0 +1 @@
1
+ {}
@@ -11,13 +11,16 @@
11
11
  "type": "git",
12
12
  "url": ""
13
13
  },
14
- "private": true,
14
+ "private": false,
15
15
  "type": "module",
16
16
  "imports": {
17
17
  "#src/*.js": "./src/*.ts"
18
18
  },
19
19
  "bin": "./dist/cli.bundle.js",
20
20
  "files": [
21
+ "scripts/buildinfo.ts",
22
+ "scripts/build-bundle.ts",
23
+ "scripts/cdn-rewrite-map.json",
21
24
  "dist",
22
25
  "CHANGELOG.md",
23
26
  "buildinfo.txt",
@@ -31,7 +34,6 @@
31
34
  "tests": "bun test",
32
35
  "typecheck": "tsc --noEmit",
33
36
  "reinstall": "rm -rf node_modules && rm -f bun.lock && bun pm cache rm && bun install && bunx tsc --version",
34
- "dev": "bun run buildinfo && bun run --watch scripts/dev.ts",
35
37
  "prebuild": "bun run buildinfo",
36
38
  "build": "bun run build:cli-bundle",
37
39
  "build:cli-bundle": "bun build src/cli.ts --entry-naming \"[dir]/[name].bundle.[ext]\" --outdir dist --target node --format esm --minify --sourcemap=external"
@@ -1,13 +0,0 @@
1
- import { fileURLToPath } from 'url';
2
-
3
-
4
- const start: number = performance.now();
5
-
6
- const packageUrl = new URL('../', import.meta.url);
7
- console.log(`'packagePath':`, fileURLToPath(packageUrl));
8
-
9
-
10
- // dev scratchpad
11
-
12
-
13
- console.log(`${(performance.now() - start).toFixed(3)}ms`);