Skip to content

Commit 5ba6897

Browse files
fix!: publish as ESM, tree-shaking
1 parent 630f249 commit 5ba6897

File tree

8 files changed

+35
-43
lines changed

8 files changed

+35
-43
lines changed

package.json

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,7 @@
2222
"src/*"
2323
],
2424
"type": "module",
25-
"types": "./dist/index.d.ts",
26-
"main": "./dist/index.cjs",
27-
"module": "./dist/index.js",
28-
"exports": {
29-
"require": {
30-
"types": "./dist/index.d.cts",
31-
"default": "./dist/index.cjs"
32-
},
33-
"import": {
34-
"types": "./dist/index.d.ts",
35-
"default": "./dist/index.js"
36-
}
37-
},
25+
"exports": "./dist/index.js",
3826
"sideEffects": false,
3927
"devDependencies": {
4028
"@types/node": "^22.10.2",

src/generator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type AST, type Program } from './ast'
1+
import { type AST, type Program } from './ast.js'
22

33
function formatLayout(layout: Record<string, string | boolean> | null): string {
44
if (!layout) return ''

src/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
export * from './ast'
2-
export * from './constants'
3-
export * from './generator'
4-
export * from './minifier'
5-
export * from './parser'
6-
export * from './tokenizer'
1+
export * from './ast.js'
2+
export * from './constants.js'
3+
export * from './generator.js'
4+
export * from './minifier.js'
5+
export * from './parser.js'
6+
export * from './tokenizer.js'

src/minifier.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type Token, tokenize } from './tokenizer'
1+
import { type Token, tokenize } from './tokenizer.js'
22

33
export type MangleMatcher = (token: Token, index: number, tokens: Token[]) => boolean
44

@@ -11,11 +11,13 @@ export interface MinifyOptions {
1111
mangleExternals: boolean
1212
}
1313

14-
const isWord = RegExp.prototype.test.bind(/^\w/)
15-
const isSymbol = RegExp.prototype.test.bind(/[^\w\\]/)
16-
const isName = RegExp.prototype.test.bind(/^[_A-Za-z]/)
17-
const isScoped = RegExp.prototype.test.bind(/[;{}\\@]/)
18-
const isStorage = RegExp.prototype.test.bind(/^(binding|group|layout|uniform|in|out|attribute|varying)$/)
14+
const isWord = /* @__PURE__ */ RegExp.prototype.test.bind(/^\w/)
15+
const isSymbol = /* @__PURE__ */ RegExp.prototype.test.bind(/[^\w\\]/)
16+
const isName = /* @__PURE__ */ RegExp.prototype.test.bind(/^[_A-Za-z]/)
17+
const isScoped = /* @__PURE__ */ RegExp.prototype.test.bind(/[;{}\\@]/)
18+
const isStorage = /* @__PURE__ */ RegExp.prototype.test.bind(
19+
/^(binding|group|layout|uniform|in|out|attribute|varying)$/,
20+
)
1921

2022
/**
2123
* Minifies a string of GLSL or WGSL code.

src/parser.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ import {
3535
VariableDeclaration,
3636
VariableDeclarator,
3737
WhileStatement,
38-
} from './ast'
39-
import { type Token, tokenize } from './tokenizer'
38+
} from './ast.js'
39+
import { type Token, tokenize } from './tokenizer.js'
4040

4141
// https://engineering.desmos.com/articles/pratt-parser
4242
// https://matklad.github.io/2020/04/13/simple-but-powerful-pratt-parsing.html
@@ -123,10 +123,10 @@ const TYPE_REGEX = /^(void|bool|float|u?int|[uib]?vec\d|mat\d(x\d)?)$/
123123
const QUALIFIER_REGEX = /^(const|uniform|in|out|inout|centroid|flat|smooth|invariant|lowp|mediump|highp)$/
124124
const VARIABLE_REGEX = new RegExp(`${TYPE_REGEX.source}|${QUALIFIER_REGEX.source}|layout`)
125125

126-
const isDeclaration = RegExp.prototype.test.bind(VARIABLE_REGEX)
126+
const isDeclaration = /* @__PURE__ */ RegExp.prototype.test.bind(VARIABLE_REGEX)
127127

