@textmode/build 0.1.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/LICENSE +121 -0
- package/README.md +177 -0
- package/index.d.ts +35 -0
- package/index.js +3 -0
- package/package.json +65 -0
- package/src/cli/textmode-build.mjs +134 -0
- package/src/internal/engines.mjs +43 -0
- package/src/lib/glsl/glsl-minify.d.ts +6 -0
- package/src/lib/glsl/host-rewriter.js +15 -0
- package/src/lib/glsl/identifiers.js +109 -0
- package/src/lib/glsl/include-graph.js +64 -0
- package/src/lib/glsl/include-resolver.js +91 -0
- package/src/lib/glsl/module-builder.js +38 -0
- package/src/lib/glsl/paths.js +45 -0
- package/src/lib/glsl/plugin.js +164 -0
- package/src/lib/glsl/preserved-symbols.js +75 -0
- package/src/lib/glsl/scanner.js +42 -0
- package/src/lib/glsl/shader-minifier.js +38 -0
- package/src/lib/vite-config.js +154 -0
- package/src/lib/vitest-config.js +35 -0
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
|
|
4
|
+
import { defineConfig } from 'vite';
|
|
5
|
+
|
|
6
|
+
const TEXTMODE_PEER = 'textmode.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Create the standard ESM and UMD build configuration for a textmode.js library.
|
|
10
|
+
*
|
|
11
|
+
* @param {{
|
|
12
|
+
* globalName: string;
|
|
13
|
+
* plugins?: import('vite').PluginOption[];
|
|
14
|
+
* root?: string;
|
|
15
|
+
* }} options
|
|
16
|
+
* @returns {import('vite').UserConfig}
|
|
17
|
+
*/
|
|
18
|
+
export function defineTextmodeLibrary({ globalName, plugins = [], root = process.cwd() }) {
|
|
19
|
+
const resolvedRoot = path.resolve(root);
|
|
20
|
+
const manifestPath = path.join(resolvedRoot, 'package.json');
|
|
21
|
+
const manifest = readManifest(manifestPath);
|
|
22
|
+
const contract = deriveContract(manifest, resolvedRoot, manifestPath);
|
|
23
|
+
|
|
24
|
+
if (typeof globalName !== 'string' || globalName.trim().length === 0) {
|
|
25
|
+
throw new TypeError('@textmode/build: globalName must be a non-empty string.');
|
|
26
|
+
}
|
|
27
|
+
if (!Array.isArray(plugins)) {
|
|
28
|
+
throw new TypeError('@textmode/build: plugins must be an array when provided.');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const external = contract.externalTextmode ? [TEXTMODE_PEER] : [];
|
|
32
|
+
const globals = contract.externalTextmode ? { [TEXTMODE_PEER]: 'textmode' } : {};
|
|
33
|
+
|
|
34
|
+
return defineConfig({
|
|
35
|
+
root: resolvedRoot,
|
|
36
|
+
plugins,
|
|
37
|
+
resolve: {
|
|
38
|
+
alias: {
|
|
39
|
+
[contract.packageName]: contract.entry,
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
server: {
|
|
43
|
+
open: '/examples/index.html',
|
|
44
|
+
},
|
|
45
|
+
define: {
|
|
46
|
+
__TEXTMODE_VERSION__: JSON.stringify(contract.version),
|
|
47
|
+
IS_MINIFIED: 'true',
|
|
48
|
+
},
|
|
49
|
+
build: {
|
|
50
|
+
emptyOutDir: true,
|
|
51
|
+
lib: {
|
|
52
|
+
entry: contract.entry,
|
|
53
|
+
},
|
|
54
|
+
minify: 'terser',
|
|
55
|
+
terserOptions: {
|
|
56
|
+
ecma: 2020,
|
|
57
|
+
compress: {
|
|
58
|
+
drop_debugger: true,
|
|
59
|
+
passes: 2,
|
|
60
|
+
},
|
|
61
|
+
mangle: {
|
|
62
|
+
properties: {
|
|
63
|
+
regex: /^_/,
|
|
64
|
+
keep_quoted: true,
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
format: {
|
|
68
|
+
comments: false,
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
rolldownOptions: {
|
|
72
|
+
external,
|
|
73
|
+
treeshake: {
|
|
74
|
+
moduleSideEffects: true,
|
|
75
|
+
},
|
|
76
|
+
output: [
|
|
77
|
+
{
|
|
78
|
+
format: 'es',
|
|
79
|
+
entryFileNames: contract.esmFileName,
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
format: 'umd',
|
|
83
|
+
name: globalName,
|
|
84
|
+
entryFileNames: contract.umdFileName,
|
|
85
|
+
globals,
|
|
86
|
+
},
|
|
87
|
+
],
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function readManifest(manifestPath) {
|
|
94
|
+
if (!existsSync(manifestPath)) {
|
|
95
|
+
throw new Error(`@textmode/build: package.json not found at ${manifestPath}.`);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
try {
|
|
99
|
+
return JSON.parse(readFileSync(manifestPath, 'utf8'));
|
|
100
|
+
} catch (error) {
|
|
101
|
+
throw new Error(`@textmode/build: could not parse ${manifestPath}.`, { cause: error });
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function deriveContract(manifest, root, manifestPath) {
|
|
106
|
+
const packageName = requireNonEmptyString(manifest.name, 'package.json#name', manifestPath);
|
|
107
|
+
const version = requireNonEmptyString(manifest.version, 'package.json#version', manifestPath);
|
|
108
|
+
const outputStem = packageName.endsWith('.js') ? packageName.slice(0, -3) : packageName;
|
|
109
|
+
const entry = path.join(root, 'src', 'index.ts');
|
|
110
|
+
const esmFileName = `${outputStem}.esm.js`;
|
|
111
|
+
const umdFileName = `${outputStem}.umd.js`;
|
|
112
|
+
const expectedEsm = `./dist/${esmFileName}`;
|
|
113
|
+
const expectedUmd = `./dist/${umdFileName}`;
|
|
114
|
+
|
|
115
|
+
if (!existsSync(entry)) {
|
|
116
|
+
throw new Error(`@textmode/build: expected library entry ${entry}.`);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
assertManifestField(manifest.main, expectedUmd, 'package.json#main', manifestPath);
|
|
120
|
+
assertManifestField(manifest.module, expectedEsm, 'package.json#module', manifestPath);
|
|
121
|
+
|
|
122
|
+
const rootExport = manifest.exports?.['.'];
|
|
123
|
+
if (!rootExport || typeof rootExport !== 'object' || Array.isArray(rootExport)) {
|
|
124
|
+
throw new Error(
|
|
125
|
+
`@textmode/build: package.json#exports["."] in ${manifestPath} must declare import and require outputs.`
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
assertManifestField(rootExport.import, expectedEsm, 'package.json#exports["."].import', manifestPath);
|
|
129
|
+
assertManifestField(rootExport.require, expectedUmd, 'package.json#exports["."].require', manifestPath);
|
|
130
|
+
|
|
131
|
+
return {
|
|
132
|
+
entry,
|
|
133
|
+
esmFileName,
|
|
134
|
+
externalTextmode: Object.hasOwn(manifest.peerDependencies ?? {}, TEXTMODE_PEER),
|
|
135
|
+
packageName,
|
|
136
|
+
umdFileName,
|
|
137
|
+
version,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function requireNonEmptyString(value, field, manifestPath) {
|
|
142
|
+
if (typeof value !== 'string' || value.trim().length === 0) {
|
|
143
|
+
throw new Error(`@textmode/build: ${field} in ${manifestPath} must be a non-empty string.`);
|
|
144
|
+
}
|
|
145
|
+
return value;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function assertManifestField(actual, expected, field, manifestPath) {
|
|
149
|
+
if (actual !== expected) {
|
|
150
|
+
throw new Error(
|
|
151
|
+
`@textmode/build: ${field} in ${manifestPath} must be "${expected}", received ${JSON.stringify(actual)}.`
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
|
|
3
|
+
import { defineConfig, defineProject } from 'vitest/config';
|
|
4
|
+
|
|
5
|
+
export function defineTextmodeProject({ alias = {}, coverage, plugins = [], projects = [], setupFiles = [] } = {}) {
|
|
6
|
+
const sharedTestConfig = {
|
|
7
|
+
environment: 'jsdom',
|
|
8
|
+
globals: true,
|
|
9
|
+
...(setupFiles.length > 0 ? { setupFiles } : {}),
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
return defineConfig({
|
|
13
|
+
root: process.cwd(),
|
|
14
|
+
plugins,
|
|
15
|
+
test: {
|
|
16
|
+
...sharedTestConfig,
|
|
17
|
+
...(coverage ? { coverage } : {}),
|
|
18
|
+
projects: projects.map((project) =>
|
|
19
|
+
defineProject({
|
|
20
|
+
extends: project.extends,
|
|
21
|
+
test: {
|
|
22
|
+
...sharedTestConfig,
|
|
23
|
+
...project.test,
|
|
24
|
+
},
|
|
25
|
+
})
|
|
26
|
+
),
|
|
27
|
+
},
|
|
28
|
+
resolve: {
|
|
29
|
+
alias: {
|
|
30
|
+
'@': path.resolve(process.cwd(), 'src'),
|
|
31
|
+
...alias,
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
}
|