1 | "use strict";
|
2 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3 | if (k2 === undefined) k2 = k;
|
4 | Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
5 | }) : (function(o, m, k, k2) {
|
6 | if (k2 === undefined) k2 = k;
|
7 | o[k2] = m[k];
|
8 | }));
|
9 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
10 | Object.defineProperty(o, "default", { enumerable: true, value: v });
|
11 | }) : function(o, v) {
|
12 | o["default"] = v;
|
13 | });
|
14 | var __importStar = (this && this.__importStar) || function (mod) {
|
15 | if (mod && mod.__esModule) return mod;
|
16 | var result = {};
|
17 | if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
18 | __setModuleDefault(result, mod);
|
19 | return result;
|
20 | };
|
21 | Object.defineProperty(exports, "__esModule", { value: true });
|
22 | exports.sveltePreprocess = exports.runTransformer = void 0;
|
23 | const utils_1 = require("./modules/utils");
|
24 | const tagInfo_1 = require("./modules/tagInfo");
|
25 | const language_1 = require("./modules/language");
|
26 | const prepareContent_1 = require("./modules/prepareContent");
|
27 | const LANG_SPECIFIC_OPTIONS = {
|
28 | sass: {
|
29 | indentedSyntax: true,
|
30 | stripIndent: true,
|
31 | },
|
32 | pug: {
|
33 | stripIndent: true,
|
34 | },
|
35 | coffeescript: {
|
36 | stripIndent: true,
|
37 | },
|
38 | stylus: {
|
39 | stripIndent: true,
|
40 | },
|
41 | };
|
42 | exports.runTransformer = async (name, options, { content, map, filename, attributes }) => {
|
43 | if (options === false) {
|
44 | return { code: content };
|
45 | }
|
46 | if (typeof options === 'function') {
|
47 | return options({ content, map, filename, attributes });
|
48 | }
|
49 | const { transformer } = await Promise.resolve().then(() => __importStar(require(`./transformers/${name}`)));
|
50 | return transformer({
|
51 | content,
|
52 | filename,
|
53 | map,
|
54 | attributes,
|
55 | options: typeof options === 'boolean' ? null : options,
|
56 | });
|
57 | };
|
58 | function sveltePreprocess({ aliases, markupTagName = 'template', preserve = [], defaults, sourceMap = false, ...rest } = {}) {
|
59 | markupTagName = markupTagName.toLocaleLowerCase();
|
60 | const defaultLanguages = Object.freeze({
|
61 | markup: 'html',
|
62 | style: 'css',
|
63 | script: 'javascript',
|
64 | ...defaults,
|
65 | });
|
66 | const transformers = rest;
|
67 | const markupPattern = new RegExp(`<${markupTagName}([\\s\\S]*?)(?:>([\\s\\S]*)<\\/${markupTagName}>|/>)`);
|
68 | if (aliases === null || aliases === void 0 ? void 0 : aliases.length) {
|
69 | language_1.addLanguageAlias(aliases);
|
70 | }
|
71 | const getTransformerOptions = (name, alias) => {
|
72 | const { [name]: nameOpts, [alias]: aliasOpts } = transformers;
|
73 | if (typeof aliasOpts === 'function')
|
74 | return aliasOpts;
|
75 | if (typeof nameOpts === 'function')
|
76 | return nameOpts;
|
77 | if (aliasOpts === false || nameOpts === false)
|
78 | return false;
|
79 | const opts = {};
|
80 | if (typeof nameOpts === 'object') {
|
81 | Object.assign(opts, nameOpts);
|
82 | }
|
83 | if (name !== alias) {
|
84 | Object.assign(opts, LANG_SPECIFIC_OPTIONS[alias] || null);
|
85 | if (typeof aliasOpts === 'object') {
|
86 | Object.assign(opts, aliasOpts);
|
87 | }
|
88 | }
|
89 | if (sourceMap && name in language_1.SOURCE_MAP_PROP_MAP) {
|
90 | const [propName, value] = language_1.SOURCE_MAP_PROP_MAP[name];
|
91 | opts[propName] = value;
|
92 | }
|
93 | return opts;
|
94 | };
|
95 | const getTransformerTo = (type, targetLanguage) => async (svelteFile) => {
|
96 | let { content, filename, lang, alias, dependencies, attributes, } = await tagInfo_1.getTagInfo(svelteFile);
|
97 | if (lang == null || alias == null) {
|
98 | alias = defaultLanguages[type];
|
99 | lang = language_1.getLanguageFromAlias(alias);
|
100 | }
|
101 | if (preserve.includes(lang) || preserve.includes(alias)) {
|
102 | return { code: content };
|
103 | }
|
104 | const transformerOptions = getTransformerOptions(lang, alias);
|
105 | content = prepareContent_1.prepareContent({
|
106 | options: transformerOptions,
|
107 | content,
|
108 | });
|
109 | if (lang === targetLanguage) {
|
110 | return { code: content, dependencies };
|
111 | }
|
112 | const transformed = await exports.runTransformer(lang, transformerOptions, {
|
113 | content,
|
114 | filename,
|
115 | attributes,
|
116 | });
|
117 | return {
|
118 | ...transformed,
|
119 | dependencies: utils_1.concat(dependencies, transformed.dependencies),
|
120 | };
|
121 | };
|
122 | const scriptTransformer = getTransformerTo('script', 'javascript');
|
123 | const cssTransformer = getTransformerTo('style', 'css');
|
124 | const markupTransformer = getTransformerTo('markup', 'html');
|
125 | const markup = async ({ content, filename }) => {
|
126 | if (transformers.replace) {
|
127 | const transformed = await exports.runTransformer('replace', transformers.replace, { content, filename });
|
128 | content = transformed.code;
|
129 | }
|
130 | const templateMatch = content.match(markupPattern);
|
131 |
|
132 | if (!templateMatch) {
|
133 | return markupTransformer({ content, attributes: {}, filename });
|
134 | }
|
135 | const [fullMatch, attributesStr, templateCode] = templateMatch;
|
136 |
|
137 | const attributes = attributesStr
|
138 | .split(/\s+/)
|
139 | .filter(Boolean)
|
140 | .reduce((acc, attr) => {
|
141 | const [name, value] = attr.split('=');
|
142 |
|
143 | acc[name] = value ? value.replace(/['"]/g, '') : true;
|
144 | return acc;
|
145 | }, {});
|
146 |
|
147 | let { code, map, dependencies } = await markupTransformer({
|
148 | content: templateCode,
|
149 | attributes,
|
150 | filename,
|
151 | });
|
152 | code =
|
153 | content.slice(0, templateMatch.index) +
|
154 | code +
|
155 | content.slice(templateMatch.index + fullMatch.length);
|
156 | return { code, map, dependencies };
|
157 | };
|
158 | const script = async ({ content, attributes, filename, }) => {
|
159 | const transformResult = await scriptTransformer({
|
160 | content,
|
161 | attributes,
|
162 | filename,
|
163 | });
|
164 | let { code, map, dependencies, diagnostics } = transformResult;
|
165 | if (transformers.babel) {
|
166 | const transformed = await exports.runTransformer('babel', getTransformerOptions('babel'), {
|
167 | content: code,
|
168 | map,
|
169 | filename,
|
170 | attributes,
|
171 | });
|
172 | code = transformed.code;
|
173 | map = transformed.map;
|
174 | dependencies = utils_1.concat(dependencies, transformed.dependencies);
|
175 | diagnostics = utils_1.concat(diagnostics, transformed.diagnostics);
|
176 | }
|
177 | return { code, map, dependencies, diagnostics };
|
178 | };
|
179 | const style = async ({ content, attributes, filename, }) => {
|
180 | const transformResult = await cssTransformer({
|
181 | content,
|
182 | attributes,
|
183 | filename,
|
184 | });
|
185 | let { code, map, dependencies } = transformResult;
|
186 |
|
187 | if (await utils_1.hasDepInstalled('postcss')) {
|
188 | if (transformers.postcss) {
|
189 | const transformed = await exports.runTransformer('postcss', getTransformerOptions('postcss'), { content: code, map, filename, attributes });
|
190 | code = transformed.code;
|
191 | map = transformed.map;
|
192 | dependencies = utils_1.concat(dependencies, transformed.dependencies);
|
193 | }
|
194 | const transformed = await exports.runTransformer('globalStyle', getTransformerOptions('globalStyle'), { content: code, map, filename, attributes });
|
195 | code = transformed.code;
|
196 | map = transformed.map;
|
197 | }
|
198 | return { code, map, dependencies };
|
199 | };
|
200 | return {
|
201 | defaultLanguages,
|
202 | markup,
|
203 | script,
|
204 | style,
|
205 | };
|
206 | }
|
207 | exports.sveltePreprocess = sveltePreprocess;
|