H5-contact/node_modules/@dcloudio/uni-console/lib/uni.plugin.js

91 lines
4.1 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
var path = require('path');
var uniCliShared = require('@dcloudio/uni-cli-shared');
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
var path__default = /*#__PURE__*/_interopDefault(path);
const uniConsoleRuntimePlugin = () => {
return {
name: 'uni:console:runtime',
config() {
const isProd = process.env.NODE_ENV === 'production';
let keepOriginal = true;
if (process.env.UNI_PLATFORM == 'mp-harmony' ||
process.env.UNI_PLATFORM === 'app-harmony') {
keepOriginal = false;
}
return {
define: {
'process.env.UNI_CONSOLE_KEEP_ORIGINAL': process.env
.UNI_CONSOLE_KEEP_ORIGINAL
? process.env.UNI_CONSOLE_KEEP_ORIGINAL === 'true'
: keepOriginal,
'process.env.UNI_SOCKET_HOSTS': JSON.stringify(isProd ? '' : process.env.UNI_SOCKET_HOSTS),
'process.env.UNI_SOCKET_PORT': JSON.stringify(isProd ? '' : process.env.UNI_SOCKET_PORT),
'process.env.UNI_SOCKET_ID': JSON.stringify(isProd ? '' : process.env.UNI_SOCKET_ID),
},
};
},
};
};
var index = () => {
return [
uniConsoleRuntimePlugin(),
uniCliShared.defineUniMainJsPlugin((opts) => {
let hasRuntimeSocket = uniCliShared.isEnableConsole();
const isX = process.env.UNI_APP_X === 'true';
const useUniAppXAndroidNative = uniCliShared.isUniAppXAndroidNative();
// 基座类型为custom时不启用运行时socket
// 需要判断自定义基座是否包含socket模块有的话才可以启用
if (isX && process.env.UNI_PLATFORM === 'app') {
if (process.env.HX_USE_BASE_TYPE === 'custom') {
hasRuntimeSocket = false;
}
}
let uniConsolePath = uniCliShared.resolveBuiltIn(path__default.default.join('@dcloudio/uni-console', `dist/${(process.env.UNI_PLATFORM || '').startsWith('mp-') ? 'mp' : 'index'}.esm.js`));
if (isX) {
if (useUniAppXAndroidNative) {
uniConsolePath = uniCliShared.resolveBuiltIn(path__default.default.join('@dcloudio/uni-console', 'src/runtime/app/index.ts'));
}
else if (process.env.UNI_UTS_PLATFORM === 'app-ios' ||
process.env.UNI_UTS_PLATFORM === 'app-android') {
uniConsolePath = uniCliShared.resolveBuiltIn(path__default.default.join('@dcloudio/uni-console', 'dist/app.esm.js'));
}
}
else {
if (process.env.UNI_PLATFORM === 'app-harmony') {
uniConsolePath = uniCliShared.resolveBuiltIn(path__default.default.join('@dcloudio/uni-console', 'dist/harmony.jsvm.esm.js'));
}
}
return {
name: 'uni:console-main-js',
enforce:
// 仅旧版 Android x 需要提前不然拿到的code是解析后的仅保留import语句的
useUniAppXAndroidNative ? 'pre' : 'post',
transform(code, id) {
if (!hasRuntimeSocket) {
return;
}
const independentRoot = uniCliShared.parseIndependentMainRoot(id);
if (!opts.filter(id) && !independentRoot) {
return;
}
const runtimePath = independentRoot
? uniCliShared.withIndependentRoot(uniConsolePath, independentRoot)
: uniCliShared.normalizePath(uniConsolePath);
return {
// 采用绝对路径引入此时tsc失效代码里需要自己处理好各种类型问题
code: `import '${runtimePath}';${code}`,
map: null,
};
},
};
}),
];
};
module.exports = index;