-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.config.js
97 lines (88 loc) · 2.98 KB
/
rollup.config.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
95
96
97
import typescript from "@rollup/plugin-typescript"
import glob from "glob"
import * as path from "path"
import * as fs from "fs"
function getHtmlSnippet(jsFilename) {
const htmlSnippet = `
{% comment %}
This file was generated with 'npm run build'.
{% endcomment %}
{% load static %}
<script src="{% static '${jsFilename}' %}" type="text/javascript"></script>
`
return htmlSnippet
}
/**
*
* @param {*} fileOut - project-name/static/project-name/built/page-name/timestamp.js
*
* Plugin to save the HTML snippet <script src="{% static 'project-name/built/page-name/timestamp.js' %}" type="text/javascript"></script>
* And save it to a reliable location for import with name page-name.html
*
* @returns rollup plugin
*/
function createHtmlScriptSnippet(fileOut) {
return {
name: 'create-html-script-snippet',
async generateBundle() {
let { dir: pageNameBase, base: timestampFilename } = path.parse(fileOut)
let { dir: builtBase, base: pageName } = path.parse(pageNameBase)
let { dir: projectNameBase, base: _built } = path.parse(builtBase)
let { base: projectName } = path.parse(projectNameBase)
const htmlStaticImportPath = path.join(projectName, 'built', pageName, timestampFilename)
const htmlSnippet = getHtmlSnippet(htmlStaticImportPath)
const dirName = path.join(projectName, "templates", projectName, "built")
const fileName = path.join(dirName, `${pageName}.html`)
fs.mkdirSync(dirName, { recursive: true });
fs.writeFileSync(fileName, htmlSnippet)
}
}
}
// Find all page.ts files in the project
// Assumes that page.ts files are in a folder namespaced by the page name
// We can filter for a particular page to speed up build times
function getEntryPoints(pageName = undefined) {
let pageFilter = "**/page.ts"
if (pageName) {
console.log(`Building ${pageName}`)
pageFilter = `**/${pageName}/page.ts`
}
const entryPoints = glob.sync(pageFilter).map((file) => {
const { dir } = path.parse(file)
const pagesParentDir = path.dirname(dir)
const pageDir = path.basename(dir)
const timestamp = Date.now()
const builtFilePath = path.join(
pagesParentDir,
"built",
pageDir,
`${timestamp}.js`,
)
return [file, builtFilePath]
})
return entryPoints
}
const defaultPlugins = [typescript()]
function createConfig(entryPoints) {
const rollupConfig = entryPoints.map(([inputFilePath, outputFilePath]) => {
return {
input: inputFilePath,
output: {
file: outputFilePath,
format: "cjs",
sourcemap: true,
},
plugins: [
...defaultPlugins,
createHtmlScriptSnippet(outputFilePath),
],
}
})
return rollupConfig
}
// Users can specify configEntrypointFile to filter page to build
// example: npm run build -- --configEntrypointFile game will build any 'game' pages only
export default commandLineArgs => {
const entryPoints = getEntryPoints(commandLineArgs.configEntrypointFile)
return createConfig(entryPoints)
}