128-
const isOpen = RegExp.prototype.test.bind(/^[\(\[\{]$/)
129-
const isClose = RegExp.prototype.test.bind(/^[\)\]\}]$/)
128+
const isOpen = /* @__PURE__ */ RegExp.prototype.test.bind(/^[\(\[\{]$/)
129+
const isClose = /* @__PURE__ */ RegExp.prototype.test.bind(/^[\)\]\}]$/)
130130

131131
function getScopeDelta(token: Token): number {
132132
if (isOpen(token.value)) return 1

src/tokenizer.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { WGSL_KEYWORDS, WGSL_SYMBOLS, GLSL_KEYWORDS, GLSL_SYMBOLS } from './constants'
1+
import { WGSL_KEYWORDS, WGSL_SYMBOLS, GLSL_KEYWORDS, GLSL_SYMBOLS } from './constants.js'
22

33
export type TokenType = 'whitespace' | 'comment' | 'symbol' | 'bool' | 'float' | 'int' | 'identifier' | 'keyword'
44

@@ -8,11 +8,11 @@ export interface Token<T = TokenType, V = string> {
88
}
99

1010
// Checks for WGSL-specific `fn foo(`, `var bar =`, `let baz =`, `const qux =`
11-
const isWGSL = RegExp.prototype.test.bind(/\bfn\s+\w+\s*\(|\b(var|let|const)\s+\w+\s*[:=]/)
11+
const isWGSL = /* @__PURE__ */ RegExp.prototype.test.bind(/\bfn\s+\w+\s*\(|\b(var|let|const)\s+\w+\s*[:=]/)
1212

13-
const isFloat = RegExp.prototype.test.bind(/^(\d+\.\d*|\d*\.\d+)([eEpP][-+]?\d+)?[fFhH]?$/)
14-
const isInt = RegExp.prototype.test.bind(/^(0[xX][\w\d]+|\d+)[iIuU]?$/)
15-
const isBool = RegExp.prototype.test.bind(/^(true|false)$/)
13+
const isFloat = /* @__PURE__ */ RegExp.prototype.test.bind(/^(\d+\.\d*|\d*\.\d+)([eEpP][-+]?\d+)?[fFhH]?$/)
14+
const isInt = /* @__PURE__ */ RegExp.prototype.test.bind(/^(0[xX][\w\d]+|\d+)[iIuU]?$/)
15+
const isBool = /* @__PURE__ */ RegExp.prototype.test.bind(/^(true|false)$/)
1616

1717
const ZERO = 48
1818
const NINE = 57

tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
"pretty": true,
1010
"noEmit": true,
1111
"forceConsistentCasingInFileNames": true,
12-
"allowSyntheticDefaultImports": true,
1312
"paths": {
1413
"shaderkit": ["./src"]
1514
}

vite.config.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ export default vite.defineConfig({
99
},
1010
},
1111
build: {
12-
target: 'es2018',
12+
target: 'esnext',
1313
sourcemap: true,
1414
lib: {
15-
formats: ['es', 'cjs'],
15+
formats: ['es'],
1616
entry: 'src/index.ts',
1717
fileName: '[name]',
1818
},
@@ -27,16 +27,19 @@ export default vite.defineConfig({
2727
{
2828
name: 'vite-tsc',
2929
generateBundle(options) {
30-
const ext = options.format === 'cjs' ? 'cts' : 'ts'
31-
this.emitFile({ type: 'asset', fileName: `index.d.${ext}`, source: `export * from '../src'` })
30+
this.emitFile({ type: 'asset', fileName: 'index.d.ts', source: `export * from '../src/index.ts'` })
3231
},
3332
},
3433
{
3534
name: 'vite-minify',
3635
renderChunk: {
3736
order: 'post',
38-
handler(code, { fileName }) {
39-
return vite.transformWithEsbuild(code, fileName, { minify: true, target: 'es2018' })
37+
async handler(code, { fileName }) {
38+
// Preserve pure annotations, but remove all other comments and whitespace
39+
code = code.replaceAll('/* @__PURE__ */', '__PURE__ || ')
40+
const result = await vite.transformWithEsbuild(code, fileName, { minify: true, target: 'es2020' })
41+
result.code = result.code.replaceAll('__PURE__||', '/*@__PURE__*/')
42+
return result
4043
},
4144
},
4245
},

0 commit comments

Comments
 (0)