File size: 9,071 Bytes
bc20498 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.sveltePreprocess = exports.transform = void 0;
const utils_1 = require("./modules/utils");
const tagInfo_1 = require("./modules/tagInfo");
const language_1 = require("./modules/language");
const prepareContent_1 = require("./modules/prepareContent");
const markup_1 = require("./modules/markup");
const TARGET_LANGUAGES = Object.freeze({
markup: 'html',
style: 'css',
script: 'javascript',
});
const transform = async (name, options, { content, markup, map, filename, attributes }) => {
if (name == null || options === false) {
return { code: content };
}
if (typeof options === 'function') {
return options({ content, map, filename, attributes });
}
// todo: maybe add a try-catch here looking for module-not-found errors
const { transformer } = await Promise.resolve(`${`./transformers/${name}`}`).then(s => __importStar(require(s)));
return transformer({
content,
markup,
filename,
map,
attributes,
options: typeof options === 'boolean' ? null : options,
});
};
exports.transform = transform;
function sveltePreprocess(_a) {
var _b, _c;
var { aliases, markupTagName = 'template', preserve = [], sourceMap = (_c = ((_b = process === null || process === void 0 ? void 0 : process.env) === null || _b === void 0 ? void 0 : _b.NODE_ENV) === 'development') !== null && _c !== void 0 ? _c : false, ...rest } = _a === void 0 ? {} : _a;
const transformers = rest;
if (aliases === null || aliases === void 0 ? void 0 : aliases.length) {
(0, language_1.addLanguageAlias)(aliases);
}
function resolveLanguageArgs(lang, alias) {
const langOpts = transformers[lang];
const aliasOpts = alias ? transformers[alias] : undefined;
const opts = {};
if (typeof langOpts === 'object') {
Object.assign(opts, langOpts);
}
Object.assign(opts, (0, language_1.getLanguageDefaults)(lang), (0, language_1.getLanguageDefaults)(alias));
if (lang !== alias && typeof aliasOpts === 'object') {
Object.assign(opts, aliasOpts);
}
if (sourceMap && lang in language_1.SOURCE_MAP_PROP_MAP) {
const [path, value] = language_1.SOURCE_MAP_PROP_MAP[lang];
(0, utils_1.setProp)(opts, path, value);
}
return opts;
}
function getTransformerOptions(lang, alias, { ignoreAliasOverride } = {}) {
if (lang == null)
return null;
const langOpts = transformers[lang];
const aliasOpts = alias ? transformers[alias] : undefined;
if (!ignoreAliasOverride && typeof aliasOpts === 'function') {
return aliasOpts;
}
if (typeof langOpts === 'function')
return langOpts;
if (aliasOpts === false || langOpts === false)
return false;
return resolveLanguageArgs(lang, alias);
}
const getTransformerTo = (type, targetLanguage) => async (svelteFile) => {
let { content, markup, filename, lang, alias, dependencies, attributes } = await (0, tagInfo_1.getTagInfo)(svelteFile);
if (lang == null || alias == null) {
alias = TARGET_LANGUAGES[type];
lang = (0, language_1.getLanguageFromAlias)(alias);
}
if ((lang && preserve.includes(lang)) || preserve.includes(alias)) {
return { code: content };
}
const transformerOptions = getTransformerOptions(lang, alias);
content = (0, prepareContent_1.prepareContent)({
options: transformerOptions,
content,
});
if (lang === targetLanguage) {
// has override method for alias
// example: sugarss override should work apart from postcss
if (typeof transformerOptions === 'function' && alias !== lang) {
return transformerOptions({ content, filename, attributes });
}
// otherwise, we're done here
return { code: content, dependencies };
}
const transformed = await (0, exports.transform)(lang, transformerOptions, {
content,
markup,
filename,
attributes,
});
return {
...transformed,
dependencies: (0, utils_1.concat)(dependencies, transformed.dependencies),
};
};
const scriptTransformer = getTransformerTo('script', 'javascript');
const cssTransformer = getTransformerTo('style', 'css');
const markupTransformer = getTransformerTo('markup', 'html');
const markup = async ({ content, filename }) => {
if (transformers.replace) {
const transformed = await (0, exports.transform)('replace', transformers.replace, {
content,
markup: content,
filename,
});
content = transformed.code;
}
return (0, markup_1.transformMarkup)({ content, filename }, markupTransformer, {
// we only pass the markupTagName because the rest of options
// is fetched internally by the `markupTransformer`
markupTagName,
});
};
const script = async ({ content, attributes, markup: fullMarkup, filename, }) => {
const transformResult = await scriptTransformer({
content,
attributes,
markup: fullMarkup,
filename,
});
let { code, map, dependencies, diagnostics } = transformResult;
if (transformers.babel) {
const transformed = await (0, exports.transform)('babel', getTransformerOptions('babel'), { content: code, markup: fullMarkup, map, filename, attributes });
code = transformed.code;
map = transformed.map;
dependencies = (0, utils_1.concat)(dependencies, transformed.dependencies);
diagnostics = (0, utils_1.concat)(diagnostics, transformed.diagnostics);
}
return { code, map, dependencies, diagnostics };
};
const style = async ({ content, attributes, markup: fullMarkup, filename, }) => {
const transformResult = await cssTransformer({
content,
attributes,
markup: fullMarkup,
filename,
});
let { code, map, dependencies } = transformResult;
const hasPostcss = await (0, utils_1.hasDepInstalled)('postcss');
// istanbul ignore else
if (hasPostcss) {
if (transformers.postcss) {
const { alias, lang } = (0, language_1.getLanguage)(attributes);
const postcssOptions = getTransformerOptions('postcss', (0, language_1.isAliasOf)(alias, lang) ? alias : null,
// todo: this seems wrong and ugly
{ ignoreAliasOverride: true });
const transformed = await (0, exports.transform)('postcss', postcssOptions, {
content: code,
markup: fullMarkup,
map,
filename,
attributes,
});
code = transformed.code;
map = transformed.map;
dependencies = (0, utils_1.concat)(dependencies, transformed.dependencies);
}
const transformed = await (0, exports.transform)('globalStyle', getTransformerOptions('globalStyle'), { content: code, markup: fullMarkup, map, filename, attributes });
code = transformed.code;
map = transformed.map;
}
else if ('global' in attributes) {
console.warn(`[svelte-preprocess] 'global' attribute found, but 'postcss' is not installed. 'postcss' is used to walk through the CSS and transform any necessary selector.`);
}
return { code, map, dependencies };
};
return {
markup,
script,
style,
};
}
exports.sveltePreprocess = sveltePreprocess;
|