Skip to content

Commit 812730d

Browse files
authored
feat: support custom element styles (#173)
1 parent 6fb4c4f commit 812730d

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed

src/store.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ export interface Store {
104104
reloadLanguageTools?: undefined | (() => void)
105105
initialShowOutput: boolean
106106
initialOutputMode: OutputModes
107+
customElement: boolean | string | RegExp | (string | RegExp)[]
107108
}
108109

109110
export interface StoreOptions {
@@ -115,6 +116,7 @@ export interface StoreOptions {
115116
defaultVueRuntimeURL?: string
116117
defaultVueRuntimeProdURL?: string
117118
defaultVueServerRendererURL?: string
119+
customElement?: boolean | string | RegExp | (string | RegExp)[]
118120
}
119121

120122
export class ReplStore implements Store {
@@ -126,6 +128,7 @@ export class ReplStore implements Store {
126128
initialShowOutput: boolean
127129
initialOutputMode: OutputModes
128130
reloadLanguageTools: undefined | (() => void)
131+
customElement: boolean | string | RegExp | (string | RegExp)[]
129132

130133
private defaultVueRuntimeDevURL: string
131134
private defaultVueRuntimeProdURL: string
@@ -140,6 +143,7 @@ export class ReplStore implements Store {
140143
showOutput = false,
141144
outputMode = 'preview',
142145
productionMode = false,
146+
customElement = /\.ce\.vue$/,
143147
}: StoreOptions = {}) {
144148
const files: StoreState['files'] = {}
145149

@@ -157,6 +161,7 @@ export class ReplStore implements Store {
157161
this.defaultVueRuntimeProdURL = defaultVueRuntimeProdURL
158162
this.defaultVueServerRendererURL = defaultVueServerRendererURL
159163
this.initialShowOutput = showOutput
164+
this.customElement = customElement
160165
this.initialOutputMode = outputMode as OutputModes
161166

162167
let mainFile = defaultMainFile

src/transform.ts

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,33 @@ export async function compileFile(
167167
)
168168
}
169169

170-
if (clientCode || ssrCode) {
171-
appendSharedCode(
172-
`\n${COMP_IDENTIFIER}.__file = ${JSON.stringify(filename)}` +
173-
`\nexport default ${COMP_IDENTIFIER}`
174-
)
175-
compiled.js = clientCode.trimStart()
176-
compiled.ssr = ssrCode.trimStart()
170+
// styles
171+
const ceFilter = store.customElement
172+
function isCustomElement(filters: typeof ceFilter): boolean {
173+
if (typeof filters === 'boolean') {
174+
return filters
175+
}
176+
177+
if (typeof filters === 'string') {
178+
return filters.includes(filename)
179+
} else if (
180+
!Array.isArray(filters) &&
181+
Object.prototype.toString.call(filters) === '[object RegExp]'
182+
) {
183+
return filters.test(filename)
184+
}
185+
186+
if (Array.isArray(filters)) {
187+
return filters.some((filter) => {
188+
return isCustomElement(filter)
189+
})
190+
}
191+
return false
177192
}
193+
let isCE = isCustomElement(ceFilter)
178194

179-
// styles
180195
let css = ''
196+
let styles: string[] = []
181197
for (const style of descriptor.styles) {
182198
if (style.module) {
183199
return [`<style module> is not supported in the playground.`]
@@ -199,13 +215,27 @@ export async function compileFile(
199215
}
200216
// proceed even if css compile errors
201217
} else {
202-
css += styleResult.code + '\n'
218+
isCE ? styles.push(styleResult.code) : (css += styleResult.code + '\n')
203219
}
204220
}
205221
if (css) {
206222
compiled.css = css.trim()
207223
} else {
208-
compiled.css = '/* No <style> tags present */'
224+
compiled.css = isCE ? (compiled.css = '/* The component style of the custom element will be compiled into the component object */')
225+
: ('/* No <style> tags present */')
226+
}
227+
228+
if (clientCode || ssrCode) {
229+
const ceStyles = isCE
230+
? `\n${COMP_IDENTIFIER}.styles = ${JSON.stringify(styles)}`
231+
: ''
232+
appendSharedCode(
233+
`\n${COMP_IDENTIFIER}.__file = ${JSON.stringify(filename)}` +
234+
ceStyles +
235+
`\nexport default ${COMP_IDENTIFIER}`
236+
)
237+
compiled.js = clientCode.trimStart()
238+
compiled.ssr = ssrCode.trimStart()
209239
}
210240

211241
return []

0 commit comments

Comments
 (0)