@amisa/style-plugin
Version:
Transfom css's file to java scripts style element and js to script element
42 lines (37 loc) • 1.4 kB
text/typescript
const regex = /=\s*amisa.createStyleElement\(['|"].*\.css['|"]\)/g
export function AmisaCssTransformStylePlugin() {
// const config: PluginOption = {
const config = {
name: 'amisa-css-plugin', // Name of the plugin
transform(code: string, id: string): string | void | null | undefined {
let m;
if ((m = regex.exec(code)) !== null) {
const cssFilePath = getCssFileName(m, id);
const cssContent = getCssContent(cssFilePath), amisaMethod = "";
const result = code.replace(regex, `= amisa.getAmisaStyleElement(\`${cssContent}\`);`) + ` `;
return result;
} else {
console.error(id)
return code;
}
}
}
return config
}
document
// function getCssFileName(m: RegExpExecArray, path: string) {
function getCssFileName(m: RegExpExecArray, path: string) {
const finded = m[0]
const fileName = finded
.replace(/=\s*amisa.createStyleElement\(['|"]\.\//, '')
.replace(/=\s*amisa.createStyleElement\(['|"]/, '')
.replace(/["|']\)/, '')
const folder = require('path').dirname(path)
const folderSrc = folder.replace(/\/dist/, '')
return require('path').resolve(folderSrc, fileName)
}
// function getCssContent(cssFilePath: string) {
function getCssContent(cssFilePath: string) {
const fileString = require('fs').readFileSync(cssFilePath).toString()
return fileString.replace(/\n\s*/g, ' ')
}