溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

微信小程序轉(zhuǎn)換器之loader的實現(xiàn)方法

發(fā)布時間:2021-01-19 10:00:52 來源:億速云 閱讀:284 作者:小新 欄目:移動開發(fā)

這篇文章主要介紹了微信小程序轉(zhuǎn)換器之loader的實現(xiàn)方法,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

配置文件中的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

具體loader實現(xiàn)
以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

不同文件選擇對應(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)
}
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) // 將處理過后的源碼寫入文件
}

loader過濾和執(zhí)行

loader執(zhí)行是個逆序的執(zhí)行,從右邊向左依次執(zhí)行。在這里我們先用同步的loader來做討論。
loader執(zhí)行前還有個pitch階段,感覺pitch這個起名方式并不是特別合適,我更愿意叫它過濾篩選階段。先去順序執(zhí)行l(wèi)oader上的pitch方法,要是pitch有返回值,就不再執(zhí)行在該loader之前執(zhí)行的loader。

function useLoader(source, loaders = []) {
    // 執(zhí)行l(wèi)oader存儲列表
    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)
        }
    }
    // 大概,暫時用不到。。。
    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
}

實驗
寫個signLoader,看看loader能不能像我們想的那樣逆序執(zhí)行

function loader(source) {
let sign = `/**
* @Author: LY
*/
`
    source = sign + source
    return source
}
module.exports = loader

結(jié)果:

這樣簡易的loader部分算是完成了,但這么寫只能執(zhí)行一些同步的loader,異步的loader無法等待執(zhí)行完成后再寫入。

微信小程序轉(zhuǎn)換器之loader的實現(xiàn)方法

感謝你能夠認真閱讀完這篇文章,希望小編分享的“微信小程序轉(zhuǎn)換器之loader的實現(xiàn)方法”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI