This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: unplugin-vue-ce support ES module import css (#120)
* feat: unplugin-vue-ce support ES module import css * docs: updated @unplugin-vue-ce/sub-style es module import css document
- Loading branch information
1 parent
4d4f8ca
commit 441ad70
Showing
12 changed files
with
437 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { MagicStringBase } from 'magic-string-ast' | ||
import type { ESCSSImport, ESCSSParseRes, IESCSSValuePos, ILoc } from '../parse' | ||
|
||
export function injectStyleESCSS( | ||
mgcStr: MagicStringBase, | ||
parseRes: ESCSSParseRes, | ||
) { | ||
if (parseRes.scriptSetup) { | ||
const { loc, cssImport } = parseRes.scriptSetup | ||
genStyleTagCode(mgcStr, loc, cssImport) | ||
} | ||
|
||
if (parseRes.script) { | ||
const { loc, cssImport } = parseRes.script | ||
genStyleTagCode(mgcStr, loc, cssImport) | ||
} | ||
} | ||
|
||
function genStyleTagCode( | ||
mgcStr: MagicStringBase, | ||
loc: ILoc, | ||
cssImport: ESCSSImport, | ||
) { | ||
if (loc && cssImport) { | ||
for (const [key, css] of Object.entries(cssImport)) { | ||
css.forEach((v: IESCSSValuePos) => { | ||
const offset = mgcStr.original.length - mgcStr.length() | ||
mgcStr.appendRight(mgcStr.length() + offset, `<style lang="${key}"> @import "${v.value}";</style>\n | ||
`) | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { parse } from '@vue/compiler-sfc' | ||
import { log, setGlobalPrefix } from 'baiwusanyu-utils' | ||
import { parse as babelParse } from '@babel/parser' | ||
import { walk } from 'estree-walker-ts' | ||
import type { SFCParseOptions } from '@vue/compiler-sfc' | ||
import type { ImportDeclaration, StringLiteral } from '@babel/types' | ||
|
||
interface ILocPos { | ||
column: number | ||
line: number | ||
offset: number | ||
} | ||
export interface ILoc { | ||
source: string | ||
start: ILocPos | ||
end: ILocPos | ||
} | ||
|
||
export interface IESCSSValuePos { | ||
value: string | ||
start: number | ||
end: number | ||
} | ||
|
||
export interface ESCSSImport { | ||
css: IESCSSValuePos[] | ||
sass: IESCSSValuePos[] | ||
less: IESCSSValuePos[] | ||
scss: IESCSSValuePos[] | ||
} | ||
export interface ESCSSParseRes { | ||
script: { | ||
loc?: ILoc // 替换 sfc 源码 | ||
cssImport: ESCSSImport | ||
} | ||
scriptSetup: { | ||
loc?: ILoc // 替换 sfc 源码 | ||
cssImport: ESCSSImport | ||
} | ||
} | ||
export function parseESCSS( | ||
code: string, | ||
options: SFCParseOptions) { | ||
const parseSFCRes = parse(code, options) | ||
if (parseSFCRes.errors) { | ||
setGlobalPrefix('[unplugin-vue-ce]:') | ||
parseSFCRes.errors.forEach((error) => { | ||
log('error', error.message) | ||
}) | ||
} | ||
|
||
const { descriptor } = parseSFCRes | ||
const parseRes: ESCSSParseRes = { | ||
script: { | ||
cssImport: { | ||
css: [], | ||
sass: [], | ||
less: [], | ||
scss: [], | ||
}, | ||
}, | ||
scriptSetup: { | ||
cssImport: { | ||
css: [], | ||
sass: [], | ||
less: [], | ||
scss: [], | ||
}, | ||
}, | ||
} | ||
if (descriptor.scriptSetup) { | ||
const { loc } = descriptor.scriptSetup | ||
parseRes.scriptSetup.loc = loc | ||
parseScript(loc.source, parseRes, 'scriptSetup') | ||
} | ||
if (descriptor.script) { | ||
const { loc } = descriptor.script | ||
parseRes.script.loc = loc | ||
parseScript(loc.source, parseRes, 'script') | ||
} | ||
return parseRes | ||
} | ||
|
||
function parseScript( | ||
code: string, | ||
parseRes: ESCSSParseRes, | ||
type: 'scriptSetup' | 'script', | ||
) { | ||
const ast = babelParse(code, { | ||
sourceType: 'module', | ||
plugins: ['typescript'], | ||
}) | ||
|
||
const regx = /\.(css|sass|less|scss)$/ | ||
;(walk as any)(ast, { | ||
enter( | ||
node: ImportDeclaration | StringLiteral, | ||
parent: ImportDeclaration | StringLiteral, | ||
) { | ||
if (parent && node.type === 'StringLiteral' | ||
&& parent.type === 'ImportDeclaration') { | ||
const matched = node.value.match(regx) | ||
if (matched) { | ||
const key = matched[1] as 'css' | 'sass' | 'less' | 'scss' | ||
parseRes[type].cssImport[key].push({ | ||
value: node.value, | ||
start: parent.start!, | ||
end: parent.end!, | ||
}) | ||
} | ||
} | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import type { ESCSSImport, ESCSSParseRes, IESCSSValuePos, ILoc } from '../parse' | ||
import type { MagicStringBase } from 'magic-string-ast' | ||
|
||
export function transformESCSS( | ||
mgcStr: MagicStringBase, | ||
parseRes: ESCSSParseRes, | ||
) { | ||
if (parseRes.scriptSetup) { | ||
const { loc, cssImport } = parseRes.scriptSetup | ||
overwriteCode(mgcStr, loc, cssImport) | ||
} | ||
|
||
if (parseRes.script) { | ||
const { loc, cssImport } = parseRes.script | ||
overwriteCode(mgcStr, loc, cssImport) | ||
} | ||
} | ||
|
||
function overwriteCode( | ||
mgcStr: MagicStringBase, | ||
loc: ILoc, | ||
cssImport: ESCSSImport, | ||
) { | ||
if (loc && cssImport) { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
for (const [_, css] of Object.entries(cssImport)) { | ||
css.forEach((v: IESCSSValuePos) => { | ||
mgcStr.update(v.start + loc.start.offset, v.end + loc.start.offset, '') | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
p { | ||
background-color: #ffcd94; | ||
} | ||
body { | ||
background-color: aliceblue; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.