Skip to content

Commit 70ba69e

Browse files
committed
index.js package.json init
1 parent 7b5cf61 commit 70ba69e

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ jspm_packages
3535

3636
# Optional REPL history
3737
.node_repl_history
38+
39+
# Web/PHPStorm IDE setting directory
40+
.idea

index.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'use strict';
2+
const rollup = require('rollup').rollup;
3+
const p_prefix = 'rollup-plugin-';
4+
const r_plugin = {
5+
npm: require(p_prefix + 'node-resolve'),
6+
cjs: require(p_prefix + 'commonjs'),
7+
multiEntry: require(p_prefix + 'multi-entry')
8+
};
9+
10+
const path = require('path');
11+
12+
class hexoRollupRenderer {
13+
constructor(hexo) {
14+
this._hexo = hexo;
15+
}
16+
17+
static isString(str){
18+
return typeof str === 'string';
19+
}
20+
21+
static get rollupPlugins() {
22+
return [
23+
r_plugin.multiEntry(),
24+
r_plugin.npm({
25+
browser: true
26+
}),
27+
r_plugin.cjs()
28+
]
29+
}
30+
31+
//
32+
// Convert config of the entry to object.
33+
//
34+
get entry() {
35+
const cwd = process.cwd();
36+
let entry = this.userConfig.entry;
37+
if (hexoRollupRenderer.isString(entry)) entry = Array.of(entry);
38+
if (!entry) return [];
39+
return entry.filter(n => n.indexOf('source') !== -1).map(n => path.join(cwd, n));
40+
}
41+
42+
get userConfig() {
43+
return Object.assign({},
44+
this._hexo.theme.config.rollup || {},
45+
this._hexo.config.rollup || {}
46+
);
47+
}
48+
49+
get renderer() {
50+
const self = this;
51+
return function(data){
52+
if (self.entry.length === 0) {
53+
return Promise.resolve(data.text);
54+
}
55+
const config = Object.assign({}, self.userConfig, {
56+
entry: data.path,
57+
plugins: hexoRollupRenderer.rollupPlugins
58+
});
59+
return rollup(config).then(bundle => bundle.generate({
60+
format: 'iife',
61+
moduleName: 'hexo_rollup'
62+
}).code);
63+
};
64+
}
65+
}
66+
67+
/* globals hexo */
68+
const classes = new hexoRollupRenderer(hexo);
69+
hexo.extend.renderer.register('js', 'js', classes.renderer);

package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "hexo-plugin-rollup",
3+
"version": "0.0.1",
4+
"main": "index.js",
5+
"dependencies": {
6+
"rollup": "^0.41.1",
7+
"rollup-plugin-commonjs": "^7.0.0",
8+
"rollup-plugin-multi-entry": "^2.0.1",
9+
"rollup-plugin-node-resolve": "^2.0.0"
10+
},
11+
"license": "MIT License"
12+
}

0 commit comments

Comments
 (0)