【问题标题】:Generating sourcemaps with npm scripts using node-sass and postcss autoprefixer使用 node-sass 和 postcss autoprefixer 使用 npm 脚本生成源映射
【发布时间】:2017-04-12 16:27:12
【问题描述】:

在将输出从一个管道传输到另一个时,是否可以使用 node-sass 和 postcss autoprefixer 生成完全工作的源映射?我目前在 package.json 中有以下内容:

"scripts": {
    "sass": "node-sass sass/app.scss --source-map true --source-map-embed true",
    "postcss:autoprefixer": "postcss --use autoprefixer -b 'last 2 versions' --map",
    "css": "npm run sass -s | npm run postcss:autoprefixer -s > css/app.css"
}

这会产生一个半工作的内联源映射,但指向原始文件的链接不正确,因此在 Chrome 开发工具中单击它们不会加载它们(似乎它们被处理为相对链接,然后从 css 文件夹中引用)。我试图通过在 node-sass 中添加 --source-map-contents true 选项来解决此问题,但随后 autoprefixer 出错了,我怀疑是因为它不喜欢 dataUri 的行长。

理想情况下,无论如何我都希望输出一个单独的 .map 文件,但是将 node-sass 选项设置为 --source-map css/app.css.map 不会写出任何内容,大概是因为只有 css 输出到 stdout。

【问题讨论】:

    标签: npm node-sass postcss autoprefixer npm-scripts


    【解决方案1】:

    source-map-root 和文件系统 URL 替换 source-map 似乎可以解决问题:

    "scripts": {
        "sass": "node-sass sass/app.scss --source-map-root file://${PWD}/ --source-map-embed true",
        "postcss:autoprefixer": "postcss --use autoprefixer -b 'last 2 versions' --map",
        "css": "npm run sass -s | npm run postcss:autoprefixer -s > css/app.css"
    }
    

    如果可以输出单独的 .map 文件,仍然会很高兴!

    更新:

    以下是 RyanZim 评论推荐的使用 exorcist 的新 package.json:

    "scripts": {
        "sass": "node-sass sass/app.scss --source-map-root file://${PWD}/ --source-map-embed true",
        "postcss:autoprefixer": "postcss --use autoprefixer -b 'last 2 versions' --map",
        "css": "npm run sass -s | npm run postcss:autoprefixer -s | exorcist css/app.css.map > css/app.css"
    }
    

    【讨论】:

    • 对于单独的地图文件,您可以使用github.com/thlorenz/exorcist
    • 您也可以让 postcss 为您编写文件,并像这样传递地图文件名:postcss [...] --map <map filename>
    • @RyanZim 我想避免在 postcss 任务本身中输出,这样我可以在必要时继续在其他地方传递命令,但是 exorcist 工作得很好,谢谢!
    猜你喜欢
    • 2017-10-13
    • 2018-04-06
    • 1970-01-01
    • 2016-05-13
    • 2014-12-18
    • 2021-06-24
    • 2019-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多