@reckona/mreact-compiler 0.0.82 → 0.0.84

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/src/internal.ts CHANGED
@@ -166,6 +166,27 @@ export function demoteTopLevelExportDeclarations(input: {
166
166
  return code;
167
167
  }
168
168
 
169
+ export function stripUnusedStaticValueImports(input: {
170
+ code: string;
171
+ filename?: string | undefined;
172
+ }): string {
173
+ const parsed = parseModule(input.code, input.filename);
174
+ const referencedNames = new Set(collectIdentifierReferenceNames(input));
175
+ const replacements = programBody(parsed.program)
176
+ .map((statement) =>
177
+ unusedStaticValueImportReplacement(input.code, statement, referencedNames),
178
+ )
179
+ .filter((replacement): replacement is Replacement => replacement !== undefined)
180
+ .sort((left, right) => right.start - left.start);
181
+ let code = input.code;
182
+
183
+ for (const replacement of replacements) {
184
+ code = `${code.slice(0, replacement.start)}${replacement.text}${code.slice(replacement.end)}`;
185
+ }
186
+
187
+ return code;
188
+ }
189
+
169
190
  export function collectStaticModuleSpecifiers(input: {
170
191
  code: string;
171
192
  filename?: string | undefined;
@@ -1096,6 +1117,38 @@ function partialSpecifierExportReplacement(
1096
1117
  };
1097
1118
  }
1098
1119
 
1120
+ function unusedStaticValueImportReplacement(
1121
+ code: string,
1122
+ statement: Record<string, unknown>,
1123
+ referencedNames: ReadonlySet<string>,
1124
+ ): Replacement | undefined {
1125
+ if (statement.type !== "ImportDeclaration" || statement.importKind === "type") {
1126
+ return undefined;
1127
+ }
1128
+
1129
+ const specifiers = Array.isArray(statement.specifiers)
1130
+ ? statement.specifiers.map(readObject)
1131
+ : [];
1132
+
1133
+ if (specifiers.length === 0) {
1134
+ return undefined;
1135
+ }
1136
+
1137
+ const localNames = specifiers
1138
+ .filter((specifier) => specifier.importKind !== "type")
1139
+ .flatMap((specifier) => {
1140
+ const local = readOptionalObject(specifier.local);
1141
+ return typeof local?.name === "string" ? [local.name] : [];
1142
+ });
1143
+
1144
+ if (localNames.length === 0 || localNames.some((name) => referencedNames.has(name))) {
1145
+ return undefined;
1146
+ }
1147
+
1148
+ const range = statementRange(code, statement);
1149
+ return range === undefined ? undefined : { ...range, text: "" };
1150
+ }
1151
+
1099
1152
  function staticModuleSpecifier(statement: Record<string, unknown>): string[] {
1100
1153
  if (statement.type === "ImportDeclaration") {
1101
1154
  if (statement.importKind === "type") {
package/src/types.ts CHANGED
@@ -1,8 +1,25 @@
1
- export type CompileTarget = "client" | "server";
2
- export type ServerOutputMode = "string" | "stream";
3
- export type ServerBootstrapMode = "none" | "out-of-order-reorder";
1
+ import type {
2
+ ClientReferenceMetadata as SharedClientReferenceMetadata,
3
+ CompileTarget as SharedCompileTarget,
4
+ CompilerFrontend as SharedCompilerFrontend,
5
+ ComponentMetadata as SharedComponentMetadata,
6
+ Diagnostic as SharedDiagnostic,
7
+ DiagnosticSuggestion as SharedDiagnosticSuggestion,
8
+ EventHydrationEntryMetadata as SharedEventHydrationEntryMetadata,
9
+ EventHydrationManifestMetadata as SharedEventHydrationManifestMetadata,
10
+ ModuleMetadata as SharedModuleMetadata,
11
+ RuntimeImport as SharedRuntimeImport,
12
+ ServerBootstrapMode as SharedServerBootstrapMode,
13
+ ServerOutputMode as SharedServerOutputMode,
14
+ SourceLocation as SharedSourceLocation,
15
+ TransformOutput as SharedTransformOutput,
16
+ } from "@reckona/mreact-shared/compiler-contract";
17
+
18
+ export type CompileTarget = SharedCompileTarget;
19
+ export type ServerOutputMode = SharedServerOutputMode;
20
+ export type ServerBootstrapMode = SharedServerBootstrapMode;
4
21
  export type ParserMode = "oxc";
5
- export type CompilerFrontend = "oxc";
22
+ export type CompilerFrontend = SharedCompilerFrontend;
6
23
 
7
24
  export type BodyStatementJsxMode = "dom-node" | "compat-object" | "server-string" | "unsupported";
8
25
 
@@ -40,78 +57,14 @@ export interface ServerEscapeOptions {
40
57
  batchImportSource: string;
41
58
  }
42
59
 
43
- export interface TransformOutput {
44
- code: string;
45
- map?: string | null;
46
- diagnostics: Diagnostic[];
47
- metadata: ModuleMetadata;
48
- }
49
-
50
- export interface Diagnostic {
51
- level: "info" | "warn" | "error";
52
- code: string;
53
- message: string;
54
- loc?: SourceLocation;
55
- suggestion?: DiagnosticSuggestion;
56
- }
57
-
58
- export interface DiagnosticSuggestion {
59
- title: string;
60
- replacement?: string;
61
- link?: string;
62
- }
63
-
64
- export interface SourceLocation {
65
- line: number;
66
- column: number;
67
- }
68
-
69
- export interface ModuleMetadata {
70
- filename: string;
71
- target: CompileTarget;
72
- compiler: CompilerMetadata;
73
- serverOutput?: ServerOutputMode;
74
- serverBootstrap?: ServerBootstrapMode;
75
- serverBootstrapNonce?: string;
76
- serverBootstrapSrc?: string;
77
- serverHydration?: boolean;
78
- reactSuspenseRevealScriptSrc?: string;
79
- components: ComponentMetadata[];
80
- imports: RuntimeImport[];
81
- clientReferences?: string[];
82
- clientReferenceManifest?: ClientReferenceMetadata[];
83
- serverReferences?: string[];
84
- eventHydrationManifest?: EventHydrationManifestMetadata;
85
- }
86
-
87
- export interface CompilerMetadata {
88
- frontend: CompilerFrontend;
89
- typescriptFallback: boolean;
90
- }
91
-
92
- export interface ClientReferenceMetadata {
93
- name: string;
94
- moduleId: string;
95
- exportName: string;
96
- }
97
-
98
- export interface ComponentMetadata {
99
- name: string;
100
- exportName: string;
101
- }
102
-
103
- export interface RuntimeImport {
104
- source: string;
105
- specifiers: string[];
106
- }
107
-
108
- export interface EventHydrationManifestMetadata {
109
- version: 1;
110
- events: EventHydrationEntryMetadata[];
111
- }
112
-
113
- export interface EventHydrationEntryMetadata {
114
- id: string;
115
- event: string;
116
- handler: string;
117
- }
60
+ export type TransformOutput = SharedTransformOutput;
61
+ export type Diagnostic = SharedDiagnostic;
62
+ export type DiagnosticSuggestion = SharedDiagnosticSuggestion;
63
+ export type SourceLocation = SharedSourceLocation;
64
+ export type ModuleMetadata = SharedModuleMetadata;
65
+ export type CompilerMetadata = SharedModuleMetadata["compiler"];
66
+ export type ClientReferenceMetadata = SharedClientReferenceMetadata;
67
+ export type ComponentMetadata = SharedComponentMetadata;
68
+ export type RuntimeImport = SharedRuntimeImport;
69
+ export type EventHydrationManifestMetadata = SharedEventHydrationManifestMetadata;
70
+ export type EventHydrationEntryMetadata = SharedEventHydrationEntryMetadata;