@jesscss/plugin-node-modules 2.0.0-alpha.7 → 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 +33 -72
- package/lib/index.cjs +43 -6
- package/lib/index.d.ts +18 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +43 -6
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,74 +1,35 @@
|
|
|
1
1
|
# @jesscss/plugin-node-modules
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
##
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
### `loadPackage(packageName: string): Promise<Record<string, any> | null>`
|
|
38
|
-
|
|
39
|
-
Load an npm package module.
|
|
40
|
-
|
|
41
|
-
```typescript
|
|
42
|
-
const plugin = new NodeModulesPlugin();
|
|
43
|
-
const module = await plugin.loadPackage('less-plugin-clean-css');
|
|
44
|
-
// Returns: the module exports, or null if not found
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
### `tryResolvePackages(packageNames: string[]): Promise<{ name: string; module: Record<string, any> } | null>`
|
|
48
|
-
|
|
49
|
-
Try to resolve a package name with multiple possible names. Returns the first successfully resolved package.
|
|
50
|
-
|
|
51
|
-
```typescript
|
|
52
|
-
const plugin = new NodeModulesPlugin();
|
|
53
|
-
const result = await plugin.tryResolvePackages([
|
|
54
|
-
'clean-css',
|
|
55
|
-
'less-plugin-clean-css'
|
|
56
|
-
]);
|
|
57
|
-
// Returns: { name: 'less-plugin-clean-css', module: {...} } or null
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
## Integration with Other Plugins
|
|
61
|
-
|
|
62
|
-
Other plugins (like `@jesscss/plugin-less-compat`) can use this plugin to resolve npm packages:
|
|
63
|
-
|
|
64
|
-
```typescript
|
|
65
|
-
// In jess-plugin-less-compat
|
|
66
|
-
const nodeModulesPlugin = plugins.find(p => p.name === 'node-modules');
|
|
67
|
-
if (nodeModulesPlugin instanceof NodeModulesPlugin) {
|
|
68
|
-
const module = await nodeModulesPlugin.loadPackage('less-plugin-clean-css');
|
|
69
|
-
}
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
## Options
|
|
73
|
-
|
|
74
|
-
- `enabled` (boolean, default: `true`): Whether to enable auto-resolution of npm packages.
|
|
3
|
+
**Import resolver that loads npm packages from `node_modules` — a seed of the
|
|
4
|
+
JavaScript-execution / CSS-in-JS story.**
|
|
5
|
+
|
|
6
|
+
This plugin gives Jess's language plugins a way to resolve and load npm packages
|
|
7
|
+
by name, using Node's module resolution (`require.resolve` /
|
|
8
|
+
`createRequire`). Other plugins use it to pull in packages referenced from a
|
|
9
|
+
stylesheet — for example [`@jesscss/plugin-less-compat`](../jess-plugin-less-compat)
|
|
10
|
+
resolving a Less `@plugin "package-name"` off `node_modules`.
|
|
11
|
+
|
|
12
|
+
## Why it exists — the convergence angle
|
|
13
|
+
|
|
14
|
+
One of the four tools [Jess](https://github.com/jesscss/jess) aims to converge is
|
|
15
|
+
**CSS-in-JS**: running real JavaScript inside your stylesheets (`@use` /
|
|
16
|
+
`@plugin`) so styles can be dynamic without leaving CSS files. Reaching npm
|
|
17
|
+
packages is a building block of that story. Paired with
|
|
18
|
+
[`@jesscss/plugin-js`](../jess-plugin-js) (which executes the modules), it lets a
|
|
19
|
+
stylesheet pull logic and data from the JS ecosystem.
|
|
20
|
+
|
|
21
|
+
That convergence is **roadmap — being proven through the alpha, not claimed as
|
|
22
|
+
done.** What ships here today is just the resolver seam; don't read it as a
|
|
23
|
+
finished CSS-in-JS system.
|
|
24
|
+
|
|
25
|
+
## Status
|
|
26
|
+
|
|
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
|
+
[docs site](https://jesscss.github.io/) for the API once it settles.
|
|
31
|
+
|
|
32
|
+
- Project overview & positioning: <https://github.com/jesscss/jess#readme>
|
|
33
|
+
- Docs: <https://jesscss.github.io/> (currently pre-alpha content)
|
|
34
|
+
- Issues: <https://github.com/jesscss/jess/issues>
|
|
35
|
+
- License: MIT
|
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 (
|
|
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
|
-
|
|
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
|
-
|
|
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
|
*
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"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class extends AbstractPlugin {
|
|
|
23
23
|
this.opts = opts;
|
|
24
24
|
try {
|
|
25
25
|
let currentDir;
|
|
26
|
-
if (
|
|
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
|
-
|
|
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
|
-
|
|
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.
|
|
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.
|
|
19
|
+
"@jesscss/core": "2.0.0-alpha.9"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
22
|
"@types/node": "^20.0.0",
|