@jesscss/plugin-node-modules 2.0.0-alpha.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Matthew Dean
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,74 @@
1
+ # @jesscss/plugin-node-modules
2
+
3
+ Jess plugin for resolving and loading npm packages from `node_modules`.
4
+
5
+ ## Overview
6
+
7
+ This plugin provides npm/node_modules resolution and loading capabilities for all Jess language plugins. It uses Node's module resolution algorithm (`require.resolve`) to find and load npm packages.
8
+
9
+ ## Usage
10
+
11
+ ```typescript
12
+ import { Compiler } from '@jesscss/jess';
13
+ import nodeModulesPlugin from '@jesscss/plugin-node-modules';
14
+
15
+ const compiler = new Compiler({
16
+ compile: {
17
+ plugins: [
18
+ nodeModulesPlugin(),
19
+ // ... other plugins
20
+ ]
21
+ }
22
+ });
23
+ ```
24
+
25
+ ## API
26
+
27
+ ### `resolvePackage(packageName: string): string | null`
28
+
29
+ Resolve an npm package name to its absolute path.
30
+
31
+ ```typescript
32
+ const plugin = new NodeModulesPlugin();
33
+ const path = plugin.resolvePackage('less-plugin-clean-css');
34
+ // Returns: '/path/to/node_modules/less-plugin-clean-css/index.js' or null
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.
package/lib/index.d.ts ADDED
@@ -0,0 +1,62 @@
1
+ import { AbstractPlugin } from '@jesscss/core';
2
+ interface NodeModulesPluginOptions {
3
+ /**
4
+ * Whether to enable auto-resolution of npm packages.
5
+ * Default: true
6
+ */
7
+ enabled?: boolean;
8
+ }
9
+ export type { NodeModulesPluginOptions };
10
+ /**
11
+ * Plugin that provides npm/node_modules resolution and loading capabilities.
12
+ *
13
+ * This plugin implements the `import()` method to resolve and load npm packages
14
+ * using Node's module resolution algorithm (require.resolve).
15
+ *
16
+ * It can be used by other plugins (like jess-plugin-less-compat) to load
17
+ * npm packages when processing directives like `@plugin "package-name"`.
18
+ */
19
+ export declare class NodeModulesPlugin extends AbstractPlugin {
20
+ opts: NodeModulesPluginOptions;
21
+ name: string;
22
+ private _require;
23
+ constructor(opts?: NodeModulesPluginOptions);
24
+ /**
25
+ * Resolve an npm package name to its absolute path.
26
+ * Uses Node's module resolution algorithm (same as require.resolve).
27
+ *
28
+ * @param packageName - The npm package name (e.g., "less-plugin-clean-css")
29
+ * @returns The absolute path to the package, or null if not found
30
+ */
31
+ resolvePackage(packageName: string): string | null;
32
+ /**
33
+ * Load an npm package module.
34
+ *
35
+ * @param packageName - The npm package name (e.g., "less-plugin-clean-css")
36
+ * @returns The loaded module, or null if not found
37
+ */
38
+ loadPackage(packageName: string): Promise<Record<string, any> | null>;
39
+ /**
40
+ * Try to resolve a package name with multiple possible names.
41
+ * Useful for plugins that want to try different variations
42
+ * (e.g., "clean-css" and "less-plugin-clean-css").
43
+ *
44
+ * @param packageNames - Array of package names to try in order
45
+ * @returns The first successfully resolved package, or null if none found
46
+ */
47
+ tryResolvePackages(packageNames: string[]): Promise<{
48
+ name: string;
49
+ module: Record<string, any>;
50
+ } | null>;
51
+ /**
52
+ * Import method for loading JavaScript modules from npm.
53
+ * This is called by the context when importing modules.
54
+ *
55
+ * @param absoluteFilePath - The absolute path to the module
56
+ * @returns The loaded module, or throws if this plugin can't handle it
57
+ */
58
+ import(absoluteFilePath: string): Promise<Record<string, any>>;
59
+ }
60
+ declare const nodeModulesPlugin: (opts?: NodeModulesPluginOptions) => NodeModulesPlugin;
61
+ export default nodeModulesPlugin;
62
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +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;IAmCtD;;;;;;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;IAgB3E;;;;;;;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;CAmBrE;AAED,QAAA,MAAM,iBAAiB,UAAY,wBAAwB,sBAExC,CAAC;AAEpB,eAAe,iBAAiB,CAAC"}
package/lib/index.js ADDED
@@ -0,0 +1,150 @@
1
+ import { AbstractPlugin } from '@jesscss/core';
2
+ import { createRequire } from 'node:module';
3
+ import { fileURLToPath } from 'node:url';
4
+ import * as path from 'node:path';
5
+ /**
6
+ * Plugin that provides npm/node_modules resolution and loading capabilities.
7
+ *
8
+ * This plugin implements the `import()` method to resolve and load npm packages
9
+ * using Node's module resolution algorithm (require.resolve).
10
+ *
11
+ * It can be used by other plugins (like jess-plugin-less-compat) to load
12
+ * npm packages when processing directives like `@plugin "package-name"`.
13
+ */
14
+ export class NodeModulesPlugin extends AbstractPlugin {
15
+ opts;
16
+ name = 'node-modules';
17
+ _require;
18
+ constructor(opts = {}) {
19
+ super();
20
+ this.opts = opts;
21
+ // Create a require function that can resolve modules
22
+ // Use createRequire to get a require function in ES module contexts
23
+ try {
24
+ // Try to use the current file's directory as the base for resolution
25
+ let currentDir;
26
+ if (typeof __filename !== 'undefined') {
27
+ currentDir = path.dirname(__filename);
28
+ }
29
+ else {
30
+ // In ES module contexts, try to use import.meta.url
31
+ // This will only work if the module system supports it
32
+ try {
33
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
34
+ const url = globalThis.import?.meta?.url;
35
+ if (url) {
36
+ currentDir = path.dirname(fileURLToPath(url));
37
+ }
38
+ else {
39
+ throw new Error('import.meta not available');
40
+ }
41
+ }
42
+ catch {
43
+ // Fallback to process.cwd()
44
+ currentDir = process.cwd();
45
+ }
46
+ }
47
+ this._require = createRequire(currentDir + '/');
48
+ }
49
+ catch {
50
+ // Fallback to global require if available
51
+ this._require = typeof require !== 'undefined' ? require : (() => {
52
+ throw new Error('require is not available');
53
+ });
54
+ }
55
+ }
56
+ /**
57
+ * Resolve an npm package name to its absolute path.
58
+ * Uses Node's module resolution algorithm (same as require.resolve).
59
+ *
60
+ * @param packageName - The npm package name (e.g., "less-plugin-clean-css")
61
+ * @returns The absolute path to the package, or null if not found
62
+ */
63
+ resolvePackage(packageName) {
64
+ if (this.opts.enabled === false) {
65
+ return null;
66
+ }
67
+ try {
68
+ // Use require.resolve to find the package
69
+ // This will search node_modules using Node's resolution algorithm
70
+ const resolved = this._require.resolve(packageName);
71
+ return resolved;
72
+ }
73
+ catch (e) {
74
+ // MODULE_NOT_FOUND is expected when package doesn't exist
75
+ if (e.code === 'MODULE_NOT_FOUND') {
76
+ return null;
77
+ }
78
+ // Re-throw other errors
79
+ throw e;
80
+ }
81
+ }
82
+ /**
83
+ * Load an npm package module.
84
+ *
85
+ * @param packageName - The npm package name (e.g., "less-plugin-clean-css")
86
+ * @returns The loaded module, or null if not found
87
+ */
88
+ async loadPackage(packageName) {
89
+ const resolvedPath = this.resolvePackage(packageName);
90
+ if (!resolvedPath) {
91
+ return null;
92
+ }
93
+ try {
94
+ // Load the module using require
95
+ const module = this._require(resolvedPath);
96
+ return module || null;
97
+ }
98
+ catch (e) {
99
+ // If loading fails, return null
100
+ return null;
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
+ }
146
+ const nodeModulesPlugin = ((opts) => {
147
+ return new NodeModulesPlugin(opts);
148
+ });
149
+ export default nodeModulesPlugin;
150
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,cAAc,EACf,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAYlC;;;;;;;;GAQG;AACH,MAAM,OAAO,iBAAkB,SAAQ,cAAc;IAKhC;IAJnB,IAAI,GAAG,cAAc,CAAC;IAEd,QAAQ,CAAc;IAE9B,YAAmB,OAAiC,EAAE;QACpD,KAAK,EAAE,CAAC;QADS,SAAI,GAAJ,IAAI,CAA+B;QAGpD,qDAAqD;QACrD,oEAAoE;QACpE,IAAI,CAAC;YACH,qEAAqE;YACrE,IAAI,UAAkB,CAAC;YACvB,IAAI,OAAO,UAAU,KAAK,WAAW,EAAE,CAAC;gBACtC,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;YACxC,CAAC;iBAAM,CAAC;gBACN,oDAAoD;gBACpD,uDAAuD;gBACvD,IAAI,CAAC;oBACH,8DAA8D;oBAC9D,MAAM,GAAG,GAAI,UAAkB,CAAC,MAAM,EAAE,IAAI,EAAE,GAAG,CAAC;oBAClD,IAAI,GAAG,EAAE,CAAC;wBACR,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC,CAAC;oBAChD,CAAC;yBAAM,CAAC;wBACN,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;oBAC/C,CAAC;gBACH,CAAC;gBAAC,MAAM,CAAC;oBACP,4BAA4B;oBAC5B,UAAU,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC;gBAC7B,CAAC;YACH,CAAC;YACD,IAAI,CAAC,QAAQ,GAAG,aAAa,CAAC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@@
1
+ {
2
+ "name": "@jesscss/plugin-node-modules",
3
+ "version": "2.0.0-alpha.1",
4
+ "description": "Jess plugin for resolving and loading npm packages from node_modules",
5
+ "main": "lib/index.js",
6
+ "types": "lib/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./lib/index.js",
10
+ "types": "./lib/index.d.ts",
11
+ "source": "./src/index.ts"
12
+ },
13
+ "./package.json": "./package.json"
14
+ },
15
+ "files": [
16
+ "lib"
17
+ ],
18
+ "dependencies": {
19
+ "@jesscss/core": "2.0.0-alpha.1"
20
+ },
21
+ "devDependencies": {
22
+ "@types/node": "^20.0.0",
23
+ "typescript": "^5.0.0"
24
+ },
25
+ "author": "Matthew Dean <matthew-dean@users.noreply.github.com>",
26
+ "license": "MIT",
27
+ "publishConfig": {
28
+ "access": "public"
29
+ },
30
+ "scripts": {
31
+ "build": "pnpm compile",
32
+ "compile": "tsc -b tsconfig.build.json",
33
+ "dev": "tsc -b tsconfig.json -w",
34
+ "lint:fix": "eslint --fix '**/*.{js,ts}'",
35
+ "lint": "eslint '**/*.{js,ts}'"
36
+ }
37
+ }