{"size": 3077, "ext": "js", "max_stars_repo_path": "lib/middleware/less.js", "max_stars_repo_name": "f2e-server/f2e-server", "max_stars_repo_stars_event_min_datetime": "2019-11-19T01:39:32.000Z", "max_stars_repo_stars_event_max_datetime": "2019-11-19T01:40:21.000Z", "max_issues_repo_path": "lib/middleware/less.js", "max_issues_repo_name": "f2e-server/f2e-server", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/middleware/less.js", "max_forks_repo_name": "f2e-server/f2e-server", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 37.0722891566, "max_line_length": 102, "alphanum_fraction": 0.3951901202}
// @ts-check
const { readdirSync } = require('fs')
const path = require('path')
const _ = require('lodash')
const pathREG = /[^\\/]+/g

/**
 * @type {import('../../index').MiddlewareCreater}
 */
module.exports = conf => {
    const { useLess, build } = conf
    if (!useLess) {
        return
    }

    /** @type {LessStatic} */
    let less
    try {
        less = require('less')
    } catch (e) {
        console.error('useLess 需要安装less依赖')
        process.exit(0)
    }
    let imports = {}

    return {
        onSet: function (pathname, data, store) {
            if (_.isPlainObject(data)) {
                return data
            }
            if (/\.less$/.test(pathname)) {
                return new Promise((resolve, reject) => {
                    const outputPath = pathname.replace(/\.less$/, '.css')
                    const outputMapPath = outputPath.replace(/\.css$/, '.css.map')
                    less.render(data.toString().replace(/(@import.*)"(\S*\/)"/g, (impt, pre, dir) => {
                        let pkg = path.join(path.dirname(pathname), dir)
                        const d = pkg.match(pathREG).join('/')
                        const p = pathname.match(pathREG).join('/')
                        _.set(imports, [d, p], 1)
                        return readdirSync(pkg)
                            .filter(d => /\.less$/.test(d)).map(d => `${pre}"${dir}${d}";`).join('\n')
                    }), Object.assign({
                        javascriptEnabled: true,
                        paths: [path.dirname(pathname)],
                        sourceMap: {
                            sourceMapURL: outputMapPath.split('/').pop(),
                            outputSourceFiles: true
                        },
                        compress: !!build
                    }, useLess), function (err, output) {
                        if (err) {
                            reject(err)
                        } else {
                            output.imports.map(dep => {
                                const d = dep.match(pathREG).join('/')
                                const p = pathname.match(pathREG).join('/')
                                _.set(imports, [d, p], 1)
                                conf.__ignores__.add(d)
                            })
                            let data = output.css + ''
                            if (output.map) {
                                store._set(outputMapPath, output.map)
                            }
                            resolve({ data, outputPath })
                        }
                    })
                }).catch(err => console.trace(pathname, err))
            }
        },
        buildWatcher (pathname, type, build) {
            const importsMap = imports[pathname]
            if (type === 'change' && importsMap) {
                Object.keys(importsMap).map(dep => {
                    build(dep)
                })
            }
        },
        outputFilter (pathname, data) {
            return !/\.less$/.test(pathname)
        }
    }
}
