更新
This commit is contained in:
+4
@@ -0,0 +1,4 @@
|
||||
import type { Plugin, ResolvedConfig } from 'vite';
|
||||
export declare function createConfig(options: {
|
||||
resolvedConfig: ResolvedConfig | null;
|
||||
}): Plugin['config'];
|
||||
+113
@@ -0,0 +1,113 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.createConfig = void 0;
|
||||
const os_1 = __importDefault(require("os"));
|
||||
const fs_1 = __importDefault(require("fs"));
|
||||
const path_1 = __importDefault(require("path"));
|
||||
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
||||
const utils_1 = require("../utils");
|
||||
const esbuildPrePlugin_1 = require("./esbuild/esbuildPrePlugin");
|
||||
const ssr_1 = require("./configureServer/ssr");
|
||||
const shared_1 = require("@vue/shared");
|
||||
function createConfig(options) {
|
||||
return function config(config, env) {
|
||||
const inputDir = process.env.UNI_INPUT_DIR;
|
||||
if ((0, uni_cli_shared_1.isInHBuilderX)()) {
|
||||
if (!fs_1.default.existsSync(path_1.default.resolve(inputDir, 'index.html'))) {
|
||||
console.error(`请确认您的项目模板是否支持vue3:根目录缺少 index.html`);
|
||||
process.exit();
|
||||
}
|
||||
}
|
||||
const server = {
|
||||
host: true,
|
||||
hmr: process.env.UNI_AUTOMATOR_WS_ENDPOINT
|
||||
? false
|
||||
: {
|
||||
// mac 内置浏览器版本较低不支持 globalThis,而 overlay 使用了 globalThis
|
||||
overlay: os_1.default.platform() !== 'win32'
|
||||
? process.env.UNI_H5_BROWSER !== 'builtin'
|
||||
: true,
|
||||
},
|
||||
fs: { strict: false },
|
||||
watch: {
|
||||
ignored: [
|
||||
'**/uniCloud-aliyun/**',
|
||||
'**/uniCloud-tcb/**',
|
||||
'**/uniCloud-alipay/**',
|
||||
'**/uniCloud-dcloud/**',
|
||||
'**/uni_modules/uniCloud/**',
|
||||
'**/__snapshots__/**',
|
||||
(0, uni_cli_shared_1.normalizePath)(path_1.default.join(inputDir, 'unpackage/**')),
|
||||
(0, uni_cli_shared_1.normalizePath)(path_1.default.join(inputDir, 'dist/**')),
|
||||
],
|
||||
},
|
||||
...(0, uni_cli_shared_1.getDevServerOptions)((0, uni_cli_shared_1.parseManifestJsonOnce)(inputDir)),
|
||||
};
|
||||
if (server.port === '') {
|
||||
delete server.port;
|
||||
}
|
||||
const { server: userServer } = config;
|
||||
if (userServer) {
|
||||
if ((0, shared_1.hasOwn)(userServer, 'host')) {
|
||||
server.host = userServer.host;
|
||||
}
|
||||
if ((0, shared_1.hasOwn)(userServer, 'fs')) {
|
||||
(0, shared_1.extend)(server.fs, userServer.fs);
|
||||
}
|
||||
if ((0, shared_1.hasOwn)(userServer, 'watch')) {
|
||||
(0, shared_1.extend)(server.watch, userServer.watch);
|
||||
}
|
||||
}
|
||||
return {
|
||||
legacy: {
|
||||
// 目前先使用旧模式
|
||||
proxySsrExternalModules: true,
|
||||
},
|
||||
css: {
|
||||
postcss: {
|
||||
plugins: (0, uni_cli_shared_1.initPostcssPlugin)({
|
||||
uniApp: (0, uni_cli_shared_1.parseRpx2UnitOnce)(inputDir, process.env.UNI_PLATFORM),
|
||||
}),
|
||||
},
|
||||
},
|
||||
optimizeDeps: {
|
||||
entries: (0, uni_cli_shared_1.resolveMainPathOnce)(inputDir),
|
||||
exclude: ssr_1.external,
|
||||
esbuildOptions: {
|
||||
plugins: [(0, esbuildPrePlugin_1.esbuildPrePlugin)()],
|
||||
},
|
||||
},
|
||||
define: (0, utils_1.createDefine)(env.command, config),
|
||||
server,
|
||||
ssr: {
|
||||
external: ssr_1.external,
|
||||
},
|
||||
build: {
|
||||
target: process.env.UNI_APP_X === 'true'
|
||||
? ['es2015', 'edge79', 'firefox62', 'chrome64', 'safari11.1']
|
||||
: undefined,
|
||||
rollupOptions: {
|
||||
// resolveSSRExternal 会判定package.json,hbx 工程可能没有,通过 rollup 来配置
|
||||
external: (0, uni_cli_shared_1.isSsr)(env.command, config) ? ssr_1.external : [],
|
||||
output: {
|
||||
chunkFileNames(chunkInfo) {
|
||||
const { assetsDir } = options.resolvedConfig.build;
|
||||
if (chunkInfo.facadeModuleId) {
|
||||
const dirname = path_1.default.relative(inputDir, path_1.default.dirname(chunkInfo.facadeModuleId));
|
||||
if (dirname) {
|
||||
return path_1.default.posix.join(assetsDir, (0, uni_cli_shared_1.normalizePath)(dirname).replace(/\//g, '-') +
|
||||
'-[name].[hash].js');
|
||||
}
|
||||
}
|
||||
return path_1.default.posix.join(assetsDir, '[name].[hash].js');
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
};
|
||||
}
|
||||
exports.createConfig = createConfig;
|
||||
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;
|
||||
Generated
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
import type { Plugin } from 'esbuild';
|
||||
export declare const JS_TYPES_RE: RegExp;
|
||||
export declare function esbuildPrePlugin(): Plugin;
|
||||
Generated
Vendored
+33
@@ -0,0 +1,33 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.esbuildPrePlugin = exports.JS_TYPES_RE = void 0;
|
||||
const fs_1 = __importDefault(require("fs"));
|
||||
const path_1 = __importDefault(require("path"));
|
||||
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
||||
exports.JS_TYPES_RE = /\.(?:j|t)sx?$|\.mjs$/;
|
||||
function esbuildPrePlugin() {
|
||||
return {
|
||||
name: 'uni:dep-scan',
|
||||
setup(build) {
|
||||
build.onLoad({ filter: exports.JS_TYPES_RE }, ({ path: id }) => {
|
||||
let ext = path_1.default.extname(id).slice(1);
|
||||
if (ext === 'mjs')
|
||||
ext = 'js';
|
||||
if (fs_1.default.existsSync(id)) {
|
||||
let contents = fs_1.default.readFileSync(id, 'utf-8');
|
||||
if (contents.includes('#endif')) {
|
||||
contents = (0, uni_cli_shared_1.preJs)(contents, id);
|
||||
}
|
||||
return {
|
||||
loader: ext,
|
||||
contents,
|
||||
};
|
||||
}
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
exports.esbuildPrePlugin = esbuildPrePlugin;
|
||||
Generated
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
import type { Plugin } from 'vite';
|
||||
export declare function createHandleHotUpdate(): Plugin['handleHotUpdate'];
|
||||
Generated
Vendored
+87
@@ -0,0 +1,87 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.createHandleHotUpdate = void 0;
|
||||
const path_1 = __importDefault(require("path"));
|
||||
const debug_1 = __importDefault(require("debug"));
|
||||
const shared_1 = require("@vue/shared");
|
||||
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
||||
const debugHmr = (0, debug_1.default)('uni:hmr');
|
||||
async function invalidate(file, moduleGraph) {
|
||||
const mods = await moduleGraph.getModulesByFile((0, uni_cli_shared_1.normalizePath)(file));
|
||||
if (mods && mods.size) {
|
||||
;
|
||||
[...mods].forEach((mod) => {
|
||||
debugHmr('invalidate', mod.id);
|
||||
moduleGraph.invalidateModule(mod);
|
||||
});
|
||||
}
|
||||
}
|
||||
let invalidateFiles;
|
||||
function createHandleHotUpdate() {
|
||||
return async function ({ file, server }) {
|
||||
const inputDir = process.env.UNI_INPUT_DIR;
|
||||
const platform = process.env.UNI_PLATFORM;
|
||||
if (!invalidateFiles) {
|
||||
invalidateFiles = [
|
||||
path_1.default.resolve(inputDir, uni_cli_shared_1.PAGES_JSON_JS),
|
||||
path_1.default.resolve(inputDir, uni_cli_shared_1.MANIFEST_JSON_JS),
|
||||
(0, uni_cli_shared_1.resolveBuiltIn)('@dcloudio/uni-h5/' +
|
||||
(process.env.UNI_APP_X === 'true' ? 'dist-x' : 'dist') +
|
||||
'/uni-h5.es.js'),
|
||||
];
|
||||
try {
|
||||
invalidateFiles.push((0, uni_cli_shared_1.resolveBuiltIn)('vite/dist/client/env.mjs'));
|
||||
}
|
||||
catch (e) { }
|
||||
}
|
||||
// TODO 目前简单处理,当pages.json,manifest.json发生变化,就直接刷新,理想情况下,应该区分变化的内容,仅必要时做整页面刷新
|
||||
const isPagesJson = file.endsWith('pages.json');
|
||||
const isManifestJson = file.endsWith('manifest.json');
|
||||
if (!isPagesJson && !isManifestJson) {
|
||||
return;
|
||||
}
|
||||
debugHmr(file);
|
||||
const pagesJson = (0, uni_cli_shared_1.parsePagesJson)(inputDir, platform);
|
||||
// 更新define
|
||||
const { define, server: { middlewareMode }, } = server.config;
|
||||
(0, shared_1.extend)(define, (0, uni_cli_shared_1.initFeatures)({
|
||||
inputDir,
|
||||
command: 'serve',
|
||||
platform,
|
||||
pagesJson,
|
||||
manifestJson: (0, uni_cli_shared_1.parseManifestJson)(inputDir),
|
||||
ssr: !!middlewareMode,
|
||||
}));
|
||||
debugHmr('define', define);
|
||||
if (isPagesJson) {
|
||||
const easycom = pagesJson.easycom || {};
|
||||
const { easyComOptions, refresh } = (0, uni_cli_shared_1.initEasycomsOnce)(inputDir, {
|
||||
dirs: (0, uni_cli_shared_1.resolveComponentsLibDirs)(),
|
||||
platform,
|
||||
isX: process.env.UNI_APP_X === 'true',
|
||||
});
|
||||
if (!equal({ autoscan: easycom.autoscan, custom: easycom.custom }, {
|
||||
autoscan: easyComOptions.autoscan,
|
||||
custom: easyComOptions.custom,
|
||||
})) {
|
||||
refresh();
|
||||
}
|
||||
}
|
||||
// 当pages.json,manifest.json发生变化时,作废pages.json.js缓存
|
||||
for (const file of invalidateFiles) {
|
||||
await invalidate(file, server.moduleGraph);
|
||||
}
|
||||
server.ws.send({
|
||||
type: 'full-reload',
|
||||
path: '*',
|
||||
});
|
||||
return [];
|
||||
};
|
||||
}
|
||||
exports.createHandleHotUpdate = createHandleHotUpdate;
|
||||
function equal(obj1, obj2) {
|
||||
return JSON.stringify(obj1) === JSON.stringify(obj2);
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
import type { UniVitePlugin } from '@dcloudio/uni-cli-shared';
|
||||
export declare function uniH5Plugin(): UniVitePlugin;
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.uniH5Plugin = void 0;
|
||||
const handleHotUpdate_1 = require("./handleHotUpdate");
|
||||
const transformIndexHtml_1 = require("./transformIndexHtml");
|
||||
const configureServer_1 = require("./configureServer");
|
||||
const uni_1 = require("./uni");
|
||||
const config_1 = require("./config");
|
||||
function uniH5Plugin() {
|
||||
const configOptions = {
|
||||
resolvedConfig: null,
|
||||
};
|
||||
return {
|
||||
name: 'uni:h5',
|
||||
uni: (0, uni_1.createUni)(),
|
||||
config: (0, config_1.createConfig)(configOptions),
|
||||
configResolved(config) {
|
||||
configOptions.resolvedConfig = config;
|
||||
},
|
||||
configureServer: (0, configureServer_1.createConfigureServer)(),
|
||||
handleHotUpdate: (0, handleHotUpdate_1.createHandleHotUpdate)(),
|
||||
transformIndexHtml: (0, transformIndexHtml_1.createTransformIndexHtml)(),
|
||||
};
|
||||
}
|
||||
exports.uniH5Plugin = uniH5Plugin;
|
||||
Generated
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
import type { Plugin } from 'vite';
|
||||
export declare function createTransformIndexHtml(): Plugin['transformIndexHtml'];
|
||||
Generated
Vendored
+48
@@ -0,0 +1,48 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.createTransformIndexHtml = void 0;
|
||||
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
||||
function createTransformIndexHtml() {
|
||||
let warned = false;
|
||||
return async function (html) {
|
||||
const manifestJson = (0, uni_cli_shared_1.parseManifestJsonOnce)(process.env.UNI_INPUT_DIR);
|
||||
const webManifest = (0, uni_cli_shared_1.getPlatformManifestJson)(manifestJson, 'h5');
|
||||
const title = webManifest?.title || manifestJson.name || '';
|
||||
const isX = process.env.UNI_APP_X === 'true';
|
||||
if (isX) {
|
||||
// 兼容旧版本模板
|
||||
const mainJs = ` src="/main.js"`;
|
||||
const mainTs = ` src="/main.ts"`;
|
||||
const main = ` src="/main"`;
|
||||
let oldMain = '';
|
||||
if (html.includes(mainJs)) {
|
||||
oldMain = mainJs;
|
||||
}
|
||||
else if (html.includes(mainTs)) {
|
||||
oldMain = mainTs;
|
||||
}
|
||||
if (oldMain) {
|
||||
html = html.replace(oldMain, main);
|
||||
if (!warned) {
|
||||
warned = true;
|
||||
console.warn(`当前项目根目录 index.html 未兼容 uni-app x 的 web 平台,请将里边的${oldMain} 调整为${main}。`);
|
||||
}
|
||||
}
|
||||
}
|
||||
return {
|
||||
html: html.replace(/<title>(.*?)<\/title>/, `<title>${title}</title>`),
|
||||
tags: process.env.NODE_ENV === 'development'
|
||||
? [
|
||||
{
|
||||
tag: 'script',
|
||||
children: `if (typeof globalThis === 'undefined') {
|
||||
window.globalThis = window
|
||||
}`,
|
||||
injectTo: 'head-prepend',
|
||||
},
|
||||
]
|
||||
: [],
|
||||
};
|
||||
};
|
||||
}
|
||||
exports.createTransformIndexHtml = createTransformIndexHtml;
|
||||
Generated
Vendored
+2
@@ -0,0 +1,2 @@
|
||||
import type { NodeTransform } from '@vue/compiler-core';
|
||||
export declare const transformPageHead: NodeTransform;
|
||||
Generated
Vendored
+22
@@ -0,0 +1,22 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.transformPageHead = void 0;
|
||||
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
||||
const compiler_core_1 = require("@vue/compiler-core");
|
||||
const transformPageHead = (node, context) => {
|
||||
if ((0, uni_cli_shared_1.checkElementNodeTag)(node, 'page-meta')) {
|
||||
const headNode = node.children.find((child) => (0, uni_cli_shared_1.checkElementNodeTag)(child, 'head'));
|
||||
if (headNode) {
|
||||
headNode.tag = 'page-meta-head';
|
||||
headNode.tagType = compiler_core_1.ElementTypes.COMPONENT;
|
||||
}
|
||||
return;
|
||||
}
|
||||
if ((0, uni_cli_shared_1.checkElementNodeTag)(node, 'head') &&
|
||||
(0, uni_cli_shared_1.checkElementNodeTag)(context.parent, 'page-meta')) {
|
||||
;
|
||||
node.tag = 'page-meta-head';
|
||||
node.tagType = compiler_core_1.ElementTypes.COMPONENT;
|
||||
}
|
||||
};
|
||||
exports.transformPageHead = transformPageHead;
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
import { type UniVitePlugin } from '@dcloudio/uni-cli-shared';
|
||||
import type { CompilerOptions } from '@vue/compiler-core';
|
||||
export declare const compilerOptions: CompilerOptions;
|
||||
export declare function createUni(): UniVitePlugin['uni'];
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.createUni = exports.compilerOptions = void 0;
|
||||
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
||||
const uni_shared_1 = require("@dcloudio/uni-shared");
|
||||
function realIsH5CustomElement(tag) {
|
||||
return (0, uni_shared_1.isH5CustomElement)(tag, process.env.UNI_APP_X === 'true');
|
||||
}
|
||||
exports.compilerOptions = {
|
||||
isNativeTag: uni_shared_1.isH5NativeTag,
|
||||
isCustomElement: realIsH5CustomElement,
|
||||
nodeTransforms: [
|
||||
uni_cli_shared_1.transformRefresherSlot,
|
||||
uni_cli_shared_1.transformH5BuiltInComponents,
|
||||
uni_cli_shared_1.transformTapToClick,
|
||||
uni_cli_shared_1.transformMatchMedia,
|
||||
uni_cli_shared_1.transformPageHead,
|
||||
],
|
||||
};
|
||||
function createUni() {
|
||||
return {
|
||||
copyOptions: {
|
||||
assets: ['hybrid/html/**/*', 'uni_modules/*/hybrid/html/**/*'],
|
||||
},
|
||||
compilerOptions: exports.compilerOptions,
|
||||
jsxOptions: {
|
||||
babelPlugins: [uni_cli_shared_1.transformUniH5Jsx],
|
||||
},
|
||||
};
|
||||
}
|
||||
exports.createUni = createUni;
|
||||
Reference in New Issue
Block a user