onejs-core 1.0.17 → 1.0.19
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/package.json
CHANGED
|
@@ -1,6 +1,102 @@
|
|
|
1
1
|
import * as fs from "fs";
|
|
2
2
|
import * as path from "path";
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* An esbuild plugin that transforms imports from modules starting with a capital letter.
|
|
6
|
+
*
|
|
7
|
+
* ## Functionality:
|
|
8
|
+
* 1. **Externalizing Capitalized Module Imports**:
|
|
9
|
+
* - Marks imports from modules with names starting with a capital letter as external
|
|
10
|
+
* during the resolution phase.
|
|
11
|
+
*
|
|
12
|
+
* 2. **Transforming Import Statements**:
|
|
13
|
+
* - Rewrites ES module import statements to reference a global `CS` object.
|
|
14
|
+
* - Converts:
|
|
15
|
+
* ```js
|
|
16
|
+
* import { Foo, Bar } from "MyModule";
|
|
17
|
+
* ```
|
|
18
|
+
* Into:
|
|
19
|
+
* ```js
|
|
20
|
+
* const { Foo, Bar } = CS.MyModule;
|
|
21
|
+
* ```
|
|
22
|
+
* - If the import is a default import or namespace import:
|
|
23
|
+
* ```js
|
|
24
|
+
* import MyModule from "MyModule";
|
|
25
|
+
* ```
|
|
26
|
+
* Becomes:
|
|
27
|
+
* ```js
|
|
28
|
+
* const MyModule = CS.MyModule;
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* 3. **Handling `require` Statements**:
|
|
32
|
+
* - Transforms `__require("MyModule")` into `CS.MyModule`.
|
|
33
|
+
*
|
|
34
|
+
* ## Parameters:
|
|
35
|
+
* @param {Object} options - Configuration options.
|
|
36
|
+
* @param {Function} [options.moduleFilter] - A filter function that determines if a module should be transformed.
|
|
37
|
+
*
|
|
38
|
+
* ## Returns:
|
|
39
|
+
* @returns {Object} An esbuild plugin object.
|
|
40
|
+
*
|
|
41
|
+
* ## Example Usage:
|
|
42
|
+
*
|
|
43
|
+
* ```js
|
|
44
|
+
* importTransformation({
|
|
45
|
+
* moduleFilter: (moduleName) => moduleName.startsWith("MyLib")
|
|
46
|
+
* });
|
|
47
|
+
* ```
|
|
48
|
+
*
|
|
49
|
+
*/
|
|
50
|
+
export function importTransformation(options = {}) {
|
|
51
|
+
const moduleFilter = options.moduleFilter || (() => true);
|
|
52
|
+
return {
|
|
53
|
+
name: "onejs-import-transform",
|
|
54
|
+
setup(build) {
|
|
55
|
+
// First pass: Mark imports from modules starting with a capital letter as external
|
|
56
|
+
build.onResolve({ filter: /^[A-Z]/ }, (args) => {
|
|
57
|
+
return { path: args.path, external: true };
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// Second pass: Transform all JS and TSX files
|
|
61
|
+
build.onLoad({ filter: /\.(js|jsx|ts|tsx)$/ }, async (args) => {
|
|
62
|
+
let contents = await fs.promises.readFile(args.path, "utf8");
|
|
63
|
+
|
|
64
|
+
// Transform imports from modules starting with a capital letter
|
|
65
|
+
contents = contents.replace(
|
|
66
|
+
/import\s+(?:{([^}]+)})?\s*from\s*["']([A-Z][^"']*)["'];?/g,
|
|
67
|
+
(match, imports, moduleName) => {
|
|
68
|
+
if (!moduleFilter(moduleName))
|
|
69
|
+
return match;
|
|
70
|
+
moduleName = moduleName.replace(/\//g, ".");
|
|
71
|
+
if (imports) {
|
|
72
|
+
const importItems = imports.split(",").map((item) => item.trim());
|
|
73
|
+
return `const { ${importItems.join(", ")} } = CS.${moduleName};`;
|
|
74
|
+
} else {
|
|
75
|
+
const namespaceName = moduleName.split(".").pop();
|
|
76
|
+
return `const ${namespaceName} = CS.${moduleName};`;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
// Transform any remaining require statements for such modules
|
|
82
|
+
contents = contents.replace(
|
|
83
|
+
/__require\(["']([A-Z][^"']*)["']\)/g,
|
|
84
|
+
(match, moduleName) => {
|
|
85
|
+
if (!moduleFilter(moduleName))
|
|
86
|
+
return match;
|
|
87
|
+
return `CS.${moduleName.replace(/\//g, ".")}`;
|
|
88
|
+
}
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
return { contents, loader: path.extname(args.path).slice(1) };
|
|
92
|
+
});
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Keeping this one for backward-compatibility reasons. Please use `importTransformation` instead.
|
|
99
|
+
*/
|
|
4
100
|
export const importTransformPlugin = {
|
|
5
101
|
name: "onejs-import-transform",
|
|
6
102
|
setup(build) {
|
|
@@ -39,4 +135,4 @@ export const importTransformPlugin = {
|
|
|
39
135
|
return { contents, loader: path.extname(args.path).slice(1) };
|
|
40
136
|
});
|
|
41
137
|
},
|
|
42
|
-
}
|
|
138
|
+
}
|