-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
105 lines (88 loc) · 2.7 KB
/
index.js
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
const path = require('path');
const { Transformer } = require('@parcel/plugin');
const { default: SourceMap } = require('@parcel/source-map');
const { relativeUrl } = require('@parcel/utils');
const { compile, preprocess } = require('svelte/compiler.js');
const { load, preSerialize, postDeserialize } = require('./loadConfig');
Object.defineProperty(exports, '__esModule', { value: true });
function generateName(input) {
let name = path
.basename(input)
.replace(path.extname(input), '')
.replace(/[^a-zA-Z_$0-9]+/g, '_')
.replace(/^_/, '')
.replace(/_$/, '')
.replace(/^(\d)/, '_$1');
return name[0].toUpperCase() + name.slice(1);
}
function extractSourceMaps(projectRoot, asset, originalSourceMap, sourceMap) {
if (!sourceMap) return originalSourceMap;
sourceMap.sources = [asset.filePath];
const map = new SourceMap(projectRoot);
map.addVLQMap(sourceMap);
if (originalSourceMap) {
map.extends(originalSourceMap.toBuffer());
}
return map;
}
async function handleError(sourceFileName, func) {
try {
return await func();
} catch (error) {
throw new Error(`Error in file ${sourceFileName}: ${error}`);
}
}
exports.default = new Transformer({
loadConfig({ config, options, logger }) {
return load({ config, options, logger });
},
preSerializeConfig({ config }) {
return preSerialize(config);
},
postDeserializeConfig({ config, options }) {
return postDeserialize(config, options);
},
async transform({ asset, config, options }) {
let code = await asset.getCode();
const originalSourceMap = await asset.getMap();
const sourceFileName = relativeUrl(options.projectRoot, asset.filePath);
const compilerOptions = {
...(config ? config.raw.compilerOptions : null),
filename: sourceFileName,
name: generateName(sourceFileName),
};
if (config && config.hydrated.preprocess) {
const preprocessed = await handleError(sourceFileName, () =>
preprocess(code, config.hydrated.preprocess, compilerOptions),
);
code = preprocessed.toString();
}
const { js, css } = await handleError(sourceFileName, () =>
compile(code, compilerOptions),
);
return [
{
type: 'js',
content: js.code,
uniqueKey: `${asset.id}-js`,
map: extractSourceMaps(
options.projectRoot,
asset,
originalSourceMap,
js.map,
),
},
Boolean(css && css.code) && {
type: 'css',
content: css.code,
uniqueKey: `${asset.id}-css`,
map: extractSourceMaps(
options.projectRoot,
asset,
originalSourceMap,
css.map,
),
},
].filter(Boolean);
},
});