atool-doc
Version:
Static demo site generator based on atool-build & dora.
66 lines (52 loc) • 2.12 kB
JavaScript
;
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
var fs = require('fs');
var path = require('path');
var loaderUtils = require('loader-utils');
var webpack = require('atool-build/lib/webpack');
var ejs = require('ejs');
var util = require('../utils');
function calculateHtmlPath(cwd, source) {
var selfPath = path.relative(cwd, source);
return path.join(path.dirname(selfPath), path.basename(selfPath, path.extname(selfPath)) + '.html');
}
module.exports = function (content) {
var options = this.options;
var resourcePath = this.resourcePath;
var resource = new util.Resource(options.cwd, options.demoSource, resourcePath);
var query = loaderUtils.parseQuery(this.query);
var tpl = query.template;
this.addDependency(tpl);
var hasCommon = options.plugins.some(function (i) {
return i instanceof webpack.optimize.CommonsChunkPlugin;
});
var filename = loaderUtils.interpolateName(this, options.output.filename, {}) || resource.name + '.js';
var scripts = [].concat(_toConsumableArray(hasCommon ? [path.relative(resourcePath, path.join(resource.demoPath, 'common.js'))] : []), [filename]);
var link = {};
link.Index = path.relative('../', path.relative(resource.path, './index'));
Object.keys(options.entry).forEach(function (key) {
link[path.relative(options.demoSource, key)] = path.relative('../', path.relative(resource.path, key));
});
var html = ejs.render(fs.readFileSync(tpl, 'utf-8'), {
file: {
link: link,
title: resource.relativeToDemo,
filePath: resource.relativeToCwd + resource.ext,
resource: resource,
script: scripts,
desc: util.marked([{
type: 'h2',
children: 'code'
}, {
type: 'code',
props: {
lang: 'js'
},
children: content
}]),
alias: this._compiler.docSet.fileAlias
}
});
this.emitFile(calculateHtmlPath(options.cwd, resourcePath), html);
return content;
};