-
Notifications
You must be signed in to change notification settings - Fork 3
/
webpack.plugin.dev.js
43 lines (38 loc) · 1.39 KB
/
webpack.plugin.dev.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
const fs = require('fs')
const path = require('path')
const Handlebars = require('handlebars')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const parsePageMd = require('./lib/parse-page-md/index')
const parseGitLog = require('./lib/utils/git-log')
const embedHtml = require('./lib/build-page/embed-html')
const embedHtmlDummyPage = async (html, pageName, dummiesDirPath) => {
try {
const template = Handlebars.compile(html)
const mdPath = path.resolve(dummiesDirPath, `./${pageName}.md`)
const mdSource = fs.readFileSync(mdPath, 'utf-8')
const gitLog = await parseGitLog(mdPath)
const pageInfo = parsePageMd(mdSource, gitLog, dummiesDirPath, mdPath)
const embeddedHtml = embedHtml(template, pageInfo, { isEditable: true })
return Promise.resolve(embeddedHtml)
} catch (e) {
console.error(e)
return Promise.reject('')
}
}
class DevPlugin {
constructor(dummiesDirPath) {
this.dummiesDirPath = dummiesDirPath
}
apply(compiler) {
compiler.hooks.compilation.tap('DevPlugin', compilation => {
HtmlWebpackPlugin.getHooks(compilation).beforeEmit.tapAsync('DevPlugin', (data, callback) => {
const pageName = path.basename(data.outputName, '.html')
embedHtmlDummyPage(data.html, pageName, this.dummiesDirPath).then(html => {
data.html = html
callback(null, data)
})
})
})
}
}
module.exports = DevPlugin