更新
This commit is contained in:
Generated
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
import type { Plugin } from 'vite';
|
||||
export declare function createConfigureServer(): Plugin['configureServer'];
|
||||
Generated
Vendored
+20
@@ -0,0 +1,20 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.createConfigureServer = void 0;
|
||||
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
||||
const timestamp_1 = require("./middlewares/timestamp");
|
||||
const ssr_1 = require("./ssr");
|
||||
const static_1 = require("./static");
|
||||
function createConfigureServer() {
|
||||
return function (server) {
|
||||
(0, ssr_1.initSSR)(server);
|
||||
const routerOptions = (0, uni_cli_shared_1.getRouterOptions)((0, uni_cli_shared_1.parseManifestJsonOnce)(process.env.UNI_INPUT_DIR));
|
||||
if (routerOptions.mode === 'history') {
|
||||
server.middlewares.use((0, timestamp_1.uniTimestampMiddleware)(server));
|
||||
}
|
||||
return () => {
|
||||
(0, static_1.initStatic)(server);
|
||||
};
|
||||
};
|
||||
}
|
||||
exports.createConfigureServer = createConfigureServer;
|
||||
Generated
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
/// <reference types="node" />
|
||||
import type { IncomingMessage, ServerResponse } from 'http';
|
||||
interface UniStaticMiddlewareOptions {
|
||||
etag: boolean;
|
||||
resolve: (pathname: string) => string | void;
|
||||
}
|
||||
export type NextHandler = () => void | Promise<void>;
|
||||
export declare function uniStaticMiddleware(opts: UniStaticMiddlewareOptions): (req: IncomingMessage, res: ServerResponse, next: NextHandler) => void | Promise<void> | ServerResponse<IncomingMessage>;
|
||||
export {};
|
||||
Generated
Vendored
+80
@@ -0,0 +1,80 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.uniStaticMiddleware = void 0;
|
||||
const fs_1 = __importDefault(require("fs"));
|
||||
const url_1 = __importDefault(require("url"));
|
||||
const lite_1 = __importDefault(require("mime/lite"));
|
||||
function normalizeFile(filename, isEtag) {
|
||||
const stats = fs_1.default.statSync(filename);
|
||||
return {
|
||||
stats,
|
||||
headers: normalizeHeaders(filename, stats, isEtag),
|
||||
};
|
||||
}
|
||||
function normalizeHeaders(filename, stats, isEtag) {
|
||||
const headers = {
|
||||
'Content-Length': stats.size,
|
||||
'Content-Type': lite_1.default.getType(filename) || '',
|
||||
'Last-Modified': stats.mtime.toUTCString(),
|
||||
};
|
||||
if (isEtag) {
|
||||
headers['ETag'] = `W/"${stats.size}-${stats.mtime.getTime()}"`;
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
function send(req, res, filename, stats, headers) {
|
||||
let code = 200;
|
||||
headers = { ...headers };
|
||||
const opts = {};
|
||||
for (const key in headers) {
|
||||
const value = res.getHeader(key);
|
||||
if (value) {
|
||||
headers[key] = value;
|
||||
}
|
||||
}
|
||||
if (res.getHeader('content-type')) {
|
||||
headers['Content-Type'] = res.getHeader('content-type');
|
||||
}
|
||||
if (req.headers.range) {
|
||||
code = 206;
|
||||
const [x, y] = req.headers.range.replace('bytes=', '').split('-');
|
||||
const end = (opts.end = parseInt(y, 10) || stats.size - 1);
|
||||
const start = (opts.start = parseInt(x, 10) || 0);
|
||||
if (start >= stats.size || end >= stats.size) {
|
||||
res.setHeader('Content-Range', `bytes */${stats.size}`);
|
||||
res.statusCode = 416;
|
||||
return res.end();
|
||||
}
|
||||
headers['Content-Range'] = `bytes ${start}-${end}/${stats.size}`;
|
||||
headers['Content-Length'] = end - start + 1;
|
||||
headers['Accept-Ranges'] = 'bytes';
|
||||
}
|
||||
res.writeHead(code, headers);
|
||||
fs_1.default.createReadStream(filename, opts).pipe(res);
|
||||
}
|
||||
function uniStaticMiddleware(opts) {
|
||||
const isEtag = !!opts.etag;
|
||||
return function staticMiddleware(req, res, next) {
|
||||
const pathname = url_1.default.parse(req.url).pathname;
|
||||
if (!pathname) {
|
||||
return next();
|
||||
}
|
||||
const filename = opts.resolve(pathname);
|
||||
if (!filename) {
|
||||
return next();
|
||||
}
|
||||
const data = normalizeFile(filename, isEtag);
|
||||
if (!data) {
|
||||
return next();
|
||||
}
|
||||
if (isEtag && req.headers['if-none-match'] === data.headers['ETag']) {
|
||||
res.writeHead(304);
|
||||
return res.end();
|
||||
}
|
||||
return send(req, res, filename, data.stats, data.headers);
|
||||
};
|
||||
}
|
||||
exports.uniStaticMiddleware = uniStaticMiddleware;
|
||||
Generated
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
/// <reference types="node" />
|
||||
import type { IncomingMessage, ServerResponse } from 'http';
|
||||
import type { ViteDevServer } from 'vite';
|
||||
import type { NextHandler } from './static';
|
||||
export declare function uniTimestampMiddleware(server: ViteDevServer): (req: IncomingMessage, _: ServerResponse, next: NextHandler) => Promise<void>;
|
||||
TongjiUniApp/node_modules/@dcloudio/uni-h5-vite/dist/plugin/configureServer/middlewares/timestamp.js
Generated
Vendored
+36
@@ -0,0 +1,36 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.uniTimestampMiddleware = void 0;
|
||||
const url_1 = require("url");
|
||||
const path_1 = __importDefault(require("path"));
|
||||
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
||||
function uniTimestampMiddleware(server) {
|
||||
return async function timestampMiddleware(req, _, next) {
|
||||
// 当页面被作为组件引用时,会导致history刷新该页面直接显示js代码,因为该页面已被缓存为了module,
|
||||
// https://github.com/vitejs/vite/blob/702d50315535c189151c67d33e4a22124f926bed/packages/vite/src/node/server/transformRequest.ts#L52
|
||||
// /pages/tabBar/API/API
|
||||
let { url } = req;
|
||||
if (url) {
|
||||
const base = server.config.base;
|
||||
const parsed = (0, url_1.parse)(url);
|
||||
let newUrl = url;
|
||||
if ((parsed.pathname || '/').startsWith(base)) {
|
||||
newUrl = newUrl.replace(base, '/');
|
||||
}
|
||||
if (!path_1.default.extname(newUrl) &&
|
||||
!newUrl.endsWith('/') &&
|
||||
!newUrl.includes('?')) {
|
||||
const module = await server.moduleGraph.getModuleByUrl(newUrl);
|
||||
if (module && module.file && uni_cli_shared_1.EXTNAME_VUE_RE.test(module.file)) {
|
||||
// /pages/tabBar/API/API => /pages/tabBar/API/API?__t__=time
|
||||
req.url = url + '?__t__=' + Date.now();
|
||||
}
|
||||
}
|
||||
}
|
||||
next();
|
||||
};
|
||||
}
|
||||
exports.uniTimestampMiddleware = uniTimestampMiddleware;
|
||||
Generated
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
import type { ViteDevServer } from 'vite';
|
||||
export declare const external: string[];
|
||||
export declare function initSSR(server: ViteDevServer): void;
|
||||
Generated
Vendored
+57
@@ -0,0 +1,57 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.initSSR = exports.external = void 0;
|
||||
exports.external = [
|
||||
'@dcloudio/uni-app',
|
||||
'@dcloudio/uni-app-plus',
|
||||
'@dcloudio/uni-cloud',
|
||||
'@dcloudio/uni-components',
|
||||
'@dcloudio/uni-h5',
|
||||
'@dcloudio/uni-h5-vue',
|
||||
'@dcloudio/uni-i18n',
|
||||
'@dcloudio/uni-mp-alipay',
|
||||
'@dcloudio/uni-mp-baidu',
|
||||
'@dcloudio/uni-mp-kuaishou',
|
||||
'@dcloudio/uni-mp-lark',
|
||||
'@dcloudio/uni-mp-qq',
|
||||
'@dcloudio/uni-mp-toutiao',
|
||||
'@dcloudio/uni-mp-weixin',
|
||||
'@dcloudio/uni-quickapp-webview',
|
||||
'@dcloudio/uni-shared',
|
||||
'@dcloudio/uni-stat',
|
||||
'@dcloudio/uni-stacktracey',
|
||||
'@vue/shared',
|
||||
'vue',
|
||||
'vue-i18n',
|
||||
'vue-router',
|
||||
'vuex',
|
||||
// dev
|
||||
'@dcloudio/types',
|
||||
'@dcloudio/uni-automator',
|
||||
'@dcloudio/uni-cli-shared',
|
||||
'@dcloudio/vite-plugin-uni',
|
||||
'autoprefixer',
|
||||
'typescript',
|
||||
'vite',
|
||||
];
|
||||
function initSSR(server) {
|
||||
const { ssrLoadModule } = server;
|
||||
let added = false;
|
||||
server.ssrLoadModule = (url) => {
|
||||
const res = ssrLoadModule(url);
|
||||
if (!added) {
|
||||
// HBuilderX项目,根目录可能没有package.json,导致 ssrExternals 不生效
|
||||
added = true;
|
||||
if (server._ssrExternals) {
|
||||
const { _ssrExternals } = server;
|
||||
exports.external.forEach((module) => {
|
||||
if (!_ssrExternals.includes(module)) {
|
||||
_ssrExternals.push(module);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
return res;
|
||||
};
|
||||
}
|
||||
exports.initSSR = initSSR;
|
||||
Generated
Vendored
+8
@@ -0,0 +1,8 @@
|
||||
import type { ViteDevServer } from 'vite';
|
||||
/**
|
||||
* devServer时提供static等目录的静态资源服务
|
||||
* @param server
|
||||
* @param param
|
||||
*/
|
||||
export declare const initStatic: (server: ViteDevServer) => void;
|
||||
export declare function createPublicFileFilter(base?: string): (id: unknown) => boolean;
|
||||
Generated
Vendored
+59
@@ -0,0 +1,59 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.createPublicFileFilter = exports.initStatic = void 0;
|
||||
const fs_1 = __importDefault(require("fs"));
|
||||
const path_1 = __importDefault(require("path"));
|
||||
const debug_1 = __importDefault(require("debug"));
|
||||
const pluginutils_1 = require("@rollup/pluginutils");
|
||||
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
||||
const static_1 = require("./middlewares/static");
|
||||
const debugStatic = (0, debug_1.default)('uni:static');
|
||||
/**
|
||||
* devServer时提供static等目录的静态资源服务
|
||||
* @param server
|
||||
* @param param
|
||||
*/
|
||||
const initStatic = (server) => {
|
||||
const filter = createPublicFileFilter();
|
||||
const serve = (0, static_1.uniStaticMiddleware)({
|
||||
etag: true,
|
||||
resolve(pathname) {
|
||||
if (!filter(pathname)) {
|
||||
return;
|
||||
}
|
||||
const filename = path_1.default.join(process.env.UNI_INPUT_DIR, pathname);
|
||||
if (fs_1.default.existsSync(filename)) {
|
||||
debugStatic(filename, 'success');
|
||||
return filename;
|
||||
}
|
||||
else {
|
||||
debugStatic(filename, 'fail');
|
||||
}
|
||||
},
|
||||
});
|
||||
const viteServePublicMiddlewareIndex = server.middlewares.stack.findIndex((middleware) => {
|
||||
return (middleware.handle.name === 'viteServePublicMiddleware');
|
||||
});
|
||||
// 替换 vite 自带的 public middleware
|
||||
if (viteServePublicMiddlewareIndex > -1) {
|
||||
server.middlewares.stack.splice(viteServePublicMiddlewareIndex, 1, {
|
||||
route: '',
|
||||
handle: ((req, res, next) => {
|
||||
if ((0, uni_cli_shared_1.isImportRequest)(req.url) || (0, uni_cli_shared_1.isInternalRequest)(req.url)) {
|
||||
return next();
|
||||
}
|
||||
return serve(req, res, next);
|
||||
}),
|
||||
});
|
||||
}
|
||||
};
|
||||
exports.initStatic = initStatic;
|
||||
function createPublicFileFilter(base = '/') {
|
||||
const publicDir = (0, uni_cli_shared_1.normalizePath)(path_1.default.join(base, uni_cli_shared_1.PUBLIC_DIR + '/**/*'));
|
||||
const uniModulesDir = (0, uni_cli_shared_1.normalizePath)(path_1.default.join(base, 'uni_modules/*/' + uni_cli_shared_1.PUBLIC_DIR + '/**/*'));
|
||||
return (0, pluginutils_1.createFilter)([publicDir, uniModulesDir]);
|
||||
}
|
||||
exports.createPublicFileFilter = createPublicFileFilter;
|
||||
Reference in New Issue
Block a user