Skip to content

Commit

Permalink
refactor(language-core): use internal property to pass style module info
Browse files Browse the repository at this point in the history
  • Loading branch information
KazariEX committed Dec 11, 2024
1 parent 47f6715 commit fce856b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
22 changes: 14 additions & 8 deletions packages/language-core/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,6 @@ export interface SfcBlock {
attrs: Record<string, string | true>;
}

export interface SFCStyleOverride {
module?: {
name: string;
offset?: number;
};
}

export interface Sfc {
content: string;
template: SfcBlock & {
Expand All @@ -126,8 +119,12 @@ export interface Sfc {
genericOffset: number;
ast: ts.SourceFile;
} | undefined;
styles: readonly (SfcBlock & SFCStyleOverride & {
styles: readonly (SfcBlock & {
scoped: boolean;
module?: {
name: string;
offset?: number;
};
cssVars: {
text: string;
offset: number;
Expand All @@ -142,6 +139,15 @@ export interface Sfc {
})[];
}

declare module '@vue/compiler-sfc' {
interface SFCStyleBlock {
__module?: {
name: string;
offset?: number;
};
}
}

export interface TextRange {
start: number;
end: number;
Expand Down
6 changes: 2 additions & 4 deletions packages/language-core/lib/utils/parseSfc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { ElementNode, SourceLocation } from '@vue/compiler-dom';
import * as compiler from '@vue/compiler-dom';
import type { CompilerError, SFCBlock, SFCDescriptor, SFCParseResult, SFCScriptBlock, SFCStyleBlock, SFCTemplateBlock } from '@vue/compiler-sfc';
import { SFCStyleOverride } from '../types';

export function parse(source: string): SFCParseResult {

Expand Down Expand Up @@ -92,8 +91,7 @@ function createBlock(node: ElementNode, source: string) {
};
const attrs: Record<string, any> = {};
const block: SFCBlock
& Pick<SFCStyleBlock, 'scoped'>
& Pick<SFCStyleOverride, 'module'>
& Pick<SFCStyleBlock, 'scoped' | '__module'>
& Pick<SFCScriptBlock, 'setup'> = {
type,
content,
Expand All @@ -114,7 +112,7 @@ function createBlock(node: ElementNode, source: string) {
block.scoped = true;
}
else if (p.name === 'module') {
block.module = {
block.__module = {
name: p.value?.content ?? '$style',
offset: p.value?.content ? p.value?.loc.start.offset - node.loc.start.offset : undefined
};
Expand Down
10 changes: 5 additions & 5 deletions packages/language-core/lib/virtualFile/computedSfc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type * as CompilerDOM from '@vue/compiler-dom';
import type { SFCBlock, SFCParseResult } from '@vue/compiler-sfc';
import { computed, ISignal, Signal, System, Unstable } from 'alien-signals';
import type * as ts from 'typescript';
import type { Sfc, SfcBlock, SFCStyleOverride, VueLanguagePluginReturn } from '../types';
import type { Sfc, SfcBlock, VueLanguagePluginReturn } from '../types';
import { parseCssClassNames } from '../utils/parseCssClassNames';
import { parseCssVars } from '../utils/parseCssVars';

Expand Down Expand Up @@ -118,10 +118,10 @@ export function computedSfc(
(block, i) => {
const base = computedSfcBlock('style_' + i, 'css', block);
const module = computed(() => {
const _module = block.get().module as SFCStyleOverride['module'];
return _module ? {
name: _module.name,
offset: _module.offset ? base.start + _module.offset : undefined
const { __module } = block.get();
return __module ? {
name: __module.name,
offset: __module.offset ? base.start + __module.offset : undefined
} : undefined;
});
const scoped = computed(() => !!block.get().scoped);
Expand Down

0 comments on commit fce856b

Please sign in to comment.