diff --git a/src/style-preprocessor.ts b/src/style-preprocessor.ts new file mode 100644 index 00000000..084efe0f --- /dev/null +++ b/src/style-preprocessor.ts @@ -0,0 +1,24 @@ +const DefaultLessVersion = '4.3.0' + +export const importLessFromCdn = async ( + version: string = DefaultLessVersion, +) => { + return new Promise((resolve, reject) => { + if (!document?.body) { + reject(new Error('Document body not found')) + } + const el = document.createElement('script') + el.src = `https://cdn.jsdelivr.net/npm/less@${version}/dist/less.min.js` + el.onload = () => { + if ((window as any)?.less && (window as any)?.less.render) { + resolve(void 0) + } else { + reject(new Error('Import less from cdn failed')) + } + } + el.onerror = (err) => { + reject(err) + } + document.body.appendChild(el) + }) +} diff --git a/src/transform.ts b/src/transform.ts index d1ec2b19..321d979d 100644 --- a/src/transform.ts +++ b/src/transform.ts @@ -7,6 +7,7 @@ import type { import { type Transform, transform } from 'sucrase' import hashId from 'hash-sum' import { getSourceMap, toVisualizer, trimAnalyzedBindings } from './sourcemap' +import { importLessFromCdn } from './style-preprocessor' export const COMP_IDENTIFIER = `__sfc__` @@ -78,7 +79,9 @@ export async function compileFile( return errors } - const styleLangs = descriptor.styles.map((s) => s.lang).filter(Boolean) + const styleLangs = descriptor.styles + .map((s) => s.lang) + .filter(Boolean) as string[] const templateLang = descriptor.template?.lang if (styleLangs.length && templateLang) { return [ @@ -88,11 +91,20 @@ export async function compileFile( `for