forked from vuejs/rollup-plugin-vue
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
90 lines (75 loc) · 2.16 KB
/
index.ts
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
import * as fs from 'fs'
import * as path from 'path'
import {Browser, Page} from 'puppeteer'
import {rollup} from 'rollup'
import promised from '@znck/promised'
import {pluginCreateVueApp, plugins} from "./plugins"
import pluginVue from '../..'
const pluginCSS = require('rollup-plugin-css-only')
// -- rollup plugin inline file
const cache = {}
export async function build(filename, css = false): Promise<string> {
const cacheKey = JSON.stringify({filename, css})
if (cacheKey in cache) return cache[cacheKey]
let style: string | undefined
const input = filename + '__app.js'
const options = {defaultLang: {markdown: 'pluginMarkdown'}, css: css}
const bundle = await rollup({
input,
plugins: [
pluginCreateVueApp(input, filename),
pluginCSS({
output: (s: string) => {
style = s
}
}),
pluginVue(options),
...plugins
],
external: ['vue']
})
cache[cacheKey] = (await bundle.generate({
format: 'iife',
name: 'App',
globals: {
vue: 'Vue'
}
})).code + (style ? `\n;(function() {
var s = document.createElement('style');
s.type = 'text/css';
document.head.appendChild(s);
s.appendChild(document.createTextNode(${JSON.stringify(style)}))
})()` : '')
return cache[cacheKey]
}
const VUE_SOURCE = promised(fs).readFile(
path.resolve(__dirname, '../../node_modules/vue/dist/vue.min.js')
)
export async function open(name: string, browser: Browser, code: string, id: string = '#test'): Promise<Page> {
const page = await browser.newPage()
const content = `
<!doctype html>
<html>
<head>
<title>${name}</title>
</head>
<body>
<div id="app"></div>
<script>
${await VUE_SOURCE}
</script>
<script>
${await code}
</script>
</body>
</html>`
// Un-comment following lines to debug generated HTML.
if (!Boolean(process.env.CI)) {
const dir = path.join(__dirname, '../output')
if (!await promised(fs).exists(dir)) await promised(fs).mkdir(dir)
await promised(fs).writeFile(path.join(dir, name + '.html'), content)
}
await page.setContent(content)
await page.waitFor(id)
return page
}