Skip to content

Commit

Permalink
index.js package.json init
Browse files Browse the repository at this point in the history
  • Loading branch information
segayuu committed Jan 9, 2017
1 parent 7b5cf61 commit 70ba69e
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ jspm_packages

# Optional REPL history
.node_repl_history

# Web/PHPStorm IDE setting directory
.idea
69 changes: 69 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict';
const rollup = require('rollup').rollup;
const p_prefix = 'rollup-plugin-';
const r_plugin = {
npm: require(p_prefix + 'node-resolve'),
cjs: require(p_prefix + 'commonjs'),
multiEntry: require(p_prefix + 'multi-entry')
};

const path = require('path');

class hexoRollupRenderer {
constructor(hexo) {
this._hexo = hexo;
}

static isString(str){
return typeof str === 'string';
}

static get rollupPlugins() {
return [
r_plugin.multiEntry(),
r_plugin.npm({
browser: true
}),
r_plugin.cjs()
]
}

//
// Convert config of the entry to object.
//
get entry() {
const cwd = process.cwd();
let entry = this.userConfig.entry;
if (hexoRollupRenderer.isString(entry)) entry = Array.of(entry);
if (!entry) return [];
return entry.filter(n => n.indexOf('source') !== -1).map(n => path.join(cwd, n));
}

get userConfig() {
return Object.assign({},
this._hexo.theme.config.rollup || {},
this._hexo.config.rollup || {}
);
}

get renderer() {
const self = this;
return function(data){
if (self.entry.length === 0) {
return Promise.resolve(data.text);
}
const config = Object.assign({}, self.userConfig, {
entry: data.path,
plugins: hexoRollupRenderer.rollupPlugins
});
return rollup(config).then(bundle => bundle.generate({
format: 'iife',
moduleName: 'hexo_rollup'
}).code);
};
}
}

/* globals hexo */
const classes = new hexoRollupRenderer(hexo);
hexo.extend.renderer.register('js', 'js', classes.renderer);
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "hexo-plugin-rollup",
"version": "0.0.1",
"main": "index.js",
"dependencies": {
"rollup": "^0.41.1",
"rollup-plugin-commonjs": "^7.0.0",
"rollup-plugin-multi-entry": "^2.0.1",
"rollup-plugin-node-resolve": "^2.0.0"
},
"license": "MIT License"
}

0 comments on commit 70ba69e

Please sign in to comment.