| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- "use strict";
- var __importDefault = (this && this.__importDefault) || function (mod) {
- return (mod && mod.__esModule) ? mod : { "default": mod };
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.uniAppPlugin = void 0;
- const path_1 = __importDefault(require("path"));
- const fs_extra_1 = __importDefault(require("fs-extra"));
- const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
- const utils_1 = require("./utils");
- const manifestJson_1 = require("./manifestJson");
- const utils_2 = require("../utils");
- const __1 = require("../..");
- const uniCloudSpaceList = (0, utils_1.getUniCloudSpaceList)();
- let isFirst = true;
- function uniAppPlugin() {
- const inputDir = process.env.UNI_INPUT_DIR;
- const outputDir = process.env.UNI_OUTPUT_DIR;
- const mainUTS = (0, uni_cli_shared_1.resolveMainPathOnce)(inputDir);
- const tempOutputDir = (0, utils_1.uvueOutDir)();
- const manifestJson = (0, uni_cli_shared_1.parseManifestJsonOnce)(inputDir);
- // 预留一个口子,方便切换测试
- const split = manifestJson['uni-app-x']?.split;
- // 开始编译时,清空输出目录
- function emptyOutDir() {
- if (fs_extra_1.default.existsSync(outputDir)) {
- (0, uni_cli_shared_1.emptyDir)(outputDir);
- }
- }
- emptyOutDir();
- return {
- name: 'uni:app-uts',
- apply: 'build',
- uni: (0, utils_2.createUniOptions)('android'),
- config() {
- return {
- base: '/',
- build: {
- outDir: tempOutputDir,
- lib: {
- // 必须使用 lib 模式
- fileName: 'output',
- entry: (0, uni_cli_shared_1.resolveMainPathOnce)(inputDir),
- formats: ['cjs'],
- },
- rollupOptions: {
- external(source) {
- if (['vue', 'vuex', 'pinia', '@dcloudio/uni-app'].includes(source)) {
- return true;
- }
- // 相对目录
- if (source.startsWith('@/') || source.startsWith('.')) {
- return false;
- }
- if (path_1.default.isAbsolute(source)) {
- return false;
- }
- // android 系统库,三方库
- if (source.includes('.')) {
- return true;
- }
- return false;
- },
- output: {
- chunkFileNames(chunk) {
- // if (chunk.isDynamicEntry && chunk.facadeModuleId) {
- // const { filename } = parseVueRequest(chunk.facadeModuleId)
- // if (filename.endsWith('.nvue')) {
- // return (
- // removeExt(
- // normalizePath(path.relative(inputDir, filename))
- // ) + '.js'
- // )
- // }
- // }
- return '[name].js';
- },
- },
- },
- },
- };
- },
- configResolved(config) {
- (0, utils_2.configResolved)(config, true);
- },
- async transform(code, id) {
- if (process.env.UNI_APP_X_TSC === 'true') {
- return;
- }
- const { filename } = (0, uni_cli_shared_1.parseVueRequest)(id);
- if (!filename.endsWith('.uts') && !filename.endsWith('.ts')) {
- return;
- }
- // 仅处理 uts 文件
- // 忽略 uni-app-uts/lib/automator/index.uts
- if (!filename.includes('uni-app-uts')) {
- const isMainUTS = (0, uni_cli_shared_1.normalizePath)(id) === mainUTS;
- const fileName = path_1.default.relative(inputDir, id);
- this.emitFile({
- type: 'asset',
- fileName: normalizeFilename(fileName, isMainUTS),
- source: normalizeCode(code, isMainUTS),
- });
- }
- code = await (0, utils_1.parseImports)(code, (0, utils_1.createTryResolve)(id, this.resolve.bind(this)));
- return code;
- },
- generateBundle(_, bundle) {
- // 开发者仅在 script 中引入了 easyCom 类型,但模板里边没用到,此时额外生成一个辅助的.uvue文件
- checkUTSEasyComAutoImports(inputDir, bundle, this);
- },
- async writeBundle() {
- let pageCount = 0;
- if (isFirst) {
- isFirst = false;
- // 自动化测试时,不显示页面数量进度条
- if (!process.env.UNI_AUTOMATOR_WS_ENDPOINT) {
- pageCount = parseInt(process.env.UNI_APP_X_PAGE_COUNT) || 0;
- }
- }
- await (0, uni_cli_shared_1.buildUniExtApiProviders)();
- const res = await (0, uni_cli_shared_1.resolveUTSCompiler)().compileApp(path_1.default.join(tempOutputDir, 'main.uts'), {
- pageCount,
- uniCloudObjectInfo: (0, utils_1.getUniCloudObjectInfo)(uniCloudSpaceList),
- split: split !== false,
- disableSplitManifest: process.env.NODE_ENV !== 'development',
- inputDir: tempOutputDir,
- outputDir: outputDir,
- package: 'uni.' + (manifestJson.appid || utils_1.DEFAULT_APPID).replace(/_/g, ''),
- sourceMap: process.env.NODE_ENV === 'development',
- uni_modules: [...uni_cli_shared_1.utsPlugins],
- extApis: (0, uni_cli_shared_1.parseUniExtApiNamespacesOnce)(process.env.UNI_UTS_PLATFORM, process.env.UNI_UTS_TARGET_LANGUAGE),
- extApiComponents: [...(0, utils_1.getExtApiComponents)()],
- uvueClassNamePrefix: utils_1.UVUE_CLASS_NAME_PREFIX,
- autoImports: (0, uni_cli_shared_1.getUTSEasyComAutoImports)(),
- });
- if (res) {
- if (process.env.NODE_ENV === 'development') {
- const files = [];
- if (process.env.UNI_APP_UTS_CHANGED_FILES) {
- try {
- files.push(...JSON.parse(process.env.UNI_APP_UTS_CHANGED_FILES));
- }
- catch (e) { }
- }
- if (res.changed) {
- // 触发了kotlinc编译,且没有编译成功
- if (!res.changed.length && res.kotlinc) {
- throw new Error('编译失败');
- }
- files.push(...res.changed);
- }
- process.env.UNI_APP_UTS_CHANGED_FILES = JSON.stringify([
- ...new Set(files),
- ]);
- }
- else {
- // 生产环境,记录使用到的modules
- const modules = res.inject_modules;
- const manifest = (0, manifestJson_1.getOutputManifestJson)();
- if (manifest) {
- // 执行了摇树逻辑,就需要设置 modules 节点
- const app = manifest.app;
- if (!app.distribute) {
- app.distribute = {};
- }
- if (!app.distribute.modules) {
- app.distribute.modules = {};
- }
- if (modules) {
- modules.forEach((name) => {
- const value = app.distribute.modules[name];
- app.distribute.modules[name] =
- typeof value === 'object' && value !== null ? value : {};
- });
- }
- fs_extra_1.default.outputFileSync(path_1.default.resolve(process.env.UNI_OUTPUT_DIR, 'manifest.json'), JSON.stringify(manifest, null, 2));
- }
- }
- }
- },
- };
- }
- exports.uniAppPlugin = uniAppPlugin;
- function normalizeFilename(filename, isMain = false) {
- if (isMain) {
- return 'main.uts';
- }
- return (0, utils_1.parseUTSRelativeFilename)(filename);
- }
- function normalizeCode(code, isMain = false) {
- if (!isMain) {
- return code;
- }
- const automatorCode = process.env.UNI_AUTOMATOR_WS_ENDPOINT &&
- process.env.UNI_AUTOMATOR_APP_WEBVIEW !== 'true'
- ? 'initAutomator();'
- : '';
- return `${code}
- export function main(app: IApp) {
- definePageRoutes();
- defineAppConfig();
- ${automatorCode}
- (createApp()['app'] as VueApp).mount(app);
- }
- `;
- }
- function checkUTSEasyComAutoImports(inputDir, bundle, ctx) {
- const res = (0, uni_cli_shared_1.getUTSEasyComAutoImports)();
- Object.keys(res).forEach((fileName) => {
- if (fileName.endsWith('.vue') || fileName.endsWith('.uvue')) {
- if (fileName.startsWith('@/')) {
- fileName = fileName.slice(2);
- }
- const relativeFileName = (0, utils_1.parseUTSRelativeFilename)(fileName, inputDir);
- if (!bundle[relativeFileName]) {
- const className = (0, __1.genClassName)(relativeFileName, utils_1.UVUE_CLASS_NAME_PREFIX);
- ctx.emitFile({
- type: 'asset',
- fileName: relativeFileName,
- source: `function ${className}Render(): any | null { return null }
- const ${className}Styles = []`,
- });
- }
- }
- });
- return res;
- }
|