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

Commit 03895d5

Browse files
committed
add lang attribute to the script descriptor
1 parent 845a310 commit 03895d5

File tree

6 files changed

+46
-11
lines changed

6 files changed

+46
-11
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ interface DescriptorCompileResult {
5858

5959
interface CompileResult {
6060
code: string
61+
lang?: string
6162
map?: any
6263
}
6364

src/assembler.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import * as path from 'path'
77

88
export interface AssembleSource {
99
filename: string
10-
script?: { source: string; map?: any }
10+
script?: { source: string; map?: any, lang?: string }
1111
template?: { source: string; functional?: boolean }
1212
styles: Array<{
1313
source: string
@@ -221,14 +221,14 @@ export function assembleFromSource(
221221
var styleElement = document.createElement('style')
222222
styleElement.type = 'text/css'
223223
shadowRoot.appendChild(styleElement)
224-
224+
225225
return styleElement
226-
}
226+
}
227227
228228
return function addStyle(id, css) {
229229
const styleElement = createStyleElement(shadowRoot)
230230
if (css.media) styleElement.setAttribute('media', css.media)
231-
231+
232232
let code = css.source
233233
234234
if (${e(compiler.template.isProduction)} && css.map) {
@@ -241,7 +241,7 @@ export function assembleFromSource(
241241
btoa(unescape(encodeURIComponent(JSON.stringify(css.map)))) +
242242
' */'
243243
}
244-
244+
245245
if ('styleSheet' in styleElement) {
246246
styleElement.styleSheet.cssText = code
247247
} else {
@@ -306,7 +306,7 @@ export function assembleFromSource(
306306
component._ssrRegister = hook
307307
}
308308
else if (style) {
309-
hook = shadowMode
309+
hook = shadowMode
310310
? function(context) {
311311
style.call(this, createInjectorShadow(context, this.$root.$options.shadowRoot))
312312
}

src/compiler.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export interface ScriptOptions {
4747

4848
export interface CompileResult {
4949
code: string
50+
lang?: string
5051
map?: any
5152
}
5253
export type StyleCompileResult = StyleCompileResults & {
@@ -113,7 +114,8 @@ export class SFCCompiler {
113114
code: rawScript.src
114115
? this.read(rawScript.src, filename)
115116
: rawScript.content,
116-
map: rawScript.map
117+
map: rawScript.map,
118+
lang: rawScript.lang
117119
}
118120
: undefined
119121

@@ -159,7 +161,8 @@ export class SFCCompiler {
159161
code: rawScript.src
160162
? this.read(rawScript.src, filename)
161163
: rawScript.content,
162-
map: rawScript.map
164+
map: rawScript.map,
165+
lang: rawScript.lang
163166
}
164167
: undefined
165168

test/__snapshots__/compile.spec.ts.snap

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export default {
1717
}
1818
}
1919
",
20+
"lang": undefined,
2021
"map": Object {
2122
"file": "foo.vue",
2223
"mappings": ";;;;;;AAMA;AACA;AACA;AACA;AACA",
@@ -132,6 +133,7 @@ export default {
132133
}
133134
}
134135
",
136+
"lang": undefined,
135137
"map": Object {
136138
"file": "foo.vue",
137139
"mappings": ";;;;;;AAMA;AACA;AACA;AACA;AACA",

test/compile.spec.ts

+28
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,34 @@ function removeRawResult(result: DescriptorCompileResult): DescriptorCompileResu
6161
return result
6262
}
6363

64+
65+
test('detect script lang attribute', () => {
66+
const source = `
67+
<template>
68+
<h1 id="test">Hello {{ name }}!</h1>
69+
</template>
70+
71+
<script lang="ts">
72+
export default {
73+
data () {
74+
return { name: 'John Doe' }
75+
}
76+
}
77+
</script>
78+
79+
<style>
80+
.title {
81+
color: red;
82+
}
83+
</style>
84+
`
85+
86+
const compiler = createDefaultCompiler()
87+
const result = compiler.compileToDescriptor('foo.vue', source)
88+
89+
expect(result.script.lang).toBe('ts')
90+
})
91+
6492
describe('when source contains css module', () => {
6593
const componentSource = `
6694
<template>

test/fixtures/with-langs.vue

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
h1#test.title Hello {{ name }}!
33
</template>
44

5-
<script>
6-
export default {
5+
<script lang="ts">
6+
import Vue from 'vue'
7+
export default Vue.extend({
78
data () {
89
return { name: 'John Doe' }
910
}
10-
}
11+
})
1112
</script>
1213

1314
<style lang="scss">

0 commit comments

Comments
 (0)