forked from sonyboy232/better-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundler.js
94 lines (84 loc) · 3.1 KB
/
bundler.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
83
84
85
86
87
88
89
90
91
92
93
94
const fs = require('fs')
const execSync = require('child_process').execSync
const path = require('path')[process.platform === 'win32' ? 'win32' : 'posix']
var env = require('jsdoc/env');
const VUE_WRAPPER = process.env.IS_DEV ? 'src/vue-wrapper.js' : 'lib/vue-wrapper.js'
const REACT_WRAPPER = process.env.IS_DEV ? 'src/react-wrapper.jsx' : 'lib/react-wrapper.js'
const pathCrossEnv = (path) =>
process.platform !== 'win32' ? path : path.replace(/\\/g, '/')
module.exports = function bundle (Components, out, config) {
if (!Components.length || env.conf.templates.betterDocs.isReactNative) {
return
}
const vueComponents = Components.filter(c => c.component.type === 'vue')
const reactComponents = Components.filter(c => c.component.type === 'react')
const entry = path.join(out, 'entry.js')
const absoluteOut = path.resolve(out)
let init = `
window.reactComponents = {};\n
window.vueComponents = {};\n
`
if (vueComponents.length) {
init = init + `
import Vue from 'vue/dist/vue.js';\n
window.Vue = Vue;\n
import VueWrapper from '${pathCrossEnv(path.relative(absoluteOut, path.join(__dirname, VUE_WRAPPER)))}';\n
window.VueWrapper = VueWrapper;\n
`
}
if (reactComponents.length) {
const reactWrapperRelPath = pathCrossEnv(
path.relative(absoluteOut, path.join(__dirname, REACT_WRAPPER))
)
init = init + `
import React from "react";\n
import ReactDOM from "react-dom";\n
import ReactWrapper from '${reactWrapperRelPath}';\n
window.React = React;\n
window.ReactDOM = ReactDOM;\n
window.ReactWrapper = ReactWrapper;\n
`
}
// Import css
init = init + `
import './styles/reset.css';\n
import './styles/iframe.css';\n
`
if (config.betterDocs.component) {
if(config.betterDocs.component.wrapper) {
const absolute = path.resolve(config.betterDocs.component.wrapper)
const relative = pathCrossEnv(path.relative(absoluteOut, absolute))
init +=`
import _CustomWrapper from '${relative}';\n
window._CustomWrapper = _CustomWrapper;\n
`
}
if(config.betterDocs.component.entry
&& config.betterDocs.component.entry.length) {
init = `${config.betterDocs.component.entry.join('\n')}\n${init}`
}
}
const entryFile = init + Components.map((c, i) => {
const { displayName, filePath, type } = c.component
const relativePath = pathCrossEnv(path.relative(absoluteOut, filePath))
const name = `Component${i}`
return [
`import ${name} from '${relativePath}';`,
`${type}Components['${displayName}'] = ${name};`,
].join('\n')
}).join('\n\n')
console.log('Generating entry file for "components" plugin')
fs.writeFileSync(entry, entryFile)
console.log('Bundling components')
const outDist = path.join(out, 'build')
const cmd = `${process.platform === 'win32' ? 'SET ' : ''}NODE_ENV=development parcel build ${entry} --out-dir ${outDist}`
console.log(`running: ${cmd}`)
try {
execSync(cmd)
} catch (error) {
if(error.output && error.output.length){
console.log(error.output[1].toString())
}
throw error
}
}