@lass-lang/plugin-utils 0.0.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.
@@ -0,0 +1,74 @@
1
+ /**
2
+ * @lass-lang/plugin-utils
3
+ *
4
+ * Shared utilities for Lass bundler plugins.
5
+ *
6
+ * These utilities handle bundler-agnostic plugin infrastructure:
7
+ * - Virtual path conventions for .lass <-> .css mapping
8
+ * - Import rewriting for isolated execution
9
+ * - Constants shared across all bundler plugins
10
+ *
11
+ * This is separate from:
12
+ * - Core's transpilation (handled by @lass-lang/core)
13
+ * - Bundler-specific injection (each plugin implements its own injectStyle)
14
+ */
15
+ /** Extension for .lass source files */
16
+ export declare const LASS_EXT = ".lass";
17
+ /** Virtual CSS extension for regular .lass files: foo.lass -> foo.lass.css */
18
+ export declare const VIRTUAL_CSS_EXT = ".lass.css";
19
+ /** Virtual CSS extension for CSS Modules: foo.module.lass -> foo.lass.module.css */
20
+ export declare const VIRTUAL_MODULE_CSS_EXT = ".lass.module.css";
21
+ /**
22
+ * Regex for matching ES import statements with relative paths.
23
+ *
24
+ * Captures:
25
+ * - Group 1: Import prefix (e.g., "import x from '")
26
+ * - Group 2: Relative path (e.g., "./file.json")
27
+ * - Group 3: Quote suffix (e.g., "'")
28
+ * - Group 4: Trailing content (e.g., " with { type: 'json' };")
29
+ */
30
+ export declare const IMPORT_STATEMENT_RE: RegExp;
31
+ /**
32
+ * Convert a .lass file path to its virtual CSS path.
33
+ *
34
+ * @example
35
+ * toVirtualCssPath('src/styles.lass') // => 'src/styles.lass.css'
36
+ * toVirtualCssPath('src/component.module.lass') // => 'src/component.lass.module.css'
37
+ */
38
+ export declare function toVirtualCssPath(lassPath: string): string;
39
+ /**
40
+ * Convert a virtual CSS path back to its .lass source path.
41
+ *
42
+ * @example
43
+ * fromVirtualCssPath('src/styles.lass.css') // => 'src/styles.lass'
44
+ * fromVirtualCssPath('src/component.lass.module.css') // => 'src/component.module.lass'
45
+ */
46
+ export declare function fromVirtualCssPath(virtualPath: string): string;
47
+ /**
48
+ * Check if a path is a virtual CSS path (regular or module).
49
+ */
50
+ export declare function isVirtualCssPath(path: string): boolean;
51
+ /**
52
+ * Check if a path is a virtual CSS Modules path.
53
+ */
54
+ export declare function isVirtualModuleCssPath(path: string): boolean;
55
+ /**
56
+ * Normalize path for cross-platform compatibility.
57
+ * Converts backslashes to forward slashes.
58
+ */
59
+ export declare function normalizePath(path: string): string;
60
+ /**
61
+ * Rewrite relative imports to absolute file:// URLs for isolated execution.
62
+ *
63
+ * When executing transpiled Lass code in isolation (data URL or temp file),
64
+ * relative imports won't resolve correctly. This function converts them to
65
+ * absolute file:// URLs that work from any execution context.
66
+ *
67
+ * Also auto-adds JSON import assertions when missing (required by runtimes).
68
+ *
69
+ * @param code - The transpiled JS code
70
+ * @param baseDir - The directory to resolve relative paths from
71
+ * @returns The code with rewritten imports
72
+ */
73
+ export declare function rewriteImportsForExecution(code: string, baseDir: string): string;
74
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AASH,uCAAuC;AACvC,eAAO,MAAM,QAAQ,UAAU,CAAC;AAEhC,8EAA8E;AAC9E,eAAO,MAAM,eAAe,cAAc,CAAC;AAE3C,oFAAoF;AACpF,eAAO,MAAM,sBAAsB,qBAAqB,CAAC;AAEzD;;;;;;;;GAQG;AACH,eAAO,MAAM,mBAAmB,QACsE,CAAC;AAMvG;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAKzD;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,WAAW,EAAE,MAAM,GAAG,MAAM,CAQ9D;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAEtD;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAE5D;AAMD;;;GAGG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAElD;AAMD;;;;;;;;;;;;GAYG;AACH,wBAAgB,0BAA0B,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,CAiBhF"}
package/dist/index.js ADDED
@@ -0,0 +1,123 @@
1
+ /**
2
+ * @lass-lang/plugin-utils
3
+ *
4
+ * Shared utilities for Lass bundler plugins.
5
+ *
6
+ * These utilities handle bundler-agnostic plugin infrastructure:
7
+ * - Virtual path conventions for .lass <-> .css mapping
8
+ * - Import rewriting for isolated execution
9
+ * - Constants shared across all bundler plugins
10
+ *
11
+ * This is separate from:
12
+ * - Core's transpilation (handled by @lass-lang/core)
13
+ * - Bundler-specific injection (each plugin implements its own injectStyle)
14
+ */
15
+ import { posix, resolve } from 'node:path';
16
+ import { pathToFileURL } from 'node:url';
17
+ // ============================================================================
18
+ // CONSTANTS
19
+ // ============================================================================
20
+ /** Extension for .lass source files */
21
+ export const LASS_EXT = '.lass';
22
+ /** Virtual CSS extension for regular .lass files: foo.lass -> foo.lass.css */
23
+ export const VIRTUAL_CSS_EXT = '.lass.css';
24
+ /** Virtual CSS extension for CSS Modules: foo.module.lass -> foo.lass.module.css */
25
+ export const VIRTUAL_MODULE_CSS_EXT = '.lass.module.css';
26
+ /**
27
+ * Regex for matching ES import statements with relative paths.
28
+ *
29
+ * Captures:
30
+ * - Group 1: Import prefix (e.g., "import x from '")
31
+ * - Group 2: Relative path (e.g., "./file.json")
32
+ * - Group 3: Quote suffix (e.g., "'")
33
+ * - Group 4: Trailing content (e.g., " with { type: 'json' };")
34
+ */
35
+ export const IMPORT_STATEMENT_RE = /^(\s*import\s+(?:[\w*{}\s,]+\s+from\s+)?['"])(\.[^'"]+)(['"])(\s*(?:with\s+\{[^}]*\})?\s*;?\s*)$/gm;
36
+ // ============================================================================
37
+ // VIRTUAL PATH UTILITIES
38
+ // ============================================================================
39
+ /**
40
+ * Convert a .lass file path to its virtual CSS path.
41
+ *
42
+ * @example
43
+ * toVirtualCssPath('src/styles.lass') // => 'src/styles.lass.css'
44
+ * toVirtualCssPath('src/component.module.lass') // => 'src/component.lass.module.css'
45
+ */
46
+ export function toVirtualCssPath(lassPath) {
47
+ if (lassPath.endsWith('.module.lass')) {
48
+ return lassPath.slice(0, -'.module.lass'.length) + VIRTUAL_MODULE_CSS_EXT;
49
+ }
50
+ return lassPath.slice(0, -LASS_EXT.length) + VIRTUAL_CSS_EXT;
51
+ }
52
+ /**
53
+ * Convert a virtual CSS path back to its .lass source path.
54
+ *
55
+ * @example
56
+ * fromVirtualCssPath('src/styles.lass.css') // => 'src/styles.lass'
57
+ * fromVirtualCssPath('src/component.lass.module.css') // => 'src/component.module.lass'
58
+ */
59
+ export function fromVirtualCssPath(virtualPath) {
60
+ if (virtualPath.endsWith(VIRTUAL_MODULE_CSS_EXT)) {
61
+ return virtualPath.slice(0, -VIRTUAL_MODULE_CSS_EXT.length) + '.module.lass';
62
+ }
63
+ if (virtualPath.endsWith(VIRTUAL_CSS_EXT)) {
64
+ return virtualPath.slice(0, -VIRTUAL_CSS_EXT.length) + LASS_EXT;
65
+ }
66
+ return virtualPath;
67
+ }
68
+ /**
69
+ * Check if a path is a virtual CSS path (regular or module).
70
+ */
71
+ export function isVirtualCssPath(path) {
72
+ return path.endsWith(VIRTUAL_CSS_EXT) || path.endsWith(VIRTUAL_MODULE_CSS_EXT);
73
+ }
74
+ /**
75
+ * Check if a path is a virtual CSS Modules path.
76
+ */
77
+ export function isVirtualModuleCssPath(path) {
78
+ return path.endsWith(VIRTUAL_MODULE_CSS_EXT);
79
+ }
80
+ // ============================================================================
81
+ // PATH UTILITIES
82
+ // ============================================================================
83
+ /**
84
+ * Normalize path for cross-platform compatibility.
85
+ * Converts backslashes to forward slashes.
86
+ */
87
+ export function normalizePath(path) {
88
+ return path.split(/[\\/]/).join(posix.sep);
89
+ }
90
+ // ============================================================================
91
+ // IMPORT REWRITING
92
+ // ============================================================================
93
+ /**
94
+ * Rewrite relative imports to absolute file:// URLs for isolated execution.
95
+ *
96
+ * When executing transpiled Lass code in isolation (data URL or temp file),
97
+ * relative imports won't resolve correctly. This function converts them to
98
+ * absolute file:// URLs that work from any execution context.
99
+ *
100
+ * Also auto-adds JSON import assertions when missing (required by runtimes).
101
+ *
102
+ * @param code - The transpiled JS code
103
+ * @param baseDir - The directory to resolve relative paths from
104
+ * @returns The code with rewritten imports
105
+ */
106
+ export function rewriteImportsForExecution(code, baseDir) {
107
+ return code.replace(IMPORT_STATEMENT_RE, (_match, prefix, relativePath, suffix, trailing) => {
108
+ const absolutePath = resolve(baseDir, relativePath);
109
+ const fileUrl = pathToFileURL(absolutePath).href;
110
+ // Handle import assertions
111
+ let assertion = '';
112
+ if (trailing.includes('with')) {
113
+ // Already has assertion, don't add another
114
+ assertion = '';
115
+ }
116
+ else if (relativePath.endsWith('.json')) {
117
+ // Auto-add JSON assertion (required by runtimes)
118
+ assertion = " with { type: 'json' }";
119
+ }
120
+ return `${prefix}${fileUrl}${suffix}${assertion}${trailing}`;
121
+ });
122
+ }
123
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEzC,+EAA+E;AAC/E,YAAY;AACZ,+EAA+E;AAE/E,uCAAuC;AACvC,MAAM,CAAC,MAAM,QAAQ,GAAG,OAAO,CAAC;AAEhC,8EAA8E;AAC9E,MAAM,CAAC,MAAM,eAAe,GAAG,WAAW,CAAC;AAE3C,oFAAoF;AACpF,MAAM,CAAC,MAAM,sBAAsB,GAAG,kBAAkB,CAAC;AAEz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;;GAGG;AACH,MAAM,UAAU,aAAa,CAAC,IAAY;IACxC,OAAO,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;AAC7C,CAAC;AAED,+EAA+E;AAC/E,mBAAmB;AACnB,+EAA+E;AAE/E;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,0BAA0B,CAAC,IAAY,EAAE,OAAe;IACtE,OAAO,IAAI,CAAC,OAAO,CAAC,mBAAmB,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,QAAQ,EAAE,EAAE;QAC1F,MAAM,YAAY,GAAG,OAAO,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC;QACpD,MAAM,OAAO,GAAG,aAAa,CAAC,YAAY,CAAC,CAAC,IAAI,CAAC;QAEjD,2BAA2B;QAC3B,IAAI,SAAS,GAAG,EAAE,CAAC;QACnB,IAAI,QAAQ,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC;YAC9B,2CAA2C;YAC3C,SAAS,GAAG,EAAE,CAAC;QACjB,CAAC;aAAM,IAAI,YAAY,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;YAC1C,iDAAiD;YACjD,SAAS,GAAG,wBAAwB,CAAC;QACvC,CAAC;QAED,OAAO,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,GAAG,SAAS,GAAG,QAAQ,EAAE,CAAC;IAC/D,CAAC,CAAC,CAAC;AACL,CAAC"}
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@lass-lang/plugin-utils",
3
+ "version": "0.0.1",
4
+ "description": "Shared utilities for Lass bundler plugins",
5
+ "author": "Long-lazuli",
6
+ "type": "module",
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "CHANGELOG.md"
18
+ ],
19
+ "scripts": {
20
+ "build": "tsc",
21
+ "clean": "rm -rf dist",
22
+ "test": "vitest run",
23
+ "test:watch": "vitest",
24
+ "test:coverage": "vitest run --coverage"
25
+ },
26
+ "devDependencies": {
27
+ "typescript": "^5.7.0",
28
+ "vitest": "^4.0.18"
29
+ },
30
+ "keywords": [
31
+ "lass",
32
+ "plugin",
33
+ "utilities",
34
+ "bundler",
35
+ "vite",
36
+ "bun"
37
+ ],
38
+ "engines": {
39
+ "node": ">=20.0.0"
40
+ },
41
+ "license": "MIT",
42
+ "repository": {
43
+ "type": "git",
44
+ "url": "https://github.com/lass-lang/plugin-utils.git"
45
+ },
46
+ "bugs": {
47
+ "url": "https://github.com/lass-lang/plugin-utils/issues"
48
+ },
49
+ "homepage": "https://github.com/lass-lang/plugin-utils#readme"
50
+ }