From c128607b626aee3770942a569d6f63e355dbb6b2 Mon Sep 17 00:00:00 2001
From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com>
Date: Sat, 7 Sep 2024 16:58:15 -0400
Subject: [PATCH 1/9] Fix prod stripping
---
packages/@glimmer-workspace/build/index.d.ts | 1 -
packages/@glimmer-workspace/build/index.js | 1 -
.../@glimmer-workspace/build/lib/config.js | 43 +++++++++++++++++--
.../build/lib/import-meta.d.ts | 4 --
.../build/lib/import-meta.js | 23 ----------
packages/@glimmer/debug/index.ts | 29 ++++++++++++-
packages/@glimmer/debug/lib/stack-check.ts | 33 ++++++++++++++
7 files changed, 100 insertions(+), 34 deletions(-)
delete mode 100644 packages/@glimmer-workspace/build/lib/import-meta.d.ts
delete mode 100644 packages/@glimmer-workspace/build/lib/import-meta.js
diff --git a/packages/@glimmer-workspace/build/index.d.ts b/packages/@glimmer-workspace/build/index.d.ts
index 8442f97f0b..1315026703 100644
--- a/packages/@glimmer-workspace/build/index.d.ts
+++ b/packages/@glimmer-workspace/build/index.d.ts
@@ -4,4 +4,3 @@ export {
type PackageJSON,
type ViteConfig as ViteExport,
} from './lib/config.js';
-export { default as importMeta } from './lib/import-meta.js';
diff --git a/packages/@glimmer-workspace/build/index.js b/packages/@glimmer-workspace/build/index.js
index 480f7fba41..b16a2c80cf 100644
--- a/packages/@glimmer-workspace/build/index.js
+++ b/packages/@glimmer-workspace/build/index.js
@@ -1,3 +1,2 @@
export { Package } from './lib/config.js';
-export { default as importMeta } from './lib/import-meta.js';
export { default as inline } from './lib/inline.js';
diff --git a/packages/@glimmer-workspace/build/lib/config.js b/packages/@glimmer-workspace/build/lib/config.js
index 316790b915..a331ca3be6 100644
--- a/packages/@glimmer-workspace/build/lib/config.js
+++ b/packages/@glimmer-workspace/build/lib/config.js
@@ -1,6 +1,5 @@
/* eslint-disable no-console */
// @ts-check
-
import { existsSync, readFileSync } from 'node:fs';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
@@ -11,7 +10,6 @@ import * as insert from 'rollup-plugin-insert';
import rollupTS from 'rollup-plugin-ts';
import ts from 'typescript';
-import importMeta from './import-meta.js';
import inline from './inline.js';
// eslint-disable-next-line import/no-named-as-default-member
@@ -315,7 +313,31 @@ export class Package {
commonjs(),
nodeResolve(),
...this.replacements(env),
- ...(env === 'prod' ? [terser()] : []),
+ ...(env === 'prod'
+ ? [
+ terser({
+ module: true,
+ // to debug the output, uncomment this so you can read the
+ // identifiers, unchanged
+ // mangle: false,
+ compress: {
+ passes: 3,
+ },
+ }),
+ ]
+ : [
+ terser({
+ module: true,
+ mangle: false,
+ compress: {
+ passes: 3,
+ },
+ format: {
+ comments: 'all',
+ beautify: true,
+ },
+ }),
+ ]),
postcss(),
typescript(this.#package, {
target: ScriptTarget.ES2022,
@@ -356,7 +378,20 @@ export class Package {
*/
replacements(env) {
return env === 'prod'
- ? [importMeta]
+ ? [
+ replace({
+ preventAssignment: true,
+ values: {
+ // Intended to be left in the build during publish
+ // currently compiled away to `@glimmer/debug`
+ 'import.meta.env.MODE': '"production"',
+ 'import.meta.env.DEV': 'false',
+ 'import.meta.env.PROD': 'true',
+ // Not exposed at publish, compiled away
+ 'import.meta.env.VM_LOCAL_DEV': 'false',
+ },
+ }),
+ ]
: [
replace({
preventAssignment: true,
diff --git a/packages/@glimmer-workspace/build/lib/import-meta.d.ts b/packages/@glimmer-workspace/build/lib/import-meta.d.ts
deleted file mode 100644
index ecf41190e4..0000000000
--- a/packages/@glimmer-workspace/build/lib/import-meta.d.ts
+++ /dev/null
@@ -1,4 +0,0 @@
-// eslint-disable-next-line @typescript-eslint/consistent-type-imports
-declare const DEFAULT: import('rollup').Plugin;
-
-export default DEFAULT;
diff --git a/packages/@glimmer-workspace/build/lib/import-meta.js b/packages/@glimmer-workspace/build/lib/import-meta.js
deleted file mode 100644
index 922271f533..0000000000
--- a/packages/@glimmer-workspace/build/lib/import-meta.js
+++ /dev/null
@@ -1,23 +0,0 @@
-///
-
-import { createReplacePlugin } from './replace.js';
-
-const MODE = process.env['MODE'] ?? 'development';
-const DEV = MODE === 'development';
-const PROD = MODE === 'production';
-const STARBEAM_TRACE = process.env['STARBEAM_TRACE'] ?? false;
-
-export default createReplacePlugin(
- (id) => /\.[jt]sx?$/u.test(id),
- {
- // Intended to be left in the build during publish
- // currently compiled away to `@glimmer/debug`
- 'import.meta.env.MODE': process.env['MODE'] ?? 'development',
- 'import.meta.env.DEV': DEV ? 'true' : 'false',
- 'import.meta.env.PROD': PROD ? 'true' : 'false',
- // Not exposed at publish, compiled away
- 'import.meta.env.VM_LOCAL_DEV': DEV ? 'true' : 'false',
- 'import.meta.env.STARBEAM_TRACE': STARBEAM_TRACE ? 'true' : 'false',
- },
- true
-);
diff --git a/packages/@glimmer/debug/index.ts b/packages/@glimmer/debug/index.ts
index b05cc97099..4456b896c7 100644
--- a/packages/@glimmer/debug/index.ts
+++ b/packages/@glimmer/debug/index.ts
@@ -1,4 +1,31 @@
export { debug, debugSlice, logOpcode } from './lib/debug';
export * from './lib/metadata';
export { opcodeMetadata } from './lib/opcode-metadata';
-export * from './lib/stack-check';
+export type { Checker } from './lib/stack-check';
+export {
+ check,
+ CheckArray,
+ CheckBlockSymbolTable,
+ CheckBoolean,
+ CheckDict,
+ CheckDocumentFragment,
+ CheckElement,
+ CheckFunction,
+ CheckHandle,
+ CheckInstanceof,
+ CheckInterface,
+ CheckMaybe,
+ CheckNode,
+ CheckNumber,
+ CheckObject,
+ CheckOption,
+ CheckOr,
+ CheckPrimitive,
+ CheckProgramSymbolTable,
+ CheckSafeString,
+ CheckString,
+ CheckUndefined,
+ CheckUnknown,
+ recordStackSize,
+ wrap,
+} from './lib/stack-check';
diff --git a/packages/@glimmer/debug/lib/stack-check.ts b/packages/@glimmer/debug/lib/stack-check.ts
index f8abf5bbba..b25bbf8bad 100644
--- a/packages/@glimmer/debug/lib/stack-check.ts
+++ b/packages/@glimmer/debug/lib/stack-check.ts
@@ -16,7 +16,21 @@ export interface Checker {
expected(): string;
}
+class NoopChecker implements Checker {
+ declare type: T;
+ validate(value: unknown): value is T {
+ return true;
+ }
+ expected(): string {
+ return '';
+ }
+}
+
export function wrap(checker: () => Checker): Checker {
+ if (import.meta.env.PROD) {
+ return new NoopChecker();
+ }
+
class Wrapped {
declare type: T;
@@ -300,14 +314,23 @@ export function CheckInterface<
I extends { [P in keyof O]: O[P]['type'] },
O extends Dict>,
>(obj: O): Checker {
+ if (import.meta.env.PROD) {
+ return new NoopChecker();
+ }
return new PropertyChecker(obj);
}
export function CheckArray(obj: Checker): Checker {
+ if (import.meta.env.PROD) {
+ return new NoopChecker();
+ }
return new ArrayChecker(obj);
}
export function CheckDict(obj: Checker): Checker> {
+ if (import.meta.env.PROD) {
+ return new NoopChecker>();
+ }
return new DictChecker(obj);
}
@@ -326,6 +349,10 @@ export function check(
checker: Checker | ((value: unknown) => void),
message: (value: unknown, expected: string) => string = defaultMessage
): T {
+ if (import.meta.env.PROD) {
+ return value as T;
+ }
+
if (typeof checker === 'function') {
checker(value);
return value as T;
@@ -366,10 +393,16 @@ export const CheckSafeString: Checker = new SafeStringChecker();
export const CheckObject: Checker