-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,3 +35,6 @@ jspm_packages | |
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# Web/PHPStorm IDE setting directory | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |