@atscript/core 0.2.18 → 0.2.19
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/index.cjs +109 -40
- package/dist/index.d.ts +42 -4
- package/dist/index.mjs +113 -44
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1787,6 +1787,26 @@ function describeUnionMembers(def, doc) {
|
|
|
1787
1787
|
|
|
1788
1788
|
//#endregion
|
|
1789
1789
|
//#region packages/core/src/config/load-config.ts
|
|
1790
|
+
/** Numbers the bundled copies of this process (see `bundleTsConfig`). */ let bundleSeq = 0;
|
|
1791
|
+
/** The name of a bundled copy of a `.ts` config (see `bundleTsConfig`): its process id. */ const BUNDLED_COPY = /^atscript\.config\.[\w-]+\.(\d+)\.\d+\.[cm]js$/u;
|
|
1792
|
+
/** Whether process `pid` is gone (a live process of another user counts as alive). */ function isGone(pid) {
|
|
1793
|
+
try {
|
|
1794
|
+
process.kill(pid, 0);
|
|
1795
|
+
return false;
|
|
1796
|
+
} catch (error) {
|
|
1797
|
+
return error.code === "ESRCH";
|
|
1798
|
+
}
|
|
1799
|
+
}
|
|
1800
|
+
/**
|
|
1801
|
+
* Removes the bundled copies in `dir` of processes that are gone (killed while they loaded the
|
|
1802
|
+
* config, before they removed their copy); the copies of live processes are theirs.
|
|
1803
|
+
*/ async function removeLeftoverCopies(dir) {
|
|
1804
|
+
const names = await (0, node_fs_promises.readdir)(dir).catch(() => []);
|
|
1805
|
+
await Promise.all(names.map(async (name) => {
|
|
1806
|
+
const pid = Number(BUNDLED_COPY.exec(name)?.[1]);
|
|
1807
|
+
if (pid && pid !== process.pid && isGone(pid)) await (0, node_fs_promises.rm)(node_path.default.join(dir, name), { force: true }).catch(() => {});
|
|
1808
|
+
}));
|
|
1809
|
+
}
|
|
1790
1810
|
async function bundleTsConfig(configFile, forceFormat) {
|
|
1791
1811
|
const dirnameVarName = "injected_original_dirname";
|
|
1792
1812
|
const filenameVarName = "injected_original_filename";
|
|
@@ -1819,11 +1839,13 @@ async function bundleTsConfig(configFile, forceFormat) {
|
|
|
1819
1839
|
}]
|
|
1820
1840
|
});
|
|
1821
1841
|
const outputDir = node_path.default.dirname(configFile);
|
|
1842
|
+
const name = `atscript.config.[hash].${process.pid}.${bundleSeq++}`;
|
|
1843
|
+
await removeLeftoverCopies(outputDir);
|
|
1822
1844
|
const fileName = (await bundle.write({
|
|
1823
1845
|
dir: outputDir,
|
|
1824
1846
|
format: forceFormat || "esm",
|
|
1825
1847
|
sourcemap: "inline",
|
|
1826
|
-
entryFileNames: forceFormat === "cjs" ?
|
|
1848
|
+
entryFileNames: forceFormat === "cjs" ? `${name}.cjs` : `${name}.mjs`
|
|
1827
1849
|
})).output.find((chunk) => chunk.type === "chunk" && chunk.isEntry).fileName;
|
|
1828
1850
|
return node_path.default.join(outputDir, fileName);
|
|
1829
1851
|
}
|
|
@@ -1868,10 +1890,6 @@ async function loadTsConfig(configFile, forceFormat) {
|
|
|
1868
1890
|
/* @vite-ignore */
|
|
1869
1891
|
(0, node_url.pathToFileURL)(file).href
|
|
1870
1892
|
)).default;
|
|
1871
|
-
} catch (error) {
|
|
1872
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
1873
|
-
console.error(`Could not load config file ${configFile}: ${message}`);
|
|
1874
|
-
return {};
|
|
1875
1893
|
} finally {
|
|
1876
1894
|
node_fs.default.unlink(file, () => {});
|
|
1877
1895
|
}
|
|
@@ -1888,7 +1906,7 @@ async function loadConfig(configPath, forceFormat) {
|
|
|
1888
1906
|
else throw new Error(`Unsupported config format. Expected: \`${SUPPORTED_CONFIG_FORMATS.join(",")}\` but got \`${ext}\``);
|
|
1889
1907
|
} catch (error) {
|
|
1890
1908
|
console.error(error);
|
|
1891
|
-
throw new Error("Error happened while loading config.");
|
|
1909
|
+
throw new Error("Error happened while loading config.", { cause: error });
|
|
1892
1910
|
}
|
|
1893
1911
|
}
|
|
1894
1912
|
|
|
@@ -11415,23 +11433,48 @@ var PluginManager = class {
|
|
|
11415
11433
|
}
|
|
11416
11434
|
if (raw?.annotations) this._docConfig.annotations = { ...raw.annotations };
|
|
11417
11435
|
this._docConfig.unknownAnnotation = raw?.unknownAnnotation;
|
|
11418
|
-
this._docConfig.rootDir = this.root ?? (
|
|
11436
|
+
this._docConfig.rootDir = this.root ?? (this.cfg.rootDir && node_path.default.isAbsolute(this.cfg.rootDir) ? this.cfg.rootDir : void 0);
|
|
11419
11437
|
}
|
|
11420
11438
|
return this._docConfig;
|
|
11421
11439
|
}
|
|
11422
|
-
|
|
11440
|
+
/**
|
|
11441
|
+
* The config merged with the defaults and every plugin's `config` hook. Its `rootDir` (also the
|
|
11442
|
+
* one the hooks receive) is the project root, absolute: the host's, else the config's `rootDir`
|
|
11443
|
+
* resolved against the working directory (as `build()` resolves a config without a file).
|
|
11444
|
+
*/ config(config = this.cfg) {
|
|
11423
11445
|
if (!this._config) {
|
|
11424
|
-
|
|
11425
|
-
|
|
11426
|
-
|
|
11427
|
-
if (
|
|
11428
|
-
|
|
11429
|
-
config = (0, defu.defu)(await plugin.config?.(config), config);
|
|
11430
|
-
}
|
|
11431
|
-
this._config = config;
|
|
11446
|
+
const merging = this.mergeConfig(config);
|
|
11447
|
+
this._config = merging;
|
|
11448
|
+
merging.catch(() => {
|
|
11449
|
+
if (this._config === merging) this._config = void 0;
|
|
11450
|
+
});
|
|
11432
11451
|
}
|
|
11433
11452
|
return this._config;
|
|
11434
11453
|
}
|
|
11454
|
+
async mergeConfig(config) {
|
|
11455
|
+
const rootDir = this.root ?? node_path.default.resolve(config.rootDir ?? ".");
|
|
11456
|
+
let merged = {
|
|
11457
|
+
...(0, defu.defu)(config, getDefaultAtscriptConfig()),
|
|
11458
|
+
rootDir
|
|
11459
|
+
};
|
|
11460
|
+
const seen = /* @__PURE__ */ new Set();
|
|
11461
|
+
for (const plugin of this.plugins) {
|
|
11462
|
+
if (seen.has(plugin.name)) continue;
|
|
11463
|
+
seen.add(plugin.name);
|
|
11464
|
+
merged = (0, defu.defu)(await plugin.config?.(merged), merged);
|
|
11465
|
+
}
|
|
11466
|
+
return {
|
|
11467
|
+
...merged,
|
|
11468
|
+
rootDir
|
|
11469
|
+
};
|
|
11470
|
+
}
|
|
11471
|
+
/**
|
|
11472
|
+
* Starts a build: the plugins' `config` hooks (once per manager), then their `buildStart`
|
|
11473
|
+
* hooks in order, each awaited — before the host discovers or reads any `.as` file.
|
|
11474
|
+
*/ async buildStart() {
|
|
11475
|
+
const config = await this.config();
|
|
11476
|
+
for (const plugin of this.plugins) if (plugin.buildStart) await plugin.buildStart(config);
|
|
11477
|
+
}
|
|
11435
11478
|
async resolve(id) {
|
|
11436
11479
|
let newId = id;
|
|
11437
11480
|
for (const plugin of this.plugins) if (plugin.resolve) newId = await plugin.resolve(newId) ?? newId;
|
|
@@ -11469,7 +11512,7 @@ var PluginManager = class {
|
|
|
11469
11512
|
constructor(cfg, root) {
|
|
11470
11513
|
_define_property$2(this, "cfg", void 0);
|
|
11471
11514
|
_define_property$2(this, "root", void 0);
|
|
11472
|
-
_define_property$2(this, "_config", void 0);
|
|
11515
|
+
/** The merged config: the plugins' `config` hooks run once per manager (again after a failure). */ _define_property$2(this, "_config", void 0);
|
|
11473
11516
|
_define_property$2(this, "name", void 0);
|
|
11474
11517
|
_define_property$2(this, "_docConfig", void 0);
|
|
11475
11518
|
this.cfg = cfg;
|
|
@@ -11616,29 +11659,37 @@ var AtscriptRepo = class {
|
|
|
11616
11659
|
}
|
|
11617
11660
|
return annotations;
|
|
11618
11661
|
}
|
|
11619
|
-
|
|
11662
|
+
/**
|
|
11663
|
+
* The plugin manager for document `id`, its plugins' `config` hooks run (before the document
|
|
11664
|
+
* is resolved or loaded): the shared config's, or that of the config file `id` is under.
|
|
11665
|
+
*/ async loadPluginManagerFor(id) {
|
|
11620
11666
|
if (this.sharedConfig) {
|
|
11621
|
-
if (!this.
|
|
11622
|
-
|
|
11623
|
-
|
|
11624
|
-
|
|
11625
|
-
|
|
11667
|
+
if (!this.sharedPluginManagerReady) {
|
|
11668
|
+
const shared = {
|
|
11669
|
+
manager: new PluginManager(this.sharedConfig, node_path.default.resolve(this.root, this.sharedConfig.rootDir ?? ".")),
|
|
11670
|
+
dependants: /* @__PURE__ */ new Set()
|
|
11671
|
+
};
|
|
11672
|
+
const ready = shared.manager.getDocConfig().then(() => shared);
|
|
11673
|
+
this.sharedPluginManager = shared;
|
|
11674
|
+
this.sharedPluginManagerReady = ready;
|
|
11675
|
+
ready.catch(() => {
|
|
11676
|
+
if (this.sharedPluginManagerReady === ready) this.sharedPluginManagerReady = void 0;
|
|
11677
|
+
});
|
|
11678
|
+
}
|
|
11679
|
+
return this.sharedPluginManagerReady;
|
|
11626
11680
|
}
|
|
11627
11681
|
const configFile = await resolveConfigFile(id, this.root);
|
|
11628
11682
|
if (configFile) {
|
|
11629
|
-
|
|
11630
|
-
|
|
11631
|
-
this.
|
|
11683
|
+
let managers = this.configFiles.get(configFile);
|
|
11684
|
+
if (!managers) {
|
|
11685
|
+
const loading = this.loadConfigFile(configFile);
|
|
11686
|
+
managers = loading;
|
|
11687
|
+
this.configFiles.set(configFile, loading);
|
|
11688
|
+
loading.catch(() => {
|
|
11689
|
+
if (this.configFiles.get(configFile) === loading) this.configFiles.delete(configFile);
|
|
11690
|
+
});
|
|
11632
11691
|
}
|
|
11633
|
-
|
|
11634
|
-
if (!rawConfig.rootDir) rawConfig.rootDir = node_path.default.dirname(configFile);
|
|
11635
|
-
const manager = new PluginManager(rawConfig, node_path.default.resolve(node_path.default.dirname(configFile), rawConfig.rootDir));
|
|
11636
|
-
await manager.getDocConfig();
|
|
11637
|
-
return {
|
|
11638
|
-
file: configFile,
|
|
11639
|
-
manager,
|
|
11640
|
-
dependants: /* @__PURE__ */ new Set()
|
|
11641
|
-
};
|
|
11692
|
+
return managers;
|
|
11642
11693
|
} else {
|
|
11643
11694
|
const manager = new PluginManager({ rootDir: process.cwd() }, process.cwd());
|
|
11644
11695
|
await manager.getDocConfig();
|
|
@@ -11648,14 +11699,28 @@ var AtscriptRepo = class {
|
|
|
11648
11699
|
};
|
|
11649
11700
|
}
|
|
11650
11701
|
}
|
|
11702
|
+
/** Loads `configFile` and runs its plugins' `config` hooks (see `configFiles`). */ async loadConfigFile(configFile) {
|
|
11703
|
+
const rawConfig = await loadConfig(configFile, this.configFormat);
|
|
11704
|
+
if (!rawConfig.rootDir) rawConfig.rootDir = node_path.default.dirname(configFile);
|
|
11705
|
+
const manager = new PluginManager(rawConfig, node_path.default.resolve(node_path.default.dirname(configFile), rawConfig.rootDir));
|
|
11706
|
+
await manager.getDocConfig();
|
|
11707
|
+
return {
|
|
11708
|
+
file: configFile,
|
|
11709
|
+
manager,
|
|
11710
|
+
dependants: /* @__PURE__ */ new Set()
|
|
11711
|
+
};
|
|
11712
|
+
}
|
|
11651
11713
|
async resolveConfig(id) {
|
|
11652
11714
|
let config = this.configs.get(id);
|
|
11653
11715
|
if (!config) {
|
|
11654
|
-
|
|
11655
|
-
config
|
|
11716
|
+
const loading = this.loadPluginManagerFor(id);
|
|
11717
|
+
config = loading;
|
|
11718
|
+
loading.then((c) => {
|
|
11656
11719
|
c.dependants.add(id);
|
|
11720
|
+
}, () => {
|
|
11721
|
+
if (this.configs.get(id) === loading) this.configs.delete(id);
|
|
11657
11722
|
});
|
|
11658
|
-
this.configs.set(id,
|
|
11723
|
+
this.configs.set(id, loading);
|
|
11659
11724
|
}
|
|
11660
11725
|
return config;
|
|
11661
11726
|
}
|
|
@@ -11780,11 +11845,13 @@ var AtscriptRepo = class {
|
|
|
11780
11845
|
* .as Documents cache
|
|
11781
11846
|
*/ _define_property$1(this, "atscripts", void 0);
|
|
11782
11847
|
_define_property$1(this, "sharedPluginManager", void 0);
|
|
11848
|
+
/** The shared plugin manager once its plugins' `config` hooks ran (see `loadPluginManagerFor`). */ _define_property$1(this, "sharedPluginManagerReady", void 0);
|
|
11783
11849
|
/**
|
|
11784
11850
|
* Maps resolved file:// URIs back to bare specifiers (for LSP auto-import).
|
|
11785
11851
|
*/ _define_property$1(this, "resolvedToBare", void 0);
|
|
11786
11852
|
/**
|
|
11787
|
-
*
|
|
11853
|
+
* The plugin manager of each config file (by path), shared by every document under it: its
|
|
11854
|
+
* plugins' `config` hooks run once per load of the file. Dropping an entry loads the file again.
|
|
11788
11855
|
*/ _define_property$1(this, "configFiles", void 0);
|
|
11789
11856
|
this.root = root;
|
|
11790
11857
|
this.sharedConfig = sharedConfig;
|
|
@@ -11831,9 +11898,11 @@ function _define_property(obj, key, value) {
|
|
|
11831
11898
|
}
|
|
11832
11899
|
/** Upper bound on concurrent file operations — macOS defaults `ulimit -n` to 256. */ const WRITE_CONCURRENCY = 32;
|
|
11833
11900
|
async function build(config) {
|
|
11834
|
-
const rootDir = config.rootDir ? config.rootDir
|
|
11901
|
+
const rootDir = config.rootDir ? path.default.isAbsolute(config.rootDir) ? config.rootDir : path.default.join(process.cwd(), config.rootDir) : process.cwd();
|
|
11835
11902
|
config.rootDir = rootDir;
|
|
11836
11903
|
const repo = new AtscriptRepo(rootDir, config);
|
|
11904
|
+
const { manager } = await repo.loadPluginManagerFor(`file://${rootDir}`);
|
|
11905
|
+
await manager.buildStart();
|
|
11837
11906
|
const entries = [];
|
|
11838
11907
|
if (config.entries && config.entries.length > 0) for (const entry of config.entries) entries.push(path.default.join(rootDir, entry));
|
|
11839
11908
|
else {
|
package/dist/index.d.ts
CHANGED
|
@@ -166,14 +166,17 @@ declare class AtscriptRepo {
|
|
|
166
166
|
*/
|
|
167
167
|
protected readonly atscripts: Map<string, Promise<AtscriptDoc>>;
|
|
168
168
|
sharedPluginManager: TPluginManagers | undefined;
|
|
169
|
+
/** The shared plugin manager once its plugins' `config` hooks ran (see `loadPluginManagerFor`). */
|
|
170
|
+
private sharedPluginManagerReady;
|
|
169
171
|
/**
|
|
170
172
|
* Maps resolved file:// URIs back to bare specifiers (for LSP auto-import).
|
|
171
173
|
*/
|
|
172
174
|
readonly resolvedToBare: Map<string, string>;
|
|
173
175
|
/**
|
|
174
|
-
*
|
|
176
|
+
* The plugin manager of each config file (by path), shared by every document under it: its
|
|
177
|
+
* plugins' `config` hooks run once per load of the file. Dropping an entry loads the file again.
|
|
175
178
|
*/
|
|
176
|
-
protected configFiles: Map<string, Promise<
|
|
179
|
+
protected configFiles: Map<string, Promise<TPluginManagers>>;
|
|
177
180
|
getSharedPluginManager(): PluginManager | undefined;
|
|
178
181
|
getPrimitivesTags(): Promise<Set<string> | undefined>;
|
|
179
182
|
getUsedAnnotations(): Promise<Record<string, {
|
|
@@ -191,7 +194,13 @@ declare class AtscriptRepo {
|
|
|
191
194
|
type: string;
|
|
192
195
|
}>;
|
|
193
196
|
} | undefined>>;
|
|
197
|
+
/**
|
|
198
|
+
* The plugin manager for document `id`, its plugins' `config` hooks run (before the document
|
|
199
|
+
* is resolved or loaded): the shared config's, or that of the config file `id` is under.
|
|
200
|
+
*/
|
|
194
201
|
loadPluginManagerFor(id: string): Promise<TPluginManagers>;
|
|
202
|
+
/** Loads `configFile` and runs its plugins' `config` hooks (see `configFiles`). */
|
|
203
|
+
protected loadConfigFile(configFile: string): Promise<TPluginManagers>;
|
|
195
204
|
resolveConfig(id: string): Promise<TPluginManagers>;
|
|
196
205
|
openDocument(id: string, text?: string): Promise<AtscriptDoc>;
|
|
197
206
|
/**
|
|
@@ -279,6 +288,21 @@ interface TAtscriptRenderOptions {
|
|
|
279
288
|
interface TAtscriptPlugin {
|
|
280
289
|
name: string;
|
|
281
290
|
config?: (config: TAtscriptConfig) => Promise<TAtscriptConfig | undefined> | TAtscriptConfig | undefined;
|
|
291
|
+
/**
|
|
292
|
+
* The start of a build: runs after every plugin's `config`, before any `.as` file is
|
|
293
|
+
* discovered or read — the place to write `.as` files (and other files) generated from
|
|
294
|
+
* another source.
|
|
295
|
+
*
|
|
296
|
+
* `config` is the final config; its `rootDir` is absolute. Called by `build()` (the `asc` CLI)
|
|
297
|
+
* and by the bundler plugin (each build, each watch rebuild, the dev server's start), so it may
|
|
298
|
+
* run several times per process, and several runs may overlap (parallel builds, test projects
|
|
299
|
+
* sharing a plugin instance): write each file atomically (a temp file renamed over it), and only
|
|
300
|
+
* when its content changes. The language server never calls it (it reads what a build left on
|
|
301
|
+
* disk).
|
|
302
|
+
*/
|
|
303
|
+
buildStart?: (config: TAtscriptConfig & {
|
|
304
|
+
rootDir: string;
|
|
305
|
+
}) => Promise<void> | void;
|
|
282
306
|
resolve?: (id: string) => Promise<string | undefined> | string | undefined;
|
|
283
307
|
load?: (id: string) => Promise<string | undefined> | string | undefined;
|
|
284
308
|
onDocument?: (doc: AtscriptDoc) => Promise<void> | void;
|
|
@@ -297,12 +321,26 @@ declare class PluginManager {
|
|
|
297
321
|
constructor(cfg: TAtscriptConfig,
|
|
298
322
|
/** The project root (absolute), the config's `rootDir` resolved. */
|
|
299
323
|
root?: string | undefined);
|
|
300
|
-
|
|
324
|
+
/** The merged config: the plugins' `config` hooks run once per manager (again after a failure). */
|
|
325
|
+
private _config?;
|
|
301
326
|
name: string;
|
|
302
327
|
protected _docConfig: TAtscriptDocConfig | undefined;
|
|
303
328
|
get plugins(): TAtscriptPlugin[];
|
|
304
329
|
getDocConfig(): Promise<TAtscriptDocConfig>;
|
|
305
|
-
|
|
330
|
+
/**
|
|
331
|
+
* The config merged with the defaults and every plugin's `config` hook. Its `rootDir` (also the
|
|
332
|
+
* one the hooks receive) is the project root, absolute: the host's, else the config's `rootDir`
|
|
333
|
+
* resolved against the working directory (as `build()` resolves a config without a file).
|
|
334
|
+
*/
|
|
335
|
+
config(config?: TAtscriptConfig): Promise<TAtscriptConfig & {
|
|
336
|
+
rootDir: string;
|
|
337
|
+
}>;
|
|
338
|
+
private mergeConfig;
|
|
339
|
+
/**
|
|
340
|
+
* Starts a build: the plugins' `config` hooks (once per manager), then their `buildStart`
|
|
341
|
+
* hooks in order, each awaited — before the host discovers or reads any `.as` file.
|
|
342
|
+
*/
|
|
343
|
+
buildStart(): Promise<void>;
|
|
306
344
|
resolve(id: string): Promise<string>;
|
|
307
345
|
load(id: string): Promise<string>;
|
|
308
346
|
onDocument(doc: AtscriptDoc): Promise<void>;
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import path from "path";
|
|
2
2
|
import { URL } from "url";
|
|
3
|
-
import { access, readFile, readdir, realpath } from "node:fs/promises";
|
|
4
|
-
import path$1
|
|
3
|
+
import { access, readFile, readdir, realpath, rm } from "node:fs/promises";
|
|
4
|
+
import path$1 from "node:path";
|
|
5
5
|
import fs from "node:fs";
|
|
6
6
|
import { pathToFileURL } from "node:url";
|
|
7
7
|
import { rolldown } from "rolldown";
|
|
@@ -9,7 +9,7 @@ import { createHash } from "crypto";
|
|
|
9
9
|
import { readFileSync, statSync } from "fs";
|
|
10
10
|
import { Node, printTree } from "@prostojs/parser";
|
|
11
11
|
import { defu } from "defu";
|
|
12
|
-
import { mkdir, readFile as readFile$1, rename, rm, writeFile } from "fs/promises";
|
|
12
|
+
import { mkdir, readFile as readFile$1, rename, rm as rm$1, writeFile } from "fs/promises";
|
|
13
13
|
import { glob } from "glob";
|
|
14
14
|
|
|
15
15
|
//#region packages/core/src/parser/nodes/semantic-node.ts
|
|
@@ -1756,6 +1756,26 @@ function describeUnionMembers(def, doc) {
|
|
|
1756
1756
|
|
|
1757
1757
|
//#endregion
|
|
1758
1758
|
//#region packages/core/src/config/load-config.ts
|
|
1759
|
+
/** Numbers the bundled copies of this process (see `bundleTsConfig`). */ let bundleSeq = 0;
|
|
1760
|
+
/** The name of a bundled copy of a `.ts` config (see `bundleTsConfig`): its process id. */ const BUNDLED_COPY = /^atscript\.config\.[\w-]+\.(\d+)\.\d+\.[cm]js$/u;
|
|
1761
|
+
/** Whether process `pid` is gone (a live process of another user counts as alive). */ function isGone(pid) {
|
|
1762
|
+
try {
|
|
1763
|
+
process.kill(pid, 0);
|
|
1764
|
+
return false;
|
|
1765
|
+
} catch (error) {
|
|
1766
|
+
return error.code === "ESRCH";
|
|
1767
|
+
}
|
|
1768
|
+
}
|
|
1769
|
+
/**
|
|
1770
|
+
* Removes the bundled copies in `dir` of processes that are gone (killed while they loaded the
|
|
1771
|
+
* config, before they removed their copy); the copies of live processes are theirs.
|
|
1772
|
+
*/ async function removeLeftoverCopies(dir) {
|
|
1773
|
+
const names = await readdir(dir).catch(() => []);
|
|
1774
|
+
await Promise.all(names.map(async (name) => {
|
|
1775
|
+
const pid = Number(BUNDLED_COPY.exec(name)?.[1]);
|
|
1776
|
+
if (pid && pid !== process.pid && isGone(pid)) await rm(path$1.join(dir, name), { force: true }).catch(() => {});
|
|
1777
|
+
}));
|
|
1778
|
+
}
|
|
1759
1779
|
async function bundleTsConfig(configFile, forceFormat) {
|
|
1760
1780
|
const dirnameVarName = "injected_original_dirname";
|
|
1761
1781
|
const filenameVarName = "injected_original_filename";
|
|
@@ -1788,11 +1808,13 @@ async function bundleTsConfig(configFile, forceFormat) {
|
|
|
1788
1808
|
}]
|
|
1789
1809
|
});
|
|
1790
1810
|
const outputDir = path$1.dirname(configFile);
|
|
1811
|
+
const name = `atscript.config.[hash].${process.pid}.${bundleSeq++}`;
|
|
1812
|
+
await removeLeftoverCopies(outputDir);
|
|
1791
1813
|
const fileName = (await bundle.write({
|
|
1792
1814
|
dir: outputDir,
|
|
1793
1815
|
format: forceFormat || "esm",
|
|
1794
1816
|
sourcemap: "inline",
|
|
1795
|
-
entryFileNames: forceFormat === "cjs" ?
|
|
1817
|
+
entryFileNames: forceFormat === "cjs" ? `${name}.cjs` : `${name}.mjs`
|
|
1796
1818
|
})).output.find((chunk) => chunk.type === "chunk" && chunk.isEntry).fileName;
|
|
1797
1819
|
return path$1.join(outputDir, fileName);
|
|
1798
1820
|
}
|
|
@@ -1837,10 +1859,6 @@ async function loadTsConfig(configFile, forceFormat) {
|
|
|
1837
1859
|
/* @vite-ignore */
|
|
1838
1860
|
pathToFileURL(file).href
|
|
1839
1861
|
)).default;
|
|
1840
|
-
} catch (error) {
|
|
1841
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
1842
|
-
console.error(`Could not load config file ${configFile}: ${message}`);
|
|
1843
|
-
return {};
|
|
1844
1862
|
} finally {
|
|
1845
1863
|
fs.unlink(file, () => {});
|
|
1846
1864
|
}
|
|
@@ -1857,7 +1875,7 @@ async function loadConfig(configPath, forceFormat) {
|
|
|
1857
1875
|
else throw new Error(`Unsupported config format. Expected: \`${SUPPORTED_CONFIG_FORMATS.join(",")}\` but got \`${ext}\``);
|
|
1858
1876
|
} catch (error) {
|
|
1859
1877
|
console.error(error);
|
|
1860
|
-
throw new Error("Error happened while loading config.");
|
|
1878
|
+
throw new Error("Error happened while loading config.", { cause: error });
|
|
1861
1879
|
}
|
|
1862
1880
|
}
|
|
1863
1881
|
|
|
@@ -11384,23 +11402,48 @@ var PluginManager = class {
|
|
|
11384
11402
|
}
|
|
11385
11403
|
if (raw?.annotations) this._docConfig.annotations = { ...raw.annotations };
|
|
11386
11404
|
this._docConfig.unknownAnnotation = raw?.unknownAnnotation;
|
|
11387
|
-
this._docConfig.rootDir = this.root ?? (
|
|
11405
|
+
this._docConfig.rootDir = this.root ?? (this.cfg.rootDir && path$1.isAbsolute(this.cfg.rootDir) ? this.cfg.rootDir : void 0);
|
|
11388
11406
|
}
|
|
11389
11407
|
return this._docConfig;
|
|
11390
11408
|
}
|
|
11391
|
-
|
|
11409
|
+
/**
|
|
11410
|
+
* The config merged with the defaults and every plugin's `config` hook. Its `rootDir` (also the
|
|
11411
|
+
* one the hooks receive) is the project root, absolute: the host's, else the config's `rootDir`
|
|
11412
|
+
* resolved against the working directory (as `build()` resolves a config without a file).
|
|
11413
|
+
*/ config(config = this.cfg) {
|
|
11392
11414
|
if (!this._config) {
|
|
11393
|
-
|
|
11394
|
-
|
|
11395
|
-
|
|
11396
|
-
if (
|
|
11397
|
-
|
|
11398
|
-
config = defu(await plugin.config?.(config), config);
|
|
11399
|
-
}
|
|
11400
|
-
this._config = config;
|
|
11415
|
+
const merging = this.mergeConfig(config);
|
|
11416
|
+
this._config = merging;
|
|
11417
|
+
merging.catch(() => {
|
|
11418
|
+
if (this._config === merging) this._config = void 0;
|
|
11419
|
+
});
|
|
11401
11420
|
}
|
|
11402
11421
|
return this._config;
|
|
11403
11422
|
}
|
|
11423
|
+
async mergeConfig(config) {
|
|
11424
|
+
const rootDir = this.root ?? path$1.resolve(config.rootDir ?? ".");
|
|
11425
|
+
let merged = {
|
|
11426
|
+
...defu(config, getDefaultAtscriptConfig()),
|
|
11427
|
+
rootDir
|
|
11428
|
+
};
|
|
11429
|
+
const seen = /* @__PURE__ */ new Set();
|
|
11430
|
+
for (const plugin of this.plugins) {
|
|
11431
|
+
if (seen.has(plugin.name)) continue;
|
|
11432
|
+
seen.add(plugin.name);
|
|
11433
|
+
merged = defu(await plugin.config?.(merged), merged);
|
|
11434
|
+
}
|
|
11435
|
+
return {
|
|
11436
|
+
...merged,
|
|
11437
|
+
rootDir
|
|
11438
|
+
};
|
|
11439
|
+
}
|
|
11440
|
+
/**
|
|
11441
|
+
* Starts a build: the plugins' `config` hooks (once per manager), then their `buildStart`
|
|
11442
|
+
* hooks in order, each awaited — before the host discovers or reads any `.as` file.
|
|
11443
|
+
*/ async buildStart() {
|
|
11444
|
+
const config = await this.config();
|
|
11445
|
+
for (const plugin of this.plugins) if (plugin.buildStart) await plugin.buildStart(config);
|
|
11446
|
+
}
|
|
11404
11447
|
async resolve(id) {
|
|
11405
11448
|
let newId = id;
|
|
11406
11449
|
for (const plugin of this.plugins) if (plugin.resolve) newId = await plugin.resolve(newId) ?? newId;
|
|
@@ -11438,7 +11481,7 @@ var PluginManager = class {
|
|
|
11438
11481
|
constructor(cfg, root) {
|
|
11439
11482
|
_define_property$2(this, "cfg", void 0);
|
|
11440
11483
|
_define_property$2(this, "root", void 0);
|
|
11441
|
-
_define_property$2(this, "_config", void 0);
|
|
11484
|
+
/** The merged config: the plugins' `config` hooks run once per manager (again after a failure). */ _define_property$2(this, "_config", void 0);
|
|
11442
11485
|
_define_property$2(this, "name", void 0);
|
|
11443
11486
|
_define_property$2(this, "_docConfig", void 0);
|
|
11444
11487
|
this.cfg = cfg;
|
|
@@ -11585,29 +11628,37 @@ var AtscriptRepo = class {
|
|
|
11585
11628
|
}
|
|
11586
11629
|
return annotations;
|
|
11587
11630
|
}
|
|
11588
|
-
|
|
11631
|
+
/**
|
|
11632
|
+
* The plugin manager for document `id`, its plugins' `config` hooks run (before the document
|
|
11633
|
+
* is resolved or loaded): the shared config's, or that of the config file `id` is under.
|
|
11634
|
+
*/ async loadPluginManagerFor(id) {
|
|
11589
11635
|
if (this.sharedConfig) {
|
|
11590
|
-
if (!this.
|
|
11591
|
-
|
|
11592
|
-
|
|
11593
|
-
|
|
11594
|
-
|
|
11636
|
+
if (!this.sharedPluginManagerReady) {
|
|
11637
|
+
const shared = {
|
|
11638
|
+
manager: new PluginManager(this.sharedConfig, path$1.resolve(this.root, this.sharedConfig.rootDir ?? ".")),
|
|
11639
|
+
dependants: /* @__PURE__ */ new Set()
|
|
11640
|
+
};
|
|
11641
|
+
const ready = shared.manager.getDocConfig().then(() => shared);
|
|
11642
|
+
this.sharedPluginManager = shared;
|
|
11643
|
+
this.sharedPluginManagerReady = ready;
|
|
11644
|
+
ready.catch(() => {
|
|
11645
|
+
if (this.sharedPluginManagerReady === ready) this.sharedPluginManagerReady = void 0;
|
|
11646
|
+
});
|
|
11647
|
+
}
|
|
11648
|
+
return this.sharedPluginManagerReady;
|
|
11595
11649
|
}
|
|
11596
11650
|
const configFile = await resolveConfigFile(id, this.root);
|
|
11597
11651
|
if (configFile) {
|
|
11598
|
-
|
|
11599
|
-
|
|
11600
|
-
this.
|
|
11652
|
+
let managers = this.configFiles.get(configFile);
|
|
11653
|
+
if (!managers) {
|
|
11654
|
+
const loading = this.loadConfigFile(configFile);
|
|
11655
|
+
managers = loading;
|
|
11656
|
+
this.configFiles.set(configFile, loading);
|
|
11657
|
+
loading.catch(() => {
|
|
11658
|
+
if (this.configFiles.get(configFile) === loading) this.configFiles.delete(configFile);
|
|
11659
|
+
});
|
|
11601
11660
|
}
|
|
11602
|
-
|
|
11603
|
-
if (!rawConfig.rootDir) rawConfig.rootDir = path$1.dirname(configFile);
|
|
11604
|
-
const manager = new PluginManager(rawConfig, path$1.resolve(path$1.dirname(configFile), rawConfig.rootDir));
|
|
11605
|
-
await manager.getDocConfig();
|
|
11606
|
-
return {
|
|
11607
|
-
file: configFile,
|
|
11608
|
-
manager,
|
|
11609
|
-
dependants: /* @__PURE__ */ new Set()
|
|
11610
|
-
};
|
|
11661
|
+
return managers;
|
|
11611
11662
|
} else {
|
|
11612
11663
|
const manager = new PluginManager({ rootDir: process.cwd() }, process.cwd());
|
|
11613
11664
|
await manager.getDocConfig();
|
|
@@ -11617,14 +11668,28 @@ var AtscriptRepo = class {
|
|
|
11617
11668
|
};
|
|
11618
11669
|
}
|
|
11619
11670
|
}
|
|
11671
|
+
/** Loads `configFile` and runs its plugins' `config` hooks (see `configFiles`). */ async loadConfigFile(configFile) {
|
|
11672
|
+
const rawConfig = await loadConfig(configFile, this.configFormat);
|
|
11673
|
+
if (!rawConfig.rootDir) rawConfig.rootDir = path$1.dirname(configFile);
|
|
11674
|
+
const manager = new PluginManager(rawConfig, path$1.resolve(path$1.dirname(configFile), rawConfig.rootDir));
|
|
11675
|
+
await manager.getDocConfig();
|
|
11676
|
+
return {
|
|
11677
|
+
file: configFile,
|
|
11678
|
+
manager,
|
|
11679
|
+
dependants: /* @__PURE__ */ new Set()
|
|
11680
|
+
};
|
|
11681
|
+
}
|
|
11620
11682
|
async resolveConfig(id) {
|
|
11621
11683
|
let config = this.configs.get(id);
|
|
11622
11684
|
if (!config) {
|
|
11623
|
-
|
|
11624
|
-
config
|
|
11685
|
+
const loading = this.loadPluginManagerFor(id);
|
|
11686
|
+
config = loading;
|
|
11687
|
+
loading.then((c) => {
|
|
11625
11688
|
c.dependants.add(id);
|
|
11689
|
+
}, () => {
|
|
11690
|
+
if (this.configs.get(id) === loading) this.configs.delete(id);
|
|
11626
11691
|
});
|
|
11627
|
-
this.configs.set(id,
|
|
11692
|
+
this.configs.set(id, loading);
|
|
11628
11693
|
}
|
|
11629
11694
|
return config;
|
|
11630
11695
|
}
|
|
@@ -11749,11 +11814,13 @@ var AtscriptRepo = class {
|
|
|
11749
11814
|
* .as Documents cache
|
|
11750
11815
|
*/ _define_property$1(this, "atscripts", void 0);
|
|
11751
11816
|
_define_property$1(this, "sharedPluginManager", void 0);
|
|
11817
|
+
/** The shared plugin manager once its plugins' `config` hooks ran (see `loadPluginManagerFor`). */ _define_property$1(this, "sharedPluginManagerReady", void 0);
|
|
11752
11818
|
/**
|
|
11753
11819
|
* Maps resolved file:// URIs back to bare specifiers (for LSP auto-import).
|
|
11754
11820
|
*/ _define_property$1(this, "resolvedToBare", void 0);
|
|
11755
11821
|
/**
|
|
11756
|
-
*
|
|
11822
|
+
* The plugin manager of each config file (by path), shared by every document under it: its
|
|
11823
|
+
* plugins' `config` hooks run once per load of the file. Dropping an entry loads the file again.
|
|
11757
11824
|
*/ _define_property$1(this, "configFiles", void 0);
|
|
11758
11825
|
this.root = root;
|
|
11759
11826
|
this.sharedConfig = sharedConfig;
|
|
@@ -11800,9 +11867,11 @@ function _define_property(obj, key, value) {
|
|
|
11800
11867
|
}
|
|
11801
11868
|
/** Upper bound on concurrent file operations — macOS defaults `ulimit -n` to 256. */ const WRITE_CONCURRENCY = 32;
|
|
11802
11869
|
async function build(config) {
|
|
11803
|
-
const rootDir = config.rootDir ? config.rootDir
|
|
11870
|
+
const rootDir = config.rootDir ? path.isAbsolute(config.rootDir) ? config.rootDir : path.join(process.cwd(), config.rootDir) : process.cwd();
|
|
11804
11871
|
config.rootDir = rootDir;
|
|
11805
11872
|
const repo = new AtscriptRepo(rootDir, config);
|
|
11873
|
+
const { manager } = await repo.loadPluginManagerFor(`file://${rootDir}`);
|
|
11874
|
+
await manager.buildStart();
|
|
11806
11875
|
const entries = [];
|
|
11807
11876
|
if (config.entries && config.entries.length > 0) for (const entry of config.entries) entries.push(path.join(rootDir, entry));
|
|
11808
11877
|
else {
|
|
@@ -11892,7 +11961,7 @@ let tmpSeq = 0;
|
|
|
11892
11961
|
await rename(tmp, target);
|
|
11893
11962
|
} catch (error) {
|
|
11894
11963
|
try {
|
|
11895
|
-
await rm(tmp, { force: true });
|
|
11964
|
+
await rm$1(tmp, { force: true });
|
|
11896
11965
|
} catch {}
|
|
11897
11966
|
throw error;
|
|
11898
11967
|
}
|