unplugin-atscript 0.1.88 → 0.1.90

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/README.md CHANGED
@@ -40,12 +40,24 @@ module.exports = { plugins: [atscript()] }
40
40
 
41
41
  Also available: `unplugin-atscript/esbuild`, `unplugin-atscript/rolldown`, `unplugin-atscript/rspack`, `unplugin-atscript/farm`.
42
42
 
43
+ ## Options
44
+
45
+ Same options on every bundler entry:
46
+
47
+ | Option | Type | Default | Effect |
48
+ | -------- | --------- | ------------------- | -------------------------------------------------------------------------------------- |
49
+ | `strict` | `boolean` | `true` | Fail the build on parse/diagnostic **errors**. `false` = log errors but keep building. |
50
+ | `root` | `string` | bundler root or cwd | Directory `atscript.config.*` is discovered from. Absolute, or relative to the cwd. |
51
+
52
+ Vite picks the root up automatically (from `configResolved`); other bundlers need `root` when the working directory is not the project root.
53
+
43
54
  ## Features
44
55
 
45
56
  - Universal bundler support via dedicated entry points
46
57
  - Automatic `atscript.config.*` loading
47
58
  - Strict validation — build fails on `.as` errors by default (`strict: false` to disable)
48
59
  - Tree-shaking aware — marks non-mutating modules as side-effect-free
60
+ - On Vite, prebundles `@atscript/typescript/utils` so the first `.as`-backed route does not trigger a dep re-optimization reload
49
61
 
50
62
  ## Documentation
51
63
 
package/dist/esbuild.cjs CHANGED
@@ -1,5 +1,4 @@
1
- "use strict";
2
- //#region rolldown:runtime
1
+ //#region \0rolldown/runtime.js
3
2
  var __create = Object.create;
4
3
  var __defProp = Object.defineProperty;
5
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -7,12 +6,16 @@ var __getOwnPropNames = Object.getOwnPropertyNames;
7
6
  var __getProtoOf = Object.getPrototypeOf;
8
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
9
8
  var __copyProps = (to, from, except, desc) => {
10
- if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
- key = keys[i];
12
- if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
13
- get: ((k) => from[k]).bind(null, key),
14
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
15
- });
9
+ if (from && typeof from === "object" || typeof from === "function") {
10
+ for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
+ key = keys[i];
12
+ if (!__hasOwnProp.call(to, key) && key !== except) {
13
+ __defProp(to, key, {
14
+ get: ((k) => from[k]).bind(null, key),
15
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
16
+ });
17
+ }
18
+ }
16
19
  }
17
20
  return to;
18
21
  };
@@ -22,25 +25,47 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
22
25
  }) : target, mod));
23
26
 
24
27
  //#endregion
25
- const unplugin = __toESM(require("unplugin"));
26
- const fs_promises = __toESM(require("fs/promises"));
27
- const path = __toESM(require("path"));
28
- const __atscript_core = __toESM(require("@atscript/core"));
29
- const __atscript_typescript = __toESM(require("@atscript/typescript"));
28
+ let unplugin = require("unplugin");
29
+ let fs_promises = require("fs/promises");
30
+ let module$1 = require("module");
31
+ let path = require("path");
32
+ path = __toESM(path);
33
+ let _atscript_core = require("@atscript/core");
34
+ let _atscript_typescript = require("@atscript/typescript");
30
35
 
31
36
  //#region packages/unplugin/src/index.ts
32
37
  const RE_DTS = /\.d\.[cm]?ts$/;
