This repository has been archived by the owner on Sep 29, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
sassdoc-webpack-plugin.js
64 lines (55 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
/* eslint-disable no-sync, no-console, global-require */
const fs = require('fs');
const path = require('path');
const sassdoc = require('sassdoc');
const getAsset = function(entry, ext = 'css') {
if (!entry) {
return undefined;
}
let asset;
for (const thisPath of entry) {
if (thisPath.substr(0 - (ext.length + 1)) === `.${ext}`) {
asset = thisPath;
}
}
return asset;
};
class SassDocPlugin {
constructor(options, pluginOptions) {
if (!options) {
try {
// Load .sassdocrc configuration
const yaml = require('js-yaml');
options = yaml.safeLoad(
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 set = require('lodash.set');
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;