UNPKG

atool-doc

Version:

Static demo site generator based on atool-build & dora.

75 lines (58 loc) 2.69 kB
'use strict'; 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 mt = require('mark-twain'); var R = require('ramda'); var ejs = require('ejs'); var isCode = R.compose(R.contains(R.__, ['js', 'jsx', 'javascript']), R.path(['props', 'lang'])); var isStyle = R.whereEq({ type: 'code', props: { lang: 'css' } }); var isHtml = R.whereEq({ type: 'code', props: { lang: 'html' } }); var getChildren = R.compose(R.prop('children'), R.defaultTo({})); 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 fileContentTree = mt(content).content; var meta = mt(content).meta; var code = getChildren(fileContentTree.find(isCode)); var style = getChildren(fileContentTree.find(isStyle)); var html = getChildren(fileContentTree.find(isHtml)); 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 result = ejs.render(fs.readFileSync(tpl, 'utf-8'), { file: { meta: meta, link: link, title: resource.relativeToDemo, filePath: resource.relativeToCwd + resource.ext, resource: resource, // script: common ? scripts : scripts.slice(1), script: scripts, html: html, style: style, desc: util.marked(fileContentTree), alias: this._compiler.docSet.fileAlias } }); this.emitFile(calculateHtmlPath(options.cwd, resourcePath), result); return code; };