38
+ const RUNTIME_ENTRY = "@atscript/typescript/utils";
39
+ function isResolvableFrom(specifier, from) {
40
+ try {
41
+ (0, module$1.createRequire)(path.default.join(from, "package.json")).resolve(specifier);
42
+ return true;
43
+ } catch {
44
+ return false;
45
+ }
46
+ }
33
47
  const unpluginFactory = (opts) => {
34
- const root = process.cwd();
48
+ const explicitRoot = opts?.root ? path.default.resolve(opts.root) : void 0;
49
+ let detectedRoot;
50
+ const getRoot = () => explicitRoot ?? detectedRoot ?? process.cwd();
35
51
  const strict = opts?.strict ?? true;
36
52
  let repo;
37
53
  let configPromise;
38
54
  const getConfig = () => configPromise ?? (configPromise = (async () => {
39
- const p = await (0, __atscript_core.resolveConfigFile)(root);
40
- return p ? (0, __atscript_core.loadConfig)(p) : {};
55
+ const p = await (0, _atscript_core.resolveConfigFile)(getRoot());
56
+ return p ? (0, _atscript_core.loadConfig)(p) : {};
41
57
  })());
42
58
  return {
43
59
  name: "unplugin-atscript",
60
+ vite: {
61
+ config(userConfig) {
62
+ const root = path.default.resolve(explicitRoot ?? userConfig.root ?? process.cwd());
63
+ return (userConfig.optimizeDeps?.include?.includes(RUNTIME_ENTRY) ?? false) || !isResolvableFrom(RUNTIME_ENTRY, root) ? void 0 : { optimizeDeps: { include: [RUNTIME_ENTRY] } };
64
+ },
65
+ configResolved(config) {
66
+ detectedRoot = config.root;
67
+ }
68
+ },
44
69
  resolveId(id, importer) {
45
70
  if (importer && id.endsWith(".as")) {
46
71
  if (!id.startsWith(".") && !id.startsWith("/")) return null;
@@ -51,12 +76,12 @@ const unpluginFactory = (opts) => {
51
76
  },
52
77
  async load(id) {
53
78
  const isDts = id.endsWith(".as.d.ts");
54
- const sourceId = isDts ? id.slice(0, -".d.ts".length) : id;
79
+ const sourceId = isDts ? id.slice(0, -5) : id;
55
80
  if (sourceId.endsWith(".as")) {
56
81
  if (!repo) {
57
82
  const config = await getConfig();
58
- if (!config.plugins) config.plugins = [(0, __atscript_typescript.tsPlugin)()];
59
- repo = new __atscript_core.AtscriptRepo(root, config);
83
+ if (!config.plugins) config.plugins = [(0, _atscript_typescript.tsPlugin)()];
84
+ repo = new _atscript_core.AtscriptRepo(getRoot(), config);
60
85
  }
61
86
  const code = (await (0, fs_promises.readFile)(sourceId, "utf8")).toString();
62
87
  const doc = await repo.openDocument(`file://${sourceId}`, code);
@@ -68,21 +93,17 @@ const unpluginFactory = (opts) => {
68
93
  if (strict && !error) error = m.message;
69
94
  } else if (m.severity === 2) console.log(doc.renderDiagMessage(m, false, true));
70
95
  if (error) throw new Error(error);
71
- if (isDts) {
72
- const out$1 = await doc.render("dts");
73
- const content = (out$1?.[0]?.content || "").replace(/^\/{3}\s*<reference\s+path=[^\n]*\n/m, "");
74
- return {
75
- code: content,
76
- map: null
77
- };
78
- }
96
+ if (isDts) return {
97
+ code: ((await doc.render("dts"))?.[0]?.content || "").replace(/^\/{3}\s*<reference\s+path=[^\n]*\n/m, ""),
98
+ map: null
99
+ };
79
100
  const out = await doc.render("js");
80
- const hasMutatingAnnotates = doc.nodes.some((n) => (0, __atscript_core.isAnnotate)(n) && n.isMutating);
101
+ const hasMutatingAnnotates = doc.nodes.some((n) => (0, _atscript_core.isAnnotate)(n) && n.isMutating);
81
102
  return {
82
103
  code: out?.[0]?.content || "",
83
104
  moduleType: "js",
84
105
  map: null,
85
- moduleSideEffects: hasMutatingAnnotates ? undefined : false
106
+ moduleSideEffects: hasMutatingAnnotates ? void 0 : false
86
107
  };
87
108
  }
88
109
  }
package/dist/esbuild.d.ts CHANGED
@@ -6,6 +6,20 @@ interface atscriptPluginOptions {
6
6
  * @default true
7
7
  */
8
8
  strict?: boolean;
9
+ /**
10
+ * The directory `atscript.config.*` is discovered from and `.as` files are
11
+ * resolved against. Absolute, or relative to `process.cwd()`.
12
+ *
13
+ * Defaults to the bundler's root (Vite: `config.root`, picked up automatically
14
+ * in `configResolved`) or `process.cwd()`.
15
+ *
16
+ * Vite picks the root up automatically; other bundlers need `root` when the
17
+ * working directory is not the project root (monorepo `pnpm -C`, editor-launched
18
+ * builds, a root that sits next to — not above — the config). Without it the
19
+ * config is discovered from the wrong directory and valid annotations from
20
+ * config-provided plugins (`@db.*`, `@ui.*`, …) are reported as unknown.
21
+ */
22
+ root?: string;
9
23
  }
10
24
 
11
25
  declare const _default: (options?: atscriptPluginOptions | undefined) => _unplugin.EsbuildPlugin;
package/dist/esbuild.mjs CHANGED
@@ -1,22 +1,43 @@
1
1
  import { createEsbuildPlugin } from "unplugin";
2
2
  import { readFile } from "fs/promises";
3
+ import { createRequire } from "module";
3
4
  import path from "path";
4
5
  import { AtscriptRepo, isAnnotate, loadConfig, resolveConfigFile } from "@atscript/core";
5
6
  import { tsPlugin } from "@atscript/typescript";
6
7
 
7
8
  //#region packages/unplugin/src/index.ts
8
9
  const RE_DTS = /\.d\.[cm]?ts$/;
10
+ const RUNTIME_ENTRY = "@atscript/typescript/utils";
11
+ function isResolvableFrom(specifier, from) {
12
+ try {
13
+ createRequire(path.join(from, "package.json")).resolve(specifier);
14
+ return true;
15
+ } catch {
16
+ return false;
17
+ }
18
+ }
9
19
  const unpluginFactory = (opts) => {
10
- const root = process.cwd();
20
+ const explicitRoot = opts?.root ? path.resolve(opts.root) : void 0;
21
+ let detectedRoot;
22
+ const getRoot = () => explicitRoot ?? detectedRoot ?? process.cwd();
11
23
  const strict = opts?.strict ?? true;
12
24
  let repo;
13
25
  let configPromise;
14
26
  const getConfig = () => configPromise ?? (configPromise = (async () => {
15
- const p = await resolveConfigFile(root);
27
+ const p = await resolveConfigFile(getRoot());
16
28
  return p ? loadConfig(p) : {};
17
29
  })());
18
30
  return {
19
31
  name: "unplugin-atscript",
32
+ vite: {
33
+ config(userConfig) {
34
+ const root = path.resolve(explicitRoot ?? userConfig.root ?? process.cwd());
35
+ return (userConfig.optimizeDeps?.include?.includes(RUNTIME_ENTRY) ?? false) || !isResolvableFrom(RUNTIME_ENTRY, root) ? void 0 : { optimizeDeps: { include: [RUNTIME_ENTRY] } };
36
+ },
37
+ configResolved(config) {
38
+ detectedRoot = config.root;
39
+ }
40
+ },
20
41
  resolveId(id, importer) {
21
42
  if (importer && id.endsWith(".as")) {
22
43
  if (!id.startsWith(".") && !id.startsWith("/")) return null;
@@ -27,12 +48,12 @@ const unpluginFactory = (opts) => {
27
48
  },
28
49
  async load(id) {
29
50
  const isDts = id.endsWith(".as.d.ts");
30
- const sourceId = isDts ? id.slice(0, -".d.ts".length) : id;
51
+ const sourceId = isDts ? id.slice(0, -5) : id;
31
52
  if (sourceId.endsWith(".as")) {
32
53
  if (!repo) {
33
54
  const config = await getConfig();
34
55
  if (!config.plugins) config.plugins = [tsPlugin()];
35
- repo = new AtscriptRepo(root, config);
56
+ repo = new AtscriptRepo(getRoot(), config);
36
57
  }
37
58
  const code = (await readFile(sourceId, "utf8")).toString();
38
59
  const doc = await repo.openDocument(`file://${sourceId}`, code);
@@ -44,21 +65,17 @@ const unpluginFactory = (opts) => {
44
65
  if (strict && !error) error = m.message;
45
66
  } else if (m.severity === 2) console.log(doc.renderDiagMessage(m, false, true));
46
67
  if (error) throw new Error(error);
47
- if (isDts) {
48
- const out$1 = await doc.render("dts");
49
- const content = (out$1?.[0]?.content || "").replace(/^\/{3}\s*<reference\s+path=[^\n]*\n/m, "");
50
- return {
51
- code: content,
52
- map: null
53
- };
54
- }
68
+ if (isDts) return {
69
+ code: ((await doc.render("dts"))?.[0]?.content || "").replace(/^\/{3}\s*<reference\s+path=[^\n]*\n/m, ""),
70
+ map: null
71
+ };
55
72
  const out = await doc.render("js");
56
73
  const hasMutatingAnnotates = doc.nodes.some((n) => isAnnotate(n) && n.isMutating);
57
74
  return {
58
75
  code: out?.[0]?.content || "",
59
76
  moduleType: "js",
60
77
  map: null,
61
- moduleSideEffects: hasMutatingAnnotates ? undefined : false
78
+ moduleSideEffects: hasMutatingAnnotates ? void 0 : false
62
79
  };
63
80
  }
64
81
  }
package/dist/farm.cjs CHANGED
@@ -1,5 +1,4 @@
1
- "use strict";
2
- //#region rolldown:runtime
1
+ //#region \0rolldown/runtime.js
3
2
  var __create = Object.create;
4
3
  var __defProp = Object.defineProperty;
5
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -7,12 +6,16 @@ var __getOwnPropNames = Object.getOwnPropertyNames;
7
6
  var __getProtoOf = Object.getPrototypeOf;
8
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
9
8
  var __copyProps = (to, from, except, desc) => {
10
- if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
- key = keys[i];
12
- if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
13
- get: ((k) => from[k]).bind(null, key),
14
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
15
- });
9
+ if (from && typeof from === "object" || typeof from === "function") {
10
+ for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
+ key = keys[i];
12
+ if (!__hasOwnProp.call(to, key) && key !== except) {
13
+ __defProp(to, key, {
14
+ get: ((k) => from[k]).bind(null, key),
15
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
16
+ });
17
+ }
18
+ }
16
19
  }
17
20
  return to;
18
21
  };
@@ -22,25 +25,47 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
22
25
  }) : target, mod));
23
26
 
24
27
  //#endregion
25
- const unplugin = __toESM(require("unplugin"));
26
- const fs_promises = __toESM(require("fs/promises"));
27
- const path = __toESM(require("path"));
28
- const __atscript_core = __toESM(require("@atscript/core"));
29
- const __atscript_typescript = __toESM(require("@atscript/typescript"));
28
+ let unplugin = require("unplugin");
29
+ let fs_promises = require("fs/promises");
30
+ let module$1 = require("module");
31
+ let path = require("path");
32
+ path = __toESM(path);
33
+ let _atscript_core = require("@atscript/core");
34
+ let _atscript_typescript = require("@atscript/typescript");
30
35
 
31
36
  //#region packages/unplugin/src/index.ts
32
37
  const RE_DTS = /\.d\.[cm]?ts$/;
38
+ const RUNTIME_ENTRY = "@atscript/typescript/utils";
39
+ function isResolvableFrom(specifier, from) {
40
+ try {
41
+ (0, module$1.createRequire)(path.default.join(from, "package.json")).resolve(specifier);
42
+ return true;
43
+ } catch {
44
+ return false;
45
+ }
46
+ }
33
47
  const unpluginFactory = (opts) => {
34
- const root = process.cwd();
48
+ const explicitRoot = opts?.root ? path.default.resolve(opts.root) : void 0;
49
+ let detectedRoot;
50
+ const getRoot = () => explicitRoot ?? detectedRoot ?? process.cwd();
35
51
  const strict = opts?.strict ?? true;
36
52
  let repo;
37
53
  let configPromise;
38
54
  const getConfig = () => configPromise ?? (configPromise = (async () => {
39
- const p = await (0, __atscript_core.resolveConfigFile)(root);
40
- return p ? (0, __atscript_core.loadConfig)(p) : {};
55
+ const p = await (0, _atscript_core.resolveConfigFile)(getRoot());
56
+ return p ? (0, _atscript_core.loadConfig)(p) : {};
41
57
  })());
42
58
  return {
43
59
  name: "unplugin-atscript",
60
+ vite: {
61
+ config(userConfig) {
62
+ const root = path.default.resolve(explicitRoot ?? userConfig.root ?? process.cwd());
63
+ return (userConfig.optimizeDeps?.include?.includes(RUNTIME_ENTRY) ?? false) || !isResolvableFrom(RUNTIME_ENTRY, root) ? void 0 : { optimizeDeps: { include: [RUNTIME_ENTRY] } };
64
+ },
65
+ configResolved(config) {
66
+ detectedRoot = config.root;
67
+ }
68
+ },
44
69
  resolveId(id, importer) {
45
70
  if (importer && id.endsWith(".as")) {
46
71
  if (!id.startsWith(".") && !id.startsWith("/")) return null;
@@ -51,12 +76,12 @@ const unpluginFactory = (opts) => {
51
76
  },
52
77
  async load(id) {
53
78
  const isDts = id.endsWith(".as.d.ts");
54
- const sourceId = isDts ? id.slice(0, -".d.ts".length) : id;
79
+ const sourceId = isDts ? id.slice(0, -5) : id;
55
80
  if (sourceId.endsWith(".as")) {
56
81
  if (!repo) {
57
82
  const config = await getConfig();
58
- if (!config.plugins) config.plugins = [(0, __atscript_typescript.tsPlugin)()];
59
- repo = new __atscript_core.AtscriptRepo(root, config);
83
+ if (!config.plugins) config.plugins = [(0, _atscript_typescript.tsPlugin)()];
84
+ repo = new _atscript_core.AtscriptRepo(getRoot(), config);
60
85
  }
61
86
  const code = (await (0, fs_promises.readFile)(sourceId, "utf8")).toString();
62
87
  const doc = await repo.openDocument(`file://${sourceId}`, code);
@@ -68,21 +93,17 @@ const unpluginFactory = (opts) => {
68
93
  if (strict && !error) error = m.message;
69
94
  } else if (m.severity === 2) console.log(doc.renderDiagMessage(m, false, true));
70
95
  if (error) throw new Error(error);
71
- if (isDts) {
72
- const out$1 = await doc.render("dts");
73
- const content = (out$1?.[0]?.content || "").replace(/^\/{3}\s*<reference\s+path=[^\n]*\n/m, "");
74
- return {
75
- code: content,
76
- map: null
77
- };
78
- }
96
+ if (isDts) return {
97
+ code: ((await doc.render("dts"))?.[0]?.content || "").replace(/^\/{3}\s*<reference\s+path=[^\n]*\n/m, ""),
98
+ map: null
99
+ };
79
100
  const out = await doc.render("js");
80
- const hasMutatingAnnotates = doc.nodes.some((n) => (0, __atscript_core.isAnnotate)(n) && n.isMutating);
101
+ const hasMutatingAnnotates = doc.nodes.some((n) => (0, _atscript_core.isAnnotate)(n) && n.isMutating);
81
102
  return {
82
103
  code: out?.[0]?.content || "",
83
104
  moduleType: "js",
84
105
  map: null,
85
- moduleSideEffects: hasMutatingAnnotates ? undefined : false
106
+ moduleSideEffects: hasMutatingAnnotates ? void 0 : false
86
107
  };
87
108
  }
88
109
  }
package/dist/farm.d.ts CHANGED
@@ -4,6 +4,20 @@ interface atscriptPluginOptions {
4
4
  * @default true
5
5
  */
6
6
  strict?: boolean;
7
+ /**
8
+ * The directory `atscript.config.*` is discovered from and `.as` files are
9
+ * resolved against. Absolute, or relative to `process.cwd()`.
10
+ *
11
+ * Defaults to the bundler's root (Vite: `config.root`, picked up automatically
12
+ * in `configResolved`) or `process.cwd()`.
13
+ *
14
+ * Vite picks the root up automatically; other bundlers need `root` when the
15
+ * working directory is not the project root (monorepo `pnpm -C`, editor-launched
16
+ * builds, a root that sits next to — not above — the config). Without it the
17
+ * config is discovered from the wrong directory and valid annotations from
18
+ * config-provided plugins (`@db.*`, `@ui.*`, …) are reported as unknown.
19
+ */
20
+ root?: string;
7
21
  }
8
22
 
9
23
  declare const _default: (options?: atscriptPluginOptions | undefined) => JsPlugin;
package/dist/farm.mjs CHANGED
@@ -1,22 +1,43 @@
1
1
  import { createFarmPlugin } from "unplugin";
2
2
  import { readFile } from "fs/promises";
3
+ import { createRequire } from "module";
3
4
  import path from "path";
4
5
  import { AtscriptRepo, isAnnotate, loadConfig, resolveConfigFile } from "@atscript/core";
5
6
  import { tsPlugin } from "@atscript/typescript";
6
7
 
7
8
  //#region packages/unplugin/src/index.ts
8
9
  const RE_DTS = /\.d\.[cm]?ts$/;
10
+ const RUNTIME_ENTRY = "@atscript/typescript/utils";
11
+ function isResolvableFrom(specifier, from) {
12
+ try {
13
+ createRequire(path.join(from, "package.json")).resolve(specifier);
14
+ return true;
15
+ } catch {
16
+ return false;
17
+ }
18
+ }
9
19
  const unpluginFactory = (opts) => {
10
- const root = process.cwd();
20
+ const explicitRoot = opts?.root ? path.resolve(opts.root) : void 0;
21
+ let detectedRoot;
22
+ const getRoot = () => explicitRoot ?? detectedRoot ?? process.cwd();
11
23
  const strict = opts?.strict ?? true;
12
24
  let repo;
13
25
  let configPromise;
14
26
  const getConfig = () => configPromise ?? (configPromise = (async () => {
15
- const p = await resolveConfigFile(root);
27
+ const p = await resolveConfigFile(getRoot());
16
28
  return p ? loadConfig(p) : {};
17
29
  })());
18
30
  return {
19
31
  name: "unplugin-atscript",
32
+ vite: {
33
+ config(userConfig) {
34
+ const root = path.resolve(explicitRoot ?? userConfig.root ?? process.cwd());
35
+ return (userConfig.optimizeDeps?.include?.includes(RUNTIME_ENTRY) ?? false) || !isResolvableFrom(RUNTIME_ENTRY, root) ? void 0 : { optimizeDeps: { include: [RUNTIME_ENTRY] } };
36
+ },
37
+ configResolved(config) {
38
+ detectedRoot = config.root;
39
+ }
40
+ },
20
41
  resolveId(id, importer) {
21
42
  if (importer && id.endsWith(".as")) {
22
43
  if (!id.startsWith(".") && !id.startsWith("/")) return null;
@@ -27,12 +48,12 @@ const unpluginFactory = (opts) => {
27
48
  },
28
49
  async load(id) {
29
50
  const isDts = id.endsWith(".as.d.ts");
30
- const sourceId = isDts ? id.slice(0, -".d.ts".length) : id;
51
+ const sourceId = isDts ? id.slice(0, -5) : id;
31
52
  if (sourceId.endsWith(".as")) {
32
53
  if (!repo) {
33
54
  const config = await getConfig();
34
55
  if (!config.plugins) config.plugins = [tsPlugin()];
35
- repo = new AtscriptRepo(root, config);
56
+ repo = new AtscriptRepo(getRoot(), config);
36
57
  }
37
58
  const code = (await readFile(sourceId, "utf8")).toString();
38
59
  const doc = await repo.openDocument(`file://${sourceId}`, code);
@@ -44,21 +65,17 @@ const unpluginFactory = (opts) => {
44
65
  if (strict && !error) error = m.message;
45
66
  } else if (m.severity === 2) console.log(doc.renderDiagMessage(m, false, true));
46
67
  if (error) throw new Error(error);
47
- if (isDts) {
48
- const out$1 = await doc.render("dts");
49
- const content = (out$1?.[0]?.content || "").replace(/^\/{3}\s*<reference\s+path=[^\n]*\n/m, "");
50
- return {
51
- code: content,
52
- map: null
53
- };
54
- }
68
+ if (isDts) return {
69
+ code: ((await doc.render("dts"))?.[0]?.content || "").replace(/^\/{3}\s*<reference\s+path=[^\n]*\n/m, ""),
70
+ map: null
71
+ };
55
72
  const out = await doc.render("js");
56
73
  const hasMutatingAnnotates = doc.nodes.some((n) => isAnnotate(n) && n.isMutating);
57
74
  return {
58
75
  code: out?.[0]?.content || "",
59
76
  moduleType: "js",
60
77
  map: null,
61
- moduleSideEffects: hasMutatingAnnotates ? undefined : false
78
+ moduleSideEffects: hasMutatingAnnotates ? void 0 : false
62
79
  };
63
80
  }
64
81
  }
package/dist/index.cjs CHANGED
@@ -1,6 +1,5 @@
1
- "use strict";
2
- Object.defineProperty(exports, '__esModule', { value: true });
3
- //#region rolldown:runtime
1
+ Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
2
+ //#region \0rolldown/runtime.js
4
3
  var __create = Object.create;
5
4
  var __defProp = Object.defineProperty;
6
5
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -8,12 +7,16 @@ var __getOwnPropNames = Object.getOwnPropertyNames;
8
7
  var __getProtoOf = Object.getPrototypeOf;
9
8
  var __hasOwnProp = Object.prototype.hasOwnProperty;
10
9
  var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
12
- key = keys[i];
13
- if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
14
- get: ((k) => from[k]).bind(null, key),
15
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
16
- });
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
12
+ key = keys[i];
13
+ if (!__hasOwnProp.call(to, key) && key !== except) {
14
+ __defProp(to, key, {
15
+ get: ((k) => from[k]).bind(null, key),
16
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
17
+ });
18
+ }
19
+ }
17
20
  }
18
21
  return to;
19
22
  };
@@ -23,25 +26,47 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
23
26
  }) : target, mod));
