增加scss注入的支持

pull/1/head
yutent 2023-05-16 14:17:40 +08:00
parent 927aaa5bc9
commit 97a10f3235
4 changed files with 33 additions and 16 deletions

View File

@ -27,8 +27,7 @@ import {
} from './constants.js' } from './constants.js'
const OPTIONS = { const OPTIONS = {
indentType: 'space', style: 'compressed'
indentWidth: 2
} }
// 修正路径合并 避免在windows下被转义 // 修正路径合并 避免在windows下被转义
@ -102,18 +101,16 @@ function scopeCss(css = '', hash) {
/** /**
* 编译scss为css * 编译scss为css
* @param file <String> 文件路径或scss代码 * @param file <String> 文件路径或scss代码
* @param mini <Boolean> 是否压缩 * @param inject <String> 要注入的scss代码
*/ */
export function compileScss(file, mini = true) { export function compileScss(file, inject = '') {
let style = mini ? 'compressed' : 'expanded'
try { try {
if (fs.isfile(file)) { if (fs.isfile(file)) {
return scss.compile(file, { style, ...OPTIONS }).css.trim() return scss.compile(file, OPTIONS).css.trim()
} else { } else {
return scss.compileString(file, { style, ...OPTIONS }).css.trim() return scss.compileString(inject + file, OPTIONS).css.trim()
} }
} catch (err) { } catch (err) {
// console.log('compile scss: ', file)
console.error(err) console.error(err)
} }
} }
@ -283,14 +280,14 @@ export function compileVue(file, imports, options = {}) {
.map(it => { .map(it => {
let css let css
if (it.length > 2) { if (it.length > 2) {
css = compileScss(it[2]) css = compileScss(it[2], options.INJECT_SCSS)
if (it[1].includes('scoped')) { if (it[1].includes('scoped')) {
scoped = true scoped = true
css = scopeCss(css, hash) css = scopeCss(css, hash)
} }
} else { } else {
css = compileScss(it[1]) css = compileScss(it[1], options.INJECT_SCSS)
} }
return css return css
}) })

View File

@ -18,6 +18,10 @@ const noc = Buffer.from('')
const SERVER_OPTIONS = {} const SERVER_OPTIONS = {}
const CACHE = {} //文件缓存, 用于hmr const CACHE = {} //文件缓存, 用于hmr
function readFile(file) {
return (file && fs.cat(file)?.toString()) || ''
}
export default async function createServer(root = '', conf = {}) { export default async function createServer(root = '', conf = {}) {
const SOURCE_DIR = join(root, 'src') const SOURCE_DIR = join(root, 'src')
const PUBLIC_DIR = join(root, 'public') const PUBLIC_DIR = join(root, 'public')
@ -26,6 +30,7 @@ export default async function createServer(root = '', conf = {}) {
const PORT = conf.devServer.port || 8080 const PORT = conf.devServer.port || 8080
const USE_HTTPS = conf.devServer.https const USE_HTTPS = conf.devServer.https
const DOMAIN = conf.devServer.domain || 'localhost' const DOMAIN = conf.devServer.domain || 'localhost'
const INJECT_SCSS = readFile(conf.inject?.scss)
if (USE_HTTPS) { if (USE_HTTPS) {
Object.assign(SERVER_OPTIONS, conf.devServer.ssl) Object.assign(SERVER_OPTIONS, conf.devServer.ssl)
@ -185,7 +190,8 @@ export default async function createServer(root = '', conf = {}) {
currentPage, currentPage,
SOURCE_DIR, SOURCE_DIR,
CACHE, CACHE,
DEPLOY_PATH DEPLOY_PATH,
INJECT_SCSS
}) })
res.setHeader('content-type', MIME_TYPES.js) res.setHeader('content-type', MIME_TYPES.js)
@ -326,7 +332,15 @@ export default async function createServer(root = '', conf = {}) {
case 'css': case 'css':
case 'scss': case 'scss':
{ {
let content = fs.cat(filePath).toString() let content = ''
if (filePath === conf.inject?.scss) {
return
}
if (ext === 'scss') {
content = compileScss(filePath)
} else {
content = fs.cat(filePath).toString()
}
ws.send({ ws.send({
action: 'render', action: 'render',
data: { path: file.replace(/\\/g, '/'), content } data: { path: file.replace(/\\/g, '/'), content }
@ -341,7 +355,8 @@ export default async function createServer(root = '', conf = {}) {
currentPage, currentPage,
SOURCE_DIR, SOURCE_DIR,
CACHE, CACHE,
DEPLOY_PATH DEPLOY_PATH,
INJECT_SCSS
}) })
let tmp = CACHE[filePath] let tmp = CACHE[filePath]
if (tmp.changed) { if (tmp.changed) {

View File

@ -3,6 +3,10 @@ import { join, dirname, parse } from 'path'
import Es from 'esbuild' import Es from 'esbuild'
import { compileScss, parseJs, compileVue, parseHtml } from './compile-vue.js' import { compileScss, parseJs, compileVue, parseHtml } from './compile-vue.js'
function readFile(file) {
return (file && fs.cat(file)?.toString()) || ''
}
export default function compile(root = '', dist = '', conf = {}) { export default function compile(root = '', dist = '', conf = {}) {
// //
const SOURCE_DIR = join(root, 'src') const SOURCE_DIR = join(root, 'src')
@ -10,6 +14,7 @@ export default function compile(root = '', dist = '', conf = {}) {
const DEPLOY_PATH = conf.base || '' // 部署目录, 默认是根目录部署 const DEPLOY_PATH = conf.base || '' // 部署目录, 默认是根目录部署
const IS_MPA = Object.keys(conf.pages).length > 1 const IS_MPA = Object.keys(conf.pages).length > 1
const PAGES_PREFIX = Object.keys(conf.pages).map(it => `pages/${it}/`) const PAGES_PREFIX = Object.keys(conf.pages).map(it => `pages/${it}/`)
const INJECT_SCSS = readFile(conf.inject?.scss)
let timeStart = Date.now() let timeStart = Date.now()
let template = fs.cat(join(process.env.PWD, 'index.html')).toString() let template = fs.cat(join(process.env.PWD, 'index.html')).toString()
@ -31,14 +36,14 @@ export default function compile(root = '', dist = '', conf = {}) {
} }
} }
return true return it.path !== conf.inject?.scss
} }
return false return false
}) })
let compileFiles = function (currentPage, page, files) { let compileFiles = function (currentPage, page, files) {
let options = { IS_MPA, currentPage, SOURCE_DIR, DEPLOY_PATH } let options = { IS_MPA, currentPage, SOURCE_DIR, DEPLOY_PATH, INJECT_SCSS }
for (let it of files) { for (let it of files) {
// 入口文件, 特殊处理 // 入口文件, 特殊处理

View File

@ -1,7 +1,7 @@
{ {
"name": "fite", "name": "fite",
"type": "module", "type": "module",
"version": "1.0.0", "version": "1.0.1",
"bin": { "bin": {
"fite": "index.js" "fite": "index.js"
}, },