-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
161 lines (137 loc) · 3.91 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
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
import { parse, walk } from 'svelte/compiler'
const defaultConfig = {
aliases: {
onMount: 'svelte',
beforeUpdate: 'svelte',
afterUpdate: 'svelte',
onDestroy: 'svelte',
tick: 'svelte',
setContext: 'svelte',
getContext: 'svelte',
createEventDispatcher: 'svelte',
writable: 'svelte/store',
readable: 'svelte/store',
derived: 'svelte/store',
get: 'svelte/store',
tweened: 'svelte/motion',
spring: 'svelte/motion',
fade: 'svelte/transition',
blur: 'svelte/transition',
fly: 'svelte/transition',
slide: 'svelte/transition',
scale: 'svelte/transition',
draw: 'svelte/transition',
flip: 'svelte/animate',
},
createEventDispatcher: 'dispatch',
}
const parseAlias = (x, as) =>
typeof x === 'string' ? { from: x, import: as, as } : { import: as, ...x, as }
const isDeclaration = (node, parent) => {
if (!parent) return false
switch (parent.type) {
case 'VariableDeclarator':
return true
case 'ImportDefaultSpecifier':
case 'ImportSpecifier':
return parent.local.name === node.name
}
return false
}
const parseConfig = (arg = {}) => {
if (typeof arg === 'function') {
return arg(defaultConfig)
}
return {
...defaultConfig,
...arg,
}
}
const trimStyle = code =>
code.replace(/<\s*style[^>]*>[\s\S]*?<\/\s*style[^>]*>/g, x =>
x.replace(/./g, ' ')
)
const createPreprocessor = ({
aliases,
createEventDispatcher,
} = defaultConfig) => ({
markup: async ({ content, filename }) => {
let code = content
// NOTE don't try to parse <style>
// there's nothing for us to be found in there, and it breaks with style
// preprocessors
// see: https://github.com/rixo/svelte-preprocess-autoimport/issues/2
const ast = parse(trimStyle(content))
const found = new Set()
const excluded = {}
const dispatches = []
walk(ast, {
enter(node, parent) {
switch (node.type) {
case 'Transition': // #4
case 'Animation':
case 'Identifier': {
const { name } = node
if (excluded[name]) break
if (isDeclaration(node, parent)) {
excluded[name] = true
break
}
if (createEventDispatcher && createEventDispatcher === name) {
dispatches.push(node)
break
}
if (aliases[name]) found.add(name)
break
}
}
},
})
const statements = []
const grouped = {}
if (dispatches.length > 0) {
grouped['svelte'] = [
'createEventDispatcher as ___spài_createEventDispatcher',
]
const targetName = createEventDispatcher.replace(/^(\$+)/, a =>
'_'.repeat(a.length)
)
for (const { start, end, name } of dispatches) {
code = code.slice(0, start) + targetName + code.slice(end)
}
statements.push(`const ${targetName} = ___spài_createEventDispatcher()`)
}
// aliases
if (found.size > 0) {
for (const alias of found) {
const { from, import: name, as } = parseAlias(aliases[alias], alias)
if (!grouped[from]) grouped[from] = []
grouped[from].push(name === as ? name : `${name} as ${as}`)
}
}
if (Object.keys(grouped).length > 0) {
statements.unshift(
Object.entries(grouped)
.map(
([from, names]) =>
`import { ${names.sort().join(', ')} } from '${from}'`
)
.join('; ')
)
}
if (statements.length > 0) {
const target = ast.instance
const line = statements.join('; ') + ';'
if (target) {
code =
code.slice(0, target.content.start) +
line +
code.slice(target.content.start)
} else {
code = code + `\n<script>${line}</script>`
}
}
return { code, map: null }
},
})
export default config => createPreprocessor(parseConfig(config))