@codespark/plugin-webpack 1.0.0
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 +21 -0
- package/README.md +91 -0
- package/dist/chunk-CbDLau6x.cjs +34 -0
- package/dist/index.cjs +32 -0
- package/dist/index.d.cts +15 -0
- package/dist/index.d.mts +15 -0
- package/dist/index.mjs +30 -0
- package/dist/loader.cjs +329 -0
- package/dist/loader.d.cts +10 -0
- package/dist/loader.d.mts +10 -0
- package/dist/loader.mjs +323 -0
- package/package.json +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 TonyL1u
|
|
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,91 @@
|
|
|
1
|
+
# @codespark/plugin-webpack
|
|
2
|
+
|
|
3
|
+
Webpack plugin for the [Codespark](https://codesparkjs.com) ecosystem. Automatically collects dependency information from JSX code at build time.
|
|
4
|
+
|
|
5
|
+
## Documentation
|
|
6
|
+
|
|
7
|
+
Visit [https://codesparkjs.com/docs/plugin/webpack](https://codesparkjs.com/docs/plugin/webpack) to view the documentation.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @codespark/plugin-webpack
|
|
13
|
+
# or
|
|
14
|
+
pnpm add @codespark/plugin-webpack
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
### Webpack
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
// webpack.config.js
|
|
23
|
+
import CodesparkWebpackPlugin from '@codespark/plugin-webpack';
|
|
24
|
+
|
|
25
|
+
export default {
|
|
26
|
+
plugins: [
|
|
27
|
+
new CodesparkWebpackPlugin()
|
|
28
|
+
]
|
|
29
|
+
};
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Next.js (Webpack)
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
// next.config.ts
|
|
36
|
+
import type { NextConfig } from 'next';
|
|
37
|
+
import CodesparkWebpackPlugin from '@codespark/plugin-webpack';
|
|
38
|
+
|
|
39
|
+
const nextConfig: NextConfig = {
|
|
40
|
+
/* config options here */
|
|
41
|
+
webpack(config) {
|
|
42
|
+
config.plugins.push(new CodesparkWebpackPlugin());
|
|
43
|
+
|
|
44
|
+
return config;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export default nextConfig;
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Next.js (Turbopack)
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
// next.config.ts
|
|
55
|
+
const nextConfig: NextConfig = {
|
|
56
|
+
/* config options here */
|
|
57
|
+
turbopack: {
|
|
58
|
+
rules: {
|
|
59
|
+
'*.tsx': {
|
|
60
|
+
loaders: [
|
|
61
|
+
{
|
|
62
|
+
loader: '@codespark/plugin-webpack/loader'
|
|
63
|
+
}
|
|
64
|
+
]
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export default nextConfig;
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Options
|
|
74
|
+
|
|
75
|
+
| Option | Type | Default | Description |
|
|
76
|
+
|--------|------|---------|-------------|
|
|
77
|
+
| `enabled` | `boolean` | `true` | Enable/disable the plugin |
|
|
78
|
+
| `methods` | `string[]` | `['createWorkspace']` | Method names to transform |
|
|
79
|
+
| `importSource` | `string[]` | `['@codespark/react']` | Package names to detect imports from |
|
|
80
|
+
|
|
81
|
+
```js
|
|
82
|
+
new CodesparkWebpackPlugin({
|
|
83
|
+
enabled: true,
|
|
84
|
+
methods: ['createWorkspace'],
|
|
85
|
+
importSource: ['@codespark/react']
|
|
86
|
+
})
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## License
|
|
90
|
+
|
|
91
|
+
MIT
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
13
|
+
__defProp(to, key, {
|
|
14
|
+
get: ((k) => from[k]).bind(null, key),
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
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
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
|
|
29
|
+
Object.defineProperty(exports, '__toESM', {
|
|
30
|
+
enumerable: true,
|
|
31
|
+
get: function () {
|
|
32
|
+
return __toESM;
|
|
33
|
+
}
|
|
34
|
+
});
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const require_chunk = require('./chunk-CbDLau6x.cjs');
|
|
2
|
+
let node_path = require("node:path");
|
|
3
|
+
node_path = require_chunk.__toESM(node_path);
|
|
4
|
+
let node_url = require("node:url");
|
|
5
|
+
|
|
6
|
+
//#region src/index.ts
|
|
7
|
+
const __dir = node_path.default.dirname((0, node_url.fileURLToPath)(require("url").pathToFileURL(__filename).href));
|
|
8
|
+
const loaderPath = node_path.default.resolve(__dir, "loader.mjs");
|
|
9
|
+
var CodesparkWebpackPlugin = class {
|
|
10
|
+
enabled;
|
|
11
|
+
methods;
|
|
12
|
+
constructor(options) {
|
|
13
|
+
const { enabled = true, methods } = options || {};
|
|
14
|
+
this.enabled = enabled;
|
|
15
|
+
this.methods = methods;
|
|
16
|
+
}
|
|
17
|
+
apply(compiler) {
|
|
18
|
+
if (!this.enabled) return;
|
|
19
|
+
compiler.options.module.rules.push({
|
|
20
|
+
test: /\.(js|jsx|ts|tsx)$/,
|
|
21
|
+
exclude: /node_modules/,
|
|
22
|
+
enforce: "pre",
|
|
23
|
+
use: [{
|
|
24
|
+
loader: loaderPath,
|
|
25
|
+
options: { methods: this.methods }
|
|
26
|
+
}]
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
//#endregion
|
|
32
|
+
module.exports = CodesparkWebpackPlugin;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Compiler } from "webpack";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
interface Options {
|
|
5
|
+
enabled?: boolean;
|
|
6
|
+
methods?: string[];
|
|
7
|
+
}
|
|
8
|
+
declare class CodesparkWebpackPlugin {
|
|
9
|
+
private enabled;
|
|
10
|
+
private methods?;
|
|
11
|
+
constructor(options?: Options);
|
|
12
|
+
apply(compiler: Compiler): void;
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
export { Options, CodesparkWebpackPlugin as default };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Compiler } from "webpack";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
interface Options {
|
|
5
|
+
enabled?: boolean;
|
|
6
|
+
methods?: string[];
|
|
7
|
+
}
|
|
8
|
+
declare class CodesparkWebpackPlugin {
|
|
9
|
+
private enabled;
|
|
10
|
+
private methods?;
|
|
11
|
+
constructor(options?: Options);
|
|
12
|
+
apply(compiler: Compiler): void;
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
export { Options, CodesparkWebpackPlugin as default };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
|
|
4
|
+
//#region src/index.ts
|
|
5
|
+
const __dir = path.dirname(fileURLToPath(import.meta.url));
|
|
6
|
+
const loaderPath = path.resolve(__dir, "loader.mjs");
|
|
7
|
+
var CodesparkWebpackPlugin = class {
|
|
8
|
+
enabled;
|
|
9
|
+
methods;
|
|
10
|
+
constructor(options) {
|
|
11
|
+
const { enabled = true, methods } = options || {};
|
|
12
|
+
this.enabled = enabled;
|
|
13
|
+
this.methods = methods;
|
|
14
|
+
}
|
|
15
|
+
apply(compiler) {
|
|
16
|
+
if (!this.enabled) return;
|
|
17
|
+
compiler.options.module.rules.push({
|
|
18
|
+
test: /\.(js|jsx|ts|tsx)$/,
|
|
19
|
+
exclude: /node_modules/,
|
|
20
|
+
enforce: "pre",
|
|
21
|
+
use: [{
|
|
22
|
+
loader: loaderPath,
|
|
23
|
+
options: { methods: this.methods }
|
|
24
|
+
}]
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
//#endregion
|
|
30
|
+
export { CodesparkWebpackPlugin as default };
|
package/dist/loader.cjs
ADDED
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
const require_chunk = require('./chunk-CbDLau6x.cjs');
|
|
2
|
+
let node_path = require("node:path");
|
|
3
|
+
node_path = require_chunk.__toESM(node_path);
|
|
4
|
+
let node_fs = require("node:fs");
|
|
5
|
+
node_fs = require_chunk.__toESM(node_fs);
|
|
6
|
+
let __babel_generator = require("@babel/generator");
|
|
7
|
+
__babel_generator = require_chunk.__toESM(__babel_generator);
|
|
8
|
+
let __babel_parser = require("@babel/parser");
|
|
9
|
+
let __babel_traverse = require("@babel/traverse");
|
|
10
|
+
__babel_traverse = require_chunk.__toESM(__babel_traverse);
|
|
11
|
+
let __babel_types = require("@babel/types");
|
|
12
|
+
__babel_types = require_chunk.__toESM(__babel_types);
|
|
13
|
+
|
|
14
|
+
//#region src/collect-deps.ts
|
|
15
|
+
const pkgPath = process.cwd();
|
|
16
|
+
const pathsConfig = JSON.parse(node_fs.default.readFileSync(node_path.default.resolve(pkgPath, "tsconfig.json"), "utf-8")).compilerOptions?.paths || {};
|
|
17
|
+
const aliases = Object.keys(pathsConfig).map((p) => p.replace("/*", ""));
|
|
18
|
+
function parseCode(code) {
|
|
19
|
+
return (0, __babel_parser.parse)(code, {
|
|
20
|
+
sourceType: "module",
|
|
21
|
+
plugins: ["jsx", "typescript"]
|
|
22
|
+
}).program.body;
|
|
23
|
+
}
|
|
24
|
+
function collectIdentifiers(ast) {
|
|
25
|
+
const ids = /* @__PURE__ */ new Set();
|
|
26
|
+
function walk(node) {
|
|
27
|
+
if (!node || typeof node !== "object") return;
|
|
28
|
+
if (node.type === "Identifier" || node.type === "JSXIdentifier") ids.add(node.name);
|
|
29
|
+
for (const key of Object.keys(node)) {
|
|
30
|
+
if (key === "loc" || key === "range") continue;
|
|
31
|
+
const value = node[key];
|
|
32
|
+
if (Array.isArray(value)) value.forEach(walk);
|
|
33
|
+
else if (value && typeof value === "object") walk(value);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
ast.forEach(walk);
|
|
37
|
+
return ids;
|
|
38
|
+
}
|
|
39
|
+
function buildImportMap(imports) {
|
|
40
|
+
const map = /* @__PURE__ */ new Map();
|
|
41
|
+
for (const imp of imports) {
|
|
42
|
+
if (imp.importKind === "type") continue;
|
|
43
|
+
const source = imp.source.value;
|
|
44
|
+
for (const spec of imp.specifiers) {
|
|
45
|
+
if (spec.type === "ImportSpecifier" && spec.importKind === "type") continue;
|
|
46
|
+
map.set(spec.local.name, source);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return map;
|
|
50
|
+
}
|
|
51
|
+
function getUsedSources(usedIds, importMap) {
|
|
52
|
+
const sources = /* @__PURE__ */ new Set();
|
|
53
|
+
for (const id of usedIds) {
|
|
54
|
+
const source = importMap.get(id);
|
|
55
|
+
if (source) sources.add(source);
|
|
56
|
+
}
|
|
57
|
+
return sources;
|
|
58
|
+
}
|
|
59
|
+
function getDefinedNames(node) {
|
|
60
|
+
if (node.type === "ExportNamedDeclaration" && node.declaration) return getDefinedNames(node.declaration);
|
|
61
|
+
if (node.type === "VariableDeclaration") return node.declarations.flatMap((d) => d.id.type === "Identifier" ? [d.id.name] : []);
|
|
62
|
+
if (node.type === "FunctionDeclaration" && node.id) return [node.id.name];
|
|
63
|
+
if (node.type === "ClassDeclaration" && node.id) return [node.id.name];
|
|
64
|
+
return [];
|
|
65
|
+
}
|
|
66
|
+
function findMatchingAlias(source) {
|
|
67
|
+
for (const alias of aliases) if (source === alias || source.startsWith(alias + "/")) return alias;
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
function resolveAlias(source) {
|
|
71
|
+
const alias = findMatchingAlias(source);
|
|
72
|
+
if (!alias) return source;
|
|
73
|
+
const aliasPath = pathsConfig[alias + "/*"][0].replace("/*", "");
|
|
74
|
+
return source.replace(alias, node_path.default.resolve(pkgPath, aliasPath));
|
|
75
|
+
}
|
|
76
|
+
function resolveFile$1(source, fromFile) {
|
|
77
|
+
const resolved = source.startsWith(".") ? node_path.default.resolve(node_path.default.dirname(fromFile), source) : resolveAlias(source);
|
|
78
|
+
for (const ext of [
|
|
79
|
+
".tsx",
|
|
80
|
+
".ts",
|
|
81
|
+
".jsx",
|
|
82
|
+
".js",
|
|
83
|
+
"/index.tsx",
|
|
84
|
+
"/index.ts"
|
|
85
|
+
]) {
|
|
86
|
+
const tryPath = resolved + ext;
|
|
87
|
+
if (node_fs.default.existsSync(tryPath)) return tryPath;
|
|
88
|
+
}
|
|
89
|
+
return node_fs.default.existsSync(resolved) ? resolved : null;
|
|
90
|
+
}
|
|
91
|
+
function isLocalImport(source) {
|
|
92
|
+
if (source.startsWith(".") || source.startsWith("/")) return true;
|
|
93
|
+
return findMatchingAlias(source) !== null;
|
|
94
|
+
}
|
|
95
|
+
function isAliasImport(source) {
|
|
96
|
+
return !source.startsWith(".") && !source.startsWith("/") && findMatchingAlias(source) !== null;
|
|
97
|
+
}
|
|
98
|
+
function aliasToRelative(source, resolved) {
|
|
99
|
+
const alias = findMatchingAlias(source);
|
|
100
|
+
if (!alias) return source;
|
|
101
|
+
if (source === alias) return ".";
|
|
102
|
+
let rel = "./" + source.slice(alias.length + 1);
|
|
103
|
+
if (resolved) {
|
|
104
|
+
const ext = node_path.default.extname(resolved);
|
|
105
|
+
if (ext && !rel.endsWith(ext)) rel += ext;
|
|
106
|
+
}
|
|
107
|
+
return rel;
|
|
108
|
+
}
|
|
109
|
+
function rewriteAliasImports(code, filePath) {
|
|
110
|
+
let result = code;
|
|
111
|
+
const ast = parseCode(code);
|
|
112
|
+
for (const node of ast) {
|
|
113
|
+
if (node.type !== "ImportDeclaration") continue;
|
|
114
|
+
const source = node.source.value;
|
|
115
|
+
if (!isAliasImport(source)) continue;
|
|
116
|
+
const resolved = resolveFile$1(source, filePath);
|
|
117
|
+
if (!resolved) continue;
|
|
118
|
+
let rel = node_path.default.relative(node_path.default.dirname(filePath), resolved);
|
|
119
|
+
if (!rel.startsWith(".")) rel = "./" + rel;
|
|
120
|
+
result = result.replace(`'${source}'`, `'${rel}'`);
|
|
121
|
+
result = result.replace(`"${source}"`, `"${rel}"`);
|
|
122
|
+
}
|
|
123
|
+
return result;
|
|
124
|
+
}
|
|
125
|
+
function collectFromFile(file, usedSources, result, visited) {
|
|
126
|
+
if (visited.has(file)) return;
|
|
127
|
+
visited.add(file);
|
|
128
|
+
const imports = parseCode(node_fs.default.readFileSync(file, "utf-8")).filter((node) => node.type === "ImportDeclaration");
|
|
129
|
+
for (const imp of imports) {
|
|
130
|
+
if (imp.importKind === "type") continue;
|
|
131
|
+
const source = imp.source.value;
|
|
132
|
+
if (!usedSources.has(source) || !isLocalImport(source)) continue;
|
|
133
|
+
const resolved = resolveFile$1(source, file);
|
|
134
|
+
if (!resolved || result[source]) continue;
|
|
135
|
+
const childCode = node_fs.default.readFileSync(resolved, "utf-8");
|
|
136
|
+
const childAst = parseCode(childCode);
|
|
137
|
+
const childImports = childAst.filter((n) => n.type === "ImportDeclaration");
|
|
138
|
+
const childUsedSources = getUsedSources(collectIdentifiers(childAst.filter((n) => n.type !== "ImportDeclaration")), buildImportMap(childImports));
|
|
139
|
+
const externals = childImports.filter((i) => i.importKind !== "type" && !isLocalImport(i.source.value) && childUsedSources.has(i.source.value)).map((i) => i.source.value);
|
|
140
|
+
result[source] = {
|
|
141
|
+
code: rewriteAliasImports(childCode, resolved),
|
|
142
|
+
resolved,
|
|
143
|
+
externals
|
|
144
|
+
};
|
|
145
|
+
collectFromFile(resolved, childUsedSources, result, visited);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
function formatSpecifierName(spec) {
|
|
149
|
+
if (spec.type === "ImportDefaultSpecifier") return spec.local.name;
|
|
150
|
+
if (spec.type === "ImportNamespaceSpecifier") return `* as ${spec.local.name}`;
|
|
151
|
+
const imported = spec.imported;
|
|
152
|
+
return imported && "name" in imported && imported.name !== spec.local.name ? `${imported.name} as ${spec.local.name}` : spec.local.name;
|
|
153
|
+
}
|
|
154
|
+
function generateImportStatement(imp, usedIds, sourcePath) {
|
|
155
|
+
const usedSpecifiers = imp.specifiers.filter((s) => usedIds.has(s.local.name));
|
|
156
|
+
if (usedSpecifiers.length === 0) return null;
|
|
157
|
+
const parts = [];
|
|
158
|
+
const namedImports = [];
|
|
159
|
+
for (const spec of usedSpecifiers) {
|
|
160
|
+
const name = formatSpecifierName(spec);
|
|
161
|
+
if (spec.type === "ImportDefaultSpecifier") parts.unshift(name);
|
|
162
|
+
else if (spec.type === "ImportNamespaceSpecifier") parts.push(name);
|
|
163
|
+
else namedImports.push(name);
|
|
164
|
+
}
|
|
165
|
+
if (namedImports.length > 0) parts.push(`{ ${namedImports.join(", ")} }`);
|
|
166
|
+
const source = sourcePath ?? imp.source.value;
|
|
167
|
+
return `import ${parts.join(", ")} from '${source}';`;
|
|
168
|
+
}
|
|
169
|
+
function isUsedImport(imp, usedSources) {
|
|
170
|
+
return imp.importKind !== "type" && usedSources.has(imp.source.value);
|
|
171
|
+
}
|
|
172
|
+
function collectDependencies(code, hostFile) {
|
|
173
|
+
const fileInfos = {};
|
|
174
|
+
const file = node_path.default.resolve(hostFile);
|
|
175
|
+
const visited = /* @__PURE__ */ new Set();
|
|
176
|
+
try {
|
|
177
|
+
const codeUsedIds = collectIdentifiers(parseCode(code));
|
|
178
|
+
const hostContent = node_fs.default.readFileSync(file, "utf-8");
|
|
179
|
+
const hostAst = parseCode(hostContent);
|
|
180
|
+
const hostImports = hostAst.filter((node) => node.type === "ImportDeclaration");
|
|
181
|
+
const importedNames = new Set(buildImportMap(hostImports).keys());
|
|
182
|
+
const localDefs = /* @__PURE__ */ new Map();
|
|
183
|
+
for (const node of hostAst) {
|
|
184
|
+
if (node.type === "ImportDeclaration") continue;
|
|
185
|
+
for (const name of getDefinedNames(node)) if (!importedNames.has(name)) localDefs.set(name, {
|
|
186
|
+
node,
|
|
187
|
+
code: hostContent.slice(node.start, node.end)
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
function collectLocalDeps(ids, collected) {
|
|
191
|
+
for (const id of ids) {
|
|
192
|
+
if (collected.has(id) || !localDefs.has(id)) continue;
|
|
193
|
+
collected.add(id);
|
|
194
|
+
collectLocalDeps(collectIdentifiers([localDefs.get(id).node]), collected);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
const collectedNames = /* @__PURE__ */ new Set();
|
|
198
|
+
collectLocalDeps(codeUsedIds, collectedNames);
|
|
199
|
+
const locals = [];
|
|
200
|
+
for (const node of hostAst) {
|
|
201
|
+
if (node.type === "ImportDeclaration") continue;
|
|
202
|
+
if (getDefinedNames(node).some((name) => collectedNames.has(name))) locals.push(hostContent.slice(node.start, node.end));
|
|
203
|
+
}
|
|
204
|
+
const allUsedIds = new Set(codeUsedIds);
|
|
205
|
+
for (const name of collectedNames) {
|
|
206
|
+
const def = localDefs.get(name);
|
|
207
|
+
if (def) for (const id of collectIdentifiers([def.node])) allUsedIds.add(id);
|
|
208
|
+
}
|
|
209
|
+
const usedSources = getUsedSources(allUsedIds, buildImportMap(hostImports));
|
|
210
|
+
collectFromFile(file, usedSources, fileInfos, visited);
|
|
211
|
+
const files = {};
|
|
212
|
+
for (const [source, info] of Object.entries(fileInfos)) {
|
|
213
|
+
const key = isAliasImport(source) ? aliasToRelative(source, info.resolved) : source;
|
|
214
|
+
files[key] = info.code;
|
|
215
|
+
}
|
|
216
|
+
const aliasImports = [];
|
|
217
|
+
for (const imp of hostImports) {
|
|
218
|
+
if (!isUsedImport(imp, usedSources) || !isAliasImport(imp.source.value)) continue;
|
|
219
|
+
if (!resolveFile$1(imp.source.value, file)) continue;
|
|
220
|
+
const statement = generateImportStatement(imp, allUsedIds, aliasToRelative(imp.source.value));
|
|
221
|
+
if (statement) aliasImports.push(statement);
|
|
222
|
+
}
|
|
223
|
+
const externalImports = [];
|
|
224
|
+
for (const imp of hostImports) {
|
|
225
|
+
if (!isUsedImport(imp, usedSources) || isLocalImport(imp.source.value)) continue;
|
|
226
|
+
const statement = generateImportStatement(imp, allUsedIds);
|
|
227
|
+
if (statement) externalImports.push(statement);
|
|
228
|
+
}
|
|
229
|
+
return {
|
|
230
|
+
entry: {
|
|
231
|
+
code,
|
|
232
|
+
locals,
|
|
233
|
+
imports: [...aliasImports, ...externalImports]
|
|
234
|
+
},
|
|
235
|
+
files
|
|
236
|
+
};
|
|
237
|
+
} catch {
|
|
238
|
+
return {
|
|
239
|
+
entry: {
|
|
240
|
+
code,
|
|
241
|
+
locals: [],
|
|
242
|
+
imports: []
|
|
243
|
+
},
|
|
244
|
+
files: {}
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
//#endregion
|
|
250
|
+
//#region src/transform-jsx.ts
|
|
251
|
+
const traverse = __babel_traverse.default.default ?? __babel_traverse.default;
|
|
252
|
+
const generate = __babel_generator.default.default ?? __babel_generator.default;
|
|
253
|
+
const resolveFile = (source, fromFile) => {
|
|
254
|
+
const resolved = node_path.default.resolve(node_path.default.dirname(fromFile), source);
|
|
255
|
+
for (const ext of [
|
|
256
|
+
".tsx",
|
|
257
|
+
".ts",
|
|
258
|
+
".jsx",
|
|
259
|
+
".js",
|
|
260
|
+
"/index.tsx",
|
|
261
|
+
"/index.ts"
|
|
262
|
+
]) {
|
|
263
|
+
const tryPath = resolved + ext;
|
|
264
|
+
if (node_fs.default.existsSync(tryPath)) return tryPath;
|
|
265
|
+
}
|
|
266
|
+
return node_fs.default.existsSync(resolved) ? resolved : null;
|
|
267
|
+
};
|
|
268
|
+
const transformJsx = (code, id, options = {}) => {
|
|
269
|
+
const { methods = ["createWorkspace"], importSource = ["@codespark/react"] } = options;
|
|
270
|
+
const ast = (0, __babel_parser.parse)(code, {
|
|
271
|
+
sourceType: "module",
|
|
272
|
+
plugins: ["jsx", "typescript"]
|
|
273
|
+
});
|
|
274
|
+
const importMap = /* @__PURE__ */ new Map();
|
|
275
|
+
const localNames = /* @__PURE__ */ new Map();
|
|
276
|
+
const slice = (node) => code.slice(node.start, node.end);
|
|
277
|
+
let modified = false;
|
|
278
|
+
traverse(ast, {
|
|
279
|
+
ImportDeclaration(nodePath) {
|
|
280
|
+
const source = nodePath.node.source.value;
|
|
281
|
+
nodePath.node.specifiers.forEach((spec) => {
|
|
282
|
+
if (spec.type === "ImportDefaultSpecifier") {
|
|
283
|
+
const resolved = resolveFile(source, id);
|
|
284
|
+
if (resolved) importMap.set(spec.local.name, resolved);
|
|
285
|
+
}
|
|
286
|
+
if (importSource.includes(source) && spec.type === "ImportSpecifier" && __babel_types.isIdentifier(spec.imported)) {
|
|
287
|
+
if (methods.includes(spec.imported.name)) localNames.set(spec.local.name, spec.imported.name);
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
},
|
|
291
|
+
CallExpression(nodePath) {
|
|
292
|
+
const callee = nodePath.node.callee;
|
|
293
|
+
if (!__babel_types.isIdentifier(callee) || !localNames.has(callee.name)) return;
|
|
294
|
+
const localName = callee.name;
|
|
295
|
+
const [codeArg] = nodePath.node.arguments;
|
|
296
|
+
let scannedValue = null;
|
|
297
|
+
if (__babel_types.isJSXElement(codeArg) || __babel_types.isJSXFragment(codeArg)) scannedValue = __babel_types.valueToNode(collectDependencies(slice(codeArg), id));
|
|
298
|
+
else if (__babel_types.isIdentifier(codeArg)) scannedValue = __babel_types.valueToNode(collectDependencies(codeArg.name, id));
|
|
299
|
+
else if (__babel_types.isArrowFunctionExpression(codeArg) || __babel_types.isFunctionExpression(codeArg)) scannedValue = __babel_types.valueToNode(collectDependencies(slice(codeArg), id));
|
|
300
|
+
if (scannedValue) {
|
|
301
|
+
nodePath.replaceWith(__babel_types.callExpression(__babel_types.memberExpression(__babel_types.identifier(localName), __babel_types.identifier("call")), [__babel_types.objectExpression([__babel_types.objectProperty(__babel_types.identifier("__scanned"), scannedValue)]), ...nodePath.node.arguments]));
|
|
302
|
+
modified = true;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
});
|
|
306
|
+
if (modified) return generate(ast).code;
|
|
307
|
+
return null;
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
//#endregion
|
|
311
|
+
//#region src/loader.ts
|
|
312
|
+
const codesparkLoader = function(source) {
|
|
313
|
+
const id = this.resourcePath;
|
|
314
|
+
if (![
|
|
315
|
+
".js",
|
|
316
|
+
".jsx",
|
|
317
|
+
".ts",
|
|
318
|
+
".tsx"
|
|
319
|
+
].includes(node_path.default.extname(id))) return source;
|
|
320
|
+
const { methods, importSource } = this.getOptions();
|
|
321
|
+
return transformJsx(source, id, {
|
|
322
|
+
methods,
|
|
323
|
+
importSource
|
|
324
|
+
}) ?? source;
|
|
325
|
+
};
|
|
326
|
+
var loader_default = codesparkLoader;
|
|
327
|
+
|
|
328
|
+
//#endregion
|
|
329
|
+
module.exports = loader_default;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { LoaderDefinitionFunction } from "webpack";
|
|
2
|
+
|
|
3
|
+
//#region src/loader.d.ts
|
|
4
|
+
interface LoaderOptions {
|
|
5
|
+
methods?: string[];
|
|
6
|
+
importSource?: string[];
|
|
7
|
+
}
|
|
8
|
+
declare const codesparkLoader: LoaderDefinitionFunction<LoaderOptions>;
|
|
9
|
+
//#endregion
|
|
10
|
+
export { LoaderOptions, codesparkLoader as default };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { LoaderDefinitionFunction } from "webpack";
|
|
2
|
+
|
|
3
|
+
//#region src/loader.d.ts
|
|
4
|
+
interface LoaderOptions {
|
|
5
|
+
methods?: string[];
|
|
6
|
+
importSource?: string[];
|
|
7
|
+
}
|
|
8
|
+
declare const codesparkLoader: LoaderDefinitionFunction<LoaderOptions>;
|
|
9
|
+
//#endregion
|
|
10
|
+
export { LoaderOptions, codesparkLoader as default };
|
package/dist/loader.mjs
ADDED
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import _generate from "@babel/generator";
|
|
4
|
+
import { parse } from "@babel/parser";
|
|
5
|
+
import _traverse from "@babel/traverse";
|
|
6
|
+
import * as t from "@babel/types";
|
|
7
|
+
|
|
8
|
+
//#region src/collect-deps.ts
|
|
9
|
+
const pkgPath = process.cwd();
|
|
10
|
+
const pathsConfig = JSON.parse(fs.readFileSync(path.resolve(pkgPath, "tsconfig.json"), "utf-8")).compilerOptions?.paths || {};
|
|
11
|
+
const aliases = Object.keys(pathsConfig).map((p) => p.replace("/*", ""));
|
|
12
|
+
function parseCode(code) {
|
|
13
|
+
return parse(code, {
|
|
14
|
+
sourceType: "module",
|
|
15
|
+
plugins: ["jsx", "typescript"]
|
|
16
|
+
}).program.body;
|
|
17
|
+
}
|
|
18
|
+
function collectIdentifiers(ast) {
|
|
19
|
+
const ids = /* @__PURE__ */ new Set();
|
|
20
|
+
function walk(node) {
|
|
21
|
+
if (!node || typeof node !== "object") return;
|
|
22
|
+
if (node.type === "Identifier" || node.type === "JSXIdentifier") ids.add(node.name);
|
|
23
|
+
for (const key of Object.keys(node)) {
|
|
24
|
+
if (key === "loc" || key === "range") continue;
|
|
25
|
+
const value = node[key];
|
|
26
|
+
if (Array.isArray(value)) value.forEach(walk);
|
|
27
|
+
else if (value && typeof value === "object") walk(value);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
ast.forEach(walk);
|
|
31
|
+
return ids;
|
|
32
|
+
}
|
|
33
|
+
function buildImportMap(imports) {
|
|
34
|
+
const map = /* @__PURE__ */ new Map();
|
|
35
|
+
for (const imp of imports) {
|
|
36
|
+
if (imp.importKind === "type") continue;
|
|
37
|
+
const source = imp.source.value;
|
|
38
|
+
for (const spec of imp.specifiers) {
|
|
39
|
+
if (spec.type === "ImportSpecifier" && spec.importKind === "type") continue;
|
|
40
|
+
map.set(spec.local.name, source);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return map;
|
|
44
|
+
}
|
|
45
|
+
function getUsedSources(usedIds, importMap) {
|
|
46
|
+
const sources = /* @__PURE__ */ new Set();
|
|
47
|
+
for (const id of usedIds) {
|
|
48
|
+
const source = importMap.get(id);
|
|
49
|
+
if (source) sources.add(source);
|
|
50
|
+
}
|
|
51
|
+
return sources;
|
|
52
|
+
}
|
|
53
|
+
function getDefinedNames(node) {
|
|
54
|
+
if (node.type === "ExportNamedDeclaration" && node.declaration) return getDefinedNames(node.declaration);
|
|
55
|
+
if (node.type === "VariableDeclaration") return node.declarations.flatMap((d) => d.id.type === "Identifier" ? [d.id.name] : []);
|
|
56
|
+
if (node.type === "FunctionDeclaration" && node.id) return [node.id.name];
|
|
57
|
+
if (node.type === "ClassDeclaration" && node.id) return [node.id.name];
|
|
58
|
+
return [];
|
|
59
|
+
}
|
|
60
|
+
function findMatchingAlias(source) {
|
|
61
|
+
for (const alias of aliases) if (source === alias || source.startsWith(alias + "/")) return alias;
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
function resolveAlias(source) {
|
|
65
|
+
const alias = findMatchingAlias(source);
|
|
66
|
+
if (!alias) return source;
|
|
67
|
+
const aliasPath = pathsConfig[alias + "/*"][0].replace("/*", "");
|
|
68
|
+
return source.replace(alias, path.resolve(pkgPath, aliasPath));
|
|
69
|
+
}
|
|
70
|
+
function resolveFile$1(source, fromFile) {
|
|
71
|
+
const resolved = source.startsWith(".") ? path.resolve(path.dirname(fromFile), source) : resolveAlias(source);
|
|
72
|
+
for (const ext of [
|
|
73
|
+
".tsx",
|
|
74
|
+
".ts",
|
|
75
|
+
".jsx",
|
|
76
|
+
".js",
|
|
77
|
+
"/index.tsx",
|
|
78
|
+
"/index.ts"
|
|
79
|
+
]) {
|
|
80
|
+
const tryPath = resolved + ext;
|
|
81
|
+
if (fs.existsSync(tryPath)) return tryPath;
|
|
82
|
+
}
|
|
83
|
+
return fs.existsSync(resolved) ? resolved : null;
|
|
84
|
+
}
|
|
85
|
+
function isLocalImport(source) {
|
|
86
|
+
if (source.startsWith(".") || source.startsWith("/")) return true;
|
|
87
|
+
return findMatchingAlias(source) !== null;
|
|
88
|
+
}
|
|
89
|
+
function isAliasImport(source) {
|
|
90
|
+
return !source.startsWith(".") && !source.startsWith("/") && findMatchingAlias(source) !== null;
|
|
91
|
+
}
|
|
92
|
+
function aliasToRelative(source, resolved) {
|
|
93
|
+
const alias = findMatchingAlias(source);
|
|
94
|
+
if (!alias) return source;
|
|
95
|
+
if (source === alias) return ".";
|
|
96
|
+
let rel = "./" + source.slice(alias.length + 1);
|
|
97
|
+
if (resolved) {
|
|
98
|
+
const ext = path.extname(resolved);
|
|
99
|
+
if (ext && !rel.endsWith(ext)) rel += ext;
|
|
100
|
+
}
|
|
101
|
+
return rel;
|
|
102
|
+
}
|
|
103
|
+
function rewriteAliasImports(code, filePath) {
|
|
104
|
+
let result = code;
|
|
105
|
+
const ast = parseCode(code);
|
|
106
|
+
for (const node of ast) {
|
|
107
|
+
if (node.type !== "ImportDeclaration") continue;
|
|
108
|
+
const source = node.source.value;
|
|
109
|
+
if (!isAliasImport(source)) continue;
|
|
110
|
+
const resolved = resolveFile$1(source, filePath);
|
|
111
|
+
if (!resolved) continue;
|
|
112
|
+
let rel = path.relative(path.dirname(filePath), resolved);
|
|
113
|
+
if (!rel.startsWith(".")) rel = "./" + rel;
|
|
114
|
+
result = result.replace(`'${source}'`, `'${rel}'`);
|
|
115
|
+
result = result.replace(`"${source}"`, `"${rel}"`);
|
|
116
|
+
}
|
|
117
|
+
return result;
|
|
118
|
+
}
|
|
119
|
+
function collectFromFile(file, usedSources, result, visited) {
|
|
120
|
+
if (visited.has(file)) return;
|
|
121
|
+
visited.add(file);
|
|
122
|
+
const imports = parseCode(fs.readFileSync(file, "utf-8")).filter((node) => node.type === "ImportDeclaration");
|
|
123
|
+
for (const imp of imports) {
|
|
124
|
+
if (imp.importKind === "type") continue;
|
|
125
|
+
const source = imp.source.value;
|
|
126
|
+
if (!usedSources.has(source) || !isLocalImport(source)) continue;
|
|
127
|
+
const resolved = resolveFile$1(source, file);
|
|
128
|
+
if (!resolved || result[source]) continue;
|
|
129
|
+
const childCode = fs.readFileSync(resolved, "utf-8");
|
|
130
|
+
const childAst = parseCode(childCode);
|
|
131
|
+
const childImports = childAst.filter((n) => n.type === "ImportDeclaration");
|
|
132
|
+
const childUsedSources = getUsedSources(collectIdentifiers(childAst.filter((n) => n.type !== "ImportDeclaration")), buildImportMap(childImports));
|
|
133
|
+
const externals = childImports.filter((i) => i.importKind !== "type" && !isLocalImport(i.source.value) && childUsedSources.has(i.source.value)).map((i) => i.source.value);
|
|
134
|
+
result[source] = {
|
|
135
|
+
code: rewriteAliasImports(childCode, resolved),
|
|
136
|
+
resolved,
|
|
137
|
+
externals
|
|
138
|
+
};
|
|
139
|
+
collectFromFile(resolved, childUsedSources, result, visited);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
function formatSpecifierName(spec) {
|
|
143
|
+
if (spec.type === "ImportDefaultSpecifier") return spec.local.name;
|
|
144
|
+
if (spec.type === "ImportNamespaceSpecifier") return `* as ${spec.local.name}`;
|
|
145
|
+
const imported = spec.imported;
|
|
146
|
+
return imported && "name" in imported && imported.name !== spec.local.name ? `${imported.name} as ${spec.local.name}` : spec.local.name;
|
|
147
|
+
}
|
|
148
|
+
function generateImportStatement(imp, usedIds, sourcePath) {
|
|
149
|
+
const usedSpecifiers = imp.specifiers.filter((s) => usedIds.has(s.local.name));
|
|
150
|
+
if (usedSpecifiers.length === 0) return null;
|
|
151
|
+
const parts = [];
|
|
152
|
+
const namedImports = [];
|
|
153
|
+
for (const spec of usedSpecifiers) {
|
|
154
|
+
const name = formatSpecifierName(spec);
|
|
155
|
+
if (spec.type === "ImportDefaultSpecifier") parts.unshift(name);
|
|
156
|
+
else if (spec.type === "ImportNamespaceSpecifier") parts.push(name);
|
|
157
|
+
else namedImports.push(name);
|
|
158
|
+
}
|
|
159
|
+
if (namedImports.length > 0) parts.push(`{ ${namedImports.join(", ")} }`);
|
|
160
|
+
const source = sourcePath ?? imp.source.value;
|
|
161
|
+
return `import ${parts.join(", ")} from '${source}';`;
|
|
162
|
+
}
|
|
163
|
+
function isUsedImport(imp, usedSources) {
|
|
164
|
+
return imp.importKind !== "type" && usedSources.has(imp.source.value);
|
|
165
|
+
}
|
|
166
|
+
function collectDependencies(code, hostFile) {
|
|
167
|
+
const fileInfos = {};
|
|
168
|
+
const file = path.resolve(hostFile);
|
|
169
|
+
const visited = /* @__PURE__ */ new Set();
|
|
170
|
+
try {
|
|
171
|
+
const codeUsedIds = collectIdentifiers(parseCode(code));
|
|
172
|
+
const hostContent = fs.readFileSync(file, "utf-8");
|
|
173
|
+
const hostAst = parseCode(hostContent);
|
|
174
|
+
const hostImports = hostAst.filter((node) => node.type === "ImportDeclaration");
|
|
175
|
+
const importedNames = new Set(buildImportMap(hostImports).keys());
|
|
176
|
+
const localDefs = /* @__PURE__ */ new Map();
|
|
177
|
+
for (const node of hostAst) {
|
|
178
|
+
if (node.type === "ImportDeclaration") continue;
|
|
179
|
+
for (const name of getDefinedNames(node)) if (!importedNames.has(name)) localDefs.set(name, {
|
|
180
|
+
node,
|
|
181
|
+
code: hostContent.slice(node.start, node.end)
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
function collectLocalDeps(ids, collected) {
|
|
185
|
+
for (const id of ids) {
|
|
186
|
+
if (collected.has(id) || !localDefs.has(id)) continue;
|
|
187
|
+
collected.add(id);
|
|
188
|
+
collectLocalDeps(collectIdentifiers([localDefs.get(id).node]), collected);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
const collectedNames = /* @__PURE__ */ new Set();
|
|
192
|
+
collectLocalDeps(codeUsedIds, collectedNames);
|
|
193
|
+
const locals = [];
|
|
194
|
+
for (const node of hostAst) {
|
|
195
|
+
if (node.type === "ImportDeclaration") continue;
|
|
196
|
+
if (getDefinedNames(node).some((name) => collectedNames.has(name))) locals.push(hostContent.slice(node.start, node.end));
|
|
197
|
+
}
|
|
198
|
+
const allUsedIds = new Set(codeUsedIds);
|
|
199
|
+
for (const name of collectedNames) {
|
|
200
|
+
const def = localDefs.get(name);
|
|
201
|
+
if (def) for (const id of collectIdentifiers([def.node])) allUsedIds.add(id);
|
|
202
|
+
}
|
|
203
|
+
const usedSources = getUsedSources(allUsedIds, buildImportMap(hostImports));
|
|
204
|
+
collectFromFile(file, usedSources, fileInfos, visited);
|
|
205
|
+
const files = {};
|
|
206
|
+
for (const [source, info] of Object.entries(fileInfos)) {
|
|
207
|
+
const key = isAliasImport(source) ? aliasToRelative(source, info.resolved) : source;
|
|
208
|
+
files[key] = info.code;
|
|
209
|
+
}
|
|
210
|
+
const aliasImports = [];
|
|
211
|
+
for (const imp of hostImports) {
|
|
212
|
+
if (!isUsedImport(imp, usedSources) || !isAliasImport(imp.source.value)) continue;
|
|
213
|
+
if (!resolveFile$1(imp.source.value, file)) continue;
|
|
214
|
+
const statement = generateImportStatement(imp, allUsedIds, aliasToRelative(imp.source.value));
|
|
215
|
+
if (statement) aliasImports.push(statement);
|
|
216
|
+
}
|
|
217
|
+
const externalImports = [];
|
|
218
|
+
for (const imp of hostImports) {
|
|
219
|
+
if (!isUsedImport(imp, usedSources) || isLocalImport(imp.source.value)) continue;
|
|
220
|
+
const statement = generateImportStatement(imp, allUsedIds);
|
|
221
|
+
if (statement) externalImports.push(statement);
|
|
222
|
+
}
|
|
223
|
+
return {
|
|
224
|
+
entry: {
|
|
225
|
+
code,
|
|
226
|
+
locals,
|
|
227
|
+
imports: [...aliasImports, ...externalImports]
|
|
228
|
+
},
|
|
229
|
+
files
|
|
230
|
+
};
|
|
231
|
+
} catch {
|
|
232
|
+
return {
|
|
233
|
+
entry: {
|
|
234
|
+
code,
|
|
235
|
+
locals: [],
|
|
236
|
+
imports: []
|
|
237
|
+
},
|
|
238
|
+
files: {}
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
//#endregion
|
|
244
|
+
//#region src/transform-jsx.ts
|
|
245
|
+
const traverse = _traverse.default ?? _traverse;
|
|
246
|
+
const generate = _generate.default ?? _generate;
|
|
247
|
+
const resolveFile = (source, fromFile) => {
|
|
248
|
+
const resolved = path.resolve(path.dirname(fromFile), source);
|
|
249
|
+
for (const ext of [
|
|
250
|
+
".tsx",
|
|
251
|
+
".ts",
|
|
252
|
+
".jsx",
|
|
253
|
+
".js",
|
|
254
|
+
"/index.tsx",
|
|
255
|
+
"/index.ts"
|
|
256
|
+
]) {
|
|
257
|
+
const tryPath = resolved + ext;
|
|
258
|
+
if (fs.existsSync(tryPath)) return tryPath;
|
|
259
|
+
}
|
|
260
|
+
return fs.existsSync(resolved) ? resolved : null;
|
|
261
|
+
};
|
|
262
|
+
const transformJsx = (code, id, options = {}) => {
|
|
263
|
+
const { methods = ["createWorkspace"], importSource = ["@codespark/react"] } = options;
|
|
264
|
+
const ast = parse(code, {
|
|
265
|
+
sourceType: "module",
|
|
266
|
+
plugins: ["jsx", "typescript"]
|
|
267
|
+
});
|
|
268
|
+
const importMap = /* @__PURE__ */ new Map();
|
|
269
|
+
const localNames = /* @__PURE__ */ new Map();
|
|
270
|
+
const slice = (node) => code.slice(node.start, node.end);
|
|
271
|
+
let modified = false;
|
|
272
|
+
traverse(ast, {
|
|
273
|
+
ImportDeclaration(nodePath) {
|
|
274
|
+
const source = nodePath.node.source.value;
|
|
275
|
+
nodePath.node.specifiers.forEach((spec) => {
|
|
276
|
+
if (spec.type === "ImportDefaultSpecifier") {
|
|
277
|
+
const resolved = resolveFile(source, id);
|
|
278
|
+
if (resolved) importMap.set(spec.local.name, resolved);
|
|
279
|
+
}
|
|
280
|
+
if (importSource.includes(source) && spec.type === "ImportSpecifier" && t.isIdentifier(spec.imported)) {
|
|
281
|
+
if (methods.includes(spec.imported.name)) localNames.set(spec.local.name, spec.imported.name);
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
},
|
|
285
|
+
CallExpression(nodePath) {
|
|
286
|
+
const callee = nodePath.node.callee;
|
|
287
|
+
if (!t.isIdentifier(callee) || !localNames.has(callee.name)) return;
|
|
288
|
+
const localName = callee.name;
|
|
289
|
+
const [codeArg] = nodePath.node.arguments;
|
|
290
|
+
let scannedValue = null;
|
|
291
|
+
if (t.isJSXElement(codeArg) || t.isJSXFragment(codeArg)) scannedValue = t.valueToNode(collectDependencies(slice(codeArg), id));
|
|
292
|
+
else if (t.isIdentifier(codeArg)) scannedValue = t.valueToNode(collectDependencies(codeArg.name, id));
|
|
293
|
+
else if (t.isArrowFunctionExpression(codeArg) || t.isFunctionExpression(codeArg)) scannedValue = t.valueToNode(collectDependencies(slice(codeArg), id));
|
|
294
|
+
if (scannedValue) {
|
|
295
|
+
nodePath.replaceWith(t.callExpression(t.memberExpression(t.identifier(localName), t.identifier("call")), [t.objectExpression([t.objectProperty(t.identifier("__scanned"), scannedValue)]), ...nodePath.node.arguments]));
|
|
296
|
+
modified = true;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
if (modified) return generate(ast).code;
|
|
301
|
+
return null;
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
//#endregion
|
|
305
|
+
//#region src/loader.ts
|
|
306
|
+
const codesparkLoader = function(source) {
|
|
307
|
+
const id = this.resourcePath;
|
|
308
|
+
if (![
|
|
309
|
+
".js",
|
|
310
|
+
".jsx",
|
|
311
|
+
".ts",
|
|
312
|
+
".tsx"
|
|
313
|
+
].includes(path.extname(id))) return source;
|
|
314
|
+
const { methods, importSource } = this.getOptions();
|
|
315
|
+
return transformJsx(source, id, {
|
|
316
|
+
methods,
|
|
317
|
+
importSource
|
|
318
|
+
}) ?? source;
|
|
319
|
+
};
|
|
320
|
+
var loader_default = codesparkLoader;
|
|
321
|
+
|
|
322
|
+
//#endregion
|
|
323
|
+
export { loader_default as default };
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@codespark/plugin-webpack",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Webpack plugin for codespark ecosystem",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"webpack-plugin",
|
|
8
|
+
"turbopack-plugin",
|
|
9
|
+
"codespark"
|
|
10
|
+
],
|
|
11
|
+
"files": [
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.mts",
|
|
17
|
+
"import": "./dist/index.mjs",
|
|
18
|
+
"require": "./dist/index.cjs"
|
|
19
|
+
},
|
|
20
|
+
"./loader": {
|
|
21
|
+
"types": "./dist/loader.d.mts",
|
|
22
|
+
"import": "./dist/loader.mjs",
|
|
23
|
+
"require": "./dist/loader.cjs"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsdown",
|
|
28
|
+
"test": "vitest run"
|
|
29
|
+
},
|
|
30
|
+
"author": "TonyL1u",
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/codesparkjs/codespark.git",
|
|
35
|
+
"directory": "packages/webpack"
|
|
36
|
+
},
|
|
37
|
+
"homepage": "https://codesparkjs.com",
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@babel/generator": "^7.28.5",
|
|
40
|
+
"@babel/parser": "^7.28.5",
|
|
41
|
+
"@babel/traverse": "^7.28.5",
|
|
42
|
+
"@babel/types": "^7.28.5"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@codespark/react": "workspace:*",
|
|
46
|
+
"@types/babel__generator": "^7.27.0",
|
|
47
|
+
"@types/babel__traverse": "^7.28.0",
|
|
48
|
+
"@types/mdx": "^2.0.13",
|
|
49
|
+
"@types/node": "^22.19.2",
|
|
50
|
+
"@types/react": "^19.2.7",
|
|
51
|
+
"tsdown": "^0.17.3",
|
|
52
|
+
"webpack": "^5.99.9"
|
|
53
|
+
}
|
|
54
|
+
}
|