@jesscss/plugin-node-modules 2.0.0-alpha.8 → 2.0.0-alpha.9

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
@@ -24,9 +24,9 @@ finished CSS-in-JS system.
24
24
 
25
25
  ## Status
26
26
 
27
- **Alpha.** Part of Jess, the ground-up rewrite of Less.js (Jess *is* Less.js
28
- v5). The programmatic plugin/compiler API is **not yet stabilized** the `jess`
29
- / `lessc` CLIs are the public surface for the alpha. Watch the
27
+ **Alpha.** Part of Jess. The programmatic plugin/compiler API is **not yet
28
+ stabilized** the `jess` CLI is the documented public surface for the alpha.
29
+ Watch the
30
30
  [docs site](https://jesscss.github.io/) for the API once it settles.
31
31
 
32
32
  - Project overview & positioning: <https://github.com/jesscss/jess#readme>
package/lib/index.cjs CHANGED
@@ -47,7 +47,8 @@ var NodeModulesPlugin = class extends _jesscss_core.AbstractPlugin {
47
47
  this.opts = opts;
48
48
  try {
49
49
  let currentDir;
50
- if (typeof __filename !== "undefined") currentDir = node_path.dirname(__filename);
50
+ if (opts.basePath !== void 0) currentDir = opts.basePath;
51
+ else if (typeof __filename !== "undefined") currentDir = node_path.dirname(__filename);
51
52
  else try {
52
53
  const url = require("url").pathToFileURL(__filename).href;
53
54
  currentDir = node_path.dirname((0, node_url.fileURLToPath)(url));
@@ -64,18 +65,45 @@ var NodeModulesPlugin = class extends _jesscss_core.AbstractPlugin {
64
65
  * Uses Node's module resolution algorithm (same as require.resolve).
65
66
  *
66
67
  * @param packageName - The npm package name (e.g., "less-plugin-clean-css")
68
+ * @param fromDir - Optional directory to start `node_modules` resolution from
69
+ * (walked up per Node's algorithm). When given it takes precedence over the
70
+ * plugin's `basePath`; used to resolve a package relative to the importing
71
+ * file (e.g. an `@import "pkg/x"` resolving against the importer's directory).
67
72
  * @returns The absolute path to the package, or null if not found
68
73
  */
69
- resolvePackage(packageName) {
74
+ resolvePackage(packageName, fromDir) {
70
75
  if (this.opts.enabled === false) return null;
71
76
  try {
72
- return this._require.resolve(packageName);
77
+ return fromDir !== void 0 ? this._require.resolve(packageName, { paths: [fromDir] }) : this._require.resolve(packageName);
73
78
  } catch (e) {
74
- if (e.code === "MODULE_NOT_FOUND") return null;
79
+ if (e instanceof Error && e.code === "MODULE_NOT_FOUND") return null;
75
80
  throw e;
76
81
  }
77
82
  }
78
83
  /**
84
+ * Context resolution hook for bare package specifiers. Core owns only the
85
+ * resolver-plugin pipeline; this plugin owns Node's module-resolution policy.
86
+ */
87
+ resolve(filePath, currentDir, searchPaths) {
88
+ const paths = Array.isArray(filePath) ? filePath : [filePath];
89
+ const bases = [currentDir, ...searchPaths];
90
+ const resolved = [];
91
+ for (const candidate of paths) {
92
+ if (!isBareModuleSpecifier(candidate)) {
93
+ resolved.push(candidate);
94
+ continue;
95
+ }
96
+ let modulePath = null;
97
+ for (const base of bases) {
98
+ modulePath = this.resolvePackage(candidate, base) ?? this.resolvePackage(`${candidate}.less`, base);
99
+ if (modulePath) break;
100
+ }
101
+ modulePath ??= this.resolvePackage(candidate) ?? this.resolvePackage(`${candidate}.less`);
102
+ resolved.push(modulePath ?? candidate);
103
+ }
104
+ return resolved;
105
+ }
106
+ /**
79
107
  * Load an npm package module.
80
108
  *
81
109
  * @param packageName - The npm package name (e.g., "less-plugin-clean-css")
@@ -85,7 +113,8 @@ var NodeModulesPlugin = class extends _jesscss_core.AbstractPlugin {
85
113
  const resolvedPath = this.resolvePackage(packageName);
86
114
  if (!resolvedPath) return null;
87
115
  try {
88
- return this._require(resolvedPath) || null;
116
+ const module = this._require(resolvedPath);
117
+ return isRecord(module) ? module : null;
89
118
  } catch {
90
119
  return null;
91
120
  }
@@ -118,7 +147,9 @@ var NodeModulesPlugin = class extends _jesscss_core.AbstractPlugin {
118
147
  async import(absoluteFilePath) {
119
148
  if (this.opts.enabled === false) throw new Error(`Plugin "${this.name}" cannot import "${absoluteFilePath}" (disabled)`);
120
149
  if (absoluteFilePath.includes("node_modules")) try {
121
- return this._require(absoluteFilePath);
150
+ const module = this._require(absoluteFilePath);
151
+ if (!isRecord(module)) throw new TypeError(`Module "${absoluteFilePath}" did not export an object.`);
152
+ return module;
122
153
  } catch (e) {
123
154
  const message = e instanceof Error ? e.message : String(e);
124
155
  throw new Error(`Failed to import "${absoluteFilePath}": ${message}`);
@@ -126,6 +157,12 @@ var NodeModulesPlugin = class extends _jesscss_core.AbstractPlugin {
126
157
  throw new Error(`Plugin "${this.name}" cannot import "${absoluteFilePath}" (not a node_modules path)`);
127
158
  }
128
159
  };
160
+ function isBareModuleSpecifier(candidate) {
161
+ return !node_path.isAbsolute(candidate) && !candidate.startsWith("./") && !candidate.startsWith("../") && !candidate.startsWith("/") && !/^[a-zA-Z][a-zA-Z0-9+.-]*:/.test(candidate);
162
+ }
163
+ function isRecord(value) {
164
+ return value !== null && typeof value === "object";
165
+ }
129
166
  const nodeModulesPlugin = ((opts) => {
130
167
  return new NodeModulesPlugin(opts);
131
168
  });
package/lib/index.d.ts CHANGED
@@ -5,6 +5,14 @@ interface NodeModulesPluginOptions {
5
5
  * Default: true
6
6
  */
7
7
  enabled?: boolean;
8
+ /**
9
+ * Directory to anchor Node's module resolution at. When set, `require.resolve`
10
+ * walks `node_modules` starting from this directory (instead of the plugin's own
11
+ * location). Used to resolve packages relative to a consuming project or a
12
+ * fixture tree rather than where the plugin is installed.
13
+ * Default: the plugin file's directory (falling back to `process.cwd()`).
14
+ */
15
+ basePath?: string;
8
16
  }
9
17
  export type { NodeModulesPluginOptions };
10
18
  /**
@@ -26,9 +34,18 @@ export declare class NodeModulesPlugin extends AbstractPlugin {
26
34
  * Uses Node's module resolution algorithm (same as require.resolve).
27
35
  *
28
36
  * @param packageName - The npm package name (e.g., "less-plugin-clean-css")
37
+ * @param fromDir - Optional directory to start `node_modules` resolution from
38
+ * (walked up per Node's algorithm). When given it takes precedence over the
39
+ * plugin's `basePath`; used to resolve a package relative to the importing
40
+ * file (e.g. an `@import "pkg/x"` resolving against the importer's directory).
29
41
  * @returns The absolute path to the package, or null if not found
30
42
  */
31
- resolvePackage(packageName: string): string | null;
43
+ resolvePackage(packageName: string, fromDir?: string): string | null;
44
+ /**
45
+ * Context resolution hook for bare package specifiers. Core owns only the
46
+ * resolver-plugin pipeline; this plugin owns Node's module-resolution policy.
47
+ */
48
+ resolve(filePath: string | string[], currentDir: string, searchPaths: string[]): string[];
32
49
  /**
33
50
  * Load an npm package module.
34
51
  *
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,cAAc,EACf,MAAM,eAAe,CAAC;AAKvB,UAAU,wBAAwB;IAChC;;;OAGG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAED,YAAY,EAAE,wBAAwB,EAAE,CAAC;AAEzC;;;;;;;;GAQG;AACH,qBAAa,iBAAkB,SAAQ,cAAc;IAKhC,IAAI,EAAE,wBAAwB;IAJjD,IAAI,SAAkB;IAEtB,OAAO,CAAC,QAAQ,CAAc;gBAEX,IAAI,GAAE,wBAA6B;IA8BtD;;;;;;OAMG;IACH,cAAc,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IAoBlD;;;;;OAKG;IACG,WAAW,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,IAAI,CAAC;IAiB3E;;;;;;;OAOG;IACG,kBAAkB,CAAC,YAAY,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;KAAE,GAAG,IAAI,CAAC;IAU/G;;;;;;OAMG;IACG,MAAM,CAAC,gBAAgB,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAoBrE;AAED,QAAA,MAAM,iBAA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AAiB,UAAY,wBAAwB,sBAExC,CAAC;AAEpB,eAAe,iBAAiB,CAAC"}
package/lib/index.js CHANGED
@@ -23,7 +23,8 @@ var NodeModulesPlugin = class extends AbstractPlugin {
23
23
  this.opts = opts;
24
24
  try {
25
25
  let currentDir;
26
- if (typeof __filename !== "undefined") currentDir = path.dirname(__filename);
26
+ if (opts.basePath !== void 0) currentDir = opts.basePath;
27
+ else if (typeof __filename !== "undefined") currentDir = path.dirname(__filename);
27
28
  else try {
28
29
  const url = import.meta.url;
29
30
  currentDir = path.dirname(fileURLToPath(url));
@@ -40,18 +41,45 @@ var NodeModulesPlugin = class extends AbstractPlugin {
40
41
  * Uses Node's module resolution algorithm (same as require.resolve).
41
42
  *
42
43
  * @param packageName - The npm package name (e.g., "less-plugin-clean-css")
44
+ * @param fromDir - Optional directory to start `node_modules` resolution from
45
+ * (walked up per Node's algorithm). When given it takes precedence over the
46
+ * plugin's `basePath`; used to resolve a package relative to the importing
47
+ * file (e.g. an `@import "pkg/x"` resolving against the importer's directory).
43
48
  * @returns The absolute path to the package, or null if not found
44
49
  */
45
- resolvePackage(packageName) {
50
+ resolvePackage(packageName, fromDir) {
46
51
  if (this.opts.enabled === false) return null;
47
52
  try {
48
- return this._require.resolve(packageName);
53
+ return fromDir !== void 0 ? this._require.resolve(packageName, { paths: [fromDir] }) : this._require.resolve(packageName);
49
54
  } catch (e) {
50
- if (e.code === "MODULE_NOT_FOUND") return null;
55
+ if (e instanceof Error && e.code === "MODULE_NOT_FOUND") return null;
51
56
  throw e;
52
57
  }
53
58
  }
54
59
  /**
60
+ * Context resolution hook for bare package specifiers. Core owns only the
61
+ * resolver-plugin pipeline; this plugin owns Node's module-resolution policy.
62
+ */
63
+ resolve(filePath, currentDir, searchPaths) {
64
+ const paths = Array.isArray(filePath) ? filePath : [filePath];
65
+ const bases = [currentDir, ...searchPaths];
66
+ const resolved = [];
67
+ for (const candidate of paths) {
68
+ if (!isBareModuleSpecifier(candidate)) {
69
+ resolved.push(candidate);
70
+ continue;
71
+ }
72
+ let modulePath = null;
73
+ for (const base of bases) {
74
+ modulePath = this.resolvePackage(candidate, base) ?? this.resolvePackage(`${candidate}.less`, base);
75
+ if (modulePath) break;
76
+ }
77
+ modulePath ??= this.resolvePackage(candidate) ?? this.resolvePackage(`${candidate}.less`);
78
+ resolved.push(modulePath ?? candidate);
79
+ }
80
+ return resolved;
81
+ }
82
+ /**
55
83
  * Load an npm package module.
56
84
  *
57
85
  * @param packageName - The npm package name (e.g., "less-plugin-clean-css")
@@ -61,7 +89,8 @@ var NodeModulesPlugin = class extends AbstractPlugin {
61
89
  const resolvedPath = this.resolvePackage(packageName);
62
90
  if (!resolvedPath) return null;
63
91
  try {
64
- return this._require(resolvedPath) || null;
92
+ const module = this._require(resolvedPath);
93
+ return isRecord(module) ? module : null;
65
94
  } catch {
66
95
  return null;
67
96
  }
@@ -94,7 +123,9 @@ var NodeModulesPlugin = class extends AbstractPlugin {
94
123
  async import(absoluteFilePath) {
95
124
  if (this.opts.enabled === false) throw new Error(`Plugin "${this.name}" cannot import "${absoluteFilePath}" (disabled)`);
96
125
  if (absoluteFilePath.includes("node_modules")) try {
97
- return this._require(absoluteFilePath);
126
+ const module = this._require(absoluteFilePath);
127
+ if (!isRecord(module)) throw new TypeError(`Module "${absoluteFilePath}" did not export an object.`);
128
+ return module;
98
129
  } catch (e) {
99
130
  const message = e instanceof Error ? e.message : String(e);
100
131
  throw new Error(`Failed to import "${absoluteFilePath}": ${message}`);
@@ -102,6 +133,12 @@ var NodeModulesPlugin = class extends AbstractPlugin {
102
133
  throw new Error(`Plugin "${this.name}" cannot import "${absoluteFilePath}" (not a node_modules path)`);
103
134
  }
104
135
  };
136
+ function isBareModuleSpecifier(candidate) {
137
+ return !path.isAbsolute(candidate) && !candidate.startsWith("./") && !candidate.startsWith("../") && !candidate.startsWith("/") && !/^[a-zA-Z][a-zA-Z0-9+.-]*:/.test(candidate);
138
+ }
139
+ function isRecord(value) {
140
+ return value !== null && typeof value === "object";
141
+ }
105
142
  const nodeModulesPlugin = ((opts) => {
106
143
  return new NodeModulesPlugin(opts);
107
144
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jesscss/plugin-node-modules",
3
- "version": "2.0.0-alpha.8",
3
+ "version": "2.0.0-alpha.9",
4
4
  "description": "Jess plugin for resolving and loading npm packages from node_modules",
5
5
  "main": "lib/index.cjs",
6
6
  "types": "lib/index.d.ts",
@@ -16,7 +16,7 @@
16
16
  "lib"
17
17
  ],
18
18
  "dependencies": {
19
- "@jesscss/core": "2.0.0-alpha.8"
19
+ "@jesscss/core": "2.0.0-alpha.9"
20
20
  },
21
21
  "devDependencies": {
22
22
  "@types/node": "^20.0.0",