This repository has been archived by the owner on Sep 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 249
/
index.js
82 lines (66 loc) · 2.25 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// @flow
import fs from "fs"
import path from "path"
import loaderUtils from "loader-utils"
import frontMatterParser from "gray-matter"
import pathToUri from "../_utils/path-to-uri"
import urlify from "../_utils/urlify"
// Use the path of the loader directory to avoid conflicts on the loaderContext
const NS = fs.realpathSync(__dirname)
const loader = function(input: string) {
const webpackInstance: WebpackInstance = this
loader.getCollection = () => this[NS]
const options = {
...webpackInstance.options.phenomic,
...loaderUtils.parseQuery(webpackInstance.query),
}
const context = options.context || webpackInstance.options.context
const plugins = (
options.plugins ||
require("../loader-preset-markdown").default
)
const relativePath = path.relative(context, webpackInstance.resourcePath)
const frontMatter = frontMatterParser(input)
const pluginsResult = plugins.reduce((result, plugin) => {
return plugin({
frontMatter,
result,
options,
})
}, {})
let tmpUrl = urlify(
pluginsResult.head && pluginsResult.head.route
// custom route
? pluginsResult.head.route
// default route
: relativePath
)
tmpUrl = (tmpUrl.substring(0, 1) === "/") ? tmpUrl.slice(1) : tmpUrl
const url = urlify(tmpUrl)
const resourceUrl = urlify(tmpUrl, true)
const contentHash = loaderUtils.getHashDigest(input)
const dataUrl = resourceUrl + "." + contentHash + ".json"
const metadata = {
__filename: relativePath,
__url: pathToUri("/", url),
__resourceUrl: pathToUri("/", resourceUrl),
__dataUrl: pathToUri("/", dataUrl),
}
const result = {
...pluginsResult,
...metadata,
}
webpackInstance.emitFile(dataUrl, JSON.stringify(result))
if (typeof webpackInstance[NS] !== "function") {
throw new Error(
"You are using phenomic loader without the corresponding plugin. " +
"This plugin should be added automatically by Phenomic, so if you are " +
"facing this issue, you are probably playing with the fire. " +
"To get more information, you can reach us on our community chat. " +
"https://phenomic.io/"
)
}
webpackInstance[NS](result)
return "module.exports = " + JSON.stringify(pathToUri("/", dataUrl))
}
module.exports = loader