@fulcro/transform-core 0.9.0 → 0.11.0
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/program/index.d.ts +77 -0
- package/dist/program/index.js +7 -4
- package/dist/unplugin/index.mjs +11 -6
- package/package.json +1 -1
package/dist/program/index.d.ts
CHANGED
|
@@ -1,5 +1,82 @@
|
|
|
1
|
+
import typescript from 'typescript';
|
|
1
2
|
import { CallRewriter } from '../shared/index.js';
|
|
2
3
|
import { TransformerOptions } from '../transformer/index.js';
|
|
4
|
+
/**
|
|
5
|
+
* Rewrites a path the way the compiler spells it.
|
|
6
|
+
*
|
|
7
|
+
* TypeScript keeps every file name with forward slashes, on every platform,
|
|
8
|
+
* while the bundler and `path.normalize` hand back the native separator. Mixing
|
|
9
|
+
* the two silently defeats every lookup against the files of the program: a
|
|
10
|
+
* file already in it looks new, gets added as another root, and invalidates the
|
|
11
|
+
* whole program — turning one startup cost into one per file.
|
|
12
|
+
*
|
|
13
|
+
* @param fileName Path in whatever spelling it arrived.
|
|
14
|
+
* @returns The path as the compiler spells it.
|
|
15
|
+
*/
|
|
16
|
+
export declare const toCompilerPath: (fileName: string) => string;
|
|
17
|
+
/**
|
|
18
|
+
* Keeps a TypeScript program alive across recompilations.
|
|
19
|
+
*
|
|
20
|
+
* A language service rather than a one shot program: a watch run recompiles the
|
|
21
|
+
* same files repeatedly, and rebuilding the whole program on each edit would
|
|
22
|
+
* cost seconds every keystroke. The service reuses everything that did not
|
|
23
|
+
* change, and the version counters below are what tell it what did.
|
|
24
|
+
*/
|
|
25
|
+
export declare class ProgramHost implements typescript.LanguageServiceHost {
|
|
26
|
+
/** Files of the program, as resolved from the tsconfig. */
|
|
27
|
+
private readonly rootNames;
|
|
28
|
+
/** Compiler options resolved from the tsconfig. */
|
|
29
|
+
private readonly options;
|
|
30
|
+
/** Content handed over by the bundler, which may be ahead of the disk. */
|
|
31
|
+
private readonly overlays;
|
|
32
|
+
/** Bumped whenever a file changes, which is how the service invalidates. */
|
|
33
|
+
private readonly versions;
|
|
34
|
+
/** Directory the relative paths of the program resolve against. */
|
|
35
|
+
private readonly currentDirectory;
|
|
36
|
+
/**
|
|
37
|
+
* Initializes the host from a parsed tsconfig.
|
|
38
|
+
*
|
|
39
|
+
* @param parsed Parsed contents of the tsconfig.
|
|
40
|
+
* @param currentDirectory Directory the program resolves against.
|
|
41
|
+
*/
|
|
42
|
+
constructor(parsed: typescript.ParsedCommandLine, currentDirectory: string);
|
|
43
|
+
/**
|
|
44
|
+
* Records the content of a file as the bundler sees it.
|
|
45
|
+
*
|
|
46
|
+
* @param fileName File being compiled.
|
|
47
|
+
* @param content Current content of the file.
|
|
48
|
+
*/
|
|
49
|
+
update(fileName: string, content: string): void;
|
|
50
|
+
/**
|
|
51
|
+
* Tells whether the content handed over matches what is on disk.
|
|
52
|
+
*
|
|
53
|
+
* @param fileName File being compiled.
|
|
54
|
+
* @param content Content handed over by the bundler.
|
|
55
|
+
* @returns `true` when the two are identical.
|
|
56
|
+
*/
|
|
57
|
+
private matchesDisk;
|
|
58
|
+
getScriptFileNames(): string[];
|
|
59
|
+
getScriptVersion(fileName: string): string;
|
|
60
|
+
getScriptSnapshot(fileName: string): typescript.IScriptSnapshot | undefined;
|
|
61
|
+
getCurrentDirectory(): string;
|
|
62
|
+
getCompilationSettings(): typescript.CompilerOptions;
|
|
63
|
+
getDefaultLibFileName(options: typescript.CompilerOptions): string;
|
|
64
|
+
readFile(fileName: string, encoding?: string): string | undefined;
|
|
65
|
+
fileExists(fileName: string): boolean;
|
|
66
|
+
readDirectory: (path: string, extensions?: readonly string[], exclude?: readonly string[], include?: readonly string[], depth?: number) => string[];
|
|
67
|
+
directoryExists: (path: string) => boolean;
|
|
68
|
+
getDirectories: (path: string) => string[];
|
|
69
|
+
realpath: ((path: string) => string) | undefined;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Locates and parses the tsconfig driving the program.
|
|
73
|
+
*
|
|
74
|
+
* @param root Directory the search starts from.
|
|
75
|
+
* @param explicit Path given in the options, when there is one.
|
|
76
|
+
* @returns The parsed tsconfig.
|
|
77
|
+
* @throws {Error} When no tsconfig can be found or it cannot be read.
|
|
78
|
+
*/
|
|
79
|
+
export declare const parseTsconfig: (root: string, explicit: string | undefined) => typescript.ParsedCommandLine;
|
|
3
80
|
/** Options accepted by the transformer core. */
|
|
4
81
|
export interface TransformCoreOptions extends TransformerOptions {
|
|
5
82
|
/** Path of the tsconfig driving the program. Defaults to the nearest one. */
|
package/dist/program/index.js
CHANGED
|
@@ -36,7 +36,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
36
36
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.createFileTransformer = void 0;
|
|
39
|
+
exports.createFileTransformer = exports.parseTsconfig = exports.ProgramHost = exports.toCompilerPath = void 0;
|
|
40
40
|
const fs = __importStar(require("node:fs"));
|
|
41
41
|
const path = __importStar(require("node:path"));
|
|
42
42
|
const typescript_1 = __importDefault(require("typescript"));
|
|
@@ -86,6 +86,7 @@ const buildUtilityPattern = (rewriters) => {
|
|
|
86
86
|
* @returns The path as the compiler spells it.
|
|
87
87
|
*/
|
|
88
88
|
const toCompilerPath = (fileName) => path.resolve(fileName).split(path.sep).join('/');
|
|
89
|
+
exports.toCompilerPath = toCompilerPath;
|
|
89
90
|
/**
|
|
90
91
|
* Keeps a TypeScript program alive across recompilations.
|
|
91
92
|
*
|
|
@@ -112,7 +113,7 @@ class ProgramHost {
|
|
|
112
113
|
* @param currentDirectory Directory the program resolves against.
|
|
113
114
|
*/
|
|
114
115
|
constructor(parsed, currentDirectory) {
|
|
115
|
-
this.rootNames = parsed.fileNames.map(toCompilerPath);
|
|
116
|
+
this.rootNames = parsed.fileNames.map(exports.toCompilerPath);
|
|
116
117
|
this.options = parsed.options;
|
|
117
118
|
this.currentDirectory = currentDirectory;
|
|
118
119
|
}
|
|
@@ -192,6 +193,7 @@ class ProgramHost {
|
|
|
192
193
|
getDirectories = typescript_1.default.sys.getDirectories;
|
|
193
194
|
realpath = typescript_1.default.sys.realpath;
|
|
194
195
|
}
|
|
196
|
+
exports.ProgramHost = ProgramHost;
|
|
195
197
|
/**
|
|
196
198
|
* Locates and parses the tsconfig driving the program.
|
|
197
199
|
*
|
|
@@ -214,6 +216,7 @@ const parseTsconfig = (root, explicit) => {
|
|
|
214
216
|
}
|
|
215
217
|
return typescript_1.default.parseJsonConfigFileContent(read.config, typescript_1.default.sys, path.dirname(configPath));
|
|
216
218
|
};
|
|
219
|
+
exports.parseTsconfig = parseTsconfig;
|
|
217
220
|
/**
|
|
218
221
|
* Creates the transformer core.
|
|
219
222
|
*
|
|
@@ -244,9 +247,9 @@ const createFileTransformer = (rewriters, options = {}) => {
|
|
|
244
247
|
// would cost seconds for nothing.
|
|
245
248
|
if (!utilityPattern.test(code))
|
|
246
249
|
return null;
|
|
247
|
-
const fileName = toCompilerPath(id.split('?')[0]);
|
|
250
|
+
const fileName = (0, exports.toCompilerPath)(id.split('?')[0]);
|
|
248
251
|
if (host === undefined || service === undefined) {
|
|
249
|
-
host = new ProgramHost(parseTsconfig(root, options.tsconfig), root);
|
|
252
|
+
host = new ProgramHost((0, exports.parseTsconfig)(root, options.tsconfig), root);
|
|
250
253
|
service = typescript_1.default.createLanguageService(host, typescript_1.default.createDocumentRegistry());
|
|
251
254
|
}
|
|
252
255
|
host.update(fileName, code);
|
package/dist/unplugin/index.mjs
CHANGED
|
@@ -8,7 +8,15 @@ import { createFileTransformer, } from '../program/index.js';
|
|
|
8
8
|
* in its logs and timings.
|
|
9
9
|
* @returns Every adapter `unplugin` can produce.
|
|
10
10
|
*/
|
|
11
|
-
export const createTransformerUnplugin = (rewriters, name) =>
|
|
11
|
+
export const createTransformerUnplugin = (rewriters, name) => createAdapters((options) => createFileTransformer(rewriters, options), name);
|
|
12
|
+
/**
|
|
13
|
+
* Builds the adapters around the core of a package.
|
|
14
|
+
*
|
|
15
|
+
* @param build Builds the core for a set of options.
|
|
16
|
+
* @param name Name the plugin reports to the bundler.
|
|
17
|
+
* @returns Every adapter `unplugin` can produce.
|
|
18
|
+
*/
|
|
19
|
+
const createAdapters = (build, name) => {
|
|
12
20
|
const unpluginFactory = (options = {}) => {
|
|
13
21
|
let transformer;
|
|
14
22
|
/**
|
|
@@ -18,7 +26,7 @@ export const createTransformerUnplugin = (rewriters, name) => {
|
|
|
18
26
|
* @returns The transformer core.
|
|
19
27
|
*/
|
|
20
28
|
const resolve = () => {
|
|
21
|
-
transformer ??=
|
|
29
|
+
transformer ??= build(options);
|
|
22
30
|
return transformer;
|
|
23
31
|
};
|
|
24
32
|
return {
|
|
@@ -39,10 +47,7 @@ export const createTransformerUnplugin = (rewriters, name) => {
|
|
|
39
47
|
// The root of the bundler wins over the working directory, which
|
|
40
48
|
// is what makes the plugin behave inside a monorepo.
|
|
41
49
|
if (config.root !== undefined && options.root === undefined) {
|
|
42
|
-
transformer =
|
|
43
|
-
...options,
|
|
44
|
-
root: config.root,
|
|
45
|
-
});
|
|
50
|
+
transformer = build({ ...options, root: config.root });
|
|
46
51
|
}
|
|
47
52
|
},
|
|
48
53
|
},
|
package/package.json
CHANGED