更新
This commit is contained in:
+5
@@ -0,0 +1,5 @@
|
||||
import { type BuildOptions, type ServerOptions } from 'vite';
|
||||
import type { CliOptions } from '.';
|
||||
export declare function runDev(options: CliOptions & ServerOptions): Promise<undefined>;
|
||||
export declare function runBuild(options: CliOptions & BuildOptions): Promise<void>;
|
||||
export declare const stopProfiler: (log: (message: string) => void) => void | Promise<void>;
|
||||
+206
@@ -0,0 +1,206 @@
|
||||
"use strict";
|
||||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
var desc = Object.getOwnPropertyDescriptor(m, k);
|
||||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
||||
desc = { enumerable: true, get: function() { return m[k]; } };
|
||||
}
|
||||
Object.defineProperty(o, k2, desc);
|
||||
}) : (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
o[k2] = m[k];
|
||||
}));
|
||||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
||||
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
||||
}) : function(o, v) {
|
||||
o["default"] = v;
|
||||
});
|
||||
var __importStar = (this && this.__importStar) || function (mod) {
|
||||
if (mod && mod.__esModule) return mod;
|
||||
var result = {};
|
||||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
||||
__setModuleDefault(result, mod);
|
||||
return result;
|
||||
};
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.stopProfiler = exports.runBuild = exports.runDev = void 0;
|
||||
const fs_1 = __importDefault(require("fs"));
|
||||
const path_1 = __importDefault(require("path"));
|
||||
const picocolors_1 = __importDefault(require("picocolors"));
|
||||
const shared_1 = require("@vue/shared");
|
||||
const vite_1 = require("vite");
|
||||
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
||||
const build_1 = require("./build");
|
||||
const server_1 = require("./server");
|
||||
const utils_1 = require("./utils");
|
||||
const easycom_1 = require("../utils/easycom");
|
||||
const uvue_1 = require("./uvue");
|
||||
const configResolved_1 = require("../configResolved");
|
||||
async function runDev(options) {
|
||||
(0, shared_1.extend)(options, {
|
||||
watch: {},
|
||||
});
|
||||
if (process.env.UNI_MINIMIZE === 'true') {
|
||||
;
|
||||
options.minify = true;
|
||||
}
|
||||
(0, utils_1.initEnv)('dev', options);
|
||||
if (process.env.UNI_APP_X === 'true' &&
|
||||
process.env.UNI_UTS_PLATFORM === 'app-android') {
|
||||
return (0, uvue_1.runUVueAndroidDev)(options);
|
||||
}
|
||||
const createLogger = await Promise.resolve().then(() => __importStar(require('vite'))).then(({ createLogger }) => createLogger);
|
||||
try {
|
||||
if (options.platform === 'h5') {
|
||||
const server = await (options.ssr
|
||||
? (0, server_1.createSSRServer)(options)
|
||||
: (0, server_1.createServer)(options));
|
||||
(0, easycom_1.initEasycom)(server.watcher);
|
||||
}
|
||||
else {
|
||||
const watcher = (await (0, build_1.build)(options));
|
||||
(0, easycom_1.initEasycom)();
|
||||
let isFirstStart = true;
|
||||
let isFirstEnd = true;
|
||||
watcher.on('event', async (event) => {
|
||||
if (event.code === 'BUNDLE_START') {
|
||||
if (isFirstStart) {
|
||||
isFirstStart = false;
|
||||
return;
|
||||
}
|
||||
(0, uni_cli_shared_1.output)('log', uni_cli_shared_1.M['dev.watching.start']);
|
||||
}
|
||||
else if (event.code === 'BUNDLE_END') {
|
||||
event.result.close();
|
||||
if (isFirstEnd) {
|
||||
// 首次全量同步
|
||||
if (options.platform === 'app' ||
|
||||
options.platform === 'app-harmony') {
|
||||
process.env.UNI_APP_CHANGED_FILES = '';
|
||||
}
|
||||
if (options.platform === 'app') {
|
||||
process.env.UNI_APP_CHANGED_PAGES = '';
|
||||
process.env.UNI_APP_UTS_CHANGED_FILES = '';
|
||||
}
|
||||
isFirstEnd = false;
|
||||
(0, uni_cli_shared_1.output)('log', uni_cli_shared_1.M['dev.watching.end']);
|
||||
(0, utils_1.showRunPrompt)(options.platform);
|
||||
(0, utils_1.printStartupDuration)(createLogger(options.logLevel), false);
|
||||
if (process.env.UNI_UTS_ERRORS) {
|
||||
console.error(process.env.UNI_UTS_ERRORS);
|
||||
}
|
||||
if (process.env.UNI_UTS_TIPS) {
|
||||
console.warn(process.env.UNI_UTS_TIPS);
|
||||
}
|
||||
await (0, exports.stopProfiler)((message) => createLogger(options.logLevel).info(message));
|
||||
return;
|
||||
}
|
||||
if (options.platform === 'app') {
|
||||
const files = process.env.UNI_APP_CHANGED_FILES;
|
||||
const pages = process.env.UNI_APP_CHANGED_PAGES;
|
||||
const dex = process.env.UNI_APP_UTS_CHANGED_FILES;
|
||||
const changedFiles = pages || files;
|
||||
process.env.UNI_APP_CHANGED_PAGES = '';
|
||||
process.env.UNI_APP_CHANGED_FILES = '';
|
||||
process.env.UNI_APP_UTS_CHANGED_FILES = '';
|
||||
if ((changedFiles &&
|
||||
!changedFiles.includes(uni_cli_shared_1.APP_CONFIG_SERVICE) &&
|
||||
!changedFiles.includes(uni_cli_shared_1.APP_SERVICE_FILENAME)) ||
|
||||
dex) {
|
||||
if (pages) {
|
||||
return (0, uni_cli_shared_1.output)('log', uni_cli_shared_1.M['dev.watching.end.pages'].replace('{pages}', changedFiles));
|
||||
}
|
||||
return (0, uni_cli_shared_1.output)('log', uni_cli_shared_1.M['dev.watching.end.files'].replace('{files}', JSON.stringify(JSON.parse(changedFiles || JSON.stringify([])).concat(JSON.parse(dex || JSON.stringify([]))))));
|
||||
}
|
||||
}
|
||||
else if (options.platform === 'app-harmony') {
|
||||
const files = process.env.UNI_APP_CHANGED_FILES;
|
||||
if (files) {
|
||||
return (0, uni_cli_shared_1.output)('log', uni_cli_shared_1.M['dev.watching.end.files'].replace('{files}', files));
|
||||
}
|
||||
}
|
||||
return (0, uni_cli_shared_1.output)('log', uni_cli_shared_1.M['dev.watching.end']);
|
||||
}
|
||||
else if (event.code === 'END') {
|
||||
if (process.env.UNI_AUTOMATOR_WS_ENDPOINT) {
|
||||
setTimeout(() => {
|
||||
(0, uni_cli_shared_1.output)('log', uni_cli_shared_1.M['build.failed']);
|
||||
process.exit(0);
|
||||
}, 2000);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
if (options.platform === 'h5') {
|
||||
console.error(`error when starting dev server:\n${e.stack || e}`);
|
||||
}
|
||||
else {
|
||||
console.error(`error during build:\n${e.stack || e}`);
|
||||
}
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
exports.runDev = runDev;
|
||||
async function runBuild(options) {
|
||||
(0, utils_1.initEnv)('build', options);
|
||||
if (process.env.UNI_APP_X === 'true' &&
|
||||
process.env.UNI_UTS_PLATFORM === 'app-android') {
|
||||
return (0, uvue_1.runUVueAndroidBuild)(options);
|
||||
}
|
||||
try {
|
||||
await (options.ssr && options.platform === 'h5'
|
||||
? (0, build_1.buildSSR)(options)
|
||||
: (0, build_1.build)(options));
|
||||
await (0, exports.stopProfiler)((message) => (0, vite_1.createLogger)(options.logLevel).info(message));
|
||||
console.log(uni_cli_shared_1.M['build.done']);
|
||||
if (options.platform !== 'h5') {
|
||||
(0, utils_1.showRunPrompt)(options.platform);
|
||||
}
|
||||
// 开发者可能用了三方插件,三方插件有可能阻止退出,导致HBuilderX打包状态识别不正确
|
||||
if ((0, uni_cli_shared_1.isInHBuilderX)() ||
|
||||
/* 需要排查为什么ext-api编译时没有自动结束 */ process.env
|
||||
.UNI_COMPILE_TARGET === 'ext-api') {
|
||||
process.exit(0);
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
if ((0, uni_cli_shared_1.isInHBuilderX)()) {
|
||||
(0, configResolved_1.initLogger)({
|
||||
logger: (0, vite_1.createLogger)(options.logLevel),
|
||||
}).error(e.message || e);
|
||||
}
|
||||
else {
|
||||
console.error(e.message || e);
|
||||
}
|
||||
console.error(`Build failed with errors.`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
exports.runBuild = runBuild;
|
||||
let profileSession = global.__vite_profile_session;
|
||||
let profileCount = 0;
|
||||
const stopProfiler = (log) => {
|
||||
if (!profileSession)
|
||||
return;
|
||||
return new Promise((res, rej) => {
|
||||
profileSession.post('Profiler.stop', (err, result) => {
|
||||
// Write profile to disk, upload, etc.
|
||||
if (!err) {
|
||||
const outPath = path_1.default.resolve(process.env.UNI_INPUT_DIR, `./profile-${profileCount++}.cpuprofile`);
|
||||
fs_1.default.writeFileSync(outPath, JSON.stringify(result.profile));
|
||||
log(picocolors_1.default.yellow(`CPU profile written to ${picocolors_1.default.white(picocolors_1.default.dim(outPath))}`));
|
||||
profileSession = undefined;
|
||||
res();
|
||||
}
|
||||
else {
|
||||
rej(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
exports.stopProfiler = stopProfiler;
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
import type { BuildOptions, InlineConfig } from 'vite';
|
||||
import type { CliOptions } from '.';
|
||||
import type { RollupWatcher } from 'rollup';
|
||||
export declare function buildByVite(inlineConfig: InlineConfig): Promise<import("rollup").RollupOutput | import("rollup").RollupOutput[] | RollupWatcher>;
|
||||
export declare function build(options: CliOptions): Promise<RollupWatcher | void>;
|
||||
export declare function buildSSR(options: CliOptions): Promise<void>;
|
||||
export declare function initBuildOptions(options: CliOptions, build: BuildOptions): InlineConfig;
|
||||
export declare function buildApp(options: CliOptions): Promise<RollupWatcher | void>;
|
||||
+204
@@ -0,0 +1,204 @@
|
||||
"use strict";
|
||||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
var desc = Object.getOwnPropertyDescriptor(m, k);
|
||||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
||||
desc = { enumerable: true, get: function() { return m[k]; } };
|
||||
}
|
||||
Object.defineProperty(o, k2, desc);
|
||||
}) : (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
o[k2] = m[k];
|
||||
}));
|
||||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
||||
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
||||
}) : function(o, v) {
|
||||
o["default"] = v;
|
||||
});
|
||||
var __importStar = (this && this.__importStar) || function (mod) {
|
||||
if (mod && mod.__esModule) return mod;
|
||||
var result = {};
|
||||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
||||
__setModuleDefault(result, mod);
|
||||
return result;
|
||||
};
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.buildApp = exports.initBuildOptions = exports.buildSSR = exports.build = exports.buildByVite = void 0;
|
||||
const path_1 = __importDefault(require("path"));
|
||||
const fs_extra_1 = __importDefault(require("fs-extra"));
|
||||
const shared_1 = require("@vue/shared");
|
||||
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
||||
const utils_1 = require("./utils");
|
||||
async function buildByVite(inlineConfig) {
|
||||
return Promise.resolve().then(() => __importStar(require('vite'))).then(({ build }) => build(inlineConfig));
|
||||
}
|
||||
exports.buildByVite = buildByVite;
|
||||
async function build(options) {
|
||||
if (process.env.UNI_APP_X !== 'true' && options.platform === 'app') {
|
||||
return buildApp(options);
|
||||
}
|
||||
return buildByVite((0, utils_1.addConfigFile)(initBuildOptions(options, (0, utils_1.cleanOptions)(options))));
|
||||
}
|
||||
exports.build = build;
|
||||
async function buildSSR(options) {
|
||||
const outputDir = process.env.UNI_OUTPUT_DIR;
|
||||
const ssrClientDir = path_1.default.resolve(outputDir, 'client');
|
||||
process.env.UNI_OUTPUT_DIR = ssrClientDir;
|
||||
const ssrBuildClientOptions = (0, utils_1.cleanOptions)(options);
|
||||
ssrBuildClientOptions.ssrManifest = 'ssr-manifest.json';
|
||||
ssrBuildClientOptions.outDir = process.env.UNI_OUTPUT_DIR;
|
||||
process.env.UNI_SSR_CLIENT = 'true';
|
||||
await buildByVite((0, utils_1.addConfigFile)(initBuildOptions(options, ssrBuildClientOptions)));
|
||||
const ssrServerDir = path_1.default.resolve(outputDir, 'server');
|
||||
process.env.UNI_OUTPUT_DIR = ssrServerDir;
|
||||
const ssrBuildServerOptions = (0, utils_1.cleanOptions)(options);
|
||||
ssrBuildServerOptions.ssr = path_1.default.resolve(process.env.UNI_INPUT_DIR, 'entry-server.js');
|
||||
// 强制 cjs 输出
|
||||
ssrBuildServerOptions.rollupOptions = {
|
||||
output: {
|
||||
format: 'cjs',
|
||||
},
|
||||
};
|
||||
ssrBuildServerOptions.outDir = process.env.UNI_OUTPUT_DIR;
|
||||
process.env.UNI_SSR_CLIENT = '';
|
||||
process.env.UNI_SSR_SERVER = 'true';
|
||||
await buildByVite((0, utils_1.addConfigFile)(initBuildOptions(options, ssrBuildServerOptions)));
|
||||
// copy ssr-manfiest.json to server
|
||||
const assets = ['ssr-manifest.json', 'index.html'];
|
||||
assets.forEach((asset) => {
|
||||
const ssrManifestFile = path_1.default.join(ssrClientDir, asset);
|
||||
if (fs_extra_1.default.existsSync(ssrManifestFile)) {
|
||||
fs_extra_1.default.copyFileSync(ssrManifestFile, path_1.default.join(ssrServerDir, asset));
|
||||
}
|
||||
});
|
||||
}
|
||||
exports.buildSSR = buildSSR;
|
||||
function initBuildOptions(options, build) {
|
||||
return {
|
||||
root: process.env.VITE_ROOT_DIR,
|
||||
configFile: options.config,
|
||||
base: options.base,
|
||||
logLevel: options.logLevel,
|
||||
clearScreen: options.clearScreen,
|
||||
mode: options.mode,
|
||||
build,
|
||||
};
|
||||
}
|
||||
exports.initBuildOptions = initBuildOptions;
|
||||
function buildManifestJson() {
|
||||
const platform = 'app';
|
||||
const inputDir = process.env.UNI_INPUT_DIR;
|
||||
const outputDir = process.env.UNI_OUTPUT_DIR;
|
||||
const pkg = require(path_1.default.resolve(__dirname, '../../package.json'));
|
||||
process.env.UNI_COMPILER_VERSION =
|
||||
process.env.UNI_COMPILER_VERSION ||
|
||||
pkg['uni-app']?.['compilerVersion'] ||
|
||||
'';
|
||||
const manifestJson = (0, uni_cli_shared_1.normalizeAppManifestJson)((0, uni_cli_shared_1.parseManifestJsonOnce)(inputDir), (0, uni_cli_shared_1.parsePagesJsonOnce)(inputDir, platform));
|
||||
fs_extra_1.default.outputFileSync(path_1.default.resolve(outputDir, 'manifest.json'), JSON.stringify(manifestJson, null, 2));
|
||||
}
|
||||
async function buildApp(options) {
|
||||
if (options.manifest) {
|
||||
return buildManifestJson();
|
||||
}
|
||||
let appWatcher;
|
||||
if (options.watch) {
|
||||
appWatcher = new AppWatcher();
|
||||
}
|
||||
if (process.env.UNI_RENDERER === 'native') {
|
||||
// 纯原生渲染时,main.js + App.vue 需要跟页面分开,独立编译(因为需要包含 Vuex 等共享内容)
|
||||
process.env.UNI_COMPILER = 'nvue';
|
||||
process.env.UNI_RENDERER_NATIVE = 'appService';
|
||||
const nvueAppBuilder = await buildByVite((0, utils_1.addConfigFile)((0, shared_1.extend)({ nvueAppService: true, nvue: true }, initBuildOptions(options, (0, utils_1.cleanOptions)(options)))));
|
||||
if (appWatcher) {
|
||||
appWatcher.setFirstWatcher(nvueAppBuilder);
|
||||
}
|
||||
process.env.UNI_RENDERER_NATIVE = 'pages';
|
||||
const nvueBuilder = await buildByVite((0, utils_1.addConfigFile)((0, shared_1.extend)({ nvue: true }, initBuildOptions(options, (0, utils_1.cleanOptions)(options)))));
|
||||
if (appWatcher) {
|
||||
appWatcher.setSecondWatcher(nvueBuilder);
|
||||
return appWatcher;
|
||||
}
|
||||
return;
|
||||
}
|
||||
// 指定为 vue 方便 App 插件初始化 vue 所需插件列表
|
||||
process.env.UNI_COMPILER = 'vue';
|
||||
const vueBuilder = await buildByVite((0, utils_1.addConfigFile)(initBuildOptions(options, (0, utils_1.cleanOptions)(options))));
|
||||
if (!(0, uni_cli_shared_1.isNormalCompileTarget)()) {
|
||||
// 不需要 nvue 编译器
|
||||
return vueBuilder;
|
||||
}
|
||||
if (appWatcher) {
|
||||
appWatcher.setFirstWatcher(vueBuilder);
|
||||
}
|
||||
// 临时指定为 nvue 方便 App 插件初始化 nvue 所需插件列表
|
||||
process.env.UNI_COMPILER = 'nvue';
|
||||
const nvueBuilder = await buildByVite((0, utils_1.addConfigFile)((0, shared_1.extend)({ nvue: true }, initBuildOptions(options, (0, utils_1.cleanOptions)(options)))));
|
||||
// 还原为 vue
|
||||
process.env.UNI_COMPILER = 'vue';
|
||||
if (appWatcher) {
|
||||
appWatcher.setSecondWatcher(nvueBuilder);
|
||||
return appWatcher;
|
||||
}
|
||||
}
|
||||
exports.buildApp = buildApp;
|
||||
class AppWatcher {
|
||||
constructor() {
|
||||
this._firstStart = false;
|
||||
this._firstEnd = false;
|
||||
this._secondStart = false;
|
||||
this._secondEnd = false;
|
||||
}
|
||||
on(_event, callback) {
|
||||
this._callback = callback;
|
||||
}
|
||||
setFirstWatcher(firstWatcher) {
|
||||
firstWatcher.on('event', (event) => {
|
||||
if (event.code === 'BUNDLE_START') {
|
||||
this._bundleFirstStart(event);
|
||||
}
|
||||
else if (event.code === 'BUNDLE_END') {
|
||||
this._bundleFirstEnd(event);
|
||||
}
|
||||
});
|
||||
}
|
||||
setSecondWatcher(secondWatcher) {
|
||||
secondWatcher.on('event', (event) => {
|
||||
if (event.code === 'BUNDLE_START') {
|
||||
this._bundleSecondStart(event);
|
||||
}
|
||||
else if (event.code === 'BUNDLE_END') {
|
||||
this._bundleSecondEnd(event);
|
||||
}
|
||||
});
|
||||
}
|
||||
_bundleFirstStart(event) {
|
||||
this._firstStart = true;
|
||||
this._bundleStart(event);
|
||||
}
|
||||
_bundleFirstEnd(event) {
|
||||
this._firstEnd = true;
|
||||
this._bundleEnd(event);
|
||||
}
|
||||
_bundleSecondStart(event) {
|
||||
this._secondStart = true;
|
||||
this._bundleStart(event);
|
||||
}
|
||||
_bundleSecondEnd(event) {
|
||||
this._secondEnd = true;
|
||||
this._bundleEnd(event);
|
||||
}
|
||||
_bundleStart(event) {
|
||||
if (this._firstStart && this._secondStart) {
|
||||
this._callback(event);
|
||||
}
|
||||
}
|
||||
_bundleEnd(event) {
|
||||
if (this._firstEnd && this._secondEnd) {
|
||||
this._callback(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
import type { LogLevel } from 'vite';
|
||||
export interface CliOptions {
|
||||
'--'?: string[];
|
||||
c?: boolean | string;
|
||||
config?: string;
|
||||
platform?: string;
|
||||
p?: string;
|
||||
ssr?: boolean;
|
||||
base?: string;
|
||||
debug?: boolean | string;
|
||||
d?: boolean | string;
|
||||
filter?: string;
|
||||
f?: string;
|
||||
logLevel?: LogLevel;
|
||||
l?: LogLevel;
|
||||
m?: string;
|
||||
mode?: string;
|
||||
clearScreen?: boolean;
|
||||
autoHost?: string;
|
||||
autoPort?: number;
|
||||
devtools?: boolean;
|
||||
devtoolsHost?: string;
|
||||
devtoolsPort?: number;
|
||||
subpackage?: string;
|
||||
plugin?: string;
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const cac_1 = require("cac");
|
||||
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
||||
const utils_1 = require("./utils");
|
||||
const action_1 = require("./action");
|
||||
(0, uni_cli_shared_1.fixBinaryPath)();
|
||||
const cli = (0, cac_1.cac)('uni');
|
||||
cli
|
||||
.option('-c, --config <file>', `[string] use specified config file`)
|
||||
.option('-p, --platform [platform]', '[string] ' + utils_1.PLATFORMS.join(' | '), {
|
||||
default: 'h5',
|
||||
})
|
||||
.option('--base <path>', `[string] public base path (default: /)`)
|
||||
.option('-ssr', '[boolean] server-side rendering', {
|
||||
default: false,
|
||||
})
|
||||
.option('-l, --logLevel <level>', `[string] silent | error | warn | all`)
|
||||
.option('--clearScreen', `[boolean] allow/disable clear screen when logging`)
|
||||
.option('-d, --debug [feat]', `[string | boolean] show debug logs`)
|
||||
.option('-f, --filter <filter>', `[string] filter debug logs`)
|
||||
.option('-m, --mode <mode>', `[string] set env mode`)
|
||||
.option('--minify [minifier]', `[boolean | "terser" | "esbuild"] enable/disable minification, ` +
|
||||
`or specify minifier to use (default: terser)`)
|
||||
.option('--autoHost [autoHost]', `[string] specify automator hostname`)
|
||||
.option('--autoPort [autoPort]', `[number] specify automator port`)
|
||||
.option('--devtools', `[boolean] enable devtools`)
|
||||
.option('--devtoolsHost [devtoolsHost]', `[string] specify devtools hostname`)
|
||||
.option('--devtoolsPort [devtoolsPort]', `[number] specify devtools port`)
|
||||
.option('--subpackage [subpackage]', `[string] specify subpackage to build`)
|
||||
.option('--plugin [plugin]', `[string] specify plugin to build`);
|
||||
cli
|
||||
.command('')
|
||||
.alias('dev')
|
||||
.option('--host [host]', `[string] specify hostname`)
|
||||
.option('--port <port>', `[number] specify port`)
|
||||
.option('--https', `[boolean] use TLS + HTTP/2`)
|
||||
.option('--open [path]', `[boolean | string] open browser on startup`)
|
||||
.option('--cors', `[boolean] enable CORS`)
|
||||
.option('--strictPort', `[boolean] exit if specified port is already in use`)
|
||||
.option('--force', `[boolean] force the optimizer to ignore the cache and re-bundle`)
|
||||
.action(action_1.runDev);
|
||||
cli
|
||||
.command('build')
|
||||
.option('--outDir <dir>', `[string] output directory (default: dist)`)
|
||||
.option('--assetsInlineLimit <number>', `[number] static asset base64 inline threshold in bytes (default: 4096)`)
|
||||
.option('--sourcemap', `[boolean] output source maps for build (default: false)`)
|
||||
.option('--manifest', `[boolean] emit build manifest json`)
|
||||
.option('--ssrManifest', `[boolean] emit ssr manifest json`)
|
||||
.option('--emptyOutDir', `[boolean] force empty outDir when it's outside of root`, {
|
||||
default: true,
|
||||
})
|
||||
.option('-w, --watch', `[boolean] rebuilds when modules have changed on disk`)
|
||||
.action(action_1.runBuild);
|
||||
cli.help();
|
||||
cli.version(require('../../package.json').version);
|
||||
cli.parse();
|
||||
+1
@@ -0,0 +1 @@
|
||||
export declare function initNVueEnv(): void;
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.initNVueEnv = void 0;
|
||||
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
||||
function initNVueEnv() {
|
||||
if (process.env.UNI_APP_X === 'true') {
|
||||
return;
|
||||
}
|
||||
const manifestJson = (0, uni_cli_shared_1.parseManifestJsonOnce)(process.env.UNI_INPUT_DIR);
|
||||
if ((0, uni_cli_shared_1.getAppRenderer)(manifestJson) === 'native') {
|
||||
process.env.UNI_RENDERER = 'native';
|
||||
}
|
||||
const nvueCompiler = (0, uni_cli_shared_1.getNVueCompiler)(manifestJson);
|
||||
if (nvueCompiler === 'uni-app') {
|
||||
process.env.UNI_NVUE_COMPILER = 'uni-app';
|
||||
}
|
||||
else if (nvueCompiler === 'vue') {
|
||||
process.env.UNI_NVUE_COMPILER = 'vue';
|
||||
}
|
||||
if ((0, uni_cli_shared_1.getNVueStyleCompiler)(manifestJson) === 'uni-app') {
|
||||
process.env.UNI_NVUE_STYLE_COMPILER = 'uni-app';
|
||||
}
|
||||
if ((0, uni_cli_shared_1.getAppCodeSpliting)(manifestJson)) {
|
||||
process.env.UNI_APP_CODE_SPLITING = 'true';
|
||||
}
|
||||
}
|
||||
exports.initNVueEnv = initNVueEnv;
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
import type { ServerOptions, ViteDevServer } from 'vite';
|
||||
import type { CliOptions } from '.';
|
||||
export declare function createServer(options: CliOptions & ServerOptions): Promise<ViteDevServer>;
|
||||
export declare function createSSRServer(options: CliOptions & ServerOptions): Promise<ViteDevServer>;
|
||||
+229
@@ -0,0 +1,229 @@
|
||||
"use strict";
|
||||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
var desc = Object.getOwnPropertyDescriptor(m, k);
|
||||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
||||
desc = { enumerable: true, get: function() { return m[k]; } };
|
||||
}
|
||||
Object.defineProperty(o, k2, desc);
|
||||
}) : (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
o[k2] = m[k];
|
||||
}));
|
||||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
||||
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
||||
}) : function(o, v) {
|
||||
o["default"] = v;
|
||||
});
|
||||
var __importStar = (this && this.__importStar) || function (mod) {
|
||||
if (mod && mod.__esModule) return mod;
|
||||
var result = {};
|
||||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
||||
__setModuleDefault(result, mod);
|
||||
return result;
|
||||
};
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.createSSRServer = exports.createServer = void 0;
|
||||
const fs_1 = __importDefault(require("fs"));
|
||||
const os_1 = __importDefault(require("os"));
|
||||
const path_1 = __importDefault(require("path"));
|
||||
const picocolors_1 = __importDefault(require("picocolors"));
|
||||
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
||||
const utils_1 = require("./utils");
|
||||
function createLogger(level, options) {
|
||||
return Promise.resolve().then(() => __importStar(require('vite'))).then(({ createLogger }) => createLogger(level, options));
|
||||
}
|
||||
function createViteServer(inlineConfig) {
|
||||
return Promise.resolve().then(() => __importStar(require('vite'))).then(({ createServer }) => createServer(inlineConfig));
|
||||
}
|
||||
async function createServer(options) {
|
||||
const server = await createViteServer((0, utils_1.addConfigFile)({
|
||||
root: process.env.VITE_ROOT_DIR,
|
||||
configFile: options.config,
|
||||
base: options.base,
|
||||
mode: options.mode,
|
||||
logLevel: options.logLevel || 'info',
|
||||
clearScreen: options.clearScreen,
|
||||
server: (0, utils_1.cleanOptions)(options),
|
||||
}));
|
||||
await server.listen();
|
||||
const logger = server.config.logger;
|
||||
logger.info(picocolors_1.default.cyan(`\n vite v${require('vite/package.json').version}`) +
|
||||
picocolors_1.default.green(` dev server running at:\n`), {
|
||||
clear: !server.config.logger.hasWarned,
|
||||
});
|
||||
server.printUrls();
|
||||
// printUrls 会在 nextTick 中输出
|
||||
process.nextTick(() => (0, utils_1.printStartupDuration)(logger));
|
||||
return server;
|
||||
}
|
||||
exports.createServer = createServer;
|
||||
async function createSSRServer(options) {
|
||||
// 延迟加载
|
||||
const app = (await Promise.resolve().then(() => __importStar(require('express')))).default();
|
||||
/**
|
||||
* @type {import('vite').ViteDevServer}
|
||||
*/
|
||||
const vite = await createViteServer((0, utils_1.addConfigFile)({
|
||||
// custom: don't include HTML middlewares
|
||||
appType: 'custom',
|
||||
root: process.env.VITE_ROOT_DIR,
|
||||
configFile: options.config,
|
||||
base: options.base,
|
||||
mode: options.mode,
|
||||
logLevel: options.logLevel || 'info',
|
||||
clearScreen: options.clearScreen,
|
||||
server: {
|
||||
middlewareMode: true,
|
||||
watch: {
|
||||
// During tests we edit the files too fast and sometimes chokidar
|
||||
// misses change events, so enforce polling for consistency
|
||||
usePolling: true,
|
||||
interval: 100,
|
||||
},
|
||||
},
|
||||
}));
|
||||
// use vite's connect instance as middleware
|
||||
app.use(vite.middlewares);
|
||||
app.use('*', async (req, res) => {
|
||||
try {
|
||||
const manifestJson = (0, uni_cli_shared_1.parseManifestJson)(process.env.UNI_INPUT_DIR);
|
||||
const h5 = (0, uni_cli_shared_1.getPlatformManifestJson)(manifestJson, 'h5');
|
||||
const base = (h5 && h5.router && h5.router.base) || '';
|
||||
const url = req.originalUrl.replace(base, '');
|
||||
const template = await vite.transformIndexHtml(url, fs_1.default.readFileSync(path_1.default.resolve(process.env.VITE_ROOT_DIR, 'index.html'), 'utf-8'));
|
||||
const render = (await vite.ssrLoadModule(path_1.default.resolve(process.env.UNI_INPUT_DIR, 'entry-server.js'))).render;
|
||||
const { title, headMeta, preloadLinks, appHtml, appContext } = await render(url);
|
||||
const icon = template.includes('rel="icon"')
|
||||
? ''
|
||||
: '<link rel="icon" href="data:," />\n';
|
||||
const html = template
|
||||
.replace(/<title>(.*?)<\/title>/, `${icon}<title>${title}</title>`)
|
||||
.replace(`<!--head-meta-->`, headMeta)
|
||||
.replace(`<!--preload-links-->`, preloadLinks)
|
||||
.replace(`<!--app-html-->`, appHtml)
|
||||
.replace(`<!--app-context-->`, appContext);
|
||||
res.status(200).set({ 'Content-Type': 'text/html' }).end(html);
|
||||
}
|
||||
catch (e) {
|
||||
vite && vite.ssrFixStacktrace(e);
|
||||
res.status(500).end(e.stack);
|
||||
}
|
||||
});
|
||||
const logger = await createLogger(options.logLevel);
|
||||
const serverOptions = vite.config.server || {};
|
||||
let port = options.port || serverOptions.port || 5173;
|
||||
let hostname;
|
||||
if (options.host === 'localhost') {
|
||||
// Use a secure default
|
||||
hostname = '127.0.0.1';
|
||||
}
|
||||
else if (options.host === undefined || options.host === true) {
|
||||
// probably passed --host in the CLI, without arguments
|
||||
hostname = undefined; // undefined typically means 0.0.0.0 or :: (listen on all IPs)
|
||||
}
|
||||
else {
|
||||
hostname = options.host;
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
const onSuccess = () => {
|
||||
printHttpServerUrls(server, vite.config);
|
||||
process.nextTick(() => (0, utils_1.printStartupDuration)(logger));
|
||||
resolve(vite);
|
||||
};
|
||||
const onError = (e) => {
|
||||
if (e.code === 'EADDRINUSE') {
|
||||
if (options.strictPort) {
|
||||
server.off('error', onError);
|
||||
reject(new Error(`Port ${port} is already in use`));
|
||||
}
|
||||
else {
|
||||
logger.info(`Port ${port} is in use, trying another one...`);
|
||||
server = app.listen(++port, hostname, onSuccess).on('error', onError);
|
||||
}
|
||||
}
|
||||
else {
|
||||
server.off('error', onError);
|
||||
reject(e);
|
||||
}
|
||||
};
|
||||
let server = app.listen(port, hostname, onSuccess).on('error', onError);
|
||||
});
|
||||
}
|
||||
exports.createSSRServer = createSSRServer;
|
||||
function printHttpServerUrls(server, config) {
|
||||
printCommonServerUrls(server, config.server, config);
|
||||
}
|
||||
function printCommonServerUrls(server, options, config) {
|
||||
const address = server.address();
|
||||
const isAddressInfo = (x) => x?.address;
|
||||
if (isAddressInfo(address)) {
|
||||
const hostname = resolveHostname(options.host);
|
||||
const protocol = options.https ? 'https' : 'http';
|
||||
printServerUrls(hostname, protocol, address.port, config.base, config.logger.info);
|
||||
}
|
||||
}
|
||||
function printServerUrls(hostname, protocol, port, base, info) {
|
||||
if (hostname.host === '127.0.0.1') {
|
||||
const url = `${protocol}://${hostname.name}:${picocolors_1.default.bold(port)}${base}`;
|
||||
info(` - Local: ${picocolors_1.default.cyan(url)}`);
|
||||
if (hostname.name !== '127.0.0.1') {
|
||||
info(` - Network: ${picocolors_1.default.dim('use `--host` to expose')}`);
|
||||
}
|
||||
}
|
||||
else {
|
||||
Object.values(os_1.default.networkInterfaces())
|
||||
.flatMap((nInterface) => nInterface ?? [])
|
||||
.filter((detail) => detail &&
|
||||
detail.address &&
|
||||
// Node < v18
|
||||
((typeof detail.family === 'string' && detail.family === 'IPv4') ||
|
||||
// Node >= v18
|
||||
// @ts-expect-error
|
||||
(typeof detail.family === 'number' && detail.family === 4)))
|
||||
.map((detail) => {
|
||||
const type = detail.address.includes('127.0.0.1')
|
||||
? ' - Local: '
|
||||
: ' * Network: ';
|
||||
const host = detail.address.replace('127.0.0.1', hostname.name);
|
||||
const url = `${protocol}://${host}:${picocolors_1.default.bold(port)}${base}`;
|
||||
return `${type} ${picocolors_1.default.cyan(url)}`;
|
||||
})
|
||||
.sort((msg1) => {
|
||||
return msg1.indexOf('- Local') > -1 ? -1 : 1;
|
||||
})
|
||||
.forEach((msg, index, arr) => {
|
||||
if (arr.length - 1 === index) {
|
||||
info(msg.replace('* Network', '- Network'));
|
||||
}
|
||||
else {
|
||||
info(msg);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
function resolveHostname(optionsHost) {
|
||||
let host;
|
||||
if (optionsHost === undefined || optionsHost === false) {
|
||||
// Use a secure default
|
||||
host = '127.0.0.1';
|
||||
}
|
||||
else if (optionsHost === true) {
|
||||
// If passed --host in the CLI without arguments
|
||||
host = undefined; // undefined typically means 0.0.0.0 or :: (listen on all IPs)
|
||||
}
|
||||
else {
|
||||
host = optionsHost;
|
||||
}
|
||||
// Set host name to localhost when possible, unless the user explicitly asked for '127.0.0.1'
|
||||
const name = (optionsHost !== '127.0.0.1' && host === '127.0.0.1') ||
|
||||
host === '0.0.0.0' ||
|
||||
host === '::' ||
|
||||
host === undefined
|
||||
? 'localhost'
|
||||
: host;
|
||||
return { host, name };
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
import type { InlineConfig, Logger } from 'vite';
|
||||
import type { CliOptions } from '.';
|
||||
export declare const PLATFORMS: string[];
|
||||
export type PLATFORM = 'app' | 'mp-alipay' | 'mp-baidu' | 'mp-kuaishou' | 'mp-lark' | 'mp-qq' | 'mp-toutiao' | 'mp-weixin' | 'mp-xhs' | 'quickapp-webview' | 'quickapp-webview-huawei' | 'quickapp-webview-union';
|
||||
export declare function addConfigFile(inlineConfig: InlineConfig): InlineConfig;
|
||||
export declare function initEnv(type: 'unknown' | 'dev' | 'build', options: CliOptions): void;
|
||||
export declare function cleanOptions(options: CliOptions): {
|
||||
'--'?: string[] | undefined;
|
||||
c?: string | boolean | undefined;
|
||||
config?: string | undefined;
|
||||
platform?: string | undefined;
|
||||
p?: string | undefined;
|
||||
ssr?: boolean | undefined;
|
||||
base?: string | undefined;
|
||||
debug?: string | boolean | undefined;
|
||||
d?: string | boolean | undefined;
|
||||
filter?: string | undefined;
|
||||
f?: string | undefined;
|
||||
logLevel?: import("vite").LogLevel | undefined;
|
||||
l?: import("vite").LogLevel | undefined;
|
||||
m?: string | undefined;
|
||||
mode?: string | undefined;
|
||||
clearScreen?: boolean | undefined;
|
||||
autoHost?: string | undefined;
|
||||
autoPort?: number | undefined;
|
||||
devtools?: boolean | undefined;
|
||||
devtoolsHost?: string | undefined;
|
||||
devtoolsPort?: number | undefined;
|
||||
subpackage?: string | undefined;
|
||||
plugin?: string | undefined;
|
||||
};
|
||||
export declare function printStartupDuration(logger: Logger, whitespace?: boolean): void;
|
||||
export declare function showRunPrompt(platform: PLATFORM): void;
|
||||
+377
@@ -0,0 +1,377 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.showRunPrompt = exports.printStartupDuration = exports.cleanOptions = exports.initEnv = exports.addConfigFile = exports.PLATFORMS = void 0;
|
||||
const fs_1 = __importDefault(require("fs"));
|
||||
const os_1 = __importDefault(require("os"));
|
||||
const path_1 = __importDefault(require("path"));
|
||||
const picocolors_1 = __importDefault(require("picocolors"));
|
||||
const perf_hooks_1 = require("perf_hooks");
|
||||
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
||||
const nvue_1 = require("./nvue");
|
||||
const uvue_1 = require("./uvue");
|
||||
// uni -p
|
||||
exports.PLATFORMS = [
|
||||
'app',
|
||||
'h5',
|
||||
'mp-alipay',
|
||||
'mp-baidu',
|
||||
'mp-kuaishou',
|
||||
'mp-lark',
|
||||
'mp-qq',
|
||||
'mp-toutiao',
|
||||
'mp-weixin',
|
||||
'mp-xhs',
|
||||
'quickapp-webview',
|
||||
'quickapp-webview-huawei',
|
||||
'quickapp-webview-union',
|
||||
];
|
||||
function resolveConfigFile() {
|
||||
const extname = ['.js', '.ts', '.mjs', '.mts'].find((ext) => {
|
||||
return fs_1.default.existsSync(path_1.default.resolve(process.env.UNI_INPUT_DIR, 'vite.config' + ext));
|
||||
});
|
||||
if (extname) {
|
||||
return path_1.default.resolve(process.env.UNI_INPUT_DIR, 'vite.config' + extname);
|
||||
}
|
||||
return path_1.default.resolve(process.env.UNI_CLI_CONTEXT, 'vite.config.js');
|
||||
}
|
||||
function addConfigFile(inlineConfig) {
|
||||
if ((0, uni_cli_shared_1.isInHBuilderX)()) {
|
||||
inlineConfig.configFile = resolveConfigFile();
|
||||
}
|
||||
return inlineConfig;
|
||||
}
|
||||
exports.addConfigFile = addConfigFile;
|
||||
let initialized = false;
|
||||
function initEnv(type, options) {
|
||||
if (initialized) {
|
||||
return;
|
||||
}
|
||||
initialized = true;
|
||||
if (options.platform === 'mp-360') {
|
||||
console.error(uni_cli_shared_1.M['mp.360.unsupported']);
|
||||
process.exit(0);
|
||||
}
|
||||
if (options.platform === 'web') {
|
||||
options.platform = 'h5';
|
||||
}
|
||||
if (options.plugin) {
|
||||
process.env.UNI_MP_PLUGIN = options.plugin;
|
||||
}
|
||||
// TODO 需要识别 mode
|
||||
if (type === 'dev') {
|
||||
process.env.NODE_ENV = 'development';
|
||||
}
|
||||
else if (type === 'build') {
|
||||
if (options.watch) {
|
||||
process.env.NODE_ENV = 'development';
|
||||
}
|
||||
else {
|
||||
if (!(0, uni_cli_shared_1.isNormalCompileTarget)()) {
|
||||
if (!process.env.NODE_ENV) {
|
||||
process.env.NODE_ENV = 'production';
|
||||
}
|
||||
}
|
||||
else {
|
||||
process.env.NODE_ENV = 'production';
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!options.mode) {
|
||||
options.mode = process.env.NODE_ENV;
|
||||
}
|
||||
// vite 会修改 NODE_ENV,存储在 UNI_NODE_ENV 中,稍后校正 NODE_ENV
|
||||
process.env.UNI_NODE_ENV = process.env.VITE_USER_NODE_ENV =
|
||||
process.env.NODE_ENV;
|
||||
process.env.UNI_CLI_CONTEXT = (0, uni_cli_shared_1.isInHBuilderX)()
|
||||
? path_1.default.resolve(process.env.UNI_HBUILDERX_PLUGINS, 'uniapp-cli-vite')
|
||||
: process.cwd();
|
||||
// TODO 待优化
|
||||
initUTSPlatform(options);
|
||||
if (options.platform === 'quickapp-webview-huawei' ||
|
||||
options.platform === 'quickapp-webview-union') {
|
||||
process.env.UNI_SUB_PLATFORM = options.platform;
|
||||
options.platform = 'quickapp-webview';
|
||||
}
|
||||
if ((0, uni_cli_shared_1.isInHBuilderX)()) {
|
||||
process.env.VITE_ROOT_DIR =
|
||||
process.env.VITE_ROOT_DIR || process.env.UNI_INPUT_DIR || process.cwd();
|
||||
}
|
||||
else {
|
||||
process.env.VITE_ROOT_DIR = process.env.VITE_ROOT_DIR || process.cwd();
|
||||
}
|
||||
process.env.UNI_INPUT_DIR =
|
||||
process.env.UNI_INPUT_DIR || path_1.default.resolve(process.cwd(), 'src');
|
||||
initCustomScripts(options);
|
||||
process.env.UNI_PLATFORM = options.platform;
|
||||
// 需要提前初始化
|
||||
(0, uvue_1.initUVueEnv)();
|
||||
if (process.env.UNI_PLATFORM === 'app-harmony') {
|
||||
if (process.env.UNI_APP_HARMONY_PROJECT_PATH) {
|
||||
// 先通过原始outputDir设置,因为下边会修改原始的outputDir到鸿蒙项目里,而这些临时目录不应该影响到鸿蒙项目
|
||||
process.env.UNI_APP_X_TSC_DIR = path_1.default.resolve(process.env.UNI_OUTPUT_DIR, '../.tsc');
|
||||
process.env.UNI_APP_X_UVUE_DIR = path_1.default.resolve(process.env.UNI_OUTPUT_DIR, '../.uvue');
|
||||
const baseOutDir = path_1.default.basename(process.env.UNI_OUTPUT_DIR);
|
||||
process.env.UNI_APP_X_CACHE_DIR =
|
||||
process.env.UNI_APP_X_CACHE_DIR ||
|
||||
path_1.default.resolve(process.env.UNI_OUTPUT_DIR, '../cache/.' + baseOutDir);
|
||||
process.env.UNI_APP_X_TSC_CACHE_DIR = path_1.default.resolve(process.env.UNI_APP_X_CACHE_DIR, `tsc`);
|
||||
// 指定了鸿蒙项目根目录
|
||||
process.env.UNI_OUTPUT_DIR = path_1.default.resolve(process.env.UNI_APP_HARMONY_PROJECT_PATH, `entry/src/main/resources/resfile/apps/HBuilder/www`);
|
||||
}
|
||||
}
|
||||
const hasOutputDir = !!process.env.UNI_OUTPUT_DIR;
|
||||
if (hasOutputDir) {
|
||||
;
|
||||
options.outDir = process.env.UNI_OUTPUT_DIR;
|
||||
}
|
||||
else {
|
||||
if (!options.outDir) {
|
||||
;
|
||||
options.outDir = path_1.default.resolve(process.cwd(), 'dist', process.env.NODE_ENV === 'production' ? 'build' : 'dev', (0, uni_cli_shared_1.getPlatformDir)());
|
||||
}
|
||||
process.env.UNI_OUTPUT_DIR = options.outDir;
|
||||
}
|
||||
// 编译为插件、分包时,需提前计算缓存目录位置
|
||||
process.env.UNI_APP_X_CACHE_DIR =
|
||||
process.env.UNI_APP_X_CACHE_DIR ||
|
||||
path_1.default.resolve(process.env.UNI_OUTPUT_DIR, '../cache/.' + path_1.default.basename(process.env.UNI_OUTPUT_DIR));
|
||||
// 兼容 HBuilderX 旧参数
|
||||
if (process.env.UNI_SUBPACKGE) {
|
||||
options.subpackage = process.env.UNI_SUBPACKGE;
|
||||
}
|
||||
if (options.subpackage) {
|
||||
process.env.UNI_SUBPACKAGE = options.subpackage;
|
||||
if (!hasOutputDir) {
|
||||
// 未指定,则自动补充
|
||||
process.env.UNI_OUTPUT_DIR = options.outDir =
|
||||
path_1.default.resolve(process.env.UNI_OUTPUT_DIR, options.subpackage);
|
||||
}
|
||||
}
|
||||
if (options.plugin) {
|
||||
if (!hasOutputDir) {
|
||||
// 未指定,则自动补充
|
||||
process.env.UNI_OUTPUT_DIR = options.outDir =
|
||||
path_1.default.resolve(process.env.UNI_OUTPUT_DIR, options.plugin);
|
||||
}
|
||||
}
|
||||
const baseOutDir = path_1.default.basename(process.env.UNI_OUTPUT_DIR);
|
||||
if ((0, uni_cli_shared_1.isNormalCompileTarget)()) {
|
||||
process.env.HX_DEPENDENCIES_DIR =
|
||||
process.env.HX_DEPENDENCIES_DIR ||
|
||||
path_1.default.resolve(process.env.UNI_OUTPUT_DIR, '../hx/' + baseOutDir);
|
||||
}
|
||||
process.env.UNI_MODULES_ENCRYPT_CACHE_DIR = path_1.default.resolve(process.env.UNI_APP_X_CACHE_DIR, '.encrypt', process.env.NODE_ENV === 'development' ? 'dev' : 'build', process.env.UNI_UTS_PLATFORM);
|
||||
// 默认开启 tsc
|
||||
process.env.UNI_APP_X_TSC = 'true';
|
||||
try {
|
||||
// 部分模式下缺少manifest.json,比如内部编译ext-api时
|
||||
const manifestJson = (0, uni_cli_shared_1.parseManifestJsonOnce)(process.env.UNI_INPUT_DIR);
|
||||
// 留个开关
|
||||
if (manifestJson['app']?.['utsCompilerVersion'] === 'v1' ||
|
||||
manifestJson['app-plus']?.['utsCompilerVersion'] === 'v1') {
|
||||
process.env.UNI_APP_X_TSC = 'false';
|
||||
}
|
||||
}
|
||||
catch (e) { }
|
||||
if (!process.env.UNI_APP_X_TSC_DIR) {
|
||||
process.env.UNI_APP_X_TSC_DIR = path_1.default.resolve(process.env.UNI_OUTPUT_DIR, '../.tsc');
|
||||
}
|
||||
if (!process.env.UNI_APP_X_UVUE_DIR) {
|
||||
process.env.UNI_APP_X_UVUE_DIR = path_1.default.resolve(process.env.UNI_OUTPUT_DIR, '../.uvue');
|
||||
}
|
||||
if (!process.env.UNI_APP_X_TSC_CACHE_DIR) {
|
||||
process.env.UNI_APP_X_TSC_CACHE_DIR = path_1.default.resolve(process.env.UNI_APP_X_CACHE_DIR, `tsc`);
|
||||
}
|
||||
initAutomator(options);
|
||||
initDevtools(options);
|
||||
if (process.env.UNI_PLATFORM === 'app') {
|
||||
(0, nvue_1.initNVueEnv)();
|
||||
}
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
console.log(uni_cli_shared_1.M['dev.performance'] +
|
||||
(process.env.UNI_PLATFORM.startsWith('mp-')
|
||||
? uni_cli_shared_1.M['dev.performance.mp']
|
||||
: '') +
|
||||
(process.env.UNI_PLATFORM === 'web' || process.env.UNI_PLATFORM === 'h5'
|
||||
? uni_cli_shared_1.M['dev.performance.web']
|
||||
: ''));
|
||||
}
|
||||
if (options.sourcemap &&
|
||||
process.env.NODE_ENV === 'production') {
|
||||
process.env.SOURCEMAP = 'true';
|
||||
}
|
||||
(0, uni_cli_shared_1.initModulePaths)();
|
||||
const pkg = require('../../package.json');
|
||||
process.env.UNI_COMPILER_VERSION =
|
||||
process.env.UNI_COMPILER_VERSION ||
|
||||
pkg['uni-app']?.['compilerVersion'] ||
|
||||
'';
|
||||
process.env.UNI_COMPILER_VERSION_TYPE = pkg.version.includes('alpha')
|
||||
? 'a'
|
||||
: 'r';
|
||||
(0, uni_cli_shared_1.initPreContext)(process.env.UNI_PLATFORM, process.env.UNI_CUSTOM_CONTEXT, process.env.UNI_UTS_PLATFORM, process.env.UNI_APP_X === 'true');
|
||||
if (process.env.UNI_PLATFORM === 'app' ||
|
||||
process.env.UNI_PLATFORM === 'web' ||
|
||||
process.env.UNI_PLATFORM === 'h5' ||
|
||||
process.env.UNI_PLATFORM === 'app-harmony') {
|
||||
console.log(uni_cli_shared_1.M['app.compiler.version'].replace('{version}', process.env.UNI_COMPILER_VERSION +
|
||||
`(${process.env.UNI_APP_X === 'true' ? 'uni-app x' : 'vue3'})`));
|
||||
}
|
||||
console.log(uni_cli_shared_1.M['compiling']);
|
||||
}
|
||||
exports.initEnv = initEnv;
|
||||
function initUTSPlatform(options) {
|
||||
if (options.platform === 'app-android') {
|
||||
process.env.UNI_UTS_PLATFORM = 'app-android';
|
||||
options.platform = 'app';
|
||||
}
|
||||
else if (options.platform === 'app-ios') {
|
||||
process.env.UNI_UTS_PLATFORM = 'app-ios';
|
||||
options.platform = 'app';
|
||||
}
|
||||
else {
|
||||
// 运行时,可能传入了 UNI_APP_PLATFORM = 'android'|'ios'
|
||||
if (process.env.UNI_APP_PLATFORM === 'android') {
|
||||
process.env.UNI_UTS_PLATFORM = 'app-android';
|
||||
}
|
||||
if (process.env.UNI_APP_PLATFORM === 'ios') {
|
||||
process.env.UNI_UTS_PLATFORM = 'app-ios';
|
||||
}
|
||||
if (options.platform === 'app-plus') {
|
||||
options.platform = 'app';
|
||||
}
|
||||
if (options.platform === 'app') {
|
||||
// app 平台未指定 UTS_PLATFORM 时,设置未 app,一般是发行模式
|
||||
if (!process.env.UNI_UTS_PLATFORM) {
|
||||
process.env.UNI_UTS_PLATFORM = 'app';
|
||||
}
|
||||
}
|
||||
}
|
||||
if (options.platform === 'h5') {
|
||||
process.env.UNI_UTS_PLATFORM = 'web';
|
||||
}
|
||||
// 非 app 平台,自动补充 UNI_UTS_PLATFORM
|
||||
// app 平台,必须主动传入
|
||||
if (options.platform !== 'app') {
|
||||
if (!process.env.UNI_UTS_PLATFORM) {
|
||||
process.env.UNI_UTS_PLATFORM = options.platform;
|
||||
}
|
||||
}
|
||||
process.env.UNI_UTS_TARGET_LANGUAGE = 'javascript';
|
||||
if (process.env.UNI_UTS_PLATFORM === 'app-android') {
|
||||
process.env.UNI_UTS_TARGET_LANGUAGE = 'kotlin';
|
||||
}
|
||||
else if (process.env.UNI_UTS_PLATFORM === 'app-ios') {
|
||||
process.env.UNI_UTS_TARGET_LANGUAGE = 'swift';
|
||||
}
|
||||
else if (process.env.UNI_UTS_PLATFORM === 'app-harmony') {
|
||||
process.env.UNI_UTS_TARGET_LANGUAGE = 'arkts';
|
||||
}
|
||||
}
|
||||
function initDevtools({ devtools, devtoolsHost, devtoolsPort }) {
|
||||
if (!devtools) {
|
||||
return;
|
||||
}
|
||||
process.env.__VUE_PROD_DEVTOOLS__ = 'true';
|
||||
if (devtoolsHost) {
|
||||
process.env.__VUE_DEVTOOLS_HOST__ = devtoolsHost;
|
||||
}
|
||||
if (devtoolsPort) {
|
||||
process.env.__VUE_DEVTOOLS_PORT__ = devtoolsPort + '';
|
||||
}
|
||||
}
|
||||
function initAutomator({ autoHost, autoPort }) {
|
||||
const hasAutoHostOrPort = autoHost || autoPort || process.env.UNI_AUTOMATOR_HOST;
|
||||
// 发行分包,插件也不需要自动化测试
|
||||
if (!hasAutoHostOrPort ||
|
||||
process.env.UNI_SUBPACKAGE ||
|
||||
process.env.UNI_MP_PLUGIN) {
|
||||
return;
|
||||
}
|
||||
process.env.UNI_AUTOMATOR_WS_ENDPOINT =
|
||||
'ws://' +
|
||||
(autoHost || process.env.UNI_AUTOMATOR_HOST || resolveHostname()) +
|
||||
':' +
|
||||
(autoPort || process.env.UNI_AUTOMATOR_PORT);
|
||||
}
|
||||
function resolveHostname() {
|
||||
const interfaces = os_1.default.networkInterfaces();
|
||||
const keys = Object.keys(interfaces);
|
||||
for (const key of keys) {
|
||||
const interfaceInfos = interfaces[key];
|
||||
if (!interfaceInfos) {
|
||||
continue;
|
||||
}
|
||||
for (const info of interfaceInfos) {
|
||||
if ((info.family === 'IPv4' ||
|
||||
/* Node >= v18 */ info.family === 4) &&
|
||||
!info.address.includes('127.0.0.1')) {
|
||||
return info.address;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 'localhost';
|
||||
}
|
||||
function cleanOptions(options) {
|
||||
const ret = { ...options };
|
||||
delete ret['--'];
|
||||
delete ret.c;
|
||||
delete ret.config;
|
||||
delete ret.platform;
|
||||
delete ret.p;
|
||||
delete ret.ssr;
|
||||
delete ret.base;
|
||||
delete ret.debug;
|
||||
delete ret.d;
|
||||
delete ret.filter;
|
||||
delete ret.f;
|
||||
delete ret.logLevel;
|
||||
delete ret.l;
|
||||
delete ret.clearScreen;
|
||||
delete ret.m;
|
||||
delete ret.mode;
|
||||
delete ret.autoHost;
|
||||
delete ret.autoPort;
|
||||
return ret;
|
||||
}
|
||||
exports.cleanOptions = cleanOptions;
|
||||
function printStartupDuration(logger, whitespace = true) {
|
||||
if (global.__vite_start_time) {
|
||||
const startupDuration = perf_hooks_1.performance.now() - global.__vite_start_time;
|
||||
logger.info(`${whitespace ? `\n ` : ''}${picocolors_1.default.cyan(`ready in ${Math.ceil(startupDuration)}ms.`)}\n`);
|
||||
}
|
||||
}
|
||||
exports.printStartupDuration = printStartupDuration;
|
||||
function initCustomScripts(options) {
|
||||
const custom = (0, uni_cli_shared_1.parseScripts)(process.env.UNI_SCRIPT || options.platform, // process.env.UNI_SCRIPT 是 HBuilderX 传递的
|
||||
path_1.default.join(process.env.VITE_ROOT_DIR, 'package.json'));
|
||||
if (!custom) {
|
||||
return;
|
||||
}
|
||||
options.platform = custom.platform;
|
||||
process.env.UNI_CUSTOM_SCRIPT = custom.name;
|
||||
process.env.UNI_CUSTOM_DEFINE = JSON.stringify(custom.define);
|
||||
process.env.UNI_CUSTOM_CONTEXT = JSON.stringify(custom.context);
|
||||
}
|
||||
function showRunPrompt(platform) {
|
||||
if (!(0, uni_cli_shared_1.runByHBuilderX)()) {
|
||||
const devtools = getPlatformDevtools(getOriginalPlatform(platform));
|
||||
const outputDir = path_1.default.relative(process.env.UNI_CLI_CONTEXT, process.env.UNI_OUTPUT_DIR);
|
||||
(0, uni_cli_shared_1.output)('log', `${uni_cli_shared_1.M['prompt.run.message']
|
||||
.replace('{devtools}', uni_cli_shared_1.M[devtools])
|
||||
.replace('{outputDir}', picocolors_1.default.cyan(outputDir))}`);
|
||||
}
|
||||
}
|
||||
exports.showRunPrompt = showRunPrompt;
|
||||
function getOriginalPlatform(platform) {
|
||||
if (platform.startsWith('quickapp-webview') && process.env.UNI_SUB_PLATFORM) {
|
||||
return process.env.UNI_SUB_PLATFORM;
|
||||
}
|
||||
return platform;
|
||||
}
|
||||
function getPlatformDevtools(platform) {
|
||||
return `prompt.run.devtools.${platform}`;
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
import { type BuildOptions, type ServerOptions } from 'vite';
|
||||
import type { RollupWatcher } from 'rollup';
|
||||
import type { CliOptions } from '.';
|
||||
export declare function initUVueEnv(): void;
|
||||
export declare function runUVueAndroidDev(options: CliOptions & ServerOptions): Promise<undefined>;
|
||||
export declare function runUVueAndroidBuild(options: CliOptions & BuildOptions): Promise<void>;
|
||||
/**
|
||||
* 目前的简易实现逻辑
|
||||
* node层:
|
||||
* 1. 监听项目,生成资源到临时目录 .uts/android
|
||||
* 2. uvue 文件,做解析,拆分生成 render.kt, css.kt, uts.uvue
|
||||
* 3. static 文件,copy 到最终目录
|
||||
* 4. uvue、vue、uts 文件发生变化,调用 uts 编译器
|
||||
* @param options
|
||||
*/
|
||||
export declare function buildUVue(options: CliOptions): Promise<RollupWatcher | void>;
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.buildUVue = exports.runUVueAndroidBuild = exports.runUVueAndroidDev = exports.initUVueEnv = void 0;
|
||||
const vite_1 = require("vite");
|
||||
const shared_1 = require("@vue/shared");
|
||||
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
||||
const build_1 = require("./build");
|
||||
const utils_1 = require("./utils");
|
||||
const easycom_1 = require("../utils/easycom");
|
||||
const action_1 = require("./action");
|
||||
function initUVueEnv() {
|
||||
// 直接指定了
|
||||
if (process.env.UNI_APP_X === 'false') {
|
||||
return;
|
||||
}
|
||||
// 没有手动指定时,才需要动态读取 manifest.json
|
||||
if (process.env.UNI_APP_X !== 'true') {
|
||||
const manifestJson = (0, uni_cli_shared_1.parseManifestJsonOnce)(process.env.UNI_INPUT_DIR);
|
||||
if (!(0, shared_1.hasOwn)(manifestJson, 'uni-app-x')) {
|
||||
return;
|
||||
}
|
||||
process.env.UNI_APP_X = 'true';
|
||||
if (manifestJson['uni-app-x']?.singleThread === false) {
|
||||
process.env.UNI_APP_X_SINGLE_THREAD = 'false';
|
||||
}
|
||||
}
|
||||
if (process.env.UNI_PLATFORM === 'app') {
|
||||
process.env.UNI_APP_X_UVUE_SCRIPT_ENGINE = 'native';
|
||||
// 如果是app-ios,目前强制使用js引擎
|
||||
if (process.env.UNI_UTS_PLATFORM === 'app-ios') {
|
||||
process.env.UNI_APP_X_UVUE_SCRIPT_ENGINE = 'js';
|
||||
}
|
||||
}
|
||||
}
|
||||
exports.initUVueEnv = initUVueEnv;
|
||||
async function runUVueAndroidDev(options) {
|
||||
if (options.platform !== 'app') {
|
||||
(0, uni_cli_shared_1.output)('error', uni_cli_shared_1.M['uvue.unsupported'].replace('{platform}', options.platform));
|
||||
return process.exit(0);
|
||||
}
|
||||
(0, easycom_1.initEasycom)();
|
||||
const watcher = (await buildUVue(options));
|
||||
let isFirstStart = true;
|
||||
let isFirstEnd = true;
|
||||
watcher.on('event', async (event) => {
|
||||
if (event.code === 'BUNDLE_START') {
|
||||
if (isFirstStart) {
|
||||
isFirstStart = false;
|
||||
return;
|
||||
}
|
||||
(0, uni_cli_shared_1.output)('log', uni_cli_shared_1.M['dev.watching.start']);
|
||||
// 重置一下,uts编译报错会导致下一次开始差量编译紧接着上一次的差量编译,导致无法正常输出
|
||||
(0, uni_cli_shared_1.resetOutput)('log');
|
||||
}
|
||||
else if (event.code === 'BUNDLE_END') {
|
||||
event.result.close();
|
||||
const dex = process.env.UNI_APP_UTS_CHANGED_FILES;
|
||||
process.env.UNI_APP_UTS_CHANGED_FILES = '';
|
||||
if (isFirstEnd) {
|
||||
// 首次全量同步
|
||||
isFirstEnd = false;
|
||||
(0, uni_cli_shared_1.output)('log', uni_cli_shared_1.M['dev.watching.end']);
|
||||
(0, utils_1.printStartupDuration)((0, vite_1.createLogger)(options.logLevel), false);
|
||||
await (0, action_1.stopProfiler)((message) => (0, vite_1.createLogger)(options.logLevel).info(message));
|
||||
return;
|
||||
}
|
||||
if (dex) {
|
||||
const files = JSON.parse(dex);
|
||||
if (!files.length) {
|
||||
// 本次无变动
|
||||
return (0, uni_cli_shared_1.output)('log', uni_cli_shared_1.M['uvue.dev.watching.end.empty']);
|
||||
}
|
||||
return (0, uni_cli_shared_1.output)('log', uni_cli_shared_1.M['dev.watching.end.files'].replace('{files}', JSON.stringify(files)));
|
||||
}
|
||||
return (0, uni_cli_shared_1.output)('log', uni_cli_shared_1.M['dev.watching.end']);
|
||||
}
|
||||
});
|
||||
}
|
||||
exports.runUVueAndroidDev = runUVueAndroidDev;
|
||||
async function runUVueAndroidBuild(options) {
|
||||
try {
|
||||
(0, uni_cli_shared_1.initEasycomsOnce)(process.env.UNI_INPUT_DIR, {
|
||||
dirs: (0, uni_cli_shared_1.resolveComponentsLibDirs)(),
|
||||
platform: process.env.UNI_PLATFORM,
|
||||
isX: true,
|
||||
});
|
||||
await buildUVue(options);
|
||||
await (0, action_1.stopProfiler)((message) => (0, vite_1.createLogger)(options.logLevel).info(message));
|
||||
console.log(uni_cli_shared_1.M['build.done']);
|
||||
// 开发者可能用了三方插件,三方插件有可能阻止退出,导致HBuilderX打包状态识别不正确
|
||||
if ((0, uni_cli_shared_1.isInHBuilderX)()) {
|
||||
process.exit(0);
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
console.error(e.message || e);
|
||||
console.error(`Build failed with errors.`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
exports.runUVueAndroidBuild = runUVueAndroidBuild;
|
||||
/**
|
||||
* 目前的简易实现逻辑
|
||||
* node层:
|
||||
* 1. 监听项目,生成资源到临时目录 .uts/android
|
||||
* 2. uvue 文件,做解析,拆分生成 render.kt, css.kt, uts.uvue
|
||||
* 3. static 文件,copy 到最终目录
|
||||
* 4. uvue、vue、uts 文件发生变化,调用 uts 编译器
|
||||
* @param options
|
||||
*/
|
||||
async function buildUVue(options) {
|
||||
return (0, build_1.buildByVite)((0, utils_1.addConfigFile)((0, shared_1.extend)({ nvueAppService: true, uvue: true }, (0, build_1.initBuildOptions)(options, (0, utils_1.cleanOptions)(options)))));
|
||||
}
|
||||
exports.buildUVue = buildUVue;
|
||||
Reference in New Issue
Block a user