Skip to content

Commit 9156efc

Browse files
committed
feat: 🎸 add sourceMap prop to configuration object
1 parent d34125a commit 9156efc

20 files changed

+1607
-203
lines changed

Diff for: ‎examples/svelte-rollup/yarn.lock

+1,271
Large diffs are not rendered by default.

Diff for: ‎src/autoProcess.ts

+153-120
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,21 @@ import {
1010
import { hasDepInstalled } from './modules/hasDepInstalled';
1111
import { concat } from './modules/concat';
1212
import { getTagInfo } from './modules/tagInfo';
13-
import { addLanguageAlias, getLanguageFromAlias } from './modules/language';
14-
import { throwError } from './modules/errors';
13+
import {
14+
addLanguageAlias,
15+
getLanguageFromAlias,
16+
SOURCE_MAP_PROP_MAP,
17+
} from './modules/language';
1518
import { prepareContent } from './modules/prepareContent';
1619

20+
type AutoPreprocessGroup = PreprocessorGroup & {
21+
defaultLanguages: Readonly<{
22+
markup: string;
23+
style: string;
24+
script: string;
25+
}>;
26+
};
27+
1728
type AutoPreprocessOptions = {
1829
markupTagName?: string;
1930
aliases?: Array<[string, string]>;
@@ -23,6 +34,7 @@ type AutoPreprocessOptions = {
2334
style?: string;
2435
script?: string;
2536
};
37+
sourceMap?: boolean;
2638

2739
// transformers
2840
typescript?: TransformerOptions<Options.Typescript>;
@@ -64,21 +76,15 @@ export const runTransformer = async (
6476
return options({ content, map, filename, attributes });
6577
}
6678

67-
try {
68-
const { transformer } = await import(`./transformers/${name}`);
79+
const { transformer } = await import(`./transformers/${name}`);
6980

70-
return transformer({
71-
content,
72-
filename,
73-
map,
74-
attributes,
75-
options: typeof options === 'boolean' ? null : options,
76-
});
77-
} catch (e) {
78-
throwError(
79-
`Error transforming '${name}'.\n\nMessage:\n${e.message}\n\nStack:\n${e.stack}`,
80-
);
81-
}
81+
return transformer({
82+
content,
83+
filename,
84+
map,
85+
attributes,
86+
options: typeof options === 'boolean' ? null : options,
87+
});
8288
};
8389

8490
export function autoPreprocess(
@@ -87,17 +93,18 @@ export function autoPreprocess(
8793
markupTagName = 'template',
8894
preserve = [],
8995
defaults,
96+
sourceMap = false,
9097
...rest
91-
}: AutoPreprocessOptions = {} as AutoPreprocessOptions,
92-
): PreprocessorGroup {
98+
} = {} as AutoPreprocessOptions,
99+
): AutoPreprocessGroup {
93100
markupTagName = markupTagName.toLocaleLowerCase();
94101

95-
const defaultLanguages = {
102+
const defaultLanguages = Object.freeze({
96103
markup: 'html',
97104
style: 'css',
98105
script: 'javascript',
99106
...defaults,
100-
};
107+
});
101108

102109
const transformers = rest as Transformers;
103110
const markupPattern = new RegExp(
@@ -108,30 +115,37 @@ export function autoPreprocess(
108115
addLanguageAlias(aliases);
109116
}
110117

111-
const optionsCache: Record<string, any> = {};
112118
const getTransformerOptions = (
113-
lang: string,
114-
alias: string,
119+
name: string,
120+
alias?: string,
115121
): TransformerOptions<unknown> => {
116-
if (typeof transformers[alias] === 'function') return transformers[alias];
117-
if (typeof transformers[lang] === 'function') return transformers[lang];
118-
if (optionsCache[alias] != null) return optionsCache[alias];
122+
const { [name]: nameOpts, [alias]: aliasOpts } = transformers;
123+
124+
if (typeof aliasOpts === 'function') return aliasOpts;
125+
if (typeof nameOpts === 'function') return nameOpts;
126+
if (aliasOpts === false || nameOpts === false) return false;
119127

120128
const opts: TransformerOptions<unknown> = {};
121129

122-
if (typeof transformers[lang] === 'object') {
123-
Object.assign(opts, transformers[lang]);
130+
if (typeof nameOpts === 'object') {
131+
Object.assign(opts, nameOpts);
124132
}
125133

126-
if (lang !== alias) {
134+
if (name !== alias) {
127135
Object.assign(opts, ALIAS_OPTION_OVERRIDES[alias] || null);
128136

129-
if (typeof transformers[alias] === 'object') {
130-
Object.assign(opts, transformers[alias]);
137+
if (typeof aliasOpts === 'object') {
138+
Object.assign(opts, aliasOpts);
131139
}
132140
}
133141

134-
return (optionsCache[alias] = opts);
142+
if (sourceMap && name in SOURCE_MAP_PROP_MAP) {
143+
const [propName, value] = SOURCE_MAP_PROP_MAP[name];
144+
145+
opts[propName] = value;
146+
}
147+
148+
return opts;
135149
};
136150

137151
const getTransformerTo = (
@@ -183,112 +197,131 @@ export function autoPreprocess(
183197
const cssTransformer = getTransformerTo('style', 'css');
184198
const markupTransformer = getTransformerTo('markup', 'html');
185199

186-
return {
187-
async markup({ content, filename }) {
188-
if (transformers.replace) {
189-
const transformed = await runTransformer(
190-
'replace',
191-
transformers.replace,
192-
{ content, filename },
193-
);
200+
const markup: PreprocessorGroup['markup'] = async ({ content, filename }) => {
201+
if (transformers.replace) {
202+
const transformed = await runTransformer(
203+
'replace',
204+
transformers.replace,
205+
{ content, filename },
206+
);
194207

195-
content = transformed.code;
196-
}
208+
content = transformed.code;
209+
}
197210

198-
const templateMatch = content.match(markupPattern);
211+
const templateMatch = content.match(markupPattern);
199212

200-
/** If no <template> was found, just return the original markup */
201-
if (!templateMatch) {
202-
return { code: content };
203-
}
213+
/** If no <template> was found, just return the original markup */
214+
if (!templateMatch) {
215+
return { code: content };
216+
}
217+
218+
const [fullMatch, attributesStr, templateCode] = templateMatch;
219+
220+
/** Transform an attribute string into a key-value object */
221+
const attributes = attributesStr
222+
.split(/\s+/)
223+
.filter(Boolean)
224+
.reduce((acc: Record<string, string | boolean>, attr) => {
225+
const [name, value] = attr.split('=');
226+
227+
// istanbul ignore next
228+
acc[name] = value ? value.replace(/['"]/g, '') : true;
229+
230+
return acc;
231+
}, {});
232+
233+
/** Transform the found template code */
234+
let { code, map, dependencies } = await markupTransformer({
235+
content: templateCode,
236+
attributes,
237+
filename,
238+
});
204239

205-
const [fullMatch, attributesStr, templateCode] = templateMatch;
206-
207-
/** Transform an attribute string into a key-value object */
208-
const attributes = attributesStr
209-
.split(/\s+/)
210-
.filter(Boolean)
211-
.reduce((acc: Record<string, string | boolean>, attr) => {
212-
const [name, value] = attr.split('=');
213-
214-
// istanbul ignore next
215-
acc[name] = value ? value.replace(/['"]/g, '') : true;
216-
217-
return acc;
218-
}, {});
219-
220-
/** Transform the found template code */
221-
let { code, map, dependencies } = await markupTransformer({
222-
content: templateCode,
223-
attributes,
224-
filename,
225-
});
226-
227-
code =
228-
content.slice(0, templateMatch.index) +
229-
code +
230-
content.slice(templateMatch.index + fullMatch.length);
231-
232-
return { code, map, dependencies };
233-
},
234-
async script({ content, attributes, filename }) {
235-
const transformResult: Processed = await scriptTransformer({
236-
content,
237-
attributes,
238-
filename,
239-
});
240-
241-
let { code, map, dependencies, diagnostics } = transformResult;
242-
243-
if (transformers.babel) {
244-
const transformed = await runTransformer('babel', transformers.babel, {
240+
code =
241+
content.slice(0, templateMatch.index) +
242+
code +
243+
content.slice(templateMatch.index + fullMatch.length);
244+
245+
return { code, map, dependencies };
246+
};
247+
248+
const script: PreprocessorGroup['script'] = async ({
249+
content,
250+
attributes,
251+
filename,
252+
}) => {
253+
const transformResult: Processed = await scriptTransformer({
254+
content,
255+
attributes,
256+
filename,
257+
});
258+
259+
let { code, map, dependencies, diagnostics } = transformResult;
260+
261+
if (transformers.babel) {
262+
const transformed = await runTransformer(
263+
'babel',
264+
getTransformerOptions('babel'),
265+
{
245266
content: code,
246267
map,
247268
filename,
248269
attributes,
249-
});
270+
},
271+
);
250272

251-
code = transformed.code;
252-
map = transformed.map;
253-
dependencies = concat(dependencies, transformed.dependencies);
254-
diagnostics = concat(diagnostics, transformed.diagnostics);
255-
}
273+
code = transformed.code;
274+
map = transformed.map;
275+
dependencies = concat(dependencies, transformed.dependencies);
276+
diagnostics = concat(diagnostics, transformed.diagnostics);
277+
}
256278

257-
return { code, map, dependencies, diagnostics };
258-
},
259-
async style({ content, attributes, filename }) {
260-
const transformResult = await cssTransformer({
261-
content,
262-
attributes,
263-
filename,
264-
});
265-
266-
let { code, map, dependencies } = transformResult;
267-
268-
if (await hasDepInstalled('postcss')) {
269-
if (transformers.postcss) {
270-
const transformed = await runTransformer(
271-
'postcss',
272-
transformers.postcss,
273-
{ content: code, map, filename, attributes },
274-
);
275-
276-
code = transformed.code;
277-
map = transformed.map;
278-
dependencies = concat(dependencies, transformed.dependencies);
279-
}
279+
return { code, map, dependencies, diagnostics };
280+
};
281+
282+
const style: PreprocessorGroup['style'] = async ({
283+
content,
284+
attributes,
285+
filename,
286+
}) => {
287+
const transformResult = await cssTransformer({
288+
content,
289+
attributes,
290+
filename,
291+
});
292+
293+
let { code, map, dependencies } = transformResult;
280294

295+
if (await hasDepInstalled('postcss')) {
296+
if (transformers.postcss) {
281297
const transformed = await runTransformer(
282-
'globalStyle',
283-
transformers?.globalStyle,
298+
'postcss',
299+
getTransformerOptions('postcss'),
284300
{ content: code, map, filename, attributes },
285301
);
286302

287303
code = transformed.code;
288304
map = transformed.map;
305+
dependencies = concat(dependencies, transformed.dependencies);
289306
}
290307

291-
return { code, map, dependencies };
292-
},
308+
const transformed = await runTransformer(
309+
'globalStyle',
310+
getTransformerOptions('globalStyle'),
311+
{ content: code, map, filename, attributes },
312+
);
313+
314+
code = transformed.code;
315+
map = transformed.map;
316+
}
317+
318+
return { code, map, dependencies };
319+
};
320+
321+
return {
322+
defaultLanguages,
323+
markup,
324+
script,
325+
style,
293326
};
294327
}

Diff for: ‎src/modules/language.ts

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@ import { basename } from 'path';
22

33
import { PreprocessorArgs } from '../types';
44

5+
export const SOURCE_MAP_PROP_MAP: Record<string, [string, any]> = {
6+
babel: ['sourceMaps', true],
7+
typescript: ['sourceMap', true],
8+
scss: ['sourceMap', true],
9+
less: ['sourceMap', {}],
10+
stylus: ['sourcemap', true],
11+
postcss: ['map', true],
12+
coffeescript: ['sourceMap', true],
13+
globalStyle: ['sourceMap', true],
14+
};
15+
516
export const ALIAS_MAP = new Map([
617
['pcss', 'css'],
718
['postcss', 'css'],

Diff for: ‎src/transformers/babel.ts

-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ const transformer: Transformer<Options.Babel> = async ({
88
options,
99
map = undefined,
1010
}) => {
11-
if (!options || typeof options !== 'object') {
12-
return { code: content, map };
13-
}
14-
1511
const { code, map: sourcemap } = await transformAsync(content, {
1612
...options,
1713
inputSourceMap: map as any,

0 commit comments

Comments
 (0)