知識(shí)
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價(jià)值,我們?cè)谧非笃湟曈X(jué)表現(xiàn)的同時(shí),更側(cè)重于功能的便捷,營(yíng)銷的便利,運(yùn)營(yíng)的高效,讓網(wǎng)站成為營(yíng)銷工具,讓軟件能切實(shí)提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序?yàn)楹笃谏?jí)提供便捷的支持!
您當(dāng)前位置>首頁(yè) » 新聞資訊 » 小程序相關(guān) >
微信小程序轉(zhuǎn)換器(三)—— loader設(shè)計(jì)實(shí)現(xiàn)
發(fā)表時(shí)間:2021-1-5
發(fā)布人:葵宇科技
瀏覽次數(shù):53
配置文件中的loader配置
可以根據(jù)配置文件匹配到到規(guī)則,去執(zhí)行相應(yīng)的loader。
// analyze.config.js
// 引入loader
const jsLoader = require('./lib/jsLoader')
const jsonLoader = require('./lib/jsonLoader')
const cssLoader = require('./lib/cssLoader')
const htmlLoader = require('./lib/htmlLoader')
const signLoader = require('./lib/signLoader')
const config = {
entry: './',
output: {
name: 'dist',
src: './'
},
module: [
{
test: /\.js$/,
loader: [signLoader, jsLoader],
},
{
test: /\.wxss$/,
loader: [cssLoader],
outputPath: (outputPath) => outputPath.replace('.wxss', '.acss')
},
{
test: /\.wxml$/,
loader: [htmlLoader],
outputPath: (outputPath) => outputPath.replace('.wxml', '.axml')
},
{
test: /\.json$/,
loader: [jsonLoader],
},
]
}
module.exports = config
復(fù)制代碼
以jsLoader為例子,接收源碼作為參數(shù),返回編譯后獲得的新的源碼
// 前幾篇中封裝的js轉(zhuǎn)換器
const JsParser = require('./JsParser')
function loader(source) {
const jsParser = new JsParser()
let ast = jsParser.parse(source)
ast = jsParser.astConverter(ast)
return jsParser.astToCode(ast)
}
module.exports = loader
復(fù)制代碼
不同文件選擇對(duì)應(yīng)loader
// 重寫Analyze函數(shù)中的analyzeFileToLoard文件分析部分
function Analyze(filePath, outputPath){
if (fs.statSync(filePath).isDirectory()) {
const files = fs.readdirSync(filePath)
files.forEach(file => {
const currentFilePath = filePath+'/'+file
const currentOutputPath = outputPath+'/'+file
if(fs.statSync(currentFilePath).isDirectory()) {
fs.mkdirSync(currentOutputPath)
Analyze(currentFilePath, currentOutputPath)
} else analyzeFileToLoard(currentFilePath, currentOutputPath)
})
} else analyzeFileToLoard(filePath, outputPath)
}
復(fù)制代碼
function analyzeFileToLoard(inputPath, outputPath) {
let source = readFile(inputPath) // 讀取源碼
const loaders = config.module
loaders.forEach(loader => { // 遍歷配置文件,看是否有匹配文件的loader規(guī)則
if (loader.test.test(inputPath)) {
// 使用loader
source = useLoader(source, loader.loader, outputPath)
// 輸出路徑處理函數(shù)
if (loader.outputPath) outputPath = loader.outputPath(outputPath)
}
})
writeFile(outputAppPath(outputPath), source) // 將處理過(guò)后的源碼寫入文件
}
復(fù)制代碼
loader執(zhí)行是個(gè)逆序的執(zhí)行,從右邊向左依次執(zhí)行。在這里我們先用同步的loader來(lái)做討論。
loader執(zhí)行前還有個(gè)pitch階段,感覺(jué)pitch這個(gè)起名方式并不是特別合適,我更愿意叫它過(guò)濾篩選階段。先去順序執(zhí)行l(wèi)oader上的pitch方法,要是pitch有返回值,就不再執(zhí)行在該loader之前執(zhí)行的loader。
function useLoader(source, loaders = []) {
// 執(zhí)行l(wèi)oader存儲(chǔ)列表
const loaderList = []
// 遞歸去篩選需要執(zhí)行的loader
function loaderFilter(loaders) {
const [firstLoader, ...ortherLoader] = loaders
if (loaders.length === 0) return
// 執(zhí)行pitch,并將剩余的loader傳入作為參數(shù)
if (firstLoader.pitch && firstLoader.pitch(ortherLoader)) return ortherLoader
else {
// 將可用loader加入待執(zhí)行列表
loaderList.push(firstLoader)
// 剩余l(xiāng)oader作為參數(shù) 遞歸調(diào)用
loaderFilter(ortherLoader)
}
}
// 大概,暫時(shí)用不到。。。
const remainLoader = loaderFilter(loaders)
// 同步loader逆序執(zhí)行
function runLoader(source, loaderList) {
const loader = loaderList.pop()
let newSource = loader(source)
if (loaderList.length > 0) return runLoader(newSource, loaderList)
else return newSource
}
source = runLoader(source, loaderList)
return source
}
復(fù)制代碼
實(shí)驗(yàn)
寫個(gè)signLoader,看看loader能不能像我們想的那樣逆序執(zhí)行
function loader(source) {
let sign = `/**
* @Author: LY
*/
`
source = sign + source
return source
}
module.exports = loader
復(fù)制代碼
結(jié)果:
這樣簡(jiǎn)易的loader部分算是完成了,但這么寫只能執(zhí)行一些同步的loader,異步的loader無(wú)法等待執(zhí)行完成后再寫入。
相關(guān)案例查看更多
相關(guān)閱讀
- 云南網(wǎng)站建設(shè)方案 doc
- 網(wǎng)站建設(shè)服務(wù)公司
- 云南網(wǎng)站建設(shè)服務(wù)公司
- 云南網(wǎng)站建設(shè)首頁(yè)
- 昆明網(wǎng)站建設(shè)公司
- 網(wǎng)站建設(shè)首頁(yè)
- 關(guān)鍵詞快速排名
- 重慶網(wǎng)站建設(shè)公司
- 百度小程序
- 云南小程序開(kāi)發(fā)公司
- 搜索引擎排名
- 昆明小程序設(shè)計(jì)
- 云南小程序開(kāi)發(fā)報(bào)價(jià)
- 人人商城
- 網(wǎng)站建設(shè)列表網(wǎng)
- 云南軟件定制
- 南通小程序制作公司
- 小程序表單
- 網(wǎng)站搭建
- 云南網(wǎng)站建設(shè)公司
- 網(wǎng)站排名優(yōu)化
- 小程序的開(kāi)發(fā)公司
- SEO
- 小程序退款
- 云南小程序商城
- 云南省建設(shè)廳網(wǎng)站
- 網(wǎng)站上首頁(yè)
- 汽車報(bào)廢拆解管理系統(tǒng)
- 云南省建設(shè)廳網(wǎng)站官網(wǎng)
- 小程序開(kāi)發(fā)