| 36 | // published bundle on `class X` substrings — like the alphatab webpack plugin |
| 37 | // — depends on the declaration form, so we restore it here. |
| 38 | const preserveClassDeclarationsPlugin = (): Plugin => ({ |
| 39 | name: 'preserve-class-declarations', |
| 40 | renderChunk(code) { |
| 41 | const program = parseAst(code, { lang: 'js', sourceType: 'module' }); |
| 42 | const ms = new MagicString(code); |
| 43 | let changed = false; |
| 44 | |
| 45 | for (const stmt of program.body) { |
| 46 | if (stmt.type !== 'VariableDeclaration') { |
| 47 | continue; |
| 48 | } |
| 49 | const varStmt = stmt as VariableDeclaration; |
| 50 | if (varStmt.declarations.length !== 1) { |
| 51 | continue; |
| 52 | } |
| 53 | const decl = varStmt.declarations[0]; |
| 54 | if (decl.id.type !== 'Identifier' || !decl.init || decl.init.type !== 'ClassExpression') { |
| 55 | continue; |
| 56 | } |
| 57 | const classExpr = decl.init as Class; |
| 58 | // skip when the inner name differs from the outer binding — that |
| 59 | // would change semantics if the body self-references the inner name. |
| 60 | if (classExpr.id && classExpr.id.name !== decl.id.name) { |
| 61 | continue; |
| 62 | } |
| 63 | |
| 64 | // drop `var <id> = ` prefix, leaving the class expression |
| 65 | ms.remove(varStmt.start, classExpr.start); |
| 66 | // promote to class declaration: insert the binding name after `class` |
| 67 | // if it isn't already present as an inner name. |
| 68 | if (!classExpr.id) { |
| 69 | ms.appendLeft(classExpr.start + 'class'.length, ` ${decl.id.name}`); |
| 70 | } |
| 71 | // drop the trailing semicolon of the var statement |
| 72 | if (code[varStmt.end - 1] === ';') { |
| 73 | ms.remove(varStmt.end - 1, varStmt.end); |
| 74 | } |
| 75 | changed = true; |
| 76 | } |
| 77 | |
| 78 | if (!changed) { |
| 79 | return null; |
| 80 | } |
| 81 | return { code: ms.toString(), map: ms.generateMap({ hires: 'boundary' }) }; |
| 82 | } |
| 83 | }); |
| 84 | |
| 85 | export default defineConfig(({ mode }) => { |
| 86 | const config = defaultBuildUserConfig(__dirname); |