-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
sassdoc-webpack-plugin.js
68 lines (57 loc) · 1.74 KB
/
sassdoc-webpack-plugin.js
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
/* eslint-disable no-console */
'use strict';
const fs = require('fs/promises');
const path = require('path');
const yaml = require('js-yaml');
const set = require('lodash/set');
const sassdoc = require('sassdoc');
const getAsset = function (entry, ext = 'css') {
if (!entry) {
return undefined;
}
let asset;
for (const thisPath of entry) {
if (thisPath.slice(0 - (ext.length + 1)) === `.${ext}`) {
asset = thisPath;
}
}
return asset;
};
class SassDocPlugin {
constructor(opts, pluginOptions) {
let options = opts;
if (!options) {
try {
// Load .sassdocrc configuration
options = yaml.load(
fs.readFileSync(path.join(process.cwd(), '.sassdocrc'), 'utf-8'),
);
} catch (err) {
console.warn(err);
throw new Error(`Invalid or no .sassdocrc found in: ${process.cwd()}`);
}
}
if (!options.src) {
throw new Error('SassDoc Webpack Plugin: `src` is not defined');
}
this.options = options;
this.pluginOptions = pluginOptions;
}
apply(compiler) {
const self = this;
compiler.hooks.afterEmit.tapPromise('SassDocPlugin', (compilation) => {
if (self.pluginOptions && self.pluginOptions.assetPaths) {
const statsJSON = compilation.getStats().toJson();
const outputPath = self.pluginOptions.outputPath || process.cwd();
for (const { entry, ext, optPath } of self.pluginOptions.assetPaths) {
const asset = getAsset(statsJSON.assetsByChunkName[entry], ext);
if (asset) {
set(self.options, optPath, path.join(outputPath, asset));
}
}
}
return sassdoc(self.options.src, self.options).catch(console.error);
});
}
}
module.exports = SassDocPlugin;