source: trip-planner-front/node_modules/resolve-url-loader/lib/engine/rework.js@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 4.8 KB
Line 
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
10var fileProtocol = require('../file-protocol');
11
12var rework = requireOptionalPeerDependency('rework'),
13 visit = requireOptionalPeerDependency('rework-visit');
14
15/**
16 * Process the given CSS content into reworked CSS content.
17 *
18 * @param {string} sourceFile The absolute path of the file being processed
19 * @param {string} sourceContent CSS content without source-map
20 * @param {{outputSourceMap: boolean, transformDeclaration:function, absSourceMap:object,
21 * sourceMapConsumer:object}} params Named parameters
22 * @return {{content: string, map: object}} Reworked CSS and optional source-map
23 */
24function process(sourceFile, sourceContent, params) {
25
26 // embed source-map in css
27 // prepend file protocol to all sources to avoid problems with source map
28 var contentWithMap = sourceContent + (
29 params.absSourceMap ?
30 convert.fromObject(fileProtocol.prepend(params.absSourceMap)).toComment({multiline: true}) :
31 ''
32 );
33
34 // need to prepend file protocol to source as well to avoid problems with source map
35 var reworked = rework(contentWithMap, {source: fileProtocol.prepend(sourceFile)})
36 .use(reworkPlugin)
37 .toString({
38 sourcemap : params.outputSourceMap,
39 sourcemapAsObject: params.outputSourceMap
40 });
41
42 // complete with source-map
43 if (params.outputSourceMap) {
44 return {
45 content: reworked.code,
46 map : fileProtocol.remove(reworked.map)
47 };
48 }
49 // complete without source-map
50 else {
51 return {
52 content: reworked,
53 map : null
54 };
55 }
56
57 /**
58 * Plugin for css rework that follows SASS transpilation.
59 *
60 * @param {object} stylesheet AST for the CSS output from SASS
61 */
62 function reworkPlugin(stylesheet) {
63
64 // visit each node (selector) in the stylesheet recursively using the official utility method
65 // each node may have multiple declarations
66 visit(stylesheet, function visitor(declarations) {
67 if (declarations) {
68 declarations.forEach(eachDeclaration);
69 }
70 });
71
72 /**
73 * Process a declaration from the syntax tree.
74 * @param declaration
75 */
76 function eachDeclaration(declaration) {
77 var isValid = declaration.value && (declaration.value.indexOf('url') >= 0);
78 if (isValid) {
79 declaration.value = params.transformDeclaration(declaration.value, getPathsAtChar);
80 }
81
82 /**
83 * Create a hash of base path strings.
84 *
85 * Position in the declaration is not supported since rework does not refine sourcemaps to this detail.
86 *
87 * @throws Error on invalid source map
88 * @returns {{selector:string, property:string}} Hash of base path strings
89 */
90 function getPathsAtChar() {
91 var posSelector = declaration.parent && declaration.parent.position.start,
92 posProperty = declaration.position.start;
93
94 var result = {
95 property: positionToOriginalDirectory(posProperty),
96 selector: positionToOriginalDirectory(posSelector)
97 };
98
99 var isValid = [result.property, result.selector].every(Boolean);
100 if (isValid) {
101 return result;
102 }
103 else if (params.sourceMapConsumer) {
104 throw new Error('source-map information is not available at url() declaration');
105 } else {
106 throw new Error('a valid source-map is not present (ensure preceding loaders output a source-map)');
107 }
108 }
109 }
110 }
111
112 /**
113 * Given an apparent position find the directory of the original file.
114 *
115 * @param startPosApparent {{line: number, column: number}}
116 * @returns {false|string} Directory of original file or false on invalid
117 */
118 function positionToOriginalDirectory(startPosApparent) {
119 // reverse the original source-map to find the original source file before transpilation
120 var startPosOriginal =
121 !!params.sourceMapConsumer &&
122 params.sourceMapConsumer.originalPositionFor(startPosApparent);
123
124 // we require a valid directory for the specified file
125 var directory =
126 !!startPosOriginal &&
127 !!startPosOriginal.source &&
128 fileProtocol.remove(path.dirname(startPosOriginal.source));
129
130 return directory;
131 }
132}
133
134module.exports = process;
135
136/**
137 * Require the given filename but fail with an error that `requireOptionalPeerDependencies` must be installed.
138 *
139 * @param moduleName The module to require
140 * @returns {*} The module
141 * @throws Error when module is not found
142 */
143function requireOptionalPeerDependency(moduleName) {
144 try {
145 return require(moduleName);
146 }
147 catch (error) {
148 if (error.message === 'Cannot find module \'' + moduleName + '\'') {
149 throw new Error('to use the "rework" engine you must install the optionalPeerDependencies');
150 }
151 else {
152 throw error;
153 }
154 }
155}
Note: See TracBrowser for help on using the repository browser.