diff --git a/.gitignore b/.gitignore index 5148e52..315ca04 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,6 @@ jspm_packages # Optional REPL history .node_repl_history + +# Web/PHPStorm IDE setting directory +.idea diff --git a/index.js b/index.js new file mode 100644 index 0000000..e383e54 --- /dev/null +++ b/index.js @@ -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); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..75138b8 --- /dev/null +++ b/package.json @@ -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" +}