unplugin-atscript 0.2.17 → 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/esbuild.cjs CHANGED
@@ -115,7 +115,6 @@ const unpluginFactory = (opts, meta) => {
115
115
  const getRoot = () => explicitRoot ?? detectedRoot ?? process.cwd();
116
116
  const strict = opts?.strict ?? true;
117
117
  let repo;
118
- let configPromise;
119
118
  /** The documents of the `.as` modules loaded, by file. */ const loaded = /* @__PURE__ */ new Map();
120
119
  /**
121
120
  * What `load` returned for each `.as` module (by file), by render format: its generated code,
@@ -137,10 +136,71 @@ const unpluginFactory = (opts, meta) => {
137
136
  */ const affected = /* @__PURE__ */ new Map();
138
137
  /** The changed `.as` files whose hot update is a full page reload. */ const reload = /* @__PURE__ */ new Set();
139
138
  /** The Vite dev server (Vite only). */ let viteServer;
140
- const getConfig = () => configPromise ?? (configPromise = (async () => {
141
- const p = await (0, _atscript_core.resolveConfigFile)(getRoot());
142
- return p ? (0, _atscript_core.loadConfig)(p) : {};
143
- })());
139
+ let repoPromise;
140
+ /**
141
+ * The repo of the atscript config, loaded once: when the first build starts (see `start`). A
142
+ * missing config is an empty one (with the default `ts()` plugin). A config that fails to load
143
+ * rejects the build's start, where the bundler reports it, and is loaded again next time.
144
+ */ const ensureRepo = () => {
145
+ if (!repoPromise) {
146
+ const loading = (async () => {
147
+ const p = await (0, _atscript_core.resolveConfigFile)(getRoot());
148
+ const config = p ? await (0, _atscript_core.loadConfig)(p) : {};
149
+ if (!config.plugins) config.plugins = [(0, _atscript_typescript.tsPlugin)()];
150
+ repo = new _atscript_core.AtscriptRepo(getRoot(), config);
151
+ return repo;
152
+ })();
153
+ repoPromise = loading;
154
+ loading.catch(() => {
155
+ if (repoPromise === loading) repoPromise = void 0;
156
+ });
157
+ }
158
+ return repoPromise;
159
+ };
160
+ /** Runs the config plugins' `config` hooks (once), then their `buildStart` hooks. */ const start = async () => {
161
+ const { manager } = await (await ensureRepo()).loadPluginManagerFor(`file://${getRoot()}`);
162
+ await manager.buildStart();
163
+ };
164
+ /**
165
+ * The start of the current build (see `start`): `resolveId` and `load` wait for it, so no `.as`
166
+ * module is resolved or read before the config plugins wrote theirs — also in a bundler that
167
+ * resolves modules while its start hook runs (webpack, Rspack: `make`). Each bundler
168
+ * `buildStart` replaces it (a rejected one fails that build only).
169
+ *
170
+ * (webpack and Rspack call `buildStart` of a watch rebuild only after its `watchChange` calls:
171
+ * meanwhile the rebuild may resolve and read a generated file against the previous start, before
172
+ * this start rewrites it — the write then triggers one more rebuild)
173
+ */ let started;
174
+ /** {@link started} while it runs (see `resolveId`). */ let running;
175
+ /** Makes `run` (a `start()`) the current build's start (see `started`). */ const begin = (run) => {
176
+ started = run;
177
+ running = run;
178
+ const settled = () => {
179
+ if (running === run) running = void 0;
180
+ };
181
+ run.then(settled, settled);
182
+ return run;
183
+ };
184
+ /**
185
+ * {@link started}; a host that loads modules without starting a build (a Vite server that
186
+ * neither listens nor runs in middleware mode) starts it at its first `.as` module — again at
187
+ * the next one after a failure.
188
+ */ const whenStarted = () => {
189
+ if (!started) {
190
+ const own = begin(start());
191
+ own.catch(() => {
192
+ if (started === own) started = void 0;
193
+ });
194
+ }
195
+ return started;
196
+ };
197
+ /** A failed start is reported by `buildStart` and `load`, not by `resolveId`. */ const ignore = () => void 0;
198
+ /** The id of `.as` import `id` of `importer` (see `resolveId`). */ const resolveAs = (id, importer) => {
199
+ if (!id.startsWith(".") && !id.startsWith("/")) return null;
200
+ const resolved = path.default.join(path.default.dirname(importer), id);
201
+ if (RE_DTS.test(importer)) return `${resolved}.d.ts`;
202
+ return resolved;
203
+ };
144
204
  /** Records the `.as` files `doc` (loaded from `file`) imports (see `importers`). */ const recordImports = (file, doc) => {
145
205
  for (const imported of importsOf.get(file) ?? []) importers.get(imported)?.delete(file);
146
206
  const files = /* @__PURE__ */ new Set();
@@ -323,25 +383,18 @@ const unpluginFactory = (opts, meta) => {
323
383
  }
324
384
  },
325
385
  resolveId(id, importer) {
326
- if (importer && id.endsWith(".as")) {
327
- if (!id.startsWith(".") && !id.startsWith("/")) return null;
328
- const resolved = path.default.join(path.default.dirname(importer), id);
329
- if (RE_DTS.test(importer)) return `${resolved}.d.ts`;
330
- return resolved;
331
- }
386
+ if (importer && id.endsWith(".as")) return whenStarted().then(() => resolveAs(id, importer), () => resolveAs(id, importer));
387
+ return running?.then(ignore, ignore);
332
388
  },
333
- buildStart() {
389
+ async buildStart() {
334
390
  if (detects) building ?? (building = this.meta?.watchMode === false);
391
+ await begin(start());
335
392
  },
336
393
  async load(id, options) {
337
394
  const isDts = id.endsWith(".as.d.ts");
338
395
  const sourceId = isDts ? id.slice(0, -5) : id;
339
396
  if (sourceId.endsWith(".as")) {
340
- if (!repo) {
341
- const config = await getConfig();
342
- if (!config.plugins) config.plugins = [(0, _atscript_typescript.tsPlugin)()];
343
- repo = new _atscript_core.AtscriptRepo(getRoot(), config);
344
- }
397
+ await whenStarted();
345
398
  const code = (await (0, fs_promises.readFile)(sourceId, "utf8")).toString();
346
399
  const recording = isDts ? void 0 : (0, _atscript_core.recordDocumentReads)();
347
400
  let doc;
package/dist/esbuild.mjs CHANGED
@@ -87,7 +87,6 @@ const unpluginFactory = (opts, meta) => {
87
87
  const getRoot = () => explicitRoot ?? detectedRoot ?? process.cwd();
88
88
  const strict = opts?.strict ?? true;
89
89
  let repo;
90
- let configPromise;
91
90
  /** The documents of the `.as` modules loaded, by file. */ const loaded = /* @__PURE__ */ new Map();
92
91
  /**
93
92
  * What `load` returned for each `.as` module (by file), by render format: its generated code,
@@ -109,10 +108,71 @@ const unpluginFactory = (opts, meta) => {
109
108
  */ const affected = /* @__PURE__ */ new Map();
110
109
  /** The changed `.as` files whose hot update is a full page reload. */ const reload = /* @__PURE__ */ new Set();
111
110
  /** The Vite dev server (Vite only). */ let viteServer;
112
- const getConfig = () => configPromise ?? (configPromise = (async () => {
113
- const p = await resolveConfigFile(getRoot());
114
- return p ? loadConfig(p) : {};
115
- })());
111
+ let repoPromise;
112
+ /**
113
+ * The repo of the atscript config, loaded once: when the first build starts (see `start`). A
114
+ * missing config is an empty one (with the default `ts()` plugin). A config that fails to load
115
+ * rejects the build's start, where the bundler reports it, and is loaded again next time.
116
+ */ const ensureRepo = () => {
117
+ if (!repoPromise) {
118
+ const loading = (async () => {
119
+ const p = await resolveConfigFile(getRoot());
120
+ const config = p ? await loadConfig(p) : {};
121
+ if (!config.plugins) config.plugins = [tsPlugin()];
122
+ repo = new AtscriptRepo(getRoot(), config);
123
+ return repo;
124
+ })();
125
+ repoPromise = loading;
126
+ loading.catch(() => {
127
+ if (repoPromise === loading) repoPromise = void 0;
128
+ });
129
+ }
130
+ return repoPromise;
131
+ };
132
+ /** Runs the config plugins' `config` hooks (once), then their `buildStart` hooks. */ const start = async () => {
133
+ const { manager } = await (await ensureRepo()).loadPluginManagerFor(`file://${getRoot()}`);
134
+ await manager.buildStart();
135
+ };
136
+ /**
137
+ * The start of the current build (see `start`): `resolveId` and `load` wait for it, so no `.as`
138
+ * module is resolved or read before the config plugins wrote theirs — also in a bundler that
139
+ * resolves modules while its start hook runs (webpack, Rspack: `make`). Each bundler
140
+ * `buildStart` replaces it (a rejected one fails that build only).
141
+ *
142
+ * (webpack and Rspack call `buildStart` of a watch rebuild only after its `watchChange` calls:
143
+ * meanwhile the rebuild may resolve and read a generated file against the previous start, before
144
+ * this start rewrites it — the write then triggers one more rebuild)
145
+ */ let started;
146
+ /** {@link started} while it runs (see `resolveId`). */ let running;
147
+ /** Makes `run` (a `start()`) the current build's start (see `started`). */ const begin = (run) => {
148
+ started = run;
149
+ running = run;
150
+ const settled = () => {
151
+ if (running === run) running = void 0;
152
+ };
153
+ run.then(settled, settled);
154
+ return run;
155
+ };
156
+ /**
157
+ * {@link started}; a host that loads modules without starting a build (a Vite server that
158
+ * neither listens nor runs in middleware mode) starts it at its first `.as` module — again at
159
+ * the next one after a failure.
160
+ */ const whenStarted = () => {
161
+ if (!started) {
162
+ const own = begin(start());
163
+ own.catch(() => {
164
+ if (started === own) started = void 0;
165
+ });
166
+ }
167
+ return started;
168
+ };
169
+ /** A failed start is reported by `buildStart` and `load`, not by `resolveId`. */ const ignore = () => void 0;
170
+ /** The id of `.as` import `id` of `importer` (see `resolveId`). */ const resolveAs = (id, importer) => {
171
+ if (!id.startsWith(".") && !id.startsWith("/")) return null;
172
+ const resolved = path.join(path.dirname(importer), id);
173
+ if (RE_DTS.test(importer)) return `${resolved}.d.ts`;
174
+ return resolved;
175
+ };
116
176
  /** Records the `.as` files `doc` (loaded from `file`) imports (see `importers`). */ const recordImports = (file, doc) => {
117
177
  for (const imported of importsOf.get(file) ?? []) importers.get(imported)?.delete(file);
118
178
  const files = /* @__PURE__ */ new Set();
@@ -295,25 +355,18 @@ const unpluginFactory = (opts, meta) => {
295
355
  }
296
356
  },
297
357
  resolveId(id, importer) {
298
- if (importer && id.endsWith(".as")) {
299
- if (!id.startsWith(".") && !id.startsWith("/")) return null;
300
- const resolved = path.join(path.dirname(importer), id);
301
- if (RE_DTS.test(importer)) return `${resolved}.d.ts`;
302
- return resolved;
303
- }
358
+ if (importer && id.endsWith(".as")) return whenStarted().then(() => resolveAs(id, importer), () => resolveAs(id, importer));
359
+ return running?.then(ignore, ignore);
304
360
  },
305
- buildStart() {
361
+ async buildStart() {
306
362
  if (detects) building ?? (building = this.meta?.watchMode === false);
363
+ await begin(start());
307
364
  },
308
365
  async load(id, options) {
309
366
  const isDts = id.endsWith(".as.d.ts");
310
367
  const sourceId = isDts ? id.slice(0, -5) : id;
311
368
  if (sourceId.endsWith(".as")) {
312
- if (!repo) {
313
- const config = await getConfig();
314
- if (!config.plugins) config.plugins = [tsPlugin()];
315
- repo = new AtscriptRepo(getRoot(), config);
316
- }
369
+ await whenStarted();
317
370
  const code = (await readFile(sourceId, "utf8")).toString();
318
371
  const recording = isDts ? void 0 : recordDocumentReads();
319
372
  let doc;
package/dist/farm.cjs CHANGED
@@ -115,7 +115,6 @@ const unpluginFactory = (opts, meta) => {
115
115
  const getRoot = () => explicitRoot ?? detectedRoot ?? process.cwd();
116
116
  const strict = opts?.strict ?? true;
117
117
  let repo;
118
- let configPromise;
119
118
  /** The documents of the `.as` modules loaded, by file. */ const loaded = /* @__PURE__ */ new Map();
120
119
  /**
121
120
  * What `load` returned for each `.as` module (by file), by render format: its generated code,
@@ -137,10 +136,71 @@ const unpluginFactory = (opts, meta) => {
137
136
  */ const affected = /* @__PURE__ */ new Map();
138
137
  /** The changed `.as` files whose hot update is a full page reload. */ const reload = /* @__PURE__ */ new Set();
139
138
  /** The Vite dev server (Vite only). */ let viteServer;
140
- const getConfig = () => configPromise ?? (configPromise = (async () => {
141
- const p = await (0, _atscript_core.resolveConfigFile)(getRoot());
142
- return p ? (0, _atscript_core.loadConfig)(p) : {};
143
- })());
139
+ let repoPromise;
140
+ /**
141
+ * The repo of the atscript config, loaded once: when the first build starts (see `start`). A
142
+ * missing config is an empty one (with the default `ts()` plugin). A config that fails to load
143
+ * rejects the build's start, where the bundler reports it, and is loaded again next time.
144
+ */ const ensureRepo = () => {
145
+ if (!repoPromise) {
146
+ const loading = (async () => {
147
+ const p = await (0, _atscript_core.resolveConfigFile)(getRoot());
148
+ const config = p ? await (0, _atscript_core.loadConfig)(p) : {};
149
+ if (!config.plugins) config.plugins = [(0, _atscript_typescript.tsPlugin)()];
150
+ repo = new _atscript_core.AtscriptRepo(getRoot(), config);
151
+ return repo;
152
+ })();
153
+ repoPromise = loading;
154
+ loading.catch(() => {
155
+ if (repoPromise === loading) repoPromise = void 0;
156
+ });
157
+ }
158
+ return repoPromise;
159
+ };
160
+ /** Runs the config plugins' `config` hooks (once), then their `buildStart` hooks. */ const start = async () => {
161
+ const { manager } = await (await ensureRepo()).loadPluginManagerFor(`file://${getRoot()}`);
162
+ await manager.buildStart();
163
+ };
164
+ /**
165
+ * The start of the current build (see `start`): `resolveId` and `load` wait for it, so no `.as`
166
+ * module is resolved or read before the config plugins wrote theirs — also in a bundler that
167
+ * resolves modules while its start hook runs (webpack, Rspack: `make`). Each bundler
168
+ * `buildStart` replaces it (a rejected one fails that build only).
169
+ *
170
+ * (webpack and Rspack call `buildStart` of a watch rebuild only after its `watchChange` calls:
171
+ * meanwhile the rebuild may resolve and read a generated file against the previous start, before
172
+ * this start rewrites it — the write then triggers one more rebuild)
173
+ */ let started;
174
+ /** {@link started} while it runs (see `resolveId`). */ let running;
175
+ /** Makes `run` (a `start()`) the current build's start (see `started`). */ const begin = (run) => {
176
+ started = run;
177
+ running = run;
178
+ const settled = () => {
179
+ if (running === run) running = void 0;
180
+ };
181
+ run.then(settled, settled);
182
+ return run;
183
+ };
184
+ /**
185
+ * {@link started}; a host that loads modules without starting a build (a Vite server that
186
+ * neither listens nor runs in middleware mode) starts it at its first `.as` module — again at
187
+ * the next one after a failure.
188
+ */ const whenStarted = () => {
189
+ if (!started) {
190
+ const own = begin(start());
191
+ own.catch(() => {
192
+ if (started === own) started = void 0;
193
+ });
194
+ }
195
+ return started;
196
+ };
197
+ /** A failed start is reported by `buildStart` and `load`, not by `resolveId`. */ const ignore = () => void 0;
198
+ /** The id of `.as` import `id` of `importer` (see `resolveId`). */ const resolveAs = (id, importer) => {
199
+ if (!id.startsWith(".") && !id.startsWith("/")) return null;
200
+ const resolved = path.default.join(path.default.dirname(importer), id);
201
+ if (RE_DTS.test(importer)) return `${resolved}.d.ts`;
202
+ return resolved;
203
+ };
144
204
  /** Records the `.as` files `doc` (loaded from `file`) imports (see `importers`). */ const recordImports = (file, doc) => {
145
205
  for (const imported of importsOf.get(file) ?? []) importers.get(imported)?.delete(file);
146
206
  const files = /* @__PURE__ */ new Set();
@@ -323,25 +383,18 @@ const unpluginFactory = (opts, meta) => {
323
383
  }
324
384
  },
325
385
  resolveId(id, importer) {
326
- if (importer && id.endsWith(".as")) {
327
- if (!id.startsWith(".") && !id.startsWith("/")) return null;
328
- const resolved = path.default.join(path.default.dirname(importer), id);
329
- if (RE_DTS.test(importer)) return `${resolved}.d.ts`;
330
- return resolved;
331
- }
386
+ if (importer && id.endsWith(".as")) return whenStarted().then(() => resolveAs(id, importer), () => resolveAs(id, importer));
387
+ return running?.then(ignore, ignore);
332
388
  },
333
- buildStart() {
389
+ async buildStart() {
334
390
  if (detects) building ?? (building = this.meta?.watchMode === false);
391
+ await begin(start());
335
392
  },
336
393
  async load(id, options) {
337
394
  const isDts = id.endsWith(".as.d.ts");
338
395
  const sourceId = isDts ? id.slice(0, -5) : id;
339
396
  if (sourceId.endsWith(".as")) {
340
- if (!repo) {
341
- const config = await getConfig();
342
- if (!config.plugins) config.plugins = [(0, _atscript_typescript.tsPlugin)()];
343
- repo = new _atscript_core.AtscriptRepo(getRoot(), config);
344
- }
397
+ await whenStarted();
345
398
  const code = (await (0, fs_promises.readFile)(sourceId, "utf8")).toString();
346
399
  const recording = isDts ? void 0 : (0, _atscript_core.recordDocumentReads)();
347
400
  let doc;
package/dist/farm.mjs CHANGED
@@ -87,7 +87,6 @@ const unpluginFactory = (opts, meta) => {
87
87
  const getRoot = () => explicitRoot ?? detectedRoot ?? process.cwd();
88
88
  const strict = opts?.strict ?? true;
89
89
  let repo;
90
- let configPromise;
91
90
  /** The documents of the `.as` modules loaded, by file. */ const loaded = /* @__PURE__ */ new Map();
92
91
  /**
93
92
  * What `load` returned for each `.as` module (by file), by render format: its generated code,
@@ -109,10 +108,71 @@ const unpluginFactory = (opts, meta) => {
109
108
  */ const affected = /* @__PURE__ */ new Map();
110
109
  /** The changed `.as` files whose hot update is a full page reload. */ const reload = /* @__PURE__ */ new Set();
111
110
  /** The Vite dev server (Vite only). */ let viteServer;
112
- const getConfig = () => configPromise ?? (configPromise = (async () => {
113
- const p = await resolveConfigFile(getRoot());
114
- return p ? loadConfig(p) : {};
115
- })());
111
+ let repoPromise;
112
+ /**
113
+ * The repo of the atscript config, loaded once: when the first build starts (see `start`). A
114
+ * missing config is an empty one (with the default `ts()` plugin). A config that fails to load
115
+ * rejects the build's start, where the bundler reports it, and is loaded again next time.
116
+ */ const ensureRepo = () => {
117
+ if (!repoPromise) {
118
+ const loading = (async () => {
119
+ const p = await resolveConfigFile(getRoot());
120
+ const config = p ? await loadConfig(p) : {};
121
+ if (!config.plugins) config.plugins = [tsPlugin()];
122
+ repo = new AtscriptRepo(getRoot(), config);
123
+ return repo;
124
+ })();
125
+ repoPromise = loading;
126
+ loading.catch(() => {
127
+ if (repoPromise === loading) repoPromise = void 0;
128
+ });
129
+ }
130
+ return repoPromise;
131
+ };
132
+ /** Runs the config plugins' `config` hooks (once), then their `buildStart` hooks. */ const start = async () => {
133
+ const { manager } = await (await ensureRepo()).loadPluginManagerFor(`file://${getRoot()}`);
134
+ await manager.buildStart();
135
+ };
136
+ /**
137
+ * The start of the current build (see `start`): `resolveId` and `load` wait for it, so no `.as`
138
+ * module is resolved or read before the config plugins wrote theirs — also in a bundler that
139
+ * resolves modules while its start hook runs (webpack, Rspack: `make`). Each bundler
140
+ * `buildStart` replaces it (a rejected one fails that build only).
141
+ *
142
+ * (webpack and Rspack call `buildStart` of a watch rebuild only after its `watchChange` calls:
143
+ * meanwhile the rebuild may resolve and read a generated file against the previous start, before
144
+ * this start rewrites it — the write then triggers one more rebuild)
145
+ */ let started;
146
+ /** {@link started} while it runs (see `resolveId`). */ let running;
147
+ /** Makes `run` (a `start()`) the current build's start (see `started`). */ const begin = (run) => {
148
+ started = run;
149
+ running = run;
150
+ const settled = () => {
151
+ if (running === run) running = void 0;
152
+ };
153
+ run.then(settled, settled);
154
+ return run;
155
+ };
156
+ /**
157
+ * {@link started}; a host that loads modules without starting a build (a Vite server that
158
+ * neither listens nor runs in middleware mode) starts it at its first `.as` module — again at
159
+ * the next one after a failure.
160
+ */ const whenStarted = () => {
161
+ if (!started) {
162
+ const own = begin(start());
163
+ own.catch(() => {
164
+ if (started === own) started = void 0;
165
+ });
166
+ }
167
+ return started;
168
+ };
169
+ /** A failed start is reported by `buildStart` and `load`, not by `resolveId`. */ const ignore = () => void 0;
170
+ /** The id of `.as` import `id` of `importer` (see `resolveId`). */ const resolveAs = (id, importer) => {
171
+ if (!id.startsWith(".") && !id.startsWith("/")) return null;
172
+ const resolved = path.join(path.dirname(importer), id);
173
+ if (RE_DTS.test(importer)) return `${resolved}.d.ts`;
174
+ return resolved;
175
+ };
116
176
  /** Records the `.as` files `doc` (loaded from `file`) imports (see `importers`). */ const recordImports = (file, doc) => {
117
177
  for (const imported of importsOf.get(file) ?? []) importers.get(imported)?.delete(file);
118
178
  const files = /* @__PURE__ */ new Set();
@@ -295,25 +355,18 @@ const unpluginFactory = (opts, meta) => {
295
355
  }
296
356
  },
297
357
  resolveId(id, importer) {
298
- if (importer && id.endsWith(".as")) {
299
- if (!id.startsWith(".") && !id.startsWith("/")) return null;
300
- const resolved = path.join(path.dirname(importer), id);
301
- if (RE_DTS.test(importer)) return `${resolved}.d.ts`;
302
- return resolved;
303
- }
358
+ if (importer && id.endsWith(".as")) return whenStarted().then(() => resolveAs(id, importer), () => resolveAs(id, importer));
359
+ return running?.then(ignore, ignore);
304
360
  },
305
- buildStart() {
361
+ async buildStart() {
306
362
  if (detects) building ?? (building = this.meta?.watchMode === false);
363
+ await begin(start());
307
364
  },
308
365
  async load(id, options) {
309
366
  const isDts = id.endsWith(".as.d.ts");
310
367
  const sourceId = isDts ? id.slice(0, -5) : id;
311
368
  if (sourceId.endsWith(".as")) {
312
- if (!repo) {
313
- const config = await getConfig();
314
- if (!config.plugins) config.plugins = [tsPlugin()];
315
- repo = new AtscriptRepo(getRoot(), config);
316
- }
369
+ await whenStarted();
317
370
  const code = (await readFile(sourceId, "utf8")).toString();
318
371
  const recording = isDts ? void 0 : recordDocumentReads();
319
372
  let doc;
package/dist/index.cjs CHANGED
@@ -116,7 +116,6 @@ const unpluginFactory = (opts, meta) => {
116
116
  const getRoot = () => explicitRoot ?? detectedRoot ?? process.cwd();
117
117
  const strict = opts?.strict ?? true;
118
118
  let repo;
119
- let configPromise;
120
119
  /** The documents of the `.as` modules loaded, by file. */ const loaded = /* @__PURE__ */ new Map();
121
120
  /**
122
121
  * What `load` returned for each `.as` module (by file), by render format: its generated code,
@@ -138,10 +137,71 @@ const unpluginFactory = (opts, meta) => {
138
137
  */ const affected = /* @__PURE__ */ new Map();
139
138
  /** The changed `.as` files whose hot update is a full page reload. */ const reload = /* @__PURE__ */ new Set();
140
139
  /** The Vite dev server (Vite only). */ let viteServer;
141
- const getConfig = () => configPromise ?? (configPromise = (async () => {
142
- const p = await (0, _atscript_core.resolveConfigFile)(getRoot());
143
- return p ? (0, _atscript_core.loadConfig)(p) : {};
144
- })());
140
+ let repoPromise;
141
+ /**
142
+ * The repo of the atscript config, loaded once: when the first build starts (see `start`). A
143
+ * missing config is an empty one (with the default `ts()` plugin). A config that fails to load
144
+ * rejects the build's start, where the bundler reports it, and is loaded again next time.
145
+ */ const ensureRepo = () => {
146
+ if (!repoPromise) {
147
+ const loading = (async () => {
148
+ const p = await (0, _atscript_core.resolveConfigFile)(getRoot());
149
+ const config = p ? await (0, _atscript_core.loadConfig)(p) : {};
150
+ if (!config.plugins) config.plugins = [(0, _atscript_typescript.tsPlugin)()];
151
+ repo = new _atscript_core.AtscriptRepo(getRoot(), config);
152
+ return repo;
153
+ })();
154
+ repoPromise = loading;
155
+ loading.catch(() => {
156
+ if (repoPromise === loading) repoPromise = void 0;
157
+ });
158
+ }
159
+ return repoPromise;
160
+ };
161
+ /** Runs the config plugins' `config` hooks (once), then their `buildStart` hooks. */ const start = async () => {
162
+ const { manager } = await (await ensureRepo()).loadPluginManagerFor(`file://${getRoot()}`);
163
+ await manager.buildStart();
164
+ };
165
+ /**
166
+ * The start of the current build (see `start`): `resolveId` and `load` wait for it, so no `.as`
167
+ * module is resolved or read before the config plugins wrote theirs — also in a bundler that
168
+ * resolves modules while its start hook runs (webpack, Rspack: `make`). Each bundler
169
+ * `buildStart` replaces it (a rejected one fails that build only).
170
+ *
171
+ * (webpack and Rspack call `buildStart` of a watch rebuild only after its `watchChange` calls:
172
+ * meanwhile the rebuild may resolve and read a generated file against the previous start, before
173
+ * this start rewrites it — the write then triggers one more rebuild)
174
+ */ let started;
175
+ /** {@link started} while it runs (see `resolveId`). */ let running;
176
+ /** Makes `run` (a `start()`) the current build's start (see `started`). */ const begin = (run) => {
177
+ started = run;
178
+ running = run;
179
+ const settled = () => {
180
+ if (running === run) running = void 0;
181
+ };
182
+ run.then(settled, settled);
183
+ return run;
184
+ };
185
+ /**
186
+ * {@link started}; a host that loads modules without starting a build (a Vite server that
187
+ * neither listens nor runs in middleware mode) starts it at its first `.as` module — again at
188
+ * the next one after a failure.
189
+ */ const whenStarted = () => {
190
+ if (!started) {
191
+ const own = begin(start());
192
+ own.catch(() => {
193
+ if (started === own) started = void 0;
194
+ });
195
+ }
196
+ return started;
197
+ };
198
+ /** A failed start is reported by `buildStart` and `load`, not by `resolveId`. */ const ignore = () => void 0;
199
+ /** The id of `.as` import `id` of `importer` (see `resolveId`). */ const resolveAs = (id, importer) => {
200
+ if (!id.startsWith(".") && !id.startsWith("/")) return null;
201
+ const resolved = path.default.join(path.default.dirname(importer), id);
202
+ if (RE_DTS.test(importer)) return `${resolved}.d.ts`;
203
+ return resolved;
204
+ };
145
205
  /** Records the `.as` files `doc` (loaded from `file`) imports (see `importers`). */ const recordImports = (file, doc) => {
146
206
  for (const imported of importsOf.get(file) ?? []) importers.get(imported)?.delete(file);
147
207
  const files = /* @__PURE__ */ new Set();
@@ -324,25 +384,18 @@ const unpluginFactory = (opts, meta) => {
324
384
  }
325
385
  },
326
386
  resolveId(id, importer) {
327
- if (importer && id.endsWith(".as")) {
328
- if (!id.startsWith(".") && !id.startsWith("/")) return null;
329
- const resolved = path.default.join(path.default.dirname(importer), id);
330
- if (RE_DTS.test(importer)) return `${resolved}.d.ts`;
331
- return resolved;
332
- }
387
+ if (importer && id.endsWith(".as")) return whenStarted().then(() => resolveAs(id, importer), () => resolveAs(id, importer));
388
+ return running?.then(ignore, ignore);
333
389
  },
334
- buildStart() {
390
+ async buildStart() {
335
391
  if (detects) building ?? (building = this.meta?.watchMode === false);
392
+ await begin(start());
336
393
  },
337
394
  async load(id, options) {
338
395
  const isDts = id.endsWith(".as.d.ts");
339
396
  const sourceId = isDts ? id.slice(0, -5) : id;
340
397
  if (sourceId.endsWith(".as")) {
341
- if (!repo) {
342
- const config = await getConfig();
343
- if (!config.plugins) config.plugins = [(0, _atscript_typescript.tsPlugin)()];
344
- repo = new _atscript_core.AtscriptRepo(getRoot(), config);
345
- }
398
+ await whenStarted();
346
399
  const code = (await (0, fs_promises.readFile)(sourceId, "utf8")).toString();
347
400
  const recording = isDts ? void 0 : (0, _atscript_core.recordDocumentReads)();
348
401
  let doc;