Repository navigation
Expand file tree
/
Copy pathplugin.js
More file actions
146 lines (131 loc) · 4.68 KB
/
Copy pathplugin.js
File metadata and controls
146 lines (131 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import {createRequire} from 'node:module';
import path from 'path';
import stylus from 'stylus';
import postcss from 'postcss';
import autoprefixer from 'autoprefixer';
/**
* Compiler class that handles the compilation of Stylus files,
* applies Autoprefixer, and integrates with Meteor's build system.
*/
class StylusAutoprefixerCompiler {
constructor() {
this.cache = new Map();
this.config = void 0;
try {
const require = createRequire(process.cwd());
this.config = require(path.join(process.cwd(), 'stylus.config.js'));
} catch (err) {}
}
/**
* Processes Stylus files and compiles them into CSS.
* Applies Autoprefixer for vendor prefixing.
*
* @param {Array} files - Array of file objects to process.
*/
async processFilesForTarget(files) {
const promises = files.map(file => this.processFile(file));
// Wait for all files to be processed
return Promise.all(promises);
}
/**
* Processes a single Stylus file: compiles to CSS and applies Autoprefixer.
*
* @param {Object} file - The Stylus file to process.
* @returns {Promise} - Resolves when processing is complete.
*/
async processFile(file) {
const filePath = file.getPathInPackage(); // Get the file path within the package
const source = file.getContentsAsString(); // Read the file contents
try {
// Check cache to see if file has changed
const cacheKey = `${filePath}-${file.getSourceHash()}`;
if (this.cache.has(cacheKey)) {
// Retrieve cached result
const cachedResult = this.cache.get(cacheKey);
file.addStylesheet({
path: filePath.replace('.styl', '.css'),
data: cachedResult.css,
});
return;
}
// Compile Stylus to CSS
const {css, sourcemap} = await this.compileStylus(source, filePath);
// Apply Autoprefixer
const prefixed = await this.applyAutoprefixer(css, sourcemap, filePath);
// Cache the result for future builds
this.cache.set(cacheKey, {
css: prefixed.css,
});
// Add the compiled CSS to Meteor's build system
file.addStylesheet({
path: filePath.replace('.styl', '.css'),
data: prefixed.css,
});
} catch (error) {
// Handle and report errors gracefully
file.error({
message: error.message,
line: error.line || null,
column: error.column || null,
});
}
}
/**
* Compiles Stylus source code to CSS.
*
* @param {string} source - Stylus source code.
* @param {string} filePath - Name of the Stylus file.
* @returns {Promise<string>} - Compiled CSS and source map.
*/
async compileStylus(source, filePath) {
return new Promise((resolve, reject) => {
const processor = stylus(source)
.set('filename', filePath)
.set('sourcemap', { inline: false });
this.config?.includePaths?.forEach((includePath) => {
processor.include(includePath);
});
this.config?.importFiles?.forEach((importFile) => {
processor.import(importFile);
});
Object.entries(this.config?.stylusOptions || {}).forEach(([key, value]) => {
processor.set(key, value);
});
processor.render((err, css) => {
if (err) {
reject(err);
} else {
resolve({css, sourcemap: processor.sourcemap});
}
});
});
}
/**
* Applies Autoprefixer to the compiled CSS.
*
* @param {string} css - Compiled CSS from Stylus.
* @param {string} sourcemap - Sourcemap from Stylus.
* @param {string} filePath - Name of the Stylus file.
* @returns {Promise<Object>} - Object containing prefixed CSS.
*/
async applyAutoprefixer(css, sourcemap, filePath) {
return postcss([autoprefixer])
.process(css, {
from: filePath,
to: filePath.replace('.styl', '.css'),
map: {
prev: sourcemap,
inline: true,
},
})
.then(result => ({
css: result.css
}));
}
}
/**
* Registers a new compiler for '.styl' files.
*/
Plugin.registerCompiler({
extensions: ['styl'],
}, () => new StylusAutoprefixerCompiler());