-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
185 additions
and
28 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { defineBuildConfig } from 'unbuild' | ||
export default defineBuildConfig({ | ||
externals: ['sass'] | ||
}) |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './js' | ||
export * from './sass' | ||
export * from './vue' |
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,21 @@ | ||
import type { Loader, LoaderResult } from '../loader' | ||
|
||
export const sassLoader: Loader = async (input, { options }) => { | ||
if (!['.sass', '.scss'].includes(input.extension)) { | ||
return | ||
} | ||
|
||
const compileString = await import('sass').then(r => r.compileString || r.default.compileString) | ||
|
||
const output: LoaderResult = [] | ||
|
||
const contents = await input.getContents() | ||
|
||
output.push({ | ||
contents: compileString(contents).css, | ||
path: input.path, | ||
extension: `.${options.ext || 'css'}` | ||
}) | ||
|
||
return output | ||
} |
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 |
---|---|---|
@@ -1,44 +1,86 @@ | ||
import type { Loader } from '../loader' | ||
import type { Loader, LoaderResult } from '../loader' | ||
|
||
export const vueLoader: Loader = async (input, { loadFile }) => { | ||
export const vueLoader: Loader = async (input, ctx) => { | ||
if (input.extension !== '.vue') { | ||
return | ||
} | ||
|
||
const output: LoaderResult = [{ | ||
path: input.path, | ||
contents: await input.getContents() | ||
}] | ||
|
||
let earlyReturn = true | ||
|
||
for (const blockLoader of [sassLoader, scriptLoader]) { | ||
const result = await blockLoader({ ...input, getContents: () => output[0].contents! }, ctx) | ||
if (!result) { continue } | ||
|
||
earlyReturn = false | ||
const [vueFile, ...files] = result | ||
output[0] = vueFile | ||
output.push(...files) | ||
} | ||
|
||
if (earlyReturn) { return } | ||
|
||
return output | ||
} | ||
|
||
interface BlockLoaderOptions { | ||
type: 'script' | 'style' | 'template' | ||
outputLang: string | ||
defaultLang?: string | ||
validExtensions?: string[] | ||
exclude?: RegExp[] | ||
} | ||
|
||
const vueBlockLoader = (opts: BlockLoaderOptions): Loader => async (input, { loadFile }) => { | ||
const contents = await input.getContents() | ||
|
||
const BLOCK_RE = new RegExp(`<${opts.type}((\\s[^>\\s]*)*)>([\\S\\s.]*?)<\\/${opts.type}>`) | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const [scriptBlock, attrs = '', _lastAttr, script] = contents.match(/<script((\s[^>\s]*)*)>([\S\s.]*?)<\/script>/) || [] | ||
if (!scriptBlock || !script) { | ||
const [block, attrs = '', _, blockContents] = contents.match(BLOCK_RE) || [] | ||
|
||
if (!block || !blockContents) { | ||
return | ||
} | ||
|
||
// do not transpile `<script setup>`, #14 | ||
if (attrs.split(/\s+/g).includes('setup')) { | ||
if (opts.exclude?.some(re => re.test(attrs))) { | ||
return | ||
} | ||
|
||
const [, lang = 'js'] = attrs.match(/lang="([a-z]*)"/) || [] | ||
const [, lang = opts.outputLang] = attrs.match(/lang="([a-z]*)"/) || [] | ||
const extension = '.' + lang | ||
|
||
const files = await loadFile({ | ||
getContents: () => script, | ||
getContents: () => blockContents, | ||
path: `${input.path}${extension}`, | ||
srcPath: `${input.srcPath}${extension}`, | ||
extension | ||
}) || [] | ||
|
||
const scriptFile = files.find(f => ['.js', '.mjs'].includes(f.extension!)) | ||
if (!scriptFile) { | ||
return | ||
} | ||
const blockOutputFile = files.find(f => f.extension === `.${opts.outputLang}` || opts.validExtensions?.includes(f.extension!)) | ||
if (!blockOutputFile) { return } | ||
|
||
const newAttrs = attrs.replace(new RegExp(`\\s?lang="${lang}"`), '') | ||
|
||
return [ | ||
{ | ||
path: input.path, | ||
contents: contents.replace(scriptBlock, `<script${newAttrs}>\n${scriptFile.contents}</script>`) | ||
contents: contents.replace(block, `<${opts.type}${newAttrs}>\n${blockOutputFile.contents?.trim()}\n</${opts.type}>`) | ||
}, | ||
...files.filter(f => f !== scriptFile) | ||
...files.filter(f => f !== blockOutputFile) | ||
] | ||
} | ||
|
||
const sassLoader = vueBlockLoader({ | ||
outputLang: 'css', | ||
type: 'style' | ||
}) | ||
|
||
const scriptLoader = vueBlockLoader({ | ||
outputLang: 'js', | ||
type: 'script', | ||
exclude: [/\bsetup\b/], | ||
validExtensions: ['.js', '.mjs'] | ||
}) |
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 |
---|---|---|
|
@@ -14,3 +14,11 @@ export default { | |
}) | ||
} | ||
</script> | ||
|
||
<style lang="scss" scoped> | ||
$color: red; | ||
.test { | ||
color: $color; | ||
} | ||
</style> |
Oops, something went wrong.