UNPKG

code-workshop-kit

Version:
62 lines 3.54 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.queryTimestampModulesPlugin = void 0; const core_1 = require("@babel/core"); const path_1 = __importDefault(require("path")); const CwkStateSingleton_1 = require("../utils/CwkStateSingleton"); function queryTimestampModulesPlugin(dir) { return { name: 'query-timestamp-modules', transform(context) { var _a; let rewrittenBody = context.body; if (context.status === 200 && context.response.is('js') && CwkStateSingleton_1.cwkState.state.queryTimestamps && context.cookies.get('participant_name')) { // Get participant name from browser path const normalizedPath = context.path.endsWith('/') ? `${context.path}index.html` : context.path; const participantName = path_1.default.basename(path_1.default.dirname(decodeURI(normalizedPath))); const timestamp = CwkStateSingleton_1.cwkState.state.queryTimestamps[participantName]; if (timestamp) { try { rewrittenBody = ((_a = core_1.transformSync(rewrittenBody, { plugins: [ [ ({ types: t }) => ({ visitor: { ImportDeclaration(astPath, state) { if (astPath.node.source.value.startsWith('.') || astPath.node.source.value.startsWith('/')) { const participantFolderRelativeToServer = `${dir}/participants`.split(process.cwd())[1]; const importAbsoluteToServer = path_1.default.resolve(state.opts.url, '../', astPath.node.source.value); if (importAbsoluteToServer.startsWith(participantFolderRelativeToServer)) { // eslint-disable-next-line no-param-reassign astPath.node.source = t.stringLiteral(`${astPath.node.source.value}?mtime=${timestamp}`); } } }, }, }), { url: decodeURI(context.url) }, ], ], })) === null || _a === void 0 ? void 0 : _a.code) || rewrittenBody; } catch (e) { // WDS will already throw this error to the browser because it also tries to do babel parsing on it } } } return { body: rewrittenBody, transformCache: false }; }, }; } exports.queryTimestampModulesPlugin = queryTimestampModulesPlugin; //# sourceMappingURL=query-timestamp-modules-plugin.js.map