|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const path = require('path') |
| 4 | + |
1 | 5 | const loaderUtils = require('loader-utils')
|
| 6 | +const validateOptions = require('schema-utils') |
| 7 | + |
| 8 | +const schema = require('./options.json') |
| 9 | + |
2 | 10 | const posthtml = require('posthtml')
|
| 11 | +const posthtmlrc = require('posthtml-load-config') |
3 | 12 |
|
4 |
| -module.exports = function (source) { |
5 |
| - if (this.cacheable) this.cacheable() |
| 13 | +const parseOptions = require('./options') |
6 | 14 |
|
7 |
| - // configure options |
8 |
| - const qs = loaderUtils.parseQuery(this.query) |
| 15 | +const LoaderError = require('./Error') |
| 16 | +/** |
| 17 | + * PostHTML Loader |
| 18 | + * |
| 19 | + * @author Michael Ciniawsky <[email protected]> (@michael-ciniawsky) |
| 20 | + * @license MIT |
| 21 | + * |
| 22 | + * @version 1.0.0 |
| 23 | + * |
| 24 | + * @requires loader-utils |
| 25 | + * @requires schema-utils |
| 26 | + * |
| 27 | + * @requires posthtml |
| 28 | + * @requires posthtml-load-config |
| 29 | + * |
| 30 | + * @method posthtml-loader |
| 31 | + * |
| 32 | + * @param {String} html HTML |
| 33 | + * |
| 34 | + * @return {String} html HTML |
| 35 | + */ |
| 36 | +module.exports = function loader (html, map, meta) { |
| 37 | + // Loader Options |
| 38 | + const options = loaderUtils.getOptions(this) || {} |
| 39 | + |
| 40 | + validateOptions(schema, options, 'PostHTML Loader') |
| 41 | + |
| 42 | + // Make the loader async |
9 | 43 | const cb = this.async()
|
10 |
| - let config |
11 |
| - try { |
12 |
| - config = parseOptions.call(this, this.options.posthtml, qs) |
13 |
| - } catch (err) { |
14 |
| - return cb(err) |
15 |
| - } |
16 |
| - |
17 |
| - // configure custom parser argument if necessary |
18 |
| - const processArgs = [source.toString()] |
19 |
| - if (config.parser) { processArgs.push({ parser: config.parser }) } |
20 |
| - |
21 |
| - // run posthtml |
22 |
| - const ph = posthtml(config.plugins) |
23 |
| - ph.process.apply(ph, processArgs) |
24 |
| - .then((result) => cb(null, result.html), cb) |
25 |
| -} |
| 44 | + const file = this.resourcePath |
26 | 45 |
|
27 |
| -function parseOptions (config = [], qs = {}) { |
28 |
| - const res = {} |
| 46 | + Promise.resolve().then(() => { |
| 47 | + const length = Object.keys(options) |
| 48 | + .filter((option) => { |
| 49 | + switch (option) { |
| 50 | + case 'ident': |
| 51 | + case 'config': |
| 52 | + return |
| 53 | + default: |
| 54 | + return option |
| 55 | + } |
| 56 | + }) |
| 57 | + .length |
29 | 58 |
|
30 |
| - // if we have a function, run it |
31 |
| - if (typeof config === 'function') { config = config.call(this, this) } |
| 59 | + if (length) { |
| 60 | + return parseOptions.call(this, options) |
| 61 | + } |
32 | 62 |
|
33 |
| - // if it's not an object at this point, error |
34 |
| - if (typeof config !== 'object') { |
35 |
| - throw new Error('Configuration must return an array or object') |
36 |
| - } |
| 63 | + const rc = { |
| 64 | + path: path.dirname(file), |
| 65 | + ctx: { |
| 66 | + file: { |
| 67 | + extname: path.extname(file), |
| 68 | + dirname: path.dirname(file), |
| 69 | + basename: path.basename(file) |
| 70 | + }, |
| 71 | + options: {} |
| 72 | + } |
| 73 | + } |
37 | 74 |
|
38 |
| - // if we now have an array, that represents the plugins directly |
39 |
| - if (Array.isArray(config)) { |
40 |
| - res.plugins = config |
41 |
| - // if not, it's an object. if a plugin pack is being used, use it. |
42 |
| - // otherwise, use default plugins |
43 |
| - } else { |
44 |
| - res.plugins = qs.pack ? config[qs.pack] : config.plugins |
45 |
| - } |
| 75 | + if (options.config) { |
| 76 | + if (options.config.path) { |
| 77 | + rc.path = path.resolve(options.config.path) |
| 78 | + } |
46 | 79 |
|
47 |
| - // load in the custom parser if there is one |
48 |
| - if (config.parser) { res.parser = config.parser } |
| 80 | + if (options.config.ctx) { |
| 81 | + rc.ctx.options = options.config.ctx |
| 82 | + } |
| 83 | + } |
49 | 84 |
|
50 |
| - // try loading custom parser from query |
51 |
| - if (qs.parser) { |
52 |
| - res.parser = require(qs.parser) |
53 |
| - } |
| 85 | + return posthtmlrc(rc.ctx, rc.path, { argv: false }) |
| 86 | + }) |
| 87 | + .then((config) => { |
| 88 | + if (!config) config = {} |
54 | 89 |
|
55 |
| - return res |
56 |
| -} |
| 90 | + if (config.file) this.addDependency(config.file) |
| 91 | + |
| 92 | + if (config.options) { |
| 93 | + // Disable overriding `options.to` (`posthtml.config.js`) |
| 94 | + if (config.options.to) delete config.options.to |
| 95 | + // Disable overriding `options.from` (`posthtml.config.js`) |
| 96 | + if (config.options.from) delete config.options.from |
| 97 | + } |
| 98 | + |
| 99 | + let plugins = config.plugins || [] |
| 100 | + let options = Object.assign( |
| 101 | + { from: file, to: file }, |
| 102 | + config.options |
| 103 | + ) |
| 104 | + |
| 105 | + if (typeof options.parser === 'string') { |
| 106 | + options.parser = require(options.parser)() |
| 107 | + } |
| 108 | + |
| 109 | + // TODO(michael-ciniawsky) enable if when custom renderer available |
| 110 | + // if (typeof options.render === 'string') { |
| 111 | + // options.render = require(options.render)() |
| 112 | + // } |
| 113 | + |
| 114 | + return posthtml(plugins) |
| 115 | + .process(html, options) |
| 116 | + .then((result) => { |
| 117 | + if (result.messages) { |
| 118 | + result.messages.forEach((msg) => { |
| 119 | + switch (msg.type) { |
| 120 | + case 'error': |
| 121 | + this.emitError(msg.message) |
57 | 122 |
|
58 |
| -module.exports.parseOptions = parseOptions |
| 123 | + break |
| 124 | + case 'warning': |
| 125 | + this.emitWarning(msg.message) |
| 126 | + |
| 127 | + break |
| 128 | + case 'dependency': |
| 129 | + this.addDependency(msg.file) |
| 130 | + |
| 131 | + break |
| 132 | + default: |
| 133 | + break |
| 134 | + } |
| 135 | + }) |
| 136 | + } |
| 137 | + |
| 138 | + html = result.html |
| 139 | + |
| 140 | + if (this.loaderIndex === 0) { |
| 141 | + html = `export default \`${html}\`` |
| 142 | + |
| 143 | + cb(null, html) |
| 144 | + |
| 145 | + return null |
| 146 | + } |
| 147 | + |
| 148 | + if (!meta) meta = {} |
| 149 | + |
| 150 | + meta.ast = { type: 'posthtml', root: result.tree } |
| 151 | + meta.messages = result.messages |
| 152 | + |
| 153 | + cb(null, html, map, meta) |
| 154 | + |
| 155 | + return null |
| 156 | + }) |
| 157 | + }) |
| 158 | + .catch((err) => { |
| 159 | + cb(new LoaderError(err)) |
| 160 | + |
| 161 | + return null |
| 162 | + }) |
| 163 | +} |
0 commit comments