@lark-apaas/coding-html-devserver 0.1.5-alpha.0 → 0.1.5-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/lib/index.d.ts +0 -2
- package/lib/index.js +1 -3
- package/lib/plugins/raw-jsx.d.ts +12 -0
- package/lib/plugins/raw-jsx.js +39 -0
- package/lib/vite-config.js +2 -12
- package/package.json +1 -1
- package/lib/plugins/raw-serve.d.ts +0 -29
- package/lib/plugins/raw-serve.js +0 -94
package/lib/index.d.ts
CHANGED
|
@@ -25,7 +25,5 @@ export { viteClientPatchPlugin } from './plugins/vite-client-patch';
|
|
|
25
25
|
export { wsWatchdogPlugin } from './plugins/ws-watchdog';
|
|
26
26
|
export type { WsWatchdogPluginOptions } from './plugins/ws-watchdog';
|
|
27
27
|
export { basePathRewritePlugin } from './plugins/base-path';
|
|
28
|
-
export { rawServePlugin } from './plugins/raw-serve';
|
|
29
|
-
export type { RawServeOptions } from './plugins/raw-serve';
|
|
30
28
|
export { routesJsonPlugin } from './plugins/routes-json';
|
|
31
29
|
export type { RoutesJsonPluginOptions } from './plugins/routes-json';
|
package/lib/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.routesJsonPlugin = exports.
|
|
3
|
+
exports.routesJsonPlugin = exports.basePathRewritePlugin = exports.wsWatchdogPlugin = exports.viteClientPatchPlugin = exports.platformInjectPlugin = exports.HEALTH_ENDPOINT = exports.healthPlugin = exports.serializeTag = exports.injectTags = exports.injectOgMeta = exports.MIAODA_PREVIEW_BRIDGE_URL = exports.previewBridgeTags = exports.slardarTags = exports.buildRenderContext = exports.viewContextTag = exports.PLATFORM_TAGS = exports.injectPlatformTags = exports.normalizeBasePath = exports.ROUTES_JSON_FILENAME = exports.routePathFromFile = exports.resolveRoutesJson = exports.generateRoutes = exports.minifyHtmlWithHbsProtection = exports.buildStatic = exports.createHtmlViteConfig = exports.startDevServer = void 0;
|
|
4
4
|
var dev_1 = require("./dev");
|
|
5
5
|
Object.defineProperty(exports, "startDevServer", { enumerable: true, get: function () { return dev_1.startDevServer; } });
|
|
6
6
|
var vite_config_1 = require("./vite-config");
|
|
@@ -43,7 +43,5 @@ var ws_watchdog_1 = require("./plugins/ws-watchdog");
|
|
|
43
43
|
Object.defineProperty(exports, "wsWatchdogPlugin", { enumerable: true, get: function () { return ws_watchdog_1.wsWatchdogPlugin; } });
|
|
44
44
|
var base_path_1 = require("./plugins/base-path");
|
|
45
45
|
Object.defineProperty(exports, "basePathRewritePlugin", { enumerable: true, get: function () { return base_path_1.basePathRewritePlugin; } });
|
|
46
|
-
var raw_serve_1 = require("./plugins/raw-serve");
|
|
47
|
-
Object.defineProperty(exports, "rawServePlugin", { enumerable: true, get: function () { return raw_serve_1.rawServePlugin; } });
|
|
48
46
|
var routes_json_1 = require("./plugins/routes-json");
|
|
49
47
|
Object.defineProperty(exports, "routesJsonPlugin", { enumerable: true, get: function () { return routes_json_1.routesJsonPlugin; } });
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Plugin } from 'vite';
|
|
2
|
+
/**
|
|
3
|
+
* .jsx 走客户端编译,在 Vite transform 管线前原样返回。
|
|
4
|
+
*
|
|
5
|
+
* 不能用 `esbuild.exclude`:esbuild 跳过后 `vite:import-analysis` 仍会
|
|
6
|
+
* 用 es-module-lexer 解析文件,遇到 JSX 语法直接报错。middleware 在
|
|
7
|
+
* Vite 内置 transform 之前拦截请求,彻底绕过整条管线。
|
|
8
|
+
*/
|
|
9
|
+
export declare function rawJsxPlugin(opts: {
|
|
10
|
+
root: string;
|
|
11
|
+
clientBasePath: string;
|
|
12
|
+
}): Plugin;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.rawJsxPlugin = rawJsxPlugin;
|
|
7
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
8
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
9
|
+
/**
|
|
10
|
+
* .jsx 走客户端编译,在 Vite transform 管线前原样返回。
|
|
11
|
+
*
|
|
12
|
+
* 不能用 `esbuild.exclude`:esbuild 跳过后 `vite:import-analysis` 仍会
|
|
13
|
+
* 用 es-module-lexer 解析文件,遇到 JSX 语法直接报错。middleware 在
|
|
14
|
+
* Vite 内置 transform 之前拦截请求,彻底绕过整条管线。
|
|
15
|
+
*/
|
|
16
|
+
function rawJsxPlugin(opts) {
|
|
17
|
+
const { root, clientBasePath } = opts;
|
|
18
|
+
return {
|
|
19
|
+
name: 'miaoda-html-raw-jsx',
|
|
20
|
+
apply: 'serve',
|
|
21
|
+
configureServer(server) {
|
|
22
|
+
server.middlewares.use((req, res, next) => {
|
|
23
|
+
const url = (req.url ?? '').split('?')[0];
|
|
24
|
+
if (!url.endsWith('.jsx'))
|
|
25
|
+
return next();
|
|
26
|
+
const stripped = clientBasePath && url.startsWith(clientBasePath) ? url.slice(clientBasePath.length) : url;
|
|
27
|
+
const filePath = node_path_1.default.join(root, stripped);
|
|
28
|
+
try {
|
|
29
|
+
const content = node_fs_1.default.readFileSync(filePath, 'utf-8');
|
|
30
|
+
res.setHeader('Content-Type', 'application/javascript; charset=utf-8');
|
|
31
|
+
res.end(content);
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
next();
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
}
|
package/lib/vite-config.js
CHANGED
|
@@ -10,7 +10,7 @@ const inject_1 = require("./plugins/inject");
|
|
|
10
10
|
const vite_client_patch_1 = require("./plugins/vite-client-patch");
|
|
11
11
|
const ws_watchdog_1 = require("./plugins/ws-watchdog");
|
|
12
12
|
const base_path_1 = require("./plugins/base-path");
|
|
13
|
-
const
|
|
13
|
+
const raw_jsx_1 = require("./plugins/raw-jsx");
|
|
14
14
|
const routes_json_1 = require("./plugins/routes-json");
|
|
15
15
|
const normalize_base_path_1 = require("./normalize-base-path");
|
|
16
16
|
/**
|
|
@@ -50,14 +50,6 @@ function createHtmlViteConfig(opts = {}) {
|
|
|
50
50
|
base,
|
|
51
51
|
appType: 'mpa',
|
|
52
52
|
clearScreen: false,
|
|
53
|
-
// 「无编译 HTML」:vite 8 转译归 Oxc 管,整体关掉默认转译(js/ts/jsx 全不转)。
|
|
54
|
-
// `esbuild` 选项在 vite 8 已废弃、`esbuild: false` 也已失效,必须用 `oxc: false`。
|
|
55
|
-
// 仅关转译还不够——import-analysis / css 处理由 rawServePlugin 在中间件层绕过。
|
|
56
|
-
//
|
|
57
|
-
// 用展开而非直接写 `oxc: false`:本包运行时是 vite 8(下游装 vite ^8),但仓库里
|
|
58
|
-
// tsc 解析到 hoist 上来的 vite 7 类型(其 InlineConfig 无 oxc 字段)。展开 Record
|
|
59
|
-
// 可绕开字面量的 excess-property 检查,运行时仍下发 oxc:false。
|
|
60
|
-
...{ oxc: false },
|
|
61
53
|
server: {
|
|
62
54
|
host,
|
|
63
55
|
port,
|
|
@@ -69,6 +61,7 @@ function createHtmlViteConfig(opts = {}) {
|
|
|
69
61
|
: { path: '/ws' },
|
|
70
62
|
},
|
|
71
63
|
plugins: [
|
|
64
|
+
(0, raw_jsx_1.rawJsxPlugin)({ root, clientBasePath }),
|
|
72
65
|
(0, health_1.healthPlugin)(),
|
|
73
66
|
(0, inject_1.platformInjectPlugin)({ appId }),
|
|
74
67
|
// dev only:apply:'serve' 自带,build 不挂
|
|
@@ -76,9 +69,6 @@ function createHtmlViteConfig(opts = {}) {
|
|
|
76
69
|
(0, ws_watchdog_1.wsWatchdogPlugin)({ clientBasePath }),
|
|
77
70
|
// base 非 `/` 时,把无尾斜杠的 `/app/<id>` 内部 rewrite 成带尾斜杠(自守卫空 base)
|
|
78
71
|
(0, base_path_1.basePathRewritePlugin)(clientBasePath),
|
|
79
|
-
// 「无编译 HTML」:.jsx/.js/.css 在 transform 中间件之前原样下发,绕过
|
|
80
|
-
// import-analysis(jsx parse 报错根因)与 css 处理;改动靠手动刷。
|
|
81
|
-
(0, raw_serve_1.rawServePlugin)({ root, clientBasePath }),
|
|
82
72
|
// 在 `${base}routes.json` 提供路由清单(与 build 产物一致)
|
|
83
73
|
(0, routes_json_1.routesJsonPlugin)({ root, clientBasePath }),
|
|
84
74
|
],
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lark-apaas/coding-html-devserver",
|
|
3
|
-
"version": "0.1.5-alpha.
|
|
3
|
+
"version": "0.1.5-alpha.1",
|
|
4
4
|
"description": "Dev/build toolchain for the miaoda-coding html (pure HTML) stack. Wraps vite (dev + HMR) behind a bin so the template never sees vite; build is pure-copy + platform-script injection + optional minify.",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import type { Plugin } from 'vite';
|
|
2
|
-
export interface RawServeOptions {
|
|
3
|
-
/** 静态根目录(绝对路径),与 vite `root` 同源。 */
|
|
4
|
-
root: string;
|
|
5
|
-
/** base path(已 normalize,无尾斜杠),用于剥沙箱网关透传的前缀。 */
|
|
6
|
-
clientBasePath: string;
|
|
7
|
-
/** 命中即原样下发的扩展名(小写、带点),默认 `.jsx` / `.js` / `.css`。 */
|
|
8
|
-
exts?: readonly string[];
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* 原样透传插件(dev only,「无编译 HTML」栈核心)。
|
|
12
|
-
*
|
|
13
|
-
* 背景:vite 8 的转译归 Oxc 管,`oxc: false` 能关转译;但 **import-analysis 是另一条插件**,
|
|
14
|
-
* 它对每个「JS 请求」无差别跑 es-module-lexer parse(`knownJsSrcRE` 把 `.jsx` 也算 JS 请求),
|
|
15
|
-
* 而 JSX 不是合法 JS → 抛 "Failed to parse source for import analysis"。它的跳过名单
|
|
16
|
-
* (`.map` / `.json` / direct-css)是写死的,**配置加不进去**。CSS 则会被 css 插件做
|
|
17
|
-
* `@import` 内联 / `url()` 重写。要真正「原样」只能让这些文件**根本不进 transform 管线**。
|
|
18
|
-
*
|
|
19
|
-
* 做法:把中间件 `unshift` 到 middleware stack 最前(早于 baseMiddleware / transformMiddleware),
|
|
20
|
-
* 命中目标扩展名就直接读源文件原样吐回,浏览器侧自行处理(如 babel-standalone 编 jsx)。
|
|
21
|
-
* 对齐 `base-path.ts` 的 stack.unshift 手法。
|
|
22
|
-
*
|
|
23
|
-
* 取舍:绕过 vite ⇒ 这些文件**无 vite 原生 HMR**,改动靠整页 reload / 手动刷。
|
|
24
|
-
*
|
|
25
|
-
* 安全边界:dev only,serve 的是开发者自己 `root` 下的源文件。做了路径穿越守卫(`..`),
|
|
26
|
-
* 但不解符号链接——`root` 内的 symlink 会被跟随。这是 dev server 的可接受假设。
|
|
27
|
-
*/
|
|
28
|
-
export declare function rawServePlugin(opts: RawServeOptions): Plugin;
|
|
29
|
-
export default rawServePlugin;
|
package/lib/plugins/raw-serve.js
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.rawServePlugin = rawServePlugin;
|
|
7
|
-
const promises_1 = __importDefault(require("node:fs/promises"));
|
|
8
|
-
const node_path_1 = __importDefault(require("node:path"));
|
|
9
|
-
const CONTENT_TYPE = {
|
|
10
|
-
'.js': 'application/javascript; charset=utf-8',
|
|
11
|
-
'.jsx': 'application/javascript; charset=utf-8',
|
|
12
|
-
'.mjs': 'application/javascript; charset=utf-8',
|
|
13
|
-
'.css': 'text/css; charset=utf-8',
|
|
14
|
-
};
|
|
15
|
-
/**
|
|
16
|
-
* 原样透传插件(dev only,「无编译 HTML」栈核心)。
|
|
17
|
-
*
|
|
18
|
-
* 背景:vite 8 的转译归 Oxc 管,`oxc: false` 能关转译;但 **import-analysis 是另一条插件**,
|
|
19
|
-
* 它对每个「JS 请求」无差别跑 es-module-lexer parse(`knownJsSrcRE` 把 `.jsx` 也算 JS 请求),
|
|
20
|
-
* 而 JSX 不是合法 JS → 抛 "Failed to parse source for import analysis"。它的跳过名单
|
|
21
|
-
* (`.map` / `.json` / direct-css)是写死的,**配置加不进去**。CSS 则会被 css 插件做
|
|
22
|
-
* `@import` 内联 / `url()` 重写。要真正「原样」只能让这些文件**根本不进 transform 管线**。
|
|
23
|
-
*
|
|
24
|
-
* 做法:把中间件 `unshift` 到 middleware stack 最前(早于 baseMiddleware / transformMiddleware),
|
|
25
|
-
* 命中目标扩展名就直接读源文件原样吐回,浏览器侧自行处理(如 babel-standalone 编 jsx)。
|
|
26
|
-
* 对齐 `base-path.ts` 的 stack.unshift 手法。
|
|
27
|
-
*
|
|
28
|
-
* 取舍:绕过 vite ⇒ 这些文件**无 vite 原生 HMR**,改动靠整页 reload / 手动刷。
|
|
29
|
-
*
|
|
30
|
-
* 安全边界:dev only,serve 的是开发者自己 `root` 下的源文件。做了路径穿越守卫(`..`),
|
|
31
|
-
* 但不解符号链接——`root` 内的 symlink 会被跟随。这是 dev server 的可接受假设。
|
|
32
|
-
*/
|
|
33
|
-
function rawServePlugin(opts) {
|
|
34
|
-
const exts = opts.exts ?? ['.jsx', '.js', '.css'];
|
|
35
|
-
const root = opts.root;
|
|
36
|
-
const baseWithoutSlash = opts.clientBasePath.replace(/\/+$/, '');
|
|
37
|
-
return {
|
|
38
|
-
name: 'miaoda-html-raw-serve',
|
|
39
|
-
apply: 'serve',
|
|
40
|
-
configureServer(server) {
|
|
41
|
-
const stack = server.middlewares.stack;
|
|
42
|
-
if (!Array.isArray(stack))
|
|
43
|
-
return;
|
|
44
|
-
stack.unshift({
|
|
45
|
-
route: '',
|
|
46
|
-
handle: async (req, res, next) => {
|
|
47
|
-
if (req.method && req.method !== 'GET' && req.method !== 'HEAD')
|
|
48
|
-
return next();
|
|
49
|
-
const url = req.url || '';
|
|
50
|
-
const qIdx = url.indexOf('?');
|
|
51
|
-
let pathname = qIdx >= 0 ? url.slice(0, qIdx) : url;
|
|
52
|
-
// 先剥沙箱网关透传的 `/app/<id>` 前缀——内部路径判断要在剥前缀后做,
|
|
53
|
-
// 否则带前缀的 vite 内部请求(如 `/app/<id>/@ws-watchdog.js`)逃过下面的守卫。
|
|
54
|
-
if (baseWithoutSlash && pathname.startsWith(baseWithoutSlash + '/')) {
|
|
55
|
-
pathname = pathname.slice(baseWithoutSlash.length);
|
|
56
|
-
}
|
|
57
|
-
// vite 内部请求(/@vite/client、/@fs/、/@id/)和依赖目录不碰,交还 vite。
|
|
58
|
-
if (pathname.startsWith('/@') || pathname.includes('/node_modules/') || pathname.includes('/.vite/')) {
|
|
59
|
-
return next();
|
|
60
|
-
}
|
|
61
|
-
const ext = node_path_1.default.extname(pathname).toLowerCase();
|
|
62
|
-
if (!exts.includes(ext))
|
|
63
|
-
return next();
|
|
64
|
-
let raw;
|
|
65
|
-
try {
|
|
66
|
-
// decodeURIComponent 遇畸形 `%` 会抛 URIError,必须连同 fs 一起兜进 try:
|
|
67
|
-
// 这是 async 中间件,未捕获会变成 unhandledRejection(请求挂死 / 进程崩)。
|
|
68
|
-
const abs = node_path_1.default.join(root, decodeURIComponent(pathname));
|
|
69
|
-
// 防目录穿越:解析结果必须仍在 root 内。
|
|
70
|
-
if (abs !== root && !abs.startsWith(root + node_path_1.default.sep))
|
|
71
|
-
return next();
|
|
72
|
-
const stat = await promises_1.default.stat(abs);
|
|
73
|
-
if (!stat.isFile())
|
|
74
|
-
return next();
|
|
75
|
-
raw = await promises_1.default.readFile(abs);
|
|
76
|
-
}
|
|
77
|
-
catch {
|
|
78
|
-
return next(); // 畸形 URL / 不存在 / 读失败 → 交还 vite 走正常 404
|
|
79
|
-
}
|
|
80
|
-
res.setHeader('Content-Type', CONTENT_TYPE[ext] ?? 'application/octet-stream');
|
|
81
|
-
res.setHeader('Cache-Control', 'no-cache');
|
|
82
|
-
res.statusCode = 200;
|
|
83
|
-
if (req.method === 'HEAD') {
|
|
84
|
-
res.end();
|
|
85
|
-
}
|
|
86
|
-
else {
|
|
87
|
-
res.end(raw);
|
|
88
|
-
}
|
|
89
|
-
},
|
|
90
|
-
});
|
|
91
|
-
},
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
exports.default = rawServePlugin;
|