Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Commit

Permalink
feat: Skip style injection when style soure is empty (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
znck authored May 6, 2018
1 parent 0068c0f commit 5726a24
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions src/assembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@ export function assembleFromSource(
template = template || { source: '' }

const hasScopedStyle = styles.some(style => style.scoped === true)
const hasStyle = styles.length > 0
const hasStyle = styles.some(style => Boolean(style.source))
const e = (any: any): string => JSON.stringify(any)
const createImport = (name: string, value: string) => value.startsWith('~')
? `import ${name} from '${value.substr(1)}'`
: `const ${name} = ${value}`
const createImport = (name: string, value: string) =>
value.startsWith('~')
? `import ${name} from '${value.substr(1)}'`
: `const ${name} = ${value}`
const o = e
const IDENTIFIER = /^[a-z0-9]+$/i

Expand Down Expand Up @@ -293,21 +294,35 @@ const __vue_template__ = typeof __vue_render__ !== 'undefined'
/* style */
const __vue_inject_styles__ = ${hasStyle} ? function (inject) {
if (!inject) return
${styles.map(
(style, index) => {
const source = IDENTIFIER.test(style.source) ? style.source : e(style.source)
const map = !compiler.template.isProduction
? (typeof style.map === 'string' && IDENTIFIER.test(style.map) ? style.map : o(style.map))
: undefined
const tokens = typeof style.module === 'string' && IDENTIFIER.test(style.module) ? style.module : o(style.module)
return `inject("${scopeId + '_' + index}", { source: ${source}, map: ${map}, media: ${e(style.media)} })` +
'\n' +
${styles.map((style, index) => {
const source = IDENTIFIER.test(style.source)
? style.source
: e(style.source)
const map = !compiler.template.isProduction
? typeof style.map === 'string' && IDENTIFIER.test(style.map)
? style.map
: o(style.map)
: undefined
const tokens =
typeof style.module === 'string' && IDENTIFIER.test(style.module)
? style.module
: o(style.module)
return (
(source
? `inject("${scopeId +
'_' +
index}", { source: ${source}, map: ${map}, media: ${e(
style.media
)} })\n`
: '') +
(style.moduleName
? `Object.defineProperty(this, "${style.moduleName}", { value: ${tokens} })` + '\n'
? `Object.defineProperty(this, "${
style.moduleName
}", { value: ${tokens} })` + '\n'
: '')
}
)}
)
})}
} : undefined
/* scoped */
const __vue_scope_id__ = ${e(hasScopedStyle)} ? "${scopeId}" : undefined
Expand Down

0 comments on commit 5726a24

Please sign in to comment.