unplugin-atscript 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/webpack.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unplugin-atscript",
3
- "version": "0.2.18",
3
+ "version": "0.2.19",
4
4
  "description": "Atscript: Configuration and build plugins.",
5
5
  "keywords": [
6
6
  "annotations",
@@ -82,8 +82,8 @@
82
82
  "vitest": "4.1.12"
83
83
  },
84
84
  "peerDependencies": {
85
- "@atscript/core": "^0.2.18",
86
- "@atscript/typescript": "^0.2.18"
85
+ "@atscript/core": "^0.2.19",
86
+ "@atscript/typescript": "^0.2.19"
87
87
  },
88
88
  "build": [
89
89
  {},