UNPKG

3.28 kBJavaScriptView Raw
1/*
2 * MIT License http://opensource.org/licenses/MIT
3 * Author: Ben Holloway @bholloway
4 */
5'use strict';
6
7var path = require('path'),
8 convert = require('convert-source-map'),
9 rework = require('rework'),
10 visit = require('rework-visit');
11
12var fileProtocol = require('../file-protocol');
13
14/**
15 * Process the given CSS content into reworked CSS content.
16 *
17 * @param {string} sourceFile The absolute path of the file being processed
18 * @param {string} sourceContent CSS content without source-map
19 * @param {{outputSourceMap: boolean, transformDeclaration:function, absSourceMap:object,
20 * sourceMapConsumer:object}} params Named parameters
21 * @return {{content: string, map: object}} Reworked CSS and optional source-map
22 */
23function process(sourceFile, sourceContent, params) {
24
25 // embed source-map in css
26 // prepend file protocol to all sources to avoid problems with source map
27 var contentWithMap = sourceContent + (
28 params.absSourceMap ?
29 convert.fromObject(fileProtocol.prepend(params.absSourceMap)).toComment({multiline: true}) :
30 ''
31 );
32
33 // need to prepend file protocol to source as well to avoid problems with source map
34 var reworked = rework(contentWithMap, {source: fileProtocol.prepend(sourceFile)})
35 .use(reworkPlugin)
36 .toString({
37 sourcemap : params.outputSourceMap,
38 sourcemapAsObject: params.outputSourceMap
39 });
40
41 // complete with source-map
42 if (params.outputSourceMap) {
43 return {
44 content: reworked.code,
45 map : fileProtocol.remove(reworked.map)
46 };
47 }
48 // complete without source-map
49 else {
50 return {
51 content: reworked,
52 map : null
53 };
54 }
55
56 /**
57 * Plugin for css rework that follows SASS transpilation.
58 *
59 * @param {object} stylesheet AST for the CSS output from SASS
60 */
61 function reworkPlugin(stylesheet) {
62
63 // visit each node (selector) in the stylesheet recursively using the official utility method
64 // each node may have multiple declarations
65 visit(stylesheet, function visitor(declarations) {
66 if (declarations) {
67 declarations.forEach(eachDeclaration);
68 }
69 });
70
71 /**
72 * Process a declaration from the syntax tree.
73 * @param declaration
74 */
75 function eachDeclaration(declaration) {
76 var isValid = declaration.value && (declaration.value.indexOf('url') >= 0);
77 if (isValid) {
78
79 // reverse the original source-map to find the original source file before transpilation
80 var startPosApparent = declaration.position.start,
81 startPosOriginal = params.sourceMapConsumer &&
82 params.sourceMapConsumer.originalPositionFor(startPosApparent);
83
84 // we require a valid directory for the specified file
85 var directory =
86 startPosOriginal &&
87 startPosOriginal.source &&
88 fileProtocol.remove(path.dirname(startPosOriginal.source));
89 if (directory) {
90 declaration.value = params.transformDeclaration(declaration.value, directory);
91 }
92 // source-map present but invalid entry
93 else if (params.sourceMapConsumer) {
94 throw new Error('source-map information is not available at url() declaration');
95 }
96 }
97 }
98 }
99}
100
101module.exports = process;