Skip to content

Commit 09bc8af

Browse files
committed
added jsdoc webpack plugin
1 parent 6d18044 commit 09bc8af

File tree

3 files changed

+140
-2
lines changed

3 files changed

+140
-2
lines changed

README.md

+27-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,27 @@
1-
# jsdoc-webpack-plugin
2-
JsDoc Webpack Plugin
1+
jsdoc-webpack-plugin
2+
==========================
3+
4+
5+
WebPack plugin that runs [jsdoc](http://usejsdoc.org/) on your bundles
6+
7+
# Usage
8+
In webpack.config.js:
9+
```javascript
10+
var webpack = require('webpack');
11+
var JsDocPlugin = require('jsdoc-webpack-plugin');
12+
13+
module.exports = {
14+
/// ... rest of config
15+
plugins: [
16+
new JsDocPlugin({
17+
conf: './jsdoc.conf'
18+
})
19+
]
20+
}
21+
22+
```
23+
24+
There are two ways how this plugin recognizes the files
25+
26+
1. It takes the information from the jsdoc config file "source.include"
27+
2. If no "source.include" provided, it takes the whole files from your bundles

index.js

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
var fs = require('fs');
2+
var path = require('path');
3+
var merge = require('lodash/merge');
4+
var spawn = require('child_process').spawn;
5+
var fsExtra = require('fs-extra');
6+
7+
function Plugin(translationOptions) {
8+
var defaultOptions = {
9+
conf: './jsdoc.conf'
10+
};
11+
12+
this.options = merge({}, defaultOptions, translationOptions);
13+
}
14+
15+
Plugin.prototype.apply = function (compiler) {
16+
var self = this;
17+
var options = self.options;
18+
19+
compiler.plugin('watch-run', function (watching, callback) {
20+
self.webpackIsWatching = true;
21+
callback(null, null);
22+
});
23+
24+
compiler.plugin('emit', function (compilation, callback) {
25+
console.log('JSDOC Start generating');
26+
27+
fsExtra.readJson(path.resolve(process.cwd(), options.conf), function (err, obj) {
28+
var files = [], jsdocErrors = [];
29+
var jsdoc, cwd = process.cwd();
30+
31+
if(err) {
32+
callback(err);
33+
return;
34+
}
35+
36+
if (obj.source && obj.source.include) {
37+
console.log('Taking sources from config file');
38+
} else {
39+
compilation.chunks.forEach(function (chunk) {
40+
chunk.modules.forEach(function (module) {
41+
if (module.fileDependencies) {
42+
module.fileDependencies.forEach(function (filepath) {
43+
files.push(path.relative(process.cwd(), filepath));
44+
});
45+
}
46+
});
47+
});
48+
merge(obj.source, { include: files });
49+
}
50+
51+
var jsDocConfTmp = path.resolve(cwd, 'jsdoc.' + Date.now() + '.conf.tmp');
52+
fs.writeFileSync(jsDocConfTmp, JSON.stringify(obj));
53+
54+
jsdoc = spawn('./node_modules/.bin/jsdoc', files.concat('-c', jsDocConfTmp));
55+
56+
jsdoc.stdout.on('data', function (data) {
57+
console.log(data.toString());
58+
});
59+
60+
jsdoc.stderr.on('data', function (data) {
61+
jsdocErrors.push(data.toString());
62+
});
63+
64+
jsdoc.on('close', function (data, code) {
65+
if(jsdocErrors.length > 0) {
66+
jsdocErrors.forEach(function (value) {
67+
console.error(value);
68+
});
69+
} else {
70+
console.log('JsDoc successful');
71+
}
72+
fs.unlink(jsDocConfTmp, function() {
73+
callback();
74+
});
75+
});
76+
});
77+
});
78+
79+
compiler.plugin('done', function (stats) {
80+
console.log('JSDOC Finished generating');
81+
console.log('JSDOC TOTAL TIME:', stats.endTime - stats.startTime);
82+
});
83+
};
84+
85+
module.exports = Plugin;

package.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "jsdoc-webpack-plugin",
3+
"version": "0.0.1",
4+
"description": "JsDoc Webpack Plugin",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/tfiwm/jsdoc-webpack-plugin.git"
12+
},
13+
"keywords": [
14+
"jsdoc",
15+
"webpack"
16+
],
17+
"author": "Mitko Tschimev <[email protected]> (http://www.mitko-tschimev.de)",
18+
"license": "MIT",
19+
"bugs": {
20+
"url": "https://github.com/tfiwm/jsdoc-webpack-plugin/issues"
21+
},
22+
"homepage": "https://github.com/tfiwm/jsdoc-webpack-plugin#readme",
23+
"dependencies": {
24+
"fs-extra": "^0.30.0",
25+
"jsdoc": "^3.4.0",
26+
"lodash": "^4.11.2"
27+
}
28+
}

0 commit comments

Comments
 (0)