@evjs/webpack-plugin 0.0.1-alpha.5 → 0.0.1-alpha.7
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/README.md +52 -19
- package/esm/index.d.ts +9 -3
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +109 -66
- package/esm/index.js.map +1 -1
- package/esm/server-fn-loader.d.ts +13 -0
- package/esm/server-fn-loader.d.ts.map +1 -1
- package/esm/server-fn-loader.js +17 -90
- package/esm/server-fn-loader.js.map +1 -1
- package/package.json +3 -3
- package/esm/version.d.ts +0 -2
- package/esm/version.d.ts.map +0 -1
- package/esm/version.js +0 -2
- package/esm/version.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,15 +1,34 @@
|
|
|
1
1
|
# @evjs/webpack-plugin
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Webpack adapter for the **ev** framework. Thin wrapper over [`@evjs/build-tools`](../build-tools) that connects bundler-agnostic build logic to Webpack's plugin and loader APIs.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Installation
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
```bash
|
|
8
|
+
npm install @evjs/webpack-plugin
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Exports
|
|
12
|
+
|
|
13
|
+
| Export | Description |
|
|
14
|
+
|--------|-------------|
|
|
15
|
+
| `EvWebpackPlugin` | Plugin that auto-discovers `"use server"` files and manages server builds |
|
|
16
|
+
| `server-fn-loader` | Loader that transforms `"use server"` files for client/server |
|
|
17
|
+
|
|
18
|
+
## How It Works
|
|
19
|
+
|
|
20
|
+
### EvWebpackPlugin
|
|
21
|
+
|
|
22
|
+
1. **Scans client modules** for `"use server"` files via `detectUseServer()` from build-tools.
|
|
23
|
+
2. **Generates a server entry** via `generateServerEntry()` — produces a virtual data-URI module.
|
|
24
|
+
3. **Spawns a child compiler** targeting Node.js — builds the server bundle alongside the client.
|
|
25
|
+
4. **Emits `manifest.json`** mapping function IDs to their module and export names.
|
|
26
|
+
|
|
27
|
+
### server-fn-loader
|
|
28
|
+
|
|
29
|
+
Auto-detects whether it's running in the client or server compiler and delegates to `transformServerFile()`:
|
|
30
|
+
- **Client compiler** → replaces function bodies with RPC stubs.
|
|
31
|
+
- **Server compiler** → keeps original source, appends registrations, and reports to the manifest.
|
|
13
32
|
|
|
14
33
|
## Usage
|
|
15
34
|
|
|
@@ -18,7 +37,18 @@ const { EvWebpackPlugin } = require("@evjs/webpack-plugin");
|
|
|
18
37
|
|
|
19
38
|
module.exports = {
|
|
20
39
|
plugins: [
|
|
21
|
-
new EvWebpackPlugin(
|
|
40
|
+
new EvWebpackPlugin({
|
|
41
|
+
server: {
|
|
42
|
+
// App factory (default: "@evjs/runtime/server#createApp")
|
|
43
|
+
appFactory: "@evjs/runtime/server#createApp",
|
|
44
|
+
// Runner — bake into bundle for self-starting dev server
|
|
45
|
+
runner: process.env.NODE_ENV === "development"
|
|
46
|
+
? "@evjs/runtime/server#runNodeServer"
|
|
47
|
+
: undefined,
|
|
48
|
+
// Extra imports (middleware, config, etc.)
|
|
49
|
+
setup: [],
|
|
50
|
+
},
|
|
51
|
+
}),
|
|
22
52
|
],
|
|
23
53
|
module: {
|
|
24
54
|
rules: [
|
|
@@ -26,16 +56,19 @@ module.exports = {
|
|
|
26
56
|
test: /\.tsx?$/,
|
|
27
57
|
exclude: /node_modules/,
|
|
28
58
|
use: [
|
|
29
|
-
{ loader: "swc-loader"
|
|
30
|
-
{
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
]
|
|
37
|
-
}
|
|
59
|
+
{ loader: "swc-loader" },
|
|
60
|
+
{ loader: "@evjs/webpack-plugin/server-fn-loader" },
|
|
61
|
+
],
|
|
62
|
+
},
|
|
63
|
+
],
|
|
64
|
+
},
|
|
38
65
|
};
|
|
39
66
|
```
|
|
40
67
|
|
|
41
|
-
|
|
68
|
+
### Plugin Options
|
|
69
|
+
|
|
70
|
+
| Option | Type | Default | Description |
|
|
71
|
+
|--------|------|---------|-------------|
|
|
72
|
+
| `server.appFactory` | `string` | `"@evjs/runtime/server#createApp"` | Module ref for app factory |
|
|
73
|
+
| `server.runner` | `string?` | `undefined` | Module ref for auto-starting the server |
|
|
74
|
+
| `server.setup` | `string[]` | `[]` | Extra imports to prepend to server entry |
|
package/esm/index.d.ts
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
|
+
import { type ServerEntryConfig } from "@evjs/build-tools";
|
|
1
2
|
import type { Compiler } from "webpack";
|
|
3
|
+
export type { ServerEntryConfig };
|
|
4
|
+
export interface EvWebpackPluginOptions {
|
|
5
|
+
server?: ServerEntryConfig;
|
|
6
|
+
}
|
|
2
7
|
/**
|
|
3
8
|
* Webpack plugin for the ev framework.
|
|
4
9
|
*
|
|
5
|
-
*
|
|
6
|
-
* the
|
|
7
|
-
* On all builds, emits the `manifest.json` mapping function IDs to assets.
|
|
10
|
+
* Automatically discovers files with the "use server" directive based on the client dependencies
|
|
11
|
+
* and manages the server-side build via a child compiler.
|
|
8
12
|
*/
|
|
9
13
|
export declare class EvWebpackPlugin {
|
|
14
|
+
private options;
|
|
15
|
+
constructor(options?: EvWebpackPluginOptions);
|
|
10
16
|
apply(compiler: Compiler): void;
|
|
11
17
|
}
|
|
12
18
|
//# sourceMappingURL=index.d.ts.map
|
package/esm/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,KAAK,iBAAiB,EACvB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAmBxC,YAAY,EAAE,iBAAiB,EAAE,CAAC;AAElC,MAAM,WAAW,sBAAsB;IACrC,MAAM,CAAC,EAAE,iBAAiB,CAAC;CAC5B;AAED;;;;;GAKG;AACH,qBAAa,eAAe;IAC1B,OAAO,CAAC,OAAO,CAAyB;gBAE5B,OAAO,CAAC,EAAE,sBAAsB;IAG5C,KAAK,CAAC,QAAQ,EAAE,QAAQ;CA8JzB"}
|
package/esm/index.js
CHANGED
|
@@ -1,93 +1,136 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
|
-
import
|
|
3
|
-
import { glob } from "node:fs";
|
|
2
|
+
import { detectUseServer, generateServerEntry, } from "@evjs/build-tools";
|
|
4
3
|
class ManifestCollector {
|
|
5
|
-
|
|
4
|
+
serverFns = {};
|
|
6
5
|
addServerFn(id, meta) {
|
|
7
|
-
this.
|
|
6
|
+
this.serverFns[id] = meta;
|
|
8
7
|
}
|
|
9
8
|
getManifest() {
|
|
10
9
|
return {
|
|
11
10
|
version: 1,
|
|
12
|
-
|
|
11
|
+
serverFns: this.serverFns,
|
|
13
12
|
};
|
|
14
13
|
}
|
|
15
14
|
}
|
|
16
|
-
/**
|
|
17
|
-
* Check if a file starts with the "use server" directive.
|
|
18
|
-
*/
|
|
19
|
-
function hasUseServerDirective(filePath) {
|
|
20
|
-
try {
|
|
21
|
-
const content = fs.readFileSync(filePath, "utf-8");
|
|
22
|
-
const firstLine = content.trimStart().split("\n")[0];
|
|
23
|
-
return /^["']use server["'];?\s*$/.test(firstLine.trim());
|
|
24
|
-
}
|
|
25
|
-
catch {
|
|
26
|
-
return false;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
15
|
/**
|
|
30
16
|
* Webpack plugin for the ev framework.
|
|
31
17
|
*
|
|
32
|
-
*
|
|
33
|
-
* the
|
|
34
|
-
* On all builds, emits the `manifest.json` mapping function IDs to assets.
|
|
18
|
+
* Automatically discovers files with the "use server" directive based on the client dependencies
|
|
19
|
+
* and manages the server-side build via a child compiler.
|
|
35
20
|
*/
|
|
36
21
|
export class EvWebpackPlugin {
|
|
22
|
+
options;
|
|
23
|
+
constructor(options) {
|
|
24
|
+
this.options = options ?? {};
|
|
25
|
+
}
|
|
37
26
|
apply(compiler) {
|
|
38
27
|
const collector = new ManifestCollector();
|
|
39
28
|
// Attach collector to compiler so the loader can access it
|
|
40
29
|
compiler._ev_manifest_collector = collector;
|
|
41
|
-
//
|
|
42
|
-
const isServer = compiler.options.
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
for (const file of serverFiles) {
|
|
62
|
-
const absolutePath = path.resolve(context, file);
|
|
63
|
-
const dep = webpack.EntryPlugin.createDependency(absolutePath, {
|
|
64
|
-
name: undefined,
|
|
65
|
-
});
|
|
66
|
-
compilation.addEntry(context, dep, { name: undefined }, (addErr) => {
|
|
67
|
-
if (addErr) {
|
|
68
|
-
callback(addErr);
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
pending--;
|
|
72
|
-
if (pending === 0) {
|
|
73
|
-
callback();
|
|
30
|
+
// Check if the current compiler is already the Node Child Compiler
|
|
31
|
+
const isServer = compiler.options.name === "evServer" ||
|
|
32
|
+
compiler.options.target === "node";
|
|
33
|
+
if (!isServer) {
|
|
34
|
+
// We are in the Client compiler.
|
|
35
|
+
compiler.hooks.make.tapAsync("EvWebpackPlugin", async (compilation, callback) => {
|
|
36
|
+
compilation.hooks.finishModules.tapAsync("EvWebpackPlugin", (modules, finishCallback) => {
|
|
37
|
+
const serverModulePaths = [];
|
|
38
|
+
for (const module of modules) {
|
|
39
|
+
const resource = "resource" in module ? module.resource : null;
|
|
40
|
+
if (!resource || typeof resource !== "string")
|
|
41
|
+
continue;
|
|
42
|
+
if (resource.includes("node_modules") ||
|
|
43
|
+
!/\.(ts|tsx|js|jsx)$/.test(resource)) {
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
try {
|
|
47
|
+
const content = fs.readFileSync(resource, "utf-8");
|
|
48
|
+
if (detectUseServer(content)) {
|
|
49
|
+
serverModulePaths.push(resource);
|
|
74
50
|
}
|
|
75
|
-
}
|
|
51
|
+
}
|
|
52
|
+
catch (_err) {
|
|
53
|
+
// Ignore read errors for dynamically generated Webpack modules
|
|
54
|
+
}
|
|
76
55
|
}
|
|
56
|
+
if (serverModulePaths.length === 0) {
|
|
57
|
+
return finishCallback();
|
|
58
|
+
}
|
|
59
|
+
// Generate server entry using build-tools (bundler-agnostic)
|
|
60
|
+
const serverEntryContent = generateServerEntry(this.options.server, serverModulePaths);
|
|
61
|
+
// Use a Data URI as a virtual entry point
|
|
62
|
+
const serverEntryPath = `data:text/javascript,${encodeURIComponent(serverEntryContent)}`;
|
|
63
|
+
// Spawn the Node Server child compiler (webpack-specific)
|
|
64
|
+
const outputOptions = {
|
|
65
|
+
filename: "../server/index.js",
|
|
66
|
+
library: { type: "commonjs", name: "evServer" },
|
|
67
|
+
chunkFormat: "commonjs",
|
|
68
|
+
};
|
|
69
|
+
const childCompiler = compilation.createChildCompiler("evServer", outputOptions, [
|
|
70
|
+
new compiler.webpack.node.NodeTemplatePlugin({
|
|
71
|
+
asyncChunkLoading: false,
|
|
72
|
+
}),
|
|
73
|
+
new compiler.webpack.node.NodeTargetPlugin(),
|
|
74
|
+
new compiler.webpack.ExternalsPlugin("commonjs", [
|
|
75
|
+
({ request }, cb) => {
|
|
76
|
+
if (request &&
|
|
77
|
+
typeof request === "string" &&
|
|
78
|
+
(request.startsWith("node:") ||
|
|
79
|
+
[
|
|
80
|
+
"http",
|
|
81
|
+
"https",
|
|
82
|
+
"http2",
|
|
83
|
+
"fs",
|
|
84
|
+
"path",
|
|
85
|
+
"crypto",
|
|
86
|
+
"stream",
|
|
87
|
+
"os",
|
|
88
|
+
"assert",
|
|
89
|
+
"util",
|
|
90
|
+
"events",
|
|
91
|
+
"url",
|
|
92
|
+
"buffer",
|
|
93
|
+
"zlib",
|
|
94
|
+
"child_process",
|
|
95
|
+
"net",
|
|
96
|
+
"tls",
|
|
97
|
+
"querystring",
|
|
98
|
+
"worker_threads",
|
|
99
|
+
].includes(request))) {
|
|
100
|
+
return cb(null, request);
|
|
101
|
+
}
|
|
102
|
+
cb();
|
|
103
|
+
},
|
|
104
|
+
]),
|
|
105
|
+
new compiler.webpack.EntryPlugin(compiler.context, serverEntryPath, { name: "main" }),
|
|
106
|
+
]);
|
|
107
|
+
childCompiler._ev_manifest_collector = collector;
|
|
108
|
+
childCompiler.runAsChild((err, _entries, childCompilation) => {
|
|
109
|
+
if (err)
|
|
110
|
+
return finishCallback(err);
|
|
111
|
+
if (childCompilation?.errors &&
|
|
112
|
+
childCompilation.errors.length > 0) {
|
|
113
|
+
return finishCallback(childCompilation.errors[0]);
|
|
114
|
+
}
|
|
115
|
+
finishCallback();
|
|
116
|
+
});
|
|
77
117
|
});
|
|
118
|
+
callback();
|
|
78
119
|
});
|
|
79
120
|
}
|
|
80
|
-
// Emit manifest
|
|
81
|
-
compiler.hooks.
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
121
|
+
// Emit manifest using modern processAssets hook
|
|
122
|
+
compiler.hooks.thisCompilation.tap("EvWebpackPlugin", (compilation) => {
|
|
123
|
+
compilation.hooks.processAssets.tap({
|
|
124
|
+
name: "EvWebpackPlugin",
|
|
125
|
+
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
|
|
126
|
+
}, () => {
|
|
127
|
+
const manifest = collector.getManifest();
|
|
128
|
+
if (Object.keys(manifest.serverFns).length === 0) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
const content = JSON.stringify(manifest, null, 2);
|
|
132
|
+
compilation.emitAsset("../server/manifest.json", new compiler.webpack.sources.RawSource(content));
|
|
133
|
+
});
|
|
91
134
|
});
|
|
92
135
|
}
|
|
93
136
|
}
|
package/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,EACL,eAAe,EACf,mBAAmB,GAEpB,MAAM,mBAAmB,CAAC;AAI3B,MAAM,iBAAiB;IACrB,SAAS,GAAkC,EAAE,CAAC;IAE9C,WAAW,CAAC,EAAU,EAAE,IAAmB;QACzC,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC;IAC5B,CAAC;IAED,WAAW;QACT,OAAO;YACL,OAAO,EAAE,CAAC;YACV,SAAS,EAAE,IAAI,CAAC,SAAS;SAC1B,CAAC;IACJ,CAAC;CACF;AAUD;;;;;GAKG;AACH,MAAM,OAAO,eAAe;IAClB,OAAO,CAAyB;IAExC,YAAY,OAAgC;QAC1C,IAAI,CAAC,OAAO,GAAG,OAAO,IAAI,EAAE,CAAC;IAC/B,CAAC;IACD,KAAK,CAAC,QAAkB;QACtB,MAAM,SAAS,GAAG,IAAI,iBAAiB,EAAE,CAAC;QAE1C,2DAA2D;QAC1D,QAAuB,CAAC,sBAAsB,GAAG,SAAS,CAAC;QAE5D,mEAAmE;QACnE,MAAM,QAAQ,GACZ,QAAQ,CAAC,OAAO,CAAC,IAAI,KAAK,UAAU;YACpC,QAAQ,CAAC,OAAO,CAAC,MAAM,KAAK,MAAM,CAAC;QAErC,IAAI,CAAC,QAAQ,EAAE,CAAC;YACd,iCAAiC;YACjC,QAAQ,CAAC,KAAK,C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 @@
|
|
|
1
|
+
import type { Compiler } from "webpack";
|
|
1
2
|
interface LoaderContext {
|
|
2
3
|
getOptions(): {
|
|
3
4
|
isServer?: boolean;
|
|
4
5
|
};
|
|
5
6
|
resourcePath: string;
|
|
6
7
|
rootContext: string;
|
|
8
|
+
_compiler?: Compiler & {
|
|
9
|
+
_ev_manifest_collector?: {
|
|
10
|
+
addServerFn(id: string, meta: {
|
|
11
|
+
moduleId: string;
|
|
12
|
+
export: string;
|
|
13
|
+
}): void;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
7
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* Webpack loader for "use server" files.
|
|
19
|
+
* Thin wrapper that delegates to @evjs/build-tools for the actual transformation.
|
|
20
|
+
*/
|
|
8
21
|
export default function serverFnLoader(this: LoaderContext, source: string): Promise<string>;
|
|
9
22
|
export {};
|
|
10
23
|
//# sourceMappingURL=server-fn-loader.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server-fn-loader.d.ts","sourceRoot":"","sources":["../src/server-fn-loader.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"server-fn-loader.d.ts","sourceRoot":"","sources":["../src/server-fn-loader.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAExC,UAAU,aAAa;IACrB,UAAU,IAAI;QAAE,QAAQ,CAAC,EAAE,OAAO,CAAA;KAAE,CAAC;IACrC,YAAY,EAAE,MAAM,CAAC;IACrB,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,CAAC,EAAE,QAAQ,GAAG;QACrB,sBAAsB,CAAC,EAAE;YACvB,WAAW,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE;gBAAE,QAAQ,EAAE,MAAM,CAAC;gBAAC,MAAM,EAAE,MAAM,CAAA;aAAE,GAAG,IAAI,CAAC;SAC3E,CAAC;KACH,CAAC;CACH;AAED;;;GAGG;AACH,wBAA8B,cAAc,CAC1C,IAAI,EAAE,aAAa,EACnB,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,MAAM,CAAC,CAmBjB"}
|
package/esm/server-fn-loader.js
CHANGED
|
@@ -1,97 +1,24 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { parse } from "@swc/core";
|
|
3
|
-
import path from "node:path";
|
|
1
|
+
import { transformServerFile } from "@evjs/build-tools";
|
|
4
2
|
/**
|
|
5
|
-
*
|
|
3
|
+
* Webpack loader for "use server" files.
|
|
4
|
+
* Thin wrapper that delegates to @evjs/build-tools for the actual transformation.
|
|
6
5
|
*/
|
|
7
|
-
function makeFnId(rootContext, resourcePath, exportName) {
|
|
8
|
-
const relativePath = path.relative(rootContext, resourcePath);
|
|
9
|
-
return createHash("sha256")
|
|
10
|
-
.update(`${relativePath}:${exportName}`)
|
|
11
|
-
.digest("hex")
|
|
12
|
-
.slice(0, 16);
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Check whether the source starts with the "use server" directive.
|
|
16
|
-
*/
|
|
17
|
-
function hasUseServerDirective(source) {
|
|
18
|
-
const trimmed = source.replace(/^(\s|\/\/[^\n]*\n|\/\*[\s\S]*?\*\/)*/, "");
|
|
19
|
-
return /^["']use server["'];?\s/.test(trimmed);
|
|
20
|
-
}
|
|
21
6
|
export default async function serverFnLoader(source) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
syntax: "typescript",
|
|
29
|
-
tsx: true,
|
|
30
|
-
comments: false,
|
|
31
|
-
script: false,
|
|
32
|
-
});
|
|
33
|
-
const exportNames = [];
|
|
34
|
-
// Iterate through module items to find exports
|
|
35
|
-
for (const item of program.body) {
|
|
36
|
-
if (item.type === "ExportDeclaration") {
|
|
37
|
-
const decl = item.declaration;
|
|
38
|
-
if (decl.type === "FunctionDeclaration") {
|
|
39
|
-
if (decl.identifier.value)
|
|
40
|
-
exportNames.push(decl.identifier.value);
|
|
41
|
-
}
|
|
42
|
-
else if (decl.type === "VariableDeclaration") {
|
|
43
|
-
for (const v of decl.declarations) {
|
|
44
|
-
if (v.id.type === "Identifier") {
|
|
45
|
-
exportNames.push(v.id.value);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
else if (item.type === "ExportNamedDeclaration") {
|
|
51
|
-
for (const specifier of item.specifiers) {
|
|
52
|
-
if (specifier.type === "ExportSpecifier") {
|
|
53
|
-
if (specifier.exported?.type === "Identifier") {
|
|
54
|
-
exportNames.push(specifier.exported.value);
|
|
55
|
-
}
|
|
56
|
-
else if (specifier.orig.type === "Identifier") {
|
|
57
|
-
exportNames.push(specifier.orig.value);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
if (exportNames.length === 0) {
|
|
64
|
-
return source;
|
|
7
|
+
const explicitOptions = this.getOptions() || {};
|
|
8
|
+
let isServer = explicitOptions.isServer;
|
|
9
|
+
if (typeof isServer === "undefined") {
|
|
10
|
+
const compilerName = this._compiler?.name;
|
|
11
|
+
const target = this._compiler?.options?.target;
|
|
12
|
+
isServer = compilerName === "evServer" || target === "node";
|
|
65
13
|
}
|
|
66
14
|
const manifestCollector = this._compiler?._ev_manifest_collector;
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
.update(relativePath)
|
|
76
|
-
.digest("hex")
|
|
77
|
-
.slice(0, 16);
|
|
78
|
-
manifestCollector.addServerFn(fnId, {
|
|
79
|
-
moduleId,
|
|
80
|
-
export: name,
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
return `registerServerFn("${fnId}", ${name});`;
|
|
84
|
-
})
|
|
85
|
-
.join("\n");
|
|
86
|
-
return `import { registerServerFn } from "@evjs/runtime/server";\n${source}\n${registrations}\n`;
|
|
87
|
-
}
|
|
88
|
-
// Client build: replace with RPC stubs
|
|
89
|
-
const stubCode = exportNames
|
|
90
|
-
.map((name) => {
|
|
91
|
-
const fnId = makeFnId(this.rootContext, this.resourcePath, name);
|
|
92
|
-
return `export function ${name}(...args) {\n return __ev_rpc("${fnId}", args);\n}`;
|
|
93
|
-
})
|
|
94
|
-
.join("\n\n");
|
|
95
|
-
return `import { __ev_rpc } from "@evjs/runtime/client";\n\n${stubCode}\n`;
|
|
15
|
+
return transformServerFile(source, {
|
|
16
|
+
resourcePath: this.resourcePath,
|
|
17
|
+
rootContext: this.rootContext,
|
|
18
|
+
isServer: !!isServer,
|
|
19
|
+
onServerFn: manifestCollector
|
|
20
|
+
? (fnId, meta) => manifestCollector.addServerFn(fnId, meta)
|
|
21
|
+
: undefined,
|
|
22
|
+
});
|
|
96
23
|
}
|
|
97
24
|
//# sourceMappingURL=server-fn-loader.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server-fn-loader.js","sourceRoot":"","sources":["../src/server-fn-loader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"server-fn-loader.js","sourceRoot":"","sources":["../src/server-fn-loader.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAcxD;;;GAGG;AACH,MAAM,CAAC,OAAO,CAAC,KAAK,UAAU,cAAc,CAE1C,MAAc;IAEd,MAAM,eAAe,GAAG,IAAI,CAAC,UAAU,EAAE,IAAI,EAAE,CAAC;IAChD,IAAI,QAAQ,GAAG,eAAe,CAAC,QAAQ,CAAC;IACxC,IAAI,OAAO,QAAQ,KAAK,WAAW,EAAE,CAAC;QACpC,MAAM,YAAY,GAAG,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC;QAC1C,MAAM,MAAM,GAAG,IAAI,CAAC,SAAS,EAAE,OAAO,EAAE,MAAM,CAAC;QAC/C,QAAQ,GAAG,YAAY,KAAK,UAAU,IAAI,MAAM,KAAK,MAAM,CAAC;IAC9D,CAAC;IAED,MAAM,iBAAiB,GAAG,IAAI,CAAC,SAAS,EAAE,sBAAsB,CAAC;IAEjE,OAAO,mBAAmB,CAAC,MAAM,EAAE;QACjC,YAAY,EAAE,IAAI,CAAC,YAAY;QAC/B,WAAW,EAAE,IAAI,CAAC,WAAW;QAC7B,QAAQ,EAAE,CAAC,CAAC,QAAQ;QACpB,UAAU,EAAE,iBAAiB;YAC3B,CAAC,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,EAAE,CAAC,iBAAiB,CAAC,WAAW,CAAC,IAAI,EAAE,IAAI,CAAC;YAC3D,CAAC,CAAC,SAAS;KACd,CAAC,CAAC;AACL,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@evjs/webpack-plugin",
|
|
3
|
-
"version": "0.0.1-alpha.
|
|
3
|
+
"version": "0.0.1-alpha.7",
|
|
4
4
|
"description": "Webpack plugin and loaders for the ev framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"publishConfig": {
|
|
@@ -35,8 +35,8 @@
|
|
|
35
35
|
"author": "xusd320",
|
|
36
36
|
"license": "MIT",
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@evjs/
|
|
39
|
-
"@
|
|
38
|
+
"@evjs/build-tools": "0.0.1-alpha.7",
|
|
39
|
+
"@evjs/manifest": "0.0.1-alpha.7"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"typescript": "^5.7.3",
|
package/esm/version.d.ts
DELETED
package/esm/version.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../src/version.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,OAAO,kBAAkB,CAAC"}
|
package/esm/version.js
DELETED
package/esm/version.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"version.js","sourceRoot":"","sources":["../src/version.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,OAAO,GAAG,eAAe,CAAC"}
|