@jesscss/plugin-node-modules 2.0.0-alpha.5 → 2.0.0-alpha.6
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/lib/index.cjs +134 -0
- package/lib/index.d.cts +64 -0
- package/lib/index.d.cts.map +1 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +107 -146
- package/lib/index.js.map +1 -1
- package/package.json +10 -7
package/lib/index.cjs
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
Object.defineProperties(exports, {
|
|
2
|
+
__esModule: { value: true },
|
|
3
|
+
[Symbol.toStringTag]: { value: "Module" }
|
|
4
|
+
});
|
|
5
|
+
//#region \0rolldown/runtime.js
|
|
6
|
+
var __create = Object.create;
|
|
7
|
+
var __defProp = Object.defineProperty;
|
|
8
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
9
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
10
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
11
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
14
|
+
key = keys[i];
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
16
|
+
get: ((k) => from[k]).bind(null, key),
|
|
17
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
23
|
+
value: mod,
|
|
24
|
+
enumerable: true
|
|
25
|
+
}) : target, mod));
|
|
26
|
+
//#endregion
|
|
27
|
+
let _jesscss_core = require("@jesscss/core");
|
|
28
|
+
let node_module = require("node:module");
|
|
29
|
+
let node_url = require("node:url");
|
|
30
|
+
let node_path = require("node:path");
|
|
31
|
+
node_path = __toESM(node_path);
|
|
32
|
+
//#region src/index.ts
|
|
33
|
+
/**
|
|
34
|
+
* Plugin that provides npm/node_modules resolution and loading capabilities.
|
|
35
|
+
*
|
|
36
|
+
* This plugin implements the `import()` method to resolve and load npm packages
|
|
37
|
+
* using Node's module resolution algorithm (require.resolve).
|
|
38
|
+
*
|
|
39
|
+
* It can be used by other plugins (like jess-plugin-less-compat) to load
|
|
40
|
+
* npm packages when processing directives like `@plugin "package-name"`.
|
|
41
|
+
*/
|
|
42
|
+
var NodeModulesPlugin = class extends _jesscss_core.AbstractPlugin {
|
|
43
|
+
name = "node-modules";
|
|
44
|
+
_require;
|
|
45
|
+
constructor(opts = {}) {
|
|
46
|
+
super();
|
|
47
|
+
this.opts = opts;
|
|
48
|
+
try {
|
|
49
|
+
let currentDir;
|
|
50
|
+
if (typeof __filename !== "undefined") currentDir = node_path.dirname(__filename);
|
|
51
|
+
else try {
|
|
52
|
+
const url = require("url").pathToFileURL(__filename).href;
|
|
53
|
+
currentDir = node_path.dirname((0, node_url.fileURLToPath)(url));
|
|
54
|
+
} catch {
|
|
55
|
+
currentDir = process.cwd();
|
|
56
|
+
}
|
|
57
|
+
this._require = (0, node_module.createRequire)(currentDir + "/");
|
|
58
|
+
} catch {
|
|
59
|
+
this._require = typeof require !== "undefined" ? require : (0, node_module.createRequire)(process.cwd() + "/");
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Resolve an npm package name to its absolute path.
|
|
64
|
+
* Uses Node's module resolution algorithm (same as require.resolve).
|
|
65
|
+
*
|
|
66
|
+
* @param packageName - The npm package name (e.g., "less-plugin-clean-css")
|
|
67
|
+
* @returns The absolute path to the package, or null if not found
|
|
68
|
+
*/
|
|
69
|
+
resolvePackage(packageName) {
|
|
70
|
+
if (this.opts.enabled === false) return null;
|
|
71
|
+
try {
|
|
72
|
+
return this._require.resolve(packageName);
|
|
73
|
+
} catch (e) {
|
|
74
|
+
if (e.code === "MODULE_NOT_FOUND") return null;
|
|
75
|
+
throw e;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Load an npm package module.
|
|
80
|
+
*
|
|
81
|
+
* @param packageName - The npm package name (e.g., "less-plugin-clean-css")
|
|
82
|
+
* @returns The loaded module, or null if not found
|
|
83
|
+
*/
|
|
84
|
+
async loadPackage(packageName) {
|
|
85
|
+
const resolvedPath = this.resolvePackage(packageName);
|
|
86
|
+
if (!resolvedPath) return null;
|
|
87
|
+
try {
|
|
88
|
+
return this._require(resolvedPath) || null;
|
|
89
|
+
} catch {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Try to resolve a package name with multiple possible names.
|
|
95
|
+
* Useful for plugins that want to try different variations
|
|
96
|
+
* (e.g., "clean-css" and "less-plugin-clean-css").
|
|
97
|
+
*
|
|
98
|
+
* @param packageNames - Array of package names to try in order
|
|
99
|
+
* @returns The first successfully resolved package, or null if none found
|
|
100
|
+
*/
|
|
101
|
+
async tryResolvePackages(packageNames) {
|
|
102
|
+
for (const name of packageNames) {
|
|
103
|
+
const module = await this.loadPackage(name);
|
|
104
|
+
if (module !== null) return {
|
|
105
|
+
name,
|
|
106
|
+
module
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Import method for loading JavaScript modules from npm.
|
|
113
|
+
* This is called by the context when importing modules.
|
|
114
|
+
*
|
|
115
|
+
* @param absoluteFilePath - The absolute path to the module
|
|
116
|
+
* @returns The loaded module, or throws if this plugin can't handle it
|
|
117
|
+
*/
|
|
118
|
+
async import(absoluteFilePath) {
|
|
119
|
+
if (this.opts.enabled === false) throw new Error(`Plugin "${this.name}" cannot import "${absoluteFilePath}" (disabled)`);
|
|
120
|
+
if (absoluteFilePath.includes("node_modules")) try {
|
|
121
|
+
return this._require(absoluteFilePath);
|
|
122
|
+
} catch (e) {
|
|
123
|
+
const message = e instanceof Error ? e.message : String(e);
|
|
124
|
+
throw new Error(`Failed to import "${absoluteFilePath}": ${message}`);
|
|
125
|
+
}
|
|
126
|
+
throw new Error(`Plugin "${this.name}" cannot import "${absoluteFilePath}" (not a node_modules path)`);
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
const nodeModulesPlugin = ((opts) => {
|
|
130
|
+
return new NodeModulesPlugin(opts);
|
|
131
|
+
});
|
|
132
|
+
//#endregion
|
|
133
|
+
exports.NodeModulesPlugin = NodeModulesPlugin;
|
|
134
|
+
exports.default = nodeModulesPlugin;
|
package/lib/index.d.cts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { AbstractPlugin } from "@jesscss/core";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
interface NodeModulesPluginOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Whether to enable auto-resolution of npm packages.
|
|
7
|
+
* Default: true
|
|
8
|
+
*/
|
|
9
|
+
enabled?: boolean;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Plugin that provides npm/node_modules resolution and loading capabilities.
|
|
13
|
+
*
|
|
14
|
+
* This plugin implements the `import()` method to resolve and load npm packages
|
|
15
|
+
* using Node's module resolution algorithm (require.resolve).
|
|
16
|
+
*
|
|
17
|
+
* It can be used by other plugins (like jess-plugin-less-compat) to load
|
|
18
|
+
* npm packages when processing directives like `@plugin "package-name"`.
|
|
19
|
+
*/
|
|
20
|
+
declare class NodeModulesPlugin extends AbstractPlugin {
|
|
21
|
+
opts: NodeModulesPluginOptions;
|
|
22
|
+
name: string;
|
|
23
|
+
private _require;
|
|
24
|
+
constructor(opts?: NodeModulesPluginOptions);
|
|
25
|
+
/**
|
|
26
|
+
* Resolve an npm package name to its absolute path.
|
|
27
|
+
* Uses Node's module resolution algorithm (same as require.resolve).
|
|
28
|
+
*
|
|
29
|
+
* @param packageName - The npm package name (e.g., "less-plugin-clean-css")
|
|
30
|
+
* @returns The absolute path to the package, or null if not found
|
|
31
|
+
*/
|
|
32
|
+
resolvePackage(packageName: string): string | null;
|
|
33
|
+
/**
|
|
34
|
+
* Load an npm package module.
|
|
35
|
+
*
|
|
36
|
+
* @param packageName - The npm package name (e.g., "less-plugin-clean-css")
|
|
37
|
+
* @returns The loaded module, or null if not found
|
|
38
|
+
*/
|
|
39
|
+
loadPackage(packageName: string): Promise<Record<string, any> | null>;
|
|
40
|
+
/**
|
|
41
|
+
* Try to resolve a package name with multiple possible names.
|
|
42
|
+
* Useful for plugins that want to try different variations
|
|
43
|
+
* (e.g., "clean-css" and "less-plugin-clean-css").
|
|
44
|
+
*
|
|
45
|
+
* @param packageNames - Array of package names to try in order
|
|
46
|
+
* @returns The first successfully resolved package, or null if none found
|
|
47
|
+
*/
|
|
48
|
+
tryResolvePackages(packageNames: string[]): Promise<{
|
|
49
|
+
name: string;
|
|
50
|
+
module: Record<string, any>;
|
|
51
|
+
} | null>;
|
|
52
|
+
/**
|
|
53
|
+
* Import method for loading JavaScript modules from npm.
|
|
54
|
+
* This is called by the context when importing modules.
|
|
55
|
+
*
|
|
56
|
+
* @param absoluteFilePath - The absolute path to the module
|
|
57
|
+
* @returns The loaded module, or throws if this plugin can't handle it
|
|
58
|
+
*/
|
|
59
|
+
import(absoluteFilePath: string): Promise<Record<string, any>>;
|
|
60
|
+
}
|
|
61
|
+
declare const nodeModulesPlugin: (opts?: NodeModulesPluginOptions) => NodeModulesPlugin;
|
|
62
|
+
//#endregion
|
|
63
|
+
export { NodeModulesPlugin, type NodeModulesPluginOptions, nodeModulesPlugin as default };
|
|
64
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/index.ts"],"mappings":";;;UASU,wBAAA;;AALa;;;EAUrB,OAAA;AAAA;;;;;;;;;;cAcW,iBAAA,SAA0B,cAAA;EAKlB,IAAA,EAAM,wBAAA;EAJzB,IAAA;EAAA,QAEQ,QAAA;cAEW,IAAA,GAAM,wBAAA;EAAN;;;;;;;EAqCnB,cAAA,CAAe,WAAA;EAAA;;;;;;EA0BT,WAAA,CAAY,WAAA,WAAsB,OAAA,CAAQ,MAAA;EAyBE;;;;;;;;EAA5C,kBAAA,CAAmB,YAAA,aAAyB,OAAA;IAAU,IAAA;IAAc,MAAA,EAAQ,MAAA;EAAA;;;;;;;;EAiB5E,MAAA,CAAO,gBAAA,WAA2B,OAAA,CAAQ,MAAA;AAAA;AAAA,cAsB5C,iBAAA,GAAiB,IAAA,GAAY,wBAAA,KAAwB,iBAAA"}
|
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,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;
|
|
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,iBAAiB,UAAY,wBAAwB,sBAExC,CAAC;AAEpB,eAAe,iBAAiB,CAAC"}
|
package/lib/index.js
CHANGED
|
@@ -1,150 +1,111 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { fileURLToPath } from
|
|
4
|
-
import * as path from
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { AbstractPlugin } from "@jesscss/core";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import * as path from "node:path";
|
|
5
|
+
//#region \0rolldown/runtime.js
|
|
6
|
+
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
7
|
+
//#endregion
|
|
8
|
+
//#region src/index.ts
|
|
5
9
|
/**
|
|
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
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Try to resolve a package name with multiple possible names.
|
|
105
|
-
* Useful for plugins that want to try different variations
|
|
106
|
-
* (e.g., "clean-css" and "less-plugin-clean-css").
|
|
107
|
-
*
|
|
108
|
-
* @param packageNames - Array of package names to try in order
|
|
109
|
-
* @returns The first successfully resolved package, or null if none found
|
|
110
|
-
*/
|
|
111
|
-
async tryResolvePackages(packageNames) {
|
|
112
|
-
for (const name of packageNames) {
|
|
113
|
-
const module = await this.loadPackage(name);
|
|
114
|
-
if (module !== null) {
|
|
115
|
-
return { name, module: module };
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
return null;
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* Import method for loading JavaScript modules from npm.
|
|
122
|
-
* This is called by the context when importing modules.
|
|
123
|
-
*
|
|
124
|
-
* @param absoluteFilePath - The absolute path to the module
|
|
125
|
-
* @returns The loaded module, or throws if this plugin can't handle it
|
|
126
|
-
*/
|
|
127
|
-
async import(absoluteFilePath) {
|
|
128
|
-
if (this.opts.enabled === false) {
|
|
129
|
-
throw new Error(`Plugin "${this.name}" cannot import "${absoluteFilePath}" (disabled)`);
|
|
130
|
-
}
|
|
131
|
-
// Check if this looks like a node_modules path
|
|
132
|
-
// We can't directly resolve from absolute paths, but we can try to require it
|
|
133
|
-
if (absoluteFilePath.includes('node_modules')) {
|
|
134
|
-
try {
|
|
135
|
-
const module = this._require(absoluteFilePath);
|
|
136
|
-
return module;
|
|
137
|
-
}
|
|
138
|
-
catch (e) {
|
|
139
|
-
throw new Error(`Failed to import "${absoluteFilePath}": ${e.message}`);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
// For non-node_modules paths, throw to let other plugins handle it
|
|
143
|
-
throw new Error(`Plugin "${this.name}" cannot import "${absoluteFilePath}" (not a node_modules path)`);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
10
|
+
* Plugin that provides npm/node_modules resolution and loading capabilities.
|
|
11
|
+
*
|
|
12
|
+
* This plugin implements the `import()` method to resolve and load npm packages
|
|
13
|
+
* using Node's module resolution algorithm (require.resolve).
|
|
14
|
+
*
|
|
15
|
+
* It can be used by other plugins (like jess-plugin-less-compat) to load
|
|
16
|
+
* npm packages when processing directives like `@plugin "package-name"`.
|
|
17
|
+
*/
|
|
18
|
+
var NodeModulesPlugin = class extends AbstractPlugin {
|
|
19
|
+
name = "node-modules";
|
|
20
|
+
_require;
|
|
21
|
+
constructor(opts = {}) {
|
|
22
|
+
super();
|
|
23
|
+
this.opts = opts;
|
|
24
|
+
try {
|
|
25
|
+
let currentDir;
|
|
26
|
+
if (typeof __filename !== "undefined") currentDir = path.dirname(__filename);
|
|
27
|
+
else try {
|
|
28
|
+
const url = import.meta.url;
|
|
29
|
+
currentDir = path.dirname(fileURLToPath(url));
|
|
30
|
+
} catch {
|
|
31
|
+
currentDir = process.cwd();
|
|
32
|
+
}
|
|
33
|
+
this._require = createRequire(currentDir + "/");
|
|
34
|
+
} catch {
|
|
35
|
+
this._require = typeof __require !== "undefined" ? __require : createRequire(process.cwd() + "/");
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Resolve an npm package name to its absolute path.
|
|
40
|
+
* Uses Node's module resolution algorithm (same as require.resolve).
|
|
41
|
+
*
|
|
42
|
+
* @param packageName - The npm package name (e.g., "less-plugin-clean-css")
|
|
43
|
+
* @returns The absolute path to the package, or null if not found
|
|
44
|
+
*/
|
|
45
|
+
resolvePackage(packageName) {
|
|
46
|
+
if (this.opts.enabled === false) return null;
|
|
47
|
+
try {
|
|
48
|
+
return this._require.resolve(packageName);
|
|
49
|
+
} catch (e) {
|
|
50
|
+
if (e.code === "MODULE_NOT_FOUND") return null;
|
|
51
|
+
throw e;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Load an npm package module.
|
|
56
|
+
*
|
|
57
|
+
* @param packageName - The npm package name (e.g., "less-plugin-clean-css")
|
|
58
|
+
* @returns The loaded module, or null if not found
|
|
59
|
+
*/
|
|
60
|
+
async loadPackage(packageName) {
|
|
61
|
+
const resolvedPath = this.resolvePackage(packageName);
|
|
62
|
+
if (!resolvedPath) return null;
|
|
63
|
+
try {
|
|
64
|
+
return this._require(resolvedPath) || null;
|
|
65
|
+
} catch {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Try to resolve a package name with multiple possible names.
|
|
71
|
+
* Useful for plugins that want to try different variations
|
|
72
|
+
* (e.g., "clean-css" and "less-plugin-clean-css").
|
|
73
|
+
*
|
|
74
|
+
* @param packageNames - Array of package names to try in order
|
|
75
|
+
* @returns The first successfully resolved package, or null if none found
|
|
76
|
+
*/
|
|
77
|
+
async tryResolvePackages(packageNames) {
|
|
78
|
+
for (const name of packageNames) {
|
|
79
|
+
const module = await this.loadPackage(name);
|
|
80
|
+
if (module !== null) return {
|
|
81
|
+
name,
|
|
82
|
+
module
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Import method for loading JavaScript modules from npm.
|
|
89
|
+
* This is called by the context when importing modules.
|
|
90
|
+
*
|
|
91
|
+
* @param absoluteFilePath - The absolute path to the module
|
|
92
|
+
* @returns The loaded module, or throws if this plugin can't handle it
|
|
93
|
+
*/
|
|
94
|
+
async import(absoluteFilePath) {
|
|
95
|
+
if (this.opts.enabled === false) throw new Error(`Plugin "${this.name}" cannot import "${absoluteFilePath}" (disabled)`);
|
|
96
|
+
if (absoluteFilePath.includes("node_modules")) try {
|
|
97
|
+
return this._require(absoluteFilePath);
|
|
98
|
+
} catch (e) {
|
|
99
|
+
const message = e instanceof Error ? e.message : String(e);
|
|
100
|
+
throw new Error(`Failed to import "${absoluteFilePath}": ${message}`);
|
|
101
|
+
}
|
|
102
|
+
throw new Error(`Plugin "${this.name}" cannot import "${absoluteFilePath}" (not a node_modules path)`);
|
|
103
|
+
}
|
|
104
|
+
};
|
|
146
105
|
const nodeModulesPlugin = ((opts) => {
|
|
147
|
-
|
|
106
|
+
return new NodeModulesPlugin(opts);
|
|
148
107
|
});
|
|
149
|
-
|
|
108
|
+
//#endregion
|
|
109
|
+
export { NodeModulesPlugin, nodeModulesPlugin as default };
|
|
110
|
+
|
|
150
111
|
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import {\n type Plugin,\n type PluginInterface,\n AbstractPlugin\n} from '@jesscss/core';\nimport { createRequire } from 'node:module';\nimport { fileURLToPath } from 'node:url';\nimport * as path from 'node:path';\n\ninterface NodeModulesPluginOptions {\n /**\n * Whether to enable auto-resolution of npm packages.\n * Default: true\n */\n enabled?: boolean;\n}\n\nexport type { NodeModulesPluginOptions };\n\n/**\n * Plugin that provides npm/node_modules resolution and loading capabilities.\n *\n * This plugin implements the `import()` method to resolve and load npm packages\n * using Node's module resolution algorithm (require.resolve).\n *\n * It can be used by other plugins (like jess-plugin-less-compat) to load\n * npm packages when processing directives like `@plugin \"package-name\"`.\n */\nexport class NodeModulesPlugin extends AbstractPlugin {\n name = 'node-modules';\n\n private _require: NodeRequire;\n\n constructor(public opts: NodeModulesPluginOptions = {}) {\n super();\n\n // Create a require function that can resolve modules\n // Use createRequire to get a require function in ES module contexts\n try {\n // Try to use the current file's directory as the base for resolution\n let currentDir: string;\n if (typeof __filename !== 'undefined') {\n currentDir = path.dirname(__filename);\n } else {\n // In ES module contexts, try to use import.meta.url\n // This will only work if the module system supports it\n try {\n const url = import.meta.url;\n currentDir = path.dirname(fileURLToPath(url));\n } catch {\n // Fallback to process.cwd()\n currentDir = process.cwd();\n }\n }\n this._require = createRequire(currentDir + '/');\n } catch {\n // Fallback to global require if available\n this._require = typeof require !== 'undefined'\n ? require\n : createRequire(process.cwd() + '/');\n }\n }\n\n /**\n * Resolve an npm package name to its absolute path.\n * Uses Node's module resolution algorithm (same as require.resolve).\n *\n * @param packageName - The npm package name (e.g., \"less-plugin-clean-css\")\n * @returns The absolute path to the package, or null if not found\n */\n resolvePackage(packageName: string): string | null {\n if (this.opts.enabled === false) {\n return null;\n }\n\n try {\n // Use require.resolve to find the package\n // This will search node_modules using Node's resolution algorithm\n const resolved = this._require.resolve(packageName);\n return resolved;\n } catch (e: any) {\n // MODULE_NOT_FOUND is expected when package doesn't exist\n if (e.code === 'MODULE_NOT_FOUND') {\n return null;\n }\n // Re-throw other errors\n throw e;\n }\n }\n\n /**\n * Load an npm package module.\n *\n * @param packageName - The npm package name (e.g., \"less-plugin-clean-css\")\n * @returns The loaded module, or null if not found\n */\n async loadPackage(packageName: string): Promise<Record<string, any> | null> {\n const resolvedPath = this.resolvePackage(packageName);\n if (!resolvedPath) {\n return null;\n }\n\n try {\n // Load the module using require\n\n const module = this._require(resolvedPath) as Record<string, unknown>;\n return module || null;\n } catch {\n // If loading fails, return null\n return null;\n }\n }\n\n /**\n * Try to resolve a package name with multiple possible names.\n * Useful for plugins that want to try different variations\n * (e.g., \"clean-css\" and \"less-plugin-clean-css\").\n *\n * @param packageNames - Array of package names to try in order\n * @returns The first successfully resolved package, or null if none found\n */\n async tryResolvePackages(packageNames: string[]): Promise<{ name: string; module: Record<string, any> } | null> {\n for (const name of packageNames) {\n const module = await this.loadPackage(name);\n if (module !== null) {\n return { name, module };\n }\n }\n return null;\n }\n\n /**\n * Import method for loading JavaScript modules from npm.\n * This is called by the context when importing modules.\n *\n * @param absoluteFilePath - The absolute path to the module\n * @returns The loaded module, or throws if this plugin can't handle it\n */\n async import(absoluteFilePath: string): Promise<Record<string, any>> {\n if (this.opts.enabled === false) {\n throw new Error(`Plugin \"${this.name}\" cannot import \"${absoluteFilePath}\" (disabled)`);\n }\n\n // Check if this looks like a node_modules path\n // We can't directly resolve from absolute paths, but we can try to require it\n if (absoluteFilePath.includes('node_modules')) {\n try {\n const module = this._require(absoluteFilePath) as Record<string, unknown>;\n return module;\n } catch (e: unknown) {\n const message = e instanceof Error ? e.message : String(e);\n throw new Error(`Failed to import \"${absoluteFilePath}\": ${message}`);\n }\n }\n\n // For non-node_modules paths, throw to let other plugins handle it\n throw new Error(`Plugin \"${this.name}\" cannot import \"${absoluteFilePath}\" (not a node_modules path)`);\n }\n}\n\nconst nodeModulesPlugin = ((opts?: NodeModulesPluginOptions) => {\n return new NodeModulesPlugin(opts);\n}) satisfies Plugin;\n\nexport default nodeModulesPlugin;\n"],"mappings":";;;;;;;;;;;;;;;;;AA4BA,IAAa,oBAAb,cAAuC,eAAe;CACpD,OAAO;CAEP;CAEA,YAAY,OAAwC,EAAE,EAAE;AACtD,SAAO;AADU,OAAA,OAAA;AAKjB,MAAI;GAEF,IAAI;AACJ,OAAI,OAAO,eAAe,YACxB,cAAa,KAAK,QAAQ,WAAW;OAIrC,KAAI;IACF,MAAM,MAAM,OAAO,KAAK;AACxB,iBAAa,KAAK,QAAQ,cAAc,IAAI,CAAC;WACvC;AAEN,iBAAa,QAAQ,KAAK;;AAG9B,QAAK,WAAW,cAAc,aAAa,IAAI;UACzC;AAEN,QAAK,WAAW,OAAA,cAAmB,cAAA,YAE/B,cAAc,QAAQ,KAAK,GAAG,IAAI;;;;;;;;;;CAW1C,eAAe,aAAoC;AACjD,MAAI,KAAK,KAAK,YAAY,MACxB,QAAO;AAGT,MAAI;AAIF,UADiB,KAAK,SAAS,QAAQ,YAAY;WAE5C,GAAQ;AAEf,OAAI,EAAE,SAAS,mBACb,QAAO;AAGT,SAAM;;;;;;;;;CAUV,MAAM,YAAY,aAA0D;EAC1E,MAAM,eAAe,KAAK,eAAe,YAAY;AACrD,MAAI,CAAC,aACH,QAAO;AAGT,MAAI;AAIF,UADe,KAAK,SAAS,aAAa,IACzB;UACX;AAEN,UAAO;;;;;;;;;;;CAYX,MAAM,mBAAmB,cAAuF;AAC9G,OAAK,MAAM,QAAQ,cAAc;GAC/B,MAAM,SAAS,MAAM,KAAK,YAAY,KAAK;AAC3C,OAAI,WAAW,KACb,QAAO;IAAE;IAAM;IAAQ;;AAG3B,SAAO;;;;;;;;;CAUT,MAAM,OAAO,kBAAwD;AACnE,MAAI,KAAK,KAAK,YAAY,MACxB,OAAM,IAAI,MAAM,WAAW,KAAK,KAAK,mBAAmB,iBAAiB,cAAc;AAKzF,MAAI,iBAAiB,SAAS,eAAe,CAC3C,KAAI;AAEF,UADe,KAAK,SAAS,iBAAiB;WAEvC,GAAY;GACnB,MAAM,UAAU,aAAa,QAAQ,EAAE,UAAU,OAAO,EAAE;AAC1D,SAAM,IAAI,MAAM,qBAAqB,iBAAiB,KAAK,UAAU;;AAKzE,QAAM,IAAI,MAAM,WAAW,KAAK,KAAK,mBAAmB,iBAAiB,6BAA6B;;;AAI1G,MAAM,sBAAsB,SAAoC;AAC9D,QAAO,IAAI,kBAAkB,KAAK"}
|
package/package.json
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jesscss/plugin-node-modules",
|
|
3
|
-
"version": "2.0.0-alpha.
|
|
3
|
+
"version": "2.0.0-alpha.6",
|
|
4
4
|
"description": "Jess plugin for resolving and loading npm packages from node_modules",
|
|
5
|
-
"main": "lib/index.
|
|
5
|
+
"main": "lib/index.cjs",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
7
7
|
"exports": {
|
|
8
8
|
".": {
|
|
9
|
-
"import": "./lib/index.js",
|
|
10
9
|
"types": "./lib/index.d.ts",
|
|
11
|
-
"source": "./src/index.ts"
|
|
10
|
+
"source": "./src/index.ts",
|
|
11
|
+
"import": "./lib/index.js",
|
|
12
|
+
"require": "./lib/index.cjs"
|
|
12
13
|
},
|
|
13
14
|
"./package.json": "./package.json"
|
|
14
15
|
},
|
|
@@ -16,7 +17,7 @@
|
|
|
16
17
|
"lib"
|
|
17
18
|
],
|
|
18
19
|
"dependencies": {
|
|
19
|
-
"@jesscss/core": "2.0.0-alpha.
|
|
20
|
+
"@jesscss/core": "2.0.0-alpha.6"
|
|
20
21
|
},
|
|
21
22
|
"devDependencies": {
|
|
22
23
|
"@types/node": "^20.0.0",
|
|
@@ -27,10 +28,12 @@
|
|
|
27
28
|
"publishConfig": {
|
|
28
29
|
"access": "public"
|
|
29
30
|
},
|
|
31
|
+
"type": "module",
|
|
32
|
+
"module": "lib/index.js",
|
|
30
33
|
"scripts": {
|
|
31
34
|
"build": "pnpm compile",
|
|
32
|
-
"compile": "tsc -
|
|
33
|
-
"dev": "
|
|
35
|
+
"compile": "tsdown --tsconfig tsconfig.build.json && tsc -p tsconfig.build.json --emitDeclarationOnly",
|
|
36
|
+
"dev": "tsdown --tsconfig tsconfig.build.json --watch",
|
|
34
37
|
"lint:fix": "eslint --fix '**/*.{js,ts}'",
|
|
35
38
|
"lint": "eslint '**/*.{js,ts}'"
|
|
36
39
|
}
|