Compare commits

..

6 Commits

7 changed files with 103 additions and 34 deletions

View File

@ -19,6 +19,7 @@ const IS_WINDOWS = process.platform === 'win32'
const CONFIG_FILE = normalize(join(WORK_SPACE, 'fite.config.js')) const CONFIG_FILE = normalize(join(WORK_SPACE, 'fite.config.js'))
const PROTOCOL = IS_WINDOWS ? 'file://' : '' const PROTOCOL = IS_WINDOWS ? 'file://' : ''
const NODE_VERSION = process.versions.node.split('.').map(n => +n) const NODE_VERSION = process.versions.node.split('.').map(n => +n)
const ABS_CONFIG_FILEPATH = PROTOCOL + CONFIG_FILE
let args = process.argv.slice(2) let args = process.argv.slice(2)
let mode = args.shift() || 'prod' let mode = args.shift() || 'prod'
@ -40,8 +41,9 @@ switch (mode) {
case 'dev': case 'dev':
process.env.NODE_ENV = 'development' process.env.NODE_ENV = 'development'
import(PROTOCOL + CONFIG_FILE) import(ABS_CONFIG_FILEPATH)
.then(function (conf) { .then(function (conf) {
conf.default.ABS_CONFIG_FILEPATH = ABS_CONFIG_FILEPATH
createServer(WORK_SPACE, conf.default) createServer(WORK_SPACE, conf.default)
}) })
.catch(err => { .catch(err => {
@ -52,13 +54,14 @@ switch (mode) {
case 'build': case 'build':
process.env.NODE_ENV = 'production' process.env.NODE_ENV = 'production'
import(PROTOCOL + CONFIG_FILE) import(ABS_CONFIG_FILEPATH)
.then(function (conf) { .then(function (conf) {
let dist = conf.buildDir || 'dist' let dist = conf.buildDir || 'dist'
if (clean && fs.isdir(dist)) { if (clean && fs.isdir(dist)) {
console.log('清除dist目录...') console.log('清除dist目录...')
fs.rm(dist) fs.rm(dist)
} }
conf.default.ABS_CONFIG_FILEPATH = ABS_CONFIG_FILEPATH
compile(WORK_SPACE, dist, conf.default, verbose) compile(WORK_SPACE, dist, conf.default, verbose)
}) })
.catch(err => { .catch(err => {

View File

@ -64,7 +64,7 @@ function scopeCss(css = '', hash) {
if (last.startsWith(':')) { if (last.startsWith(':')) {
output = parseVDeep(last, output, true) output = parseVDeep(last, output, true)
} else { } else {
output = `${last} ${output}` output = `${last}[data-${hash}] ${output}`
} }
} else { } else {
if (last.includes(':')) { if (last.includes(':')) {
@ -115,7 +115,7 @@ export function compileScss(file, inject = '') {
export function parseJs( export function parseJs(
code = '', code = '',
imports, imports,
{ IS_MPA, currentPage, IS_ENTRY, DEPLOY_PATH, LEGACY_MODE } = {}, { IS_MPA, currentPage, IS_ENTRY, DEPLOY_PATH, LEGACY_MODE, define } = {},
filename, filename,
linePatch = 1 linePatch = 1
) { ) {
@ -241,6 +241,8 @@ export function parseJs(
` stylesheet.textContent = ${tmp};\n` + ` stylesheet.textContent = ${tmp};\n` +
` document.head.appendChild(stylesheet);\n` + ` document.head.appendChild(stylesheet);\n` +
`}\n` `}\n`
return `let ${tmp};\n!(async function(){\n ${tmp} = await __fite_import('${name}', import.meta.url);\n})()`
} else { } else {
// CSSStyleSheet.replaceSync 需要FF v101, Safari 16.4才支持 // CSSStyleSheet.replaceSync 需要FF v101, Safari 16.4才支持
fixedStyle += fixedStyle +=
@ -251,8 +253,9 @@ export function parseJs(
` __sheets__.push(stylesheet);\n` + ` __sheets__.push(stylesheet);\n` +
` document.adoptedStyleSheets = __sheets__;\n` + ` document.adoptedStyleSheets = __sheets__;\n` +
`}\n` `}\n`
}
return `const ${tmp} = await __fite_import('${name}', import.meta.url)` return `const ${tmp} = await __fite_import('${name}', import.meta.url)`
}
} else { } else {
if (name.startsWith('@/')) { if (name.startsWith('@/')) {
name = name.replace('@/', urlJoin(DEPLOY_PATH, ASSETS_DIR)) name = name.replace('@/', urlJoin(DEPLOY_PATH, ASSETS_DIR))
@ -287,6 +290,10 @@ export function parseJs(
} }
}) })
for (let key in define) {
code = code.replaceAll(key, define[key])
}
if (fixedStyle) { if (fixedStyle) {
code += '\n\n' + (IS_ENTRY ? SHEETS_DEF : '') + fixedStyle code += '\n\n' + (IS_ENTRY ? SHEETS_DEF : '') + fixedStyle
@ -303,7 +310,7 @@ export function parseJs(
* @param file <String> 文件路径 * @param file <String> 文件路径
* @return <String> 返回转换后的js代码 * @return <String> 返回转换后的js代码
*/ */
export function compileVue(file, imports, options = {}) { export async function compileVue(file, imports, options = {}) {
// 修正那反人类的windows路径 // 修正那反人类的windows路径
let filename = file.slice(options.SOURCE_DIR.length).replace(/\\/g, '/') let filename = file.slice(options.SOURCE_DIR.length).replace(/\\/g, '/')
let code = (fs.cat(file) || '').toString().replace(/\r\n/g, '\n') let code = (fs.cat(file) || '').toString().replace(/\r\n/g, '\n')
@ -333,10 +340,15 @@ export function compileVue(file, imports, options = {}) {
} else { } else {
css = compileScss(it[1], options.INJECT_SCSS) css = compileScss(it[1], options.INJECT_SCSS)
} }
return css return css
}) })
.join(' ') .join(' ')
for (let fn of options.plugin) {
scss = await fn('css', scss)
}
js = js ? js[1] : 'export default {}' js = js ? js[1] : 'export default {}'
try { try {

View File

@ -44,11 +44,14 @@ export async function compileFiles(
switch (it.ext) { switch (it.ext) {
case '.vue': case '.vue':
{ {
let code = compileVue(path, imports, options) let code = await compileVue(path, imports, options)
await Es.transform(code, { minify: true }).then(r => { await Es.transform(code, { minify: true }).then(async ({ code }) => {
for (let fn of options.plugin) {
code = await fn('js', code)
}
fs.echo( fs.echo(
r.code, code,
join(dist, 'assets/', pageDir, it.name.replace(/\.vue$/, '.js')) join(dist, 'assets/', pageDir, it.name.replace(/\.vue$/, '.js'))
) )
}) })
@ -61,8 +64,11 @@ export async function compileFiles(
code = parseJs(code + '', imports, options) code = parseJs(code + '', imports, options)
await Es.transform(code, { minify: true }).then(r => { await Es.transform(code, { minify: true }).then(async ({ code }) => {
fs.echo(r.code, join(dist, 'assets/', pageDir, it.name)) for (let fn of options.plugin) {
code = await fn('js', code)
}
fs.echo(code, join(dist, 'assets/', pageDir, it.name))
}) })
} }
break break
@ -91,6 +97,9 @@ export async function compileFiles(
fs.cp(path, target) fs.cp(path, target)
} else { } else {
let code = compileScss(path) let code = compileScss(path)
for (let fn of options.plugin) {
code = await fn('css', code)
}
fs.echo(code, target) fs.echo(code, target)
} }
} }

View File

@ -34,6 +34,7 @@ export default async function createServer(root = '', conf = {}) {
const LEGACY_MODE = !!conf.legacy const LEGACY_MODE = !!conf.legacy
const ENABLE_GZIP = !!conf.devServer.gzip const ENABLE_GZIP = !!conf.devServer.gzip
const { isCustomElement = defaultCustomElement } = conf.compileOptions || {} const { isCustomElement = defaultCustomElement } = conf.compileOptions || {}
const { plugin = [], define = {} } = conf
if (conf.imports['vue-dev']) { if (conf.imports['vue-dev']) {
conf.imports.vue = conf.imports['vue-dev'] conf.imports.vue = conf.imports['vue-dev']
@ -73,7 +74,7 @@ export default async function createServer(root = '', conf = {}) {
currentPage = '' currentPage = ''
server server
.on('request', function (req, res) { .on('request', async function (req, res) {
let prefix = DEPLOY_PATH ? DEPLOY_PATH.replace(/\/$/, '') : '' let prefix = DEPLOY_PATH ? DEPLOY_PATH.replace(/\/$/, '') : ''
let url = let url =
prefix && req.url.startsWith(prefix) prefix && req.url.startsWith(prefix)
@ -178,11 +179,18 @@ export default async function createServer(root = '', conf = {}) {
currentPage, currentPage,
IS_ENTRY: true, IS_ENTRY: true,
DEPLOY_PATH, DEPLOY_PATH,
LEGACY_MODE LEGACY_MODE,
isCustomElement,
plugin,
define
}, },
page.entry page.entry
) )
for (let fn of plugin) {
entry = await fn('js', entry)
}
code = parseHtml(html, { code = parseHtml(html, {
page, page,
imports: conf.imports, imports: conf.imports,
@ -215,7 +223,7 @@ export default async function createServer(root = '', conf = {}) {
return return
} }
code = compileVue(file, conf.imports, { code = await compileVue(file, conf.imports, {
IS_MPA, IS_MPA,
currentPage, currentPage,
SOURCE_DIR, SOURCE_DIR,
@ -223,9 +231,15 @@ export default async function createServer(root = '', conf = {}) {
DEPLOY_PATH, DEPLOY_PATH,
INJECT_SCSS, INJECT_SCSS,
LEGACY_MODE, LEGACY_MODE,
isCustomElement isCustomElement,
plugin,
define
}) })
for (let fn of plugin) {
code = await fn('js', code)
}
res.setHeader('content-type', MIME_TYPES.js) res.setHeader('content-type', MIME_TYPES.js)
} }
break break
@ -251,15 +265,20 @@ export default async function createServer(root = '', conf = {}) {
return return
} }
code = compileScss(file) code = compileScss(file)
for (let fn of plugin) {
code = await fn('css', code)
}
res.setHeader('content-type', MIME_TYPES.css) res.setHeader('content-type', MIME_TYPES.css)
} }
break break
case 'js': case 'js':
case 'wasm':
{ {
let rpath = pathname.replace('@/', '') let rpath = pathname.replace('@/', '')
let file let file
let isJson = false let isJson = false
let isWasm = rpath.endsWith('.wasm')
if (rpath.endsWith('json.js')) { if (rpath.endsWith('json.js')) {
isJson = true isJson = true
@ -295,6 +314,8 @@ export default async function createServer(root = '', conf = {}) {
} catch (err) { } catch (err) {
console.log('%s 语法错误: %s', rpath, red(err.message)) console.log('%s 语法错误: %s', rpath, red(err.message))
} }
} else if (isWasm) {
//
} else { } else {
code = parseJs( code = parseJs(
code + '', code + '',
@ -303,10 +324,16 @@ export default async function createServer(root = '', conf = {}) {
IS_MPA, IS_MPA,
currentPage, currentPage,
DEPLOY_PATH, DEPLOY_PATH,
LEGACY_MODE LEGACY_MODE,
isCustomElement,
plugin,
define
}, },
file file
) )
for (let fn of plugin) {
code = await fn('js', code)
}
} }
res.setHeader('content-type', MIME_TYPES[ext]) res.setHeader('content-type', MIME_TYPES[ext])
} }
@ -387,7 +414,7 @@ export default async function createServer(root = '', conf = {}) {
) )
chokidar chokidar
.watch([SOURCE_DIR, PUBLIC_DIR, join(root, './index.html')]) .watch([SOURCE_DIR, PUBLIC_DIR, join(root, './index.html')])
.on('all', (act, filePath) => { .on('all', async (act, filePath) => {
if (ready) { if (ready) {
let file = filePath.slice(SOURCE_DIR.length) let file = filePath.slice(SOURCE_DIR.length)
@ -407,6 +434,9 @@ export default async function createServer(root = '', conf = {}) {
} else { } else {
content = fs.cat(filePath).toString() content = fs.cat(filePath).toString()
} }
for (let fn of plugin) {
content = await fn('css', content)
}
ws.send({ ws.send({
action: 'render', action: 'render',
data: { path: file.replace(/\\/g, '/'), content } data: { path: file.replace(/\\/g, '/'), content }
@ -416,7 +446,7 @@ export default async function createServer(root = '', conf = {}) {
case 'vue': case 'vue':
{ {
let content = compileVue(filePath, conf.imports, { let content = await compileVue(filePath, conf.imports, {
IS_MPA, IS_MPA,
currentPage, currentPage,
SOURCE_DIR, SOURCE_DIR,
@ -424,7 +454,9 @@ export default async function createServer(root = '', conf = {}) {
DEPLOY_PATH, DEPLOY_PATH,
INJECT_SCSS, INJECT_SCSS,
LEGACY_MODE, LEGACY_MODE,
isCustomElement isCustomElement,
plugin,
define
}) })
let tmp = CACHE[filePath] let tmp = CACHE[filePath]
if (tmp.changed) { if (tmp.changed) {

View File

@ -52,7 +52,13 @@ export default function compile(root = '', dist = '', conf = {}, verbose) {
) )
const INJECT_SCSS = readFile(conf.inject?.scss) const INJECT_SCSS = readFile(conf.inject?.scss)
const LEGACY_MODE = !!conf.legacy const LEGACY_MODE = !!conf.legacy
const { isCustomElement } = conf.compileOptions || {} const {
ABS_CONFIG_FILEPATH,
compileOptions = {},
define = {},
plugin = []
} = conf
const { isCustomElement = defaultCustomElement } = compileOptions
conf.inject = conf.inject || { scss: '' } conf.inject = conf.inject || { scss: '' }
@ -65,13 +71,8 @@ export default function compile(root = '', dist = '', conf = {}, verbose) {
DEPLOY_PATH, DEPLOY_PATH,
INJECT_SCSS, INJECT_SCSS,
LEGACY_MODE, LEGACY_MODE,
// 线程通讯无法传递函数类型, 需要转为字符串, 之后再转回来 ABS_CONFIG_FILEPATH,
isCustomElement: define
THREADS_NUM > 0
? isCustomElement
? isCustomElement.toString()
: null
: isCustomElement || defaultCustomElement
} }
fs.ls(SOURCE_DIR, true).forEach(path => { fs.ls(SOURCE_DIR, true).forEach(path => {
@ -101,7 +102,10 @@ export default function compile(root = '', dist = '', conf = {}, verbose) {
// 创建线程池 // 创建线程池
if (THREADS_NUM > 0 && (IS_MPA || list.size > THREADS_NUM * 10)) { if (THREADS_NUM > 0 && (IS_MPA || list.size > THREADS_NUM * 10)) {
for (let i = 0; i < THREADS_NUM; i++) { // 页面数过少时, 线程数量不能比页面数多
let max = Math.min(THREADS_NUM, PAGES_KEYS.length)
for (let i = 0; i < max; i++) {
WORKER_POOL.add( WORKER_POOL.add(
new Worker(join(__dirname, './thread.js'), { new Worker(join(__dirname, './thread.js'), {
workerData: { workerData: {
@ -113,6 +117,8 @@ export default function compile(root = '', dist = '', conf = {}, verbose) {
}) })
) )
} }
} else {
options.isCustomElement = isCustomElement
} }
// 优先处理静态目录, 之后的源码目录中, 以便如果有产生相同的文件名, 则覆盖静态目录中的文件 // 优先处理静态目录, 之后的源码目录中, 以便如果有产生相同的文件名, 则覆盖静态目录中的文件
@ -202,7 +208,8 @@ export default function compile(root = '', dist = '', conf = {}, verbose) {
doJob() doJob()
} }
} else { } else {
options.isCustomElement = isCustomElement || defaultCustomElement options.plugin = plugin
options.isCustomElement = isCustomElement
compileFiles(currentPage, page, list, options, { compileFiles(currentPage, page, list, options, {
verbose, verbose,
dist, dist,

View File

@ -8,11 +8,17 @@ import { compileFiles } from './compile.js'
import { defaultCustomElement } from './utils.js' import { defaultCustomElement } from './utils.js'
const { options, verbose, dist, imports } = workerData const { options, verbose, dist, imports } = workerData
const { ABS_CONFIG_FILEPATH } = options
options.isCustomElement = options.isCustomElement const { compileOptions = {}, plugin = [] } = await import(
? Function('return ' + options.isCustomElement)() ABS_CONFIG_FILEPATH
: defaultCustomElement ).then(r => r.default)
const { isCustomElement = defaultCustomElement } = compileOptions
options.isCustomElement = isCustomElement
options.plugin = plugin
//
async function doJob(job) { async function doJob(job) {
let [currentPage, { page, files }] = job.entries().next().value let [currentPage, { page, files }] = job.entries().next().value
@ -28,7 +34,7 @@ async function doJob(job) {
dist, dist,
imports imports
}) })
parentPort.postMessage('ok') parentPort.postMessage(true)
} }
parentPort.on('message', doJob) parentPort.on('message', doJob)

View File

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