svelte-preprocess
Version:
A Svelte preprocessor wrapper with baked in support for common used preprocessors
53 lines (52 loc) • 1.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTagInfo = void 0;
const fs_1 = require("fs");
const path_1 = require("path");
const language_1 = require("./language");
const resolveSrc = (importerFile, srcPath) => path_1.resolve(path_1.dirname(importerFile), srcPath);
const getSrcContent = (file) => {
return new Promise((resolve, reject) => {
fs_1.readFile(file, (error, data) => {
// istanbul ignore if
if (error)
reject(error);
else
resolve(data.toString());
});
});
};
async function doesFileExist(file) {
return new Promise((resolve) => fs_1.access(file, 0, (err) => resolve(!err)));
}
exports.getTagInfo = async ({ attributes, filename, content, }) => {
const dependencies = [];
/** only include src file if content of tag is empty */
if (attributes.src && content.trim().length === 0) {
// istanbul ignore if
if (typeof attributes.src !== 'string') {
throw new Error('src attribute must be string');
}
let path = attributes.src;
/** Only try to get local files (path starts with ./ or ../) */
if (path.match(/^(https?:)?\/\//) == null) {
path = resolveSrc(filename, path);
if (await doesFileExist(path)) {
content = await getSrcContent(path);
dependencies.push(path);
}
else {
console.warn(`[svelte-preprocess] The file "${path}" was not found.`);
}
}
}
const { lang, alias } = language_1.getLanguage(attributes);
return {
filename,
attributes,
content,
lang,
alias,
dependencies,
};
};