86 lines
3.2 KiB
JavaScript
86 lines
3.2 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.uniMouseToTouchPlugin = exports.getMouseToTouchScript = void 0;
|
|
const fs_1 = __importDefault(require("fs"));
|
|
const path_1 = __importDefault(require("path"));
|
|
const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
|
|
let script;
|
|
function getMouseToTouchScript() {
|
|
if (script === undefined) {
|
|
const sourcePath = require.resolve('hammer-touchemulator/touch-emulator.js');
|
|
const source = fs_1.default.readFileSync(sourcePath, 'utf8');
|
|
script = `${source}\n;window.TouchEmulator();`;
|
|
}
|
|
return script;
|
|
}
|
|
exports.getMouseToTouchScript = getMouseToTouchScript;
|
|
function isEnabled() {
|
|
return ((0, uni_cli_shared_1.getPlatformManifestJson)((0, uni_cli_shared_1.parseManifestJsonOnce)(process.env.UNI_INPUT_DIR), 'h5').mouseToTouch === true);
|
|
}
|
|
function uniMouseToTouchPlugin() {
|
|
let config;
|
|
let scriptReferenceId;
|
|
let getBuildFileName;
|
|
function scriptSrc() {
|
|
const fileName = getBuildFileName
|
|
? getBuildFileName()
|
|
: path_1.default.posix.join(config.build.assetsDir, 'touch-emulator.js');
|
|
return config.base + fileName;
|
|
}
|
|
return {
|
|
name: 'uni:mouse-to-touch',
|
|
configResolved(resolvedConfig) {
|
|
config = resolvedConfig;
|
|
},
|
|
buildStart() {
|
|
scriptReferenceId = undefined;
|
|
getBuildFileName = undefined;
|
|
if (config.command === 'build' && !config.build.ssr && isEnabled()) {
|
|
scriptReferenceId = this.emitFile({
|
|
type: 'asset',
|
|
name: 'touch-emulator.js',
|
|
source: getMouseToTouchScript(),
|
|
});
|
|
}
|
|
},
|
|
renderStart() {
|
|
const referenceId = scriptReferenceId;
|
|
if (referenceId) {
|
|
getBuildFileName = () => this.getFileName(referenceId);
|
|
}
|
|
},
|
|
configureServer(server) {
|
|
const pathname = new URL(scriptSrc(), 'http://localhost').pathname;
|
|
server.middlewares.use((req, res, next) => {
|
|
if (!isEnabled() ||
|
|
(req.method !== 'GET' && req.method !== 'HEAD') ||
|
|
req.url?.split('?')[0] !== pathname) {
|
|
return next();
|
|
}
|
|
res.setHeader('Content-Type', 'text/javascript; charset=utf-8');
|
|
res.setHeader('Cache-Control', 'no-cache');
|
|
res.end(req.method === 'HEAD' ? undefined : getMouseToTouchScript());
|
|
});
|
|
},
|
|
transformIndexHtml: {
|
|
order: 'post',
|
|
handler() {
|
|
if (!isEnabled()) {
|
|
return;
|
|
}
|
|
return [
|
|
{
|
|
tag: 'script',
|
|
attrs: { src: scriptSrc() },
|
|
injectTo: 'head-prepend',
|
|
},
|
|
];
|
|
},
|
|
},
|
|
};
|
|
}
|
|
exports.uniMouseToTouchPlugin = uniMouseToTouchPlugin;
|