【问题标题】:How to convert module.exports to json to use in gulp tasks?如何将 module.exports 转换为 json 以用于 gulp 任务?
【发布时间】:2020-02-18 00:20:37
【问题描述】:

我正在尝试使用这个 gulp 插件 https://www.npmjs.com/package/gulp-json-data-to-sass 将一些变量从我的 JS 配置文件传递到 SASS,它接受 JSON 文件作为输入。

我的配置文件如下

// config.js

module.exports = {
    use: {
        gallery: true,
        animations: true,
    },
}

如何将其转换为有效的 JSON 文件,最好使用 gulp 本身,以便能够将其传递给插件?

【问题讨论】:

    标签: javascript json gulp


    【解决方案1】:

    您可以将其存储为 JSON:

    config.json:

    {
        "use": {
            "gallery": true,
            "animations": true,
        }
    }
    

    npm 包的示例就是这样做的。如果您需要从其他地方的 JS 代码中使用它,您可以要求 JSON。

    const config = require('./config.json');
    

    不确定如何将其添加到 gulp 管道中,但您可以在构建管道中编写一个助手来执行此操作:

    const config = require('./config')
    const fs = require('fs')
    
    fs.writeFileSync('./config.json', JSON.stringify(config), 'utf8')
    

    【讨论】:

    • 谢谢,但我需要将配置文件保留为 js 格式。
    • 然后添加了另一种方法。
    • 谢谢,这行得通,但我需要找到如何集成到 gulp 管道,我会尝试尝试,也许用乙烯基
    猜你喜欢
    • 1970-01-01
    • 2017-02-23
    • 2018-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-15
    • 1970-01-01
    • 2020-05-29
    相关资源
    最近更新 更多