UNPKG

rollup-plugin-svelte-draft

Version:
71 lines (70 loc) 3.43 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const rollup_pluginutils_1 = require("rollup-pluginutils"); const literator_1 = require("svelte-draft/dist/cli/literator"); const path_1 = require("path"); const fs_1 = require("fs"); const util_1 = require("util"); function SvelteDraft(options = {}) { const filter = rollup_pluginutils_1.createFilter(options.include, options.exclude); const ExistsAsync = util_1.promisify(fs_1.exists); return { resolveId(importee, importer) { return __awaiter(this, void 0, void 0, function* () { // importer of entry main.js is undefined, skip it if (!importer) return null; // console.log(`importee: ${importee}, importer: ${importer}`); const importee_absolute_path = path_1.resolve(importer, "..", importee); // importee is typedraft: *.js.tsx if (importee.endsWith(".js") && (yield ExistsAsync(`${importee_absolute_path}.tsx`))) { return `${importee_absolute_path}.tsx`; } // importee is svelte-draft: *.tsx else if (yield ExistsAsync(`${importee_absolute_path}.tsx`)) { return `${importee_absolute_path}.tsx`; } // importee is just typescript: *.ts else if (yield ExistsAsync(`${importee_absolute_path}.ts`)) { return `${importee_absolute_path}.ts`; } return null; }); }, transform(code, id) { return __awaiter(this, void 0, void 0, function* () { if (!filter(id)) return; let transformed = ""; const { config } = options; // console.log(`id: ${id}`); // it's typedraft or typescript file if (id.endsWith(".js.tsx") || id.endsWith(".ts")) { transformed = yield literator_1.TranscribeTypeDraftAsync(id, config); } // it's svelte-draft else if (id.endsWith(".tsx")) { transformed = yield literator_1.TranscribeSvelteDraftAsync(id, config); const css_path = id.replace(".tsx", ".css"); if (yield ExistsAsync(css_path)) { this.addWatchFile(css_path); } } return { code: transformed, map: null }; }); } }; } exports.default = SvelteDraft;