欧美三级国产三级日韩三级_亚洲熟妇丰满大屁股熟妇_欧美亚洲成人一区二区三区_国产精品久久久久久模特

微信小程序轉(zhuǎn)換器(三)—— loader設(shè)計(jì)實(shí)現(xiàn) - 新聞資訊 - 云南小程序開(kāi)發(fā)|云南軟件開(kāi)發(fā)|云南網(wǎng)站建設(shè)-昆明葵宇信息科技有限公司

159-8711-8523

云南網(wǎng)建設(shè)/小程序開(kāi)發(fā)/軟件開(kāi)發(fā)

知識(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ù)制代碼

具體loader實(shí)現(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

復(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過(guò)濾和執(zhí)行

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)案例查看更多