更新
This commit is contained in:
+2
@@ -0,0 +1,2 @@
|
||||
import type { Plugin, ResolvedConfig } from 'vite';
|
||||
export declare function uniAppCssPlugin(resolvedConfig: ResolvedConfig): Plugin;
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.uniAppCssPlugin = void 0;
|
||||
const picocolors_1 = __importDefault(require("picocolors"));
|
||||
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
||||
const uni_nvue_styler_1 = require("@dcloudio/uni-nvue-styler");
|
||||
function uniAppCssPlugin(resolvedConfig) {
|
||||
return {
|
||||
name: 'vite:css-post',
|
||||
buildStart() {
|
||||
// 用于覆盖原始插件方法
|
||||
// noop
|
||||
},
|
||||
async transform(source, filename) {
|
||||
if (!uni_cli_shared_1.cssLangRE.test(filename) || uni_cli_shared_1.commonjsProxyRE.test(filename)) {
|
||||
return;
|
||||
}
|
||||
if (source.includes('#endif')) {
|
||||
source = (0, uni_cli_shared_1.preUVueCss)(source, filename);
|
||||
}
|
||||
source = (0, uni_cli_shared_1.parseAssets)(resolvedConfig, source);
|
||||
// 仅做校验使用
|
||||
const { messages, code } = await (0, uni_nvue_styler_1.parse)(source, {
|
||||
filename,
|
||||
logLevel: 'WARNING',
|
||||
type: 'uvue',
|
||||
platform: process.env.UNI_UTS_PLATFORM,
|
||||
});
|
||||
messages.forEach((message) => {
|
||||
if (message.type === 'warning') {
|
||||
// 拆分成多行,第一行输出信息(有颜色),后续输出错误代码+文件行号
|
||||
resolvedConfig.logger.warn(picocolors_1.default.yellow(`[plugin:uni:app-uvue-css] ${message.text}`));
|
||||
let msg = '';
|
||||
if (message.line && message.column) {
|
||||
msg += `\n${(0, uni_cli_shared_1.generateCodeFrame)(source, {
|
||||
line: message.line,
|
||||
column: message.column,
|
||||
}).replace(/\t/g, ' ')}\n`;
|
||||
}
|
||||
msg += `${(0, uni_cli_shared_1.formatAtFilename)(filename)}`;
|
||||
resolvedConfig.logger.warn(msg);
|
||||
}
|
||||
});
|
||||
return { code: `export default ${code}`, map: { mappings: '' } };
|
||||
},
|
||||
generateBundle() {
|
||||
// 用于覆盖原始插件方法
|
||||
// noop
|
||||
},
|
||||
};
|
||||
}
|
||||
exports.uniAppCssPlugin = uniAppCssPlugin;
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
import type { Plugin } from 'vite';
|
||||
export declare function uniAppJsEngineMainPlugin(): Plugin;
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.uniAppJsEngineMainPlugin = void 0;
|
||||
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
||||
function uniAppJsEngineMainPlugin() {
|
||||
const mainUTS = (0, uni_cli_shared_1.resolveMainPathOnce)(process.env.UNI_INPUT_DIR);
|
||||
return {
|
||||
name: 'uni:app-main',
|
||||
apply: 'build',
|
||||
async transform(code, id) {
|
||||
if ((0, uni_cli_shared_1.normalizePath)(id) === mainUTS) {
|
||||
return {
|
||||
code: `
|
||||
import './${uni_cli_shared_1.MANIFEST_JSON_UTS}'
|
||||
import './${uni_cli_shared_1.PAGES_JSON_UTS}'
|
||||
const __global__ = typeof globalThis === 'undefined' ? Function('return this')() : globalThis
|
||||
__global__.__uniX = true
|
||||
${code}
|
||||
createApp().app.mount("#app");
|
||||
`,
|
||||
map: {
|
||||
mappings: '',
|
||||
},
|
||||
};
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
exports.uniAppJsEngineMainPlugin = uniAppJsEngineMainPlugin;
|
||||
Generated
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
import type { Plugin } from 'vite';
|
||||
export declare function getOutputManifestJson(): Record<string, any> | undefined;
|
||||
export declare function uniAppManifestPlugin(): Plugin;
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.uniAppManifestPlugin = exports.getOutputManifestJson = 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");
|
||||
let outputManifestJson = undefined;
|
||||
const isXHarmony = process.env.UNI_APP_X === 'true' &&
|
||||
process.env.UNI_UTS_PLATFORM === 'app-harmony';
|
||||
function getOutputManifestJson() {
|
||||
return outputManifestJson;
|
||||
}
|
||||
exports.getOutputManifestJson = getOutputManifestJson;
|
||||
function uniAppManifestPlugin() {
|
||||
const manifestJsonPath = path_1.default.resolve(process.env.UNI_INPUT_DIR, 'manifest.json');
|
||||
const manifestJsonUTSPath = path_1.default.resolve(process.env.UNI_INPUT_DIR, uni_cli_shared_1.MANIFEST_JSON_UTS);
|
||||
let manifestJson = {};
|
||||
return {
|
||||
name: 'uni:app-manifest',
|
||||
apply: 'build',
|
||||
resolveId(id) {
|
||||
if ((0, utils_1.isManifest)(id)) {
|
||||
return manifestJsonUTSPath;
|
||||
}
|
||||
},
|
||||
load(id) {
|
||||
if ((0, utils_1.isManifest)(id)) {
|
||||
return fs_extra_1.default.readFileSync(manifestJsonPath, 'utf8');
|
||||
}
|
||||
},
|
||||
transform(code, id) {
|
||||
if ((0, utils_1.isManifest)(id)) {
|
||||
this.addWatchFile(path_1.default.resolve(process.env.UNI_INPUT_DIR, 'manifest.json'));
|
||||
manifestJson = (0, uni_cli_shared_1.parseJson)(code, false, id);
|
||||
return {
|
||||
code: `export default ${JSON.stringify(manifestJson)}`,
|
||||
map: {
|
||||
mappings: '',
|
||||
},
|
||||
};
|
||||
}
|
||||
},
|
||||
buildEnd() {
|
||||
outputManifestJson = (0, utils_1.normalizeManifestJson)(manifestJson);
|
||||
const manifest = outputManifestJson;
|
||||
if (process.env.NODE_ENV !== 'development' || isXHarmony) {
|
||||
// 生产模式,记录使用到的modules
|
||||
const ids = Array.from(this.getModuleIds());
|
||||
const uniExtApis = new Set();
|
||||
ids.forEach((id) => {
|
||||
const moduleInfo = this.getModuleInfo(id);
|
||||
if (moduleInfo &&
|
||||
moduleInfo.meta &&
|
||||
Array.isArray(moduleInfo.meta.uniExtApis)) {
|
||||
moduleInfo.meta.uniExtApis.forEach((api) => {
|
||||
uniExtApis.add(api);
|
||||
});
|
||||
}
|
||||
});
|
||||
const { parseInjectModules, getPluginInjectApis, getPluginInjectComponents, } = (0, uni_cli_shared_1.resolveUTSCompiler)();
|
||||
const extApiComponents = (0, utils_1.getExtApiComponents)();
|
||||
// uts 插件里使用的 ext api 和组件
|
||||
const pluginInjectApis = getPluginInjectApis();
|
||||
const pluginInjectComponents = getPluginInjectComponents();
|
||||
if (uniExtApis.size ||
|
||||
extApiComponents.size ||
|
||||
pluginInjectApis.length ||
|
||||
pluginInjectComponents.length) {
|
||||
const modules = parseInjectModules([...uniExtApis, ...pluginInjectApis], {}, [...extApiComponents, ...pluginInjectComponents]);
|
||||
if (modules.length) {
|
||||
// 执行了摇树逻辑,就需要设置 modules 节点
|
||||
if (isXHarmony) {
|
||||
(0, utils_1.updateHarmonyManifestModules)(manifest, modules);
|
||||
}
|
||||
else {
|
||||
(0, utils_1.updateManifestModules)(manifest, modules);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
fs_extra_1.default.outputFileSync(path_1.default.resolve(process.env.UNI_OUTPUT_DIR, 'manifest.json'), JSON.stringify(manifest, null, 2));
|
||||
},
|
||||
};
|
||||
}
|
||||
exports.uniAppManifestPlugin = uniAppManifestPlugin;
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
import type { Plugin } from 'vite';
|
||||
export declare function uniAppPagesPlugin(): Plugin;
|
||||
+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.uniAppPagesPlugin = 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 utils_2 = require("../android/utils");
|
||||
function uniAppPagesPlugin() {
|
||||
const pagesJsonPath = path_1.default.resolve(process.env.UNI_INPUT_DIR, 'pages.json');
|
||||
const pagesJsonUTSPath = path_1.default.resolve(process.env.UNI_INPUT_DIR, uni_cli_shared_1.PAGES_JSON_UTS);
|
||||
let allPagePaths = [];
|
||||
let isFirst = true;
|
||||
return {
|
||||
name: 'uni:app-pages',
|
||||
apply: 'build',
|
||||
resolveId(id) {
|
||||
if ((0, utils_1.isPages)(id)) {
|
||||
return pagesJsonUTSPath;
|
||||
}
|
||||
},
|
||||
load(id) {
|
||||
if ((0, utils_1.isPages)(id)) {
|
||||
return fs_extra_1.default.readFileSync(pagesJsonPath, 'utf8');
|
||||
}
|
||||
},
|
||||
transform(code, id) {
|
||||
if (isFirst && allPagePaths.length) {
|
||||
const { filename } = (0, uni_cli_shared_1.parseVueRequest)(id);
|
||||
if ((0, utils_2.isVue)(filename)) {
|
||||
const vueFilename = (0, uni_cli_shared_1.removeExt)((0, uni_cli_shared_1.normalizePath)(path_1.default.relative(process.env.UNI_INPUT_DIR, filename)));
|
||||
// 项目内的
|
||||
if (!vueFilename.startsWith('.')) {
|
||||
// const index = allPagePaths.indexOf(pagePath)
|
||||
// if (index > -1) {
|
||||
if ((0, uni_cli_shared_1.runByHBuilderX)()) {
|
||||
console.log(`当前工程${allPagePaths.length}个页面,正在编译${vueFilename}...${'\u200b'}`);
|
||||
}
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
if ((0, utils_1.isPages)(id)) {
|
||||
this.addWatchFile(path_1.default.resolve(process.env.UNI_INPUT_DIR, 'pages.json'));
|
||||
// dark mode
|
||||
this.addWatchFile(path_1.default.resolve(process.env.UNI_INPUT_DIR, 'theme.json'));
|
||||
// pages.json
|
||||
const pagesJson = (0, uni_cli_shared_1.normalizeUniAppXAppPagesJson)(code);
|
||||
// add themeConfig - can move to uni-x/index.ts
|
||||
pagesJson.themeConfig = readThemeJSONFile();
|
||||
(0, utils_1.setGlobalPageOrientation)(pagesJson.globalStyle?.pageOrientation || '');
|
||||
allPagePaths = pagesJson.pages.map((p) => p.path);
|
||||
this.emitFile({
|
||||
fileName: uni_cli_shared_1.APP_CONFIG,
|
||||
type: 'asset',
|
||||
// 生成 app-config.js
|
||||
source: (0, uni_cli_shared_1.normalizeUniAppXAppConfig)(pagesJson, (0, uni_cli_shared_1.parseManifestJsonOnce)(process.env.UNI_INPUT_DIR)),
|
||||
});
|
||||
return {
|
||||
code: (0, uni_cli_shared_1.normalizeAppPagesJson)(pagesJson),
|
||||
map: { mappings: '' },
|
||||
};
|
||||
}
|
||||
},
|
||||
buildEnd() {
|
||||
isFirst = false;
|
||||
},
|
||||
};
|
||||
}
|
||||
exports.uniAppPagesPlugin = uniAppPagesPlugin;
|
||||
function readThemeJSONFile() {
|
||||
try {
|
||||
// 后续读取 theme location
|
||||
const themeJsonPath = path_1.default.resolve(process.env.UNI_INPUT_DIR, 'theme.json');
|
||||
let content = '{}';
|
||||
if (fs_extra_1.default.existsSync(themeJsonPath)) {
|
||||
content = fs_extra_1.default.readFileSync(themeJsonPath, 'utf8');
|
||||
}
|
||||
return JSON.parse(content);
|
||||
}
|
||||
catch (error) {
|
||||
console.error('read theme.json error:', error);
|
||||
}
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
import type { ResolvedConfig } from 'vite';
|
||||
import { type UniVitePlugin } from '@dcloudio/uni-cli-shared';
|
||||
export declare function initUniAppJsEngineCssPlugin(config: ResolvedConfig): void;
|
||||
export declare function createUniAppJsEnginePlugin(platform: 'app-ios' | 'app-harmony'): () => UniVitePlugin;
|
||||
+123
@@ -0,0 +1,123 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.createUniAppJsEnginePlugin = exports.initUniAppJsEngineCssPlugin = 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 css_1 = require("./css");
|
||||
function initUniAppJsEngineCssPlugin(config) {
|
||||
(0, uni_cli_shared_1.injectCssPlugin)(config, process.env.UNI_COMPILE_TARGET === 'uni_modules'
|
||||
? {
|
||||
createUrlReplacer: uni_cli_shared_1.createEncryptCssUrlReplacer,
|
||||
}
|
||||
: {});
|
||||
(0, uni_cli_shared_1.injectCssPostPlugin)(config, (0, css_1.uniAppCssPlugin)(config));
|
||||
}
|
||||
exports.initUniAppJsEngineCssPlugin = initUniAppJsEngineCssPlugin;
|
||||
function createUniAppJsEnginePlugin(platform) {
|
||||
return function uniAppJsEnginePlugin() {
|
||||
const inputDir = process.env.UNI_INPUT_DIR;
|
||||
const outputDir = process.env.UNI_OUTPUT_DIR;
|
||||
const uvueOutputDir = (0, uni_cli_shared_1.uvueOutDir)(platform);
|
||||
const tscOutputDir = (0, uni_cli_shared_1.tscOutDir)(platform);
|
||||
// 开始编译时,清空输出目录
|
||||
function emptyOutDir() {
|
||||
// ext-api 编译时,需要同时编译多个平台,并保留多个平台的输出目录
|
||||
if (process.env.UNI_COMPILE_TARGET === 'ext-api') {
|
||||
return;
|
||||
}
|
||||
if (fs_extra_1.default.existsSync(outputDir)) {
|
||||
(0, uni_cli_shared_1.emptyDir)(outputDir);
|
||||
}
|
||||
}
|
||||
emptyOutDir();
|
||||
function emptyUVueDir() {
|
||||
if (fs_extra_1.default.existsSync(uvueOutputDir)) {
|
||||
(0, uni_cli_shared_1.emptyDir)(uvueOutputDir);
|
||||
}
|
||||
}
|
||||
emptyUVueDir();
|
||||
function emptyTscDir() {
|
||||
if (fs_extra_1.default.existsSync(tscOutputDir)) {
|
||||
(0, uni_cli_shared_1.emptyDir)(tscOutputDir);
|
||||
}
|
||||
}
|
||||
emptyTscDir();
|
||||
return {
|
||||
name: 'uni:app-uts',
|
||||
apply: 'build',
|
||||
uni: (0, utils_1.createUniOptions)(platform),
|
||||
config() {
|
||||
return {
|
||||
base: '/', // 强制 base
|
||||
build: {
|
||||
sourcemap: (0, uni_cli_shared_1.enableSourceMap)(), //enableSourceMap() ? 'hidden' : false,
|
||||
emptyOutDir: false,
|
||||
assetsInlineLimit: 0,
|
||||
target: process.env.UNI_UTS_PLATFORM === 'app-ios'
|
||||
? [
|
||||
'ios12',
|
||||
'es2020',
|
||||
'edge88',
|
||||
'firefox78',
|
||||
'chrome87',
|
||||
'safari14',
|
||||
]
|
||||
: process.env.UNI_UTS_PLATFORM === 'app-harmony'
|
||||
? ['es2022']
|
||||
: undefined,
|
||||
rollupOptions: {
|
||||
input: (0, uni_cli_shared_1.resolveMainPathOnce)(inputDir),
|
||||
external: ['vue', '@vue/shared'],
|
||||
output: {
|
||||
name: 'AppService',
|
||||
banner: ``,
|
||||
format: 'iife',
|
||||
entryFileNames: uni_cli_shared_1.APP_SERVICE_FILENAME,
|
||||
globals: {
|
||||
vue: 'Vue',
|
||||
'@vue/shared': 'uni.VueShared',
|
||||
},
|
||||
sourcemapPathTransform: (relativeSourcePath, sourcemapPath) => {
|
||||
return (0, uni_cli_shared_1.normalizePath)(path_1.default.relative(process.env.UNI_INPUT_DIR, path_1.default.resolve(path_1.default.dirname(sourcemapPath), relativeSourcePath)));
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
},
|
||||
configResolved(config) {
|
||||
(0, utils_1.configResolved)(config);
|
||||
initUniAppJsEngineCssPlugin(config);
|
||||
},
|
||||
generateBundle(_, bundle) {
|
||||
const APP_SERVICE_FILENAME_MAP = uni_cli_shared_1.APP_SERVICE_FILENAME + '.map';
|
||||
const appServiceMap = bundle[APP_SERVICE_FILENAME_MAP];
|
||||
if (appServiceMap && appServiceMap.type === 'asset') {
|
||||
const source = JSON.parse(appServiceMap.source);
|
||||
source.sourceRoot = (0, uni_cli_shared_1.normalizePath)(inputDir);
|
||||
const newSourceMapFileName = path_1.default.resolve(process.env.UNI_APP_X_CACHE_DIR, 'sourcemap', APP_SERVICE_FILENAME_MAP);
|
||||
fs_extra_1.default.outputFileSync(newSourceMapFileName, JSON.stringify(source));
|
||||
delete bundle[APP_SERVICE_FILENAME_MAP];
|
||||
const appService = bundle[uni_cli_shared_1.APP_SERVICE_FILENAME];
|
||||
if (appService && appService.type === 'chunk') {
|
||||
appService.code = appService.code.replace(`//# sourceMappingURL=app-service.js.map`, `//# sourceMappingURL=` +
|
||||
path_1.default.relative(process.env.UNI_OUTPUT_DIR, newSourceMapFileName));
|
||||
}
|
||||
}
|
||||
},
|
||||
async writeBundle() {
|
||||
// x 上暂时编译所有uni ext api,不管代码里是否调用了
|
||||
// 框架内部编译时,不需要
|
||||
if (process.env.UNI_COMPILE_TARGET !== 'ext-api') {
|
||||
await (0, uni_cli_shared_1.buildUniExtApis)();
|
||||
}
|
||||
},
|
||||
};
|
||||
};
|
||||
}
|
||||
exports.createUniAppJsEnginePlugin = createUniAppJsEnginePlugin;
|
||||
Reference in New Issue
Block a user