code-workshop-kit
Version:
The future of remote code workshops & training
49 lines • 2.98 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.componentReplacersPlugin = void 0;
const path_1 = __importDefault(require("path"));
function componentReplacersPlugin(cfg) {
// subtract the current working dir from absolute dir to get the dir relative to the server root
const pathRelativeToServer = cfg.absoluteDir.split(process.cwd())[1];
return {
name: 'component-replacers',
transform(context) {
var _a;
let rewrittenBody = context.body;
if (context.status === 200) {
if (context.path === '/node_modules/code-workshop-kit/dist/components/SelectCookie.js' ||
context.path === '/dist/components/SelectCookie.js' ||
context.path === '/node_modules/code-workshop-kit/dist/components/AppShell.js' ||
context.path === '/dist/components/AppShell.js') {
rewrittenBody = rewrittenBody.replace(new RegExp('placeholder-import.js', 'g'), path_1.default.posix.resolve('/', `${pathRelativeToServer}/cwk.config.js`));
}
if (context.path ===
'/node_modules/code-workshop-kit/dist/components/ParticipantFrontendCapsule.js' ||
context.path === '/dist/components/ParticipantFrontendCapsule.js' ||
context.path ===
'/node_modules/code-workshop-kit/dist/components/ParticipantTerminalCapsule.js' ||
context.path === '/dist/components/ParticipantCapsule.js' ||
context.path ===
'/node_modules/code-workshop-kit/dist/components/ParticipantCapsule.js' ||
context.path === '/dist/components/ParticipantCapsule.js') {
let replacement = `${path_1.default.posix.resolve('/', pathRelativeToServer)}`;
// remove trailing slash
if (replacement.endsWith('/') || replacement.endsWith('\\')) {
replacement = replacement.substring(0, replacement.length - 1);
}
rewrittenBody = rewrittenBody.replace(new RegExp('%dir%', 'g'), replacement);
}
if (context.path === '/node_modules/code-workshop-kit/dist/components/AppShell.js' ||
context.path === '/dist/components/AppShell.js') {
rewrittenBody = rewrittenBody.replace(new RegExp("this.mode = 'iframe';", 'g'), `this.mode = '${((_a = cfg.targetOptions) === null || _a === void 0 ? void 0 : _a.mode) || 'iframe'}';`);
}
}
return { body: rewrittenBody };
},
};
}
exports.componentReplacersPlugin = componentReplacersPlugin;
//# sourceMappingURL=component-replacers-plugin.js.map