@ryanatkn/gro 0.156.0 → 0.157.1
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/dist/constants.d.ts +6 -0
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +6 -0
- package/dist/esbuild_plugin_svelte.d.ts.map +1 -1
- package/dist/esbuild_plugin_svelte.js +1 -2
- package/dist/gro_plugin_sveltekit_app.js +1 -1
- package/dist/loader.d.ts.map +1 -1
- package/dist/loader.js +1 -2
- package/dist/package.d.ts +44 -13
- package/dist/package.d.ts.map +1 -1
- package/dist/package.gen.js +1 -1
- package/dist/package.js +44 -22
- package/dist/parse_exports.d.ts +20 -0
- package/dist/parse_exports.d.ts.map +1 -0
- package/dist/parse_exports.js +65 -0
- package/dist/parse_exports_context.d.ts +21 -0
- package/dist/parse_exports_context.d.ts.map +1 -0
- package/dist/parse_exports_context.js +332 -0
- package/dist/parse_imports.d.ts.map +1 -1
- package/dist/parse_imports.js +1 -2
- package/dist/paths.d.ts +8 -0
- package/dist/paths.d.ts.map +1 -1
- package/dist/paths.js +6 -3
- package/dist/src_json.d.ts +68 -66
- package/dist/src_json.d.ts.map +1 -1
- package/dist/src_json.js +58 -55
- package/dist/test_helpers.d.ts +21 -0
- package/dist/test_helpers.d.ts.map +1 -0
- package/dist/test_helpers.js +122 -0
- package/package.json +21 -13
- package/src/lib/constants.ts +6 -0
- package/src/lib/esbuild_plugin_svelte.ts +1 -2
- package/src/lib/gro_plugin_sveltekit_app.ts +1 -1
- package/src/lib/loader.ts +6 -2
- package/src/lib/package.gen.ts +1 -1
- package/src/lib/package.ts +44 -22
- package/src/lib/parse_exports.ts +108 -0
- package/src/lib/parse_exports_context.ts +394 -0
- package/src/lib/parse_imports.ts +1 -2
- package/src/lib/paths.ts +13 -3
- package/src/lib/src_json.ts +80 -68
- package/src/lib/test_helpers.ts +159 -0
- package/dist/svelte_helpers.d.ts +0 -3
- package/dist/svelte_helpers.d.ts.map +0 -1
- package/dist/svelte_helpers.js +0 -2
- package/src/lib/svelte_helpers.ts +0 -2
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import {existsSync, readFileSync, writeFileSync} from 'node:fs';
|
|
2
|
+
import {join} from 'node:path';
|
|
3
|
+
import ts from 'typescript';
|
|
4
|
+
|
|
5
|
+
export const SOME_PUBLIC_ENV_VAR_NAME = 'PUBLIC_SOME_PUBLIC_ENV_VAR';
|
|
6
|
+
export const SOME_PUBLIC_ENV_VAR_VALUE = 'SOME_PUBLIC_ENV_VAR';
|
|
7
|
+
const name_equals = SOME_PUBLIC_ENV_VAR_NAME + '=';
|
|
8
|
+
const line = name_equals + SOME_PUBLIC_ENV_VAR_VALUE;
|
|
9
|
+
|
|
10
|
+
let inited = false;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Hacky global helper to init the test env.
|
|
14
|
+
*
|
|
15
|
+
* @returns boolean indicating if the env file was created or not
|
|
16
|
+
*/
|
|
17
|
+
export const init_test_env = (dir = process.cwd(), env_filename = '.env'): boolean => {
|
|
18
|
+
if (inited) return false;
|
|
19
|
+
inited = true;
|
|
20
|
+
|
|
21
|
+
const env_file = join(dir, env_filename);
|
|
22
|
+
|
|
23
|
+
if (!existsSync(env_file)) {
|
|
24
|
+
writeFileSync(env_file, line + '\n', 'utf8');
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const contents = readFileSync(env_file, 'utf8');
|
|
29
|
+
const lines = contents.split('\n');
|
|
30
|
+
if (lines.includes(line)) {
|
|
31
|
+
return false; // already exists
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
let new_contents: string;
|
|
35
|
+
const found_index = lines.findIndex((l) => l.startsWith(name_equals));
|
|
36
|
+
if (found_index === -1) {
|
|
37
|
+
// if the line does not exist, add it
|
|
38
|
+
new_contents = contents + (contents.endsWith('\n') ? '' : '\n') + line + '\n';
|
|
39
|
+
} else {
|
|
40
|
+
// if the line exists but with a different value, replace it
|
|
41
|
+
new_contents = contents.replace(new RegExp(`${SOME_PUBLIC_ENV_VAR_NAME}=.*`), line);
|
|
42
|
+
}
|
|
43
|
+
writeFileSync(env_file, new_contents, 'utf8');
|
|
44
|
+
|
|
45
|
+
return true;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Creates a TypeScript environment for testing.
|
|
50
|
+
* Change to `typescript-go` when it's more ready.
|
|
51
|
+
* @see https://github.com/microsoft/typescript-go?tab=readme-ov-file#what-works-so-far
|
|
52
|
+
*/
|
|
53
|
+
export const create_ts_test_env = (
|
|
54
|
+
source_code: string,
|
|
55
|
+
dir: string = process.cwd(),
|
|
56
|
+
virtual_files: Record<string, string> = {},
|
|
57
|
+
): {
|
|
58
|
+
source_file: ts.SourceFile;
|
|
59
|
+
checker: ts.TypeChecker;
|
|
60
|
+
program: ts.Program;
|
|
61
|
+
exports: Array<ts.Symbol>;
|
|
62
|
+
} => {
|
|
63
|
+
// Create a virtual file path for testing
|
|
64
|
+
const file_path = join(dir, 'virtual_test_file.ts');
|
|
65
|
+
|
|
66
|
+
// Create a compiler host with custom module resolution
|
|
67
|
+
const host = ts.createCompilerHost({});
|
|
68
|
+
const original_get_source_file = host.getSourceFile.bind(host);
|
|
69
|
+
|
|
70
|
+
// Override getSourceFile to return our test files
|
|
71
|
+
host.getSourceFile = (fileName: string, languageVersion: ts.ScriptTarget) => {
|
|
72
|
+
if (fileName === file_path) {
|
|
73
|
+
return ts.createSourceFile(fileName, source_code, languageVersion);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Check if we have a virtual file for this path
|
|
77
|
+
for (const [virtual_path, content] of Object.entries(virtual_files)) {
|
|
78
|
+
const full_path = join(dir, virtual_path);
|
|
79
|
+
if (fileName === full_path) {
|
|
80
|
+
return ts.createSourceFile(fileName, content, languageVersion);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return original_get_source_file(fileName, languageVersion);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
// TODO simplify?
|
|
88
|
+
// Add custom module resolution using resolveModuleNameLiterals
|
|
89
|
+
host.resolveModuleNameLiterals = (
|
|
90
|
+
module_literals: ReadonlyArray<ts.StringLiteralLike>,
|
|
91
|
+
containing_file: string,
|
|
92
|
+
_redirected_reference: ts.ResolvedProjectReference | undefined,
|
|
93
|
+
options: ts.CompilerOptions,
|
|
94
|
+
): Array<ts.ResolvedModuleWithFailedLookupLocations> => {
|
|
95
|
+
return module_literals.map((module_literal) => {
|
|
96
|
+
const module_name = module_literal.text;
|
|
97
|
+
|
|
98
|
+
// Handle relative imports that might be in our virtual files
|
|
99
|
+
if (module_name.startsWith('./') || module_name.startsWith('../')) {
|
|
100
|
+
const module_path = join(containing_file, '..', module_name);
|
|
101
|
+
|
|
102
|
+
// Normalize the path handling for the virtual files
|
|
103
|
+
for (const virtual_path of Object.keys(virtual_files)) {
|
|
104
|
+
const full_path = join(dir, virtual_path);
|
|
105
|
+
const normalized_module_path = module_path.replace(/\.ts$/, '') + '.ts';
|
|
106
|
+
|
|
107
|
+
if (normalized_module_path === full_path) {
|
|
108
|
+
return {
|
|
109
|
+
resolvedModule: {
|
|
110
|
+
resolvedFileName: full_path,
|
|
111
|
+
isExternalLibraryImport: false,
|
|
112
|
+
extension: ts.Extension.Ts,
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// If it's our main file
|
|
120
|
+
if (join(dir, module_name) === file_path) {
|
|
121
|
+
return {
|
|
122
|
+
resolvedModule: {
|
|
123
|
+
resolvedFileName: file_path,
|
|
124
|
+
isExternalLibraryImport: false,
|
|
125
|
+
extension: ts.Extension.Ts,
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// For non-virtual modules, try standard resolution
|
|
131
|
+
return ts.resolveModuleName(module_name, containing_file, options, host);
|
|
132
|
+
});
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
// Include all virtual files in the program files list
|
|
136
|
+
const program_files = [file_path, ...Object.keys(virtual_files).map((path) => join(dir, path))];
|
|
137
|
+
|
|
138
|
+
// TODO get from tsconfig?
|
|
139
|
+
// Create program options
|
|
140
|
+
const compiler_options: ts.CompilerOptions = {
|
|
141
|
+
target: ts.ScriptTarget.ESNext,
|
|
142
|
+
module: ts.ModuleKind.ESNext,
|
|
143
|
+
moduleResolution: ts.ModuleResolutionKind.NodeNext,
|
|
144
|
+
verbatimModuleSyntax: true,
|
|
145
|
+
isolatedModules: true,
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
// Create a program with our virtual files
|
|
149
|
+
const program = ts.createProgram(program_files, compiler_options, host);
|
|
150
|
+
|
|
151
|
+
const source_file = program.getSourceFile(file_path)!;
|
|
152
|
+
const checker = program.getTypeChecker();
|
|
153
|
+
|
|
154
|
+
// Get the exports from the source file
|
|
155
|
+
const symbol = checker.getSymbolAtLocation(source_file);
|
|
156
|
+
const exports = symbol ? checker.getExportsOfModule(symbol) : [];
|
|
157
|
+
|
|
158
|
+
return {source_file, checker, program, exports};
|
|
159
|
+
};
|
package/dist/svelte_helpers.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"svelte_helpers.d.ts","sourceRoot":"../src/lib/","sources":["../src/lib/svelte_helpers.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,cAAc,QAAc,CAAC;AAC1C,eAAO,MAAM,oBAAoB,QAAuB,CAAC"}
|
package/dist/svelte_helpers.js
DELETED