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

Add lang attribute to the CompileResult object #122

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ interface DescriptorCompileResult {

interface CompileResult {
code: string
lang?: string
map?: any
}

Expand Down
12 changes: 6 additions & 6 deletions src/assembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as path from 'path'

export interface AssembleSource {
filename: string
script?: { source: string; map?: any }
script?: { source: string; map?: any, lang?: string }
template?: { source: string; functional?: boolean }
styles: Array<{
source: string
Expand Down Expand Up @@ -221,14 +221,14 @@ export function assembleFromSource(
var styleElement = document.createElement('style')
styleElement.type = 'text/css'
shadowRoot.appendChild(styleElement)

return styleElement
}
}

return function addStyle(id, css) {
const styleElement = createStyleElement(shadowRoot)
if (css.media) styleElement.setAttribute('media', css.media)

let code = css.source

if (${e(compiler.template.isProduction)} && css.map) {
Expand All @@ -241,7 +241,7 @@ export function assembleFromSource(
btoa(unescape(encodeURIComponent(JSON.stringify(css.map)))) +
' */'
}

if ('styleSheet' in styleElement) {
styleElement.styleSheet.cssText = code
} else {
Expand Down Expand Up @@ -306,7 +306,7 @@ export function assembleFromSource(
component._ssrRegister = hook
}
else if (style) {
hook = shadowMode
hook = shadowMode
? function(context) {
style.call(this, createInjectorShadow(context, this.$root.$options.shadowRoot))
}
Expand Down
7 changes: 5 additions & 2 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface ScriptOptions {

export interface CompileResult {
code: string
lang?: string
map?: any
}
export type StyleCompileResult = StyleCompileResults & {
Expand Down Expand Up @@ -113,7 +114,8 @@ export class SFCCompiler {
code: rawScript.src
? this.read(rawScript.src, filename)
: rawScript.content,
map: rawScript.map
map: rawScript.map,
lang: rawScript.lang
}
: undefined

Expand Down Expand Up @@ -159,7 +161,8 @@ export class SFCCompiler {
code: rawScript.src
? this.read(rawScript.src, filename)
: rawScript.content,
map: rawScript.map
map: rawScript.map,
lang: rawScript.lang
}
: undefined

Expand Down
2 changes: 2 additions & 0 deletions test/__snapshots__/compile.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default {
}
}
",
"lang": undefined,
"map": Object {
"file": "foo.vue",
"mappings": ";;;;;;AAMA;AACA;AACA;AACA;AACA",
Expand Down Expand Up @@ -132,6 +133,7 @@ export default {
}
}
",
"lang": undefined,
"map": Object {
"file": "foo.vue",
"mappings": ";;;;;;AAMA;AACA;AACA;AACA;AACA",
Expand Down
12 changes: 10 additions & 2 deletions test/baseline.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,25 @@ const { join, resolve } = require('path')
import { build, open } from './setup/utils'

let browser = null
let finished = false
const fixtures = readdirSync(join(__dirname, 'fixtures'))
.filter(it => it.endsWith('.vue'))
.map(it => it.replace(/\.vue$/i, ''))

beforeAll(async () => {
beforeAll(async function setup () {
if (finished) {
return
}
browser = await puppeteer.launch({
args: ['--no-sandbox', '--disable-setuid-sandbox'],
headless: Boolean(process.env.CI)
})
browser.on('disconnected', setup);
})
afterAll(async () => {
finished = true
browser && (await browser.close())
})
afterAll(async () => browser && (await browser.close()))

fixtures.forEach(it =>
test(it, async () => {
Expand Down
28 changes: 28 additions & 0 deletions test/compile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,34 @@ function removeRawResult(result: DescriptorCompileResult): DescriptorCompileResu
return result
}


test('detect script lang attribute', () => {
const source = `
<template>
<h1 id="test">Hello {{ name }}!</h1>
</template>

<script lang="ts">
export default {
data () {
return { name: 'John Doe' }
}
}
</script>

<style>
.title {
color: red;
}
</style>
`

const compiler = createDefaultCompiler()
const result = compiler.compileToDescriptor('foo.vue', source)

expect(result.script.lang).toBe('ts')
})

describe('when source contains css module', () => {
const componentSource = `
<template>
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/with-langs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
h1#test.title Hello {{ name }}!
</template>

<script>
<script lang="ts">
export default {
data () {
return { name: 'John Doe' }
Expand Down