-
Notifications
You must be signed in to change notification settings - Fork 1
/
svelte.config.js
135 lines (115 loc) · 3.89 KB
/
svelte.config.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
import preprocess from 'svelte-preprocess'
import cssModules from 'svelte-preprocess-cssmodules'
import { parse } from 'svelte/compiler'
import { walk } from 'estree-walker'
import MagicString from 'magic-string'
const mixinsPath = import.meta.url.includes('node_modules') ? '~san-webkit/lib' : './src'
/** @type {import('@sveltejs/kit').Config} */
const config = {
// Consult https://kit.svelte.dev/docs/integrations#preprocessors
// for more information about preprocessors
preprocess: [
preprocess({
scss: {
prependData: `@import '${mixinsPath}/styles/fn.scss';`,
},
}),
componentStyleSelector(),
cssModules(),
],
}
export default config
export function componentStyleSelector() {
return {
markup: ({ content, filename }) => {
if (filename.includes('node_modules/')) return
if (content.lastIndexOf('</style') === -1) return
// TODO: Parse only non-script content [@vanguard | 18 Jul, 2023]
const ast = parse(content)
const s = new MagicString(content, { filename })
const ComponentEntries = {}
walk(ast.html, {
enter(node) {
if (node.type !== 'InlineComponent') return
const { name } = node
if (ComponentEntries[name]) {
ComponentEntries[name].tags.push(node)
} else {
ComponentEntries[name] = {
name,
styles: [],
tags: [node],
}
}
},
})
walk(ast.css, {
enter(node) {
if (node.type === 'Selector') {
this.skip()
let selector = null
node.children.forEach((child) => {
switch (child.type) {
case 'ClassSelector': {
if (!selector) return
if (!selector.classSelector) selector.classSelector = []
return selector.classSelector.push(child)
}
case 'TypeSelector': {
const { name } = child
if (name[0].toUpperCase() === name[0]) {
const entry = ComponentEntries[name]
selector = entry && child
return entry?.styles.push(child)
}
}
default: {
selector = null
}
}
})
}
},
})
Object.values(ComponentEntries).forEach(({ name, tags, styles }) => {
if (styles.length === 0) return
const className = `s-${hash(filename + name)}`
tags.forEach((node) => {
const classAttribute = node.attributes.find(({ name }) => name === 'class')
if (classAttribute) {
const { start, end } = classAttribute
let [, value] = s.slice(start, end).split('=')
if (value[0] === '{') value = `"${value}"`
value = `${value.slice(0, -1)} ${className}${value.slice(-1)}`
s.overwrite(start, end, `class=${value}`)
} else {
s.appendLeft(node.start + name.length + 1, ` class="${className}"`)
}
})
styles.forEach(({ start, end, classSelector }) => {
const classes =
classSelector
?.map((node) => {
end = node.end
return `.${node.name}`
})
.join('') || ''
s.overwrite(start, end, `:global(.${className}${classes})`, { storeName: true })
})
})
return {
code: s.toString(),
map: s.generateMap({ hires: true, file: filename }),
}
},
}
}
// https://github.com/darkskyapp/string-hash/blob/master/index.js
const regex_return_characters = /\r/g
function hash(str) {
str = str.replace(regex_return_characters, '')
let hash = 5381
let i = str.length
while (i--) hash = ((hash << 5) - hash) ^ str.charCodeAt(i)
return (hash >>> 0).toString(36)
}