24
27
 
25
28
  //#endregion
26
- const fs_promises = __toESM(require("fs/promises"));
27
- const path = __toESM(require("path"));
28
- const __atscript_core = __toESM(require("@atscript/core"));
29
- const __atscript_typescript = __toESM(require("@atscript/typescript"));
30
- const unplugin = __toESM(require("unplugin"));
29
+ let fs_promises = require("fs/promises");
30
+ let module$1 = require("module");
31
+ let path = require("path");
32
+ path = __toESM(path);
33
+ let _atscript_core = require("@atscript/core");
34
+ let _atscript_typescript = require("@atscript/typescript");
35
+ let unplugin = require("unplugin");
31
36
 
32
37
  //#region packages/unplugin/src/index.ts
33
38
  const RE_DTS = /\.d\.[cm]?ts$/;
39
+ const RUNTIME_ENTRY = "@atscript/typescript/utils";
40
+ function isResolvableFrom(specifier, from) {
41
+ try {
42
+ (0, module$1.createRequire)(path.default.join(from, "package.json")).resolve(specifier);
43
+ return true;
44
+ } catch {
45
+ return false;
46
+ }
47
+ }
34
48
  const unpluginFactory = (opts) => {
35
- const root = process.cwd();
49
+ const explicitRoot = opts?.root ? path.default.resolve(opts.root) : void 0;
50
+ let detectedRoot;
51
+ const getRoot = () => explicitRoot ?? detectedRoot ?? process.cwd();
36
52
  const strict = opts?.strict ?? true;
37
53
  let repo;
38
54
  let configPromise;
39
55
  const getConfig = () => configPromise ?? (configPromise = (async () => {
40
- const p = await (0, __atscript_core.resolveConfigFile)(root);
41
- return p ? (0, __atscript_core.loadConfig)(p) : {};
56
+ const p = await (0, _atscript_core.resolveConfigFile)(getRoot());
57
+ return p ? (0, _atscript_core.loadConfig)(p) : {};
42
58
  })());
43
59
  return {
44
60
  name: "unplugin-atscript",
61
+ vite: {
62
+ config(userConfig) {
63
+ const root = path.default.resolve(explicitRoot ?? userConfig.root ?? process.cwd());
64
+ return (userConfig.optimizeDeps?.include?.includes(RUNTIME_ENTRY) ?? false) || !isResolvableFrom(RUNTIME_ENTRY, root) ? void 0 : { optimizeDeps: { include: [RUNTIME_ENTRY] } };
65
+ },
66
+ configResolved(config) {
67
+ detectedRoot = config.root;
68
+ }
69
+ },
45
70
  resolveId(id, importer) {
46
71
  if (importer && id.endsWith(".as")) {
47
72
  if (!id.startsWith(".") && !id.startsWith("/")) return null;
@@ -52,12 +77,12 @@ const unpluginFactory = (opts) => {
52
77
  },
53
78
  async load(id) {
54
79
  const isDts = id.endsWith(".as.d.ts");
55
- const sourceId = isDts ? id.slice(0, -".d.ts".length) : id;
80
+ const sourceId = isDts ? id.slice(0, -5) : id;
56
81
  if (sourceId.endsWith(".as")) {
57
82
  if (!repo) {
58
83
  const config = await getConfig();
59
- if (!config.plugins) config.plugins = [(0, __atscript_typescript.tsPlugin)()];
60
- repo = new __atscript_core.AtscriptRepo(root, config);
84
+ if (!config.plugins) config.plugins = [(0, _atscript_typescript.tsPlugin)()];
85
+ repo = new _atscript_core.AtscriptRepo(getRoot(), config);
61
86
  }
62
87
  const code = (await (0, fs_promises.readFile)(sourceId, "utf8")).toString();
63
88
  const doc = await repo.openDocument(`file://${sourceId}`, code);
@@ -69,32 +94,27 @@ const unpluginFactory = (opts) => {
69
94
  if (strict && !error) error = m.message;
70
95
  } else if (m.severity === 2) console.log(doc.renderDiagMessage(m, false, true));
71
96
  if (error) throw new Error(error);
72
- if (isDts) {
73
- const out$1 = await doc.render("dts");
74
- const content = (out$1?.[0]?.content || "").replace(/^\/{3}\s*<reference\s+path=[^\n]*\n/m, "");
75
- return {
76
- code: content,
77
- map: null
78
- };
79
- }
97
+ if (isDts) return {
98
+ code: ((await doc.render("dts"))?.[0]?.content || "").replace(/^\/{3}\s*<reference\s+path=[^\n]*\n/m, ""),
99
+ map: null
100
+ };
80
101
  const out = await doc.render("js");
81
- const hasMutatingAnnotates = doc.nodes.some((n) => (0, __atscript_core.isAnnotate)(n) && n.isMutating);
102
+ const hasMutatingAnnotates = doc.nodes.some((n) => (0, _atscript_core.isAnnotate)(n) && n.isMutating);
82
103
  return {
83
104
  code: out?.[0]?.content || "",
84
105
  moduleType: "js",
85
106
  map: null,
86
- moduleSideEffects: hasMutatingAnnotates ? undefined : false
107
+ moduleSideEffects: hasMutatingAnnotates ? void 0 : false
87
108
  };
88
109
  }
89
110
  }
90
111
  };
91
112
  };
92
- const unplugin$1 = /* #__PURE__ */ (0, unplugin.createUnplugin)(unpluginFactory);
93
- const asPlugin = unplugin$1;
94
- var src_default = unplugin$1;
113
+ const unplugin$1 = /* @__PURE__ */ (0, unplugin.createUnplugin)(unpluginFactory);
114
+ /** @deprecated Use `import atscript from 'unplugin-atscript/vite'` (or /rollup, /webpack, etc.) instead */ const asPlugin = unplugin$1;
95
115
 
96
116
  //#endregion
97
- exports.asPlugin = asPlugin
98
- exports.default = src_default
99
- exports.unplugin = unplugin$1
100
- exports.unpluginFactory = unpluginFactory
117
+ exports.asPlugin = asPlugin;
118
+ exports.default = unplugin$1;
119
+ exports.unplugin = unplugin$1;
120
+ exports.unpluginFactory = unpluginFactory;