From 03895d5ec948e9d20b731901fc9b439dfb05525f Mon Sep 17 00:00:00 2001 From: dwightjack Date: Sun, 21 Nov 2021 17:56:30 +0900 Subject: [PATCH 1/2] add lang attribute to the script descriptor --- README.md | 1 + src/assembler.ts | 12 +++++------ src/compiler.ts | 7 +++++-- test/__snapshots__/compile.spec.ts.snap | 2 ++ test/compile.spec.ts | 28 +++++++++++++++++++++++++ test/fixtures/with-langs.vue | 7 ++++--- 6 files changed, 46 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index f13e8bd..1ea0e21 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,7 @@ interface DescriptorCompileResult { interface CompileResult { code: string + lang?: string map?: any } diff --git a/src/assembler.ts b/src/assembler.ts index 00e62c6..8996c4f 100644 --- a/src/assembler.ts +++ b/src/assembler.ts @@ -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 @@ -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) { @@ -241,7 +241,7 @@ export function assembleFromSource( btoa(unescape(encodeURIComponent(JSON.stringify(css.map)))) + ' */' } - + if ('styleSheet' in styleElement) { styleElement.styleSheet.cssText = code } else { @@ -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)) } diff --git a/src/compiler.ts b/src/compiler.ts index ff14650..d34c253 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -47,6 +47,7 @@ export interface ScriptOptions { export interface CompileResult { code: string + lang?: string map?: any } export type StyleCompileResult = StyleCompileResults & { @@ -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 @@ -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 diff --git a/test/__snapshots__/compile.spec.ts.snap b/test/__snapshots__/compile.spec.ts.snap index 2bda97a..19a6f17 100644 --- a/test/__snapshots__/compile.spec.ts.snap +++ b/test/__snapshots__/compile.spec.ts.snap @@ -17,6 +17,7 @@ export default { } } ", + "lang": undefined, "map": Object { "file": "foo.vue", "mappings": ";;;;;;AAMA;AACA;AACA;AACA;AACA", @@ -132,6 +133,7 @@ export default { } } ", + "lang": undefined, "map": Object { "file": "foo.vue", "mappings": ";;;;;;AAMA;AACA;AACA;AACA;AACA", diff --git a/test/compile.spec.ts b/test/compile.spec.ts index d6a6b85..3492824 100644 --- a/test/compile.spec.ts +++ b/test/compile.spec.ts @@ -61,6 +61,34 @@ function removeRawResult(result: DescriptorCompileResult): DescriptorCompileResu return result } + +test('detect script lang attribute', () => { + const source = ` + + + + + + ` + + const compiler = createDefaultCompiler() + const result = compiler.compileToDescriptor('foo.vue', source) + + expect(result.script.lang).toBe('ts') +}) + describe('when source contains css module', () => { const componentSource = ` -