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