forked from kodeklubben/codeclub-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.static.config.babel.js
112 lines (89 loc) · 3.07 KB
/
webpack.static.config.babel.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* The webpack static config file
* ------------------------------
*
* Writes index.html files for all courses and all lessons, so that search engines can reach them,
* and as an alternative to server side rendering (static html files is possible since we don't
* have data in a database). After the initial index.html is loaded, the scripts take over and
* make it a single page app.
*
* Note that (identical copies of) all images are output again, which isn't really necessary.
* Also the static-bundle.js isn't used for anything.
* If we could find a way of not outputting these files (only the html files) during build,
* that would be good.
*
*/
////////////////////////////////////////
// DEFINE GLOBAL VARIABLES FOR ESLINT //
////////////////////////////////////////
/* eslint-env node */
//////////////////////
// IMPORT / REQUIRE //
//////////////////////
import baseConfig, {lessonSrc, buildDir, publicPath} from './webpack.base.config.babel';
import path from 'path';
import StaticSiteGeneratorPlugin from 'static-site-generator-webpack-plugin';
import SitemapPlugin from 'sitemap-webpack-plugin';
import yamlFront from 'yaml-front-matter';
///////////////
// CONSTANTS //
///////////////
const scope = {window: {}};
const locals = {};
///////////////
// FUNCTIONS //
///////////////
function getStaticSitePaths() {
const glob = require('glob');
const absLessonSrc = path.resolve(__dirname, lessonSrc);
const coursePaths = glob.sync(path.join(absLessonSrc, '*/'), {dot: true})
.map(p => p.replace(new RegExp(`^(${absLessonSrc}\/)(.*)(\/)$`), '$2/'));
const lessonPaths = glob.sync(path.join(absLessonSrc, '*/*/*.md'))
.filter(p => !p.endsWith('index.md') && !p.endsWith('README.md'))
.filter(p => {
const {title, external} = yamlFront.loadFront(p);
if (external) { console.log('Skipping external course "' + title + '" (' + p + ')'); }
return !external;
})
.map(p => p.replace(new RegExp(`^(${absLessonSrc}\/)(.*)(\.md)$`), '$2/'));
const staticPaths = ['/'].concat(coursePaths).concat(lessonPaths);
console.log('Static paths:');
console.log(staticPaths);
// [
// '/scratch',
// ... (more courses)
// 'scratch/3d_flakser/3d_flakser_1',
// ... (more lessons)
// ]
return staticPaths;
}
const staticSitePaths = getStaticSitePaths();
///////////////////////
// THE ACTUAL CONFIG //
///////////////////////
const config = {
...baseConfig,
entry: {
staticbundle: './src/index-static.js'
},
output: {
...baseConfig.output,
filename: 'static-bundle.js',
// static-site-generator must have files compiled to UMD or CommonJS
// so they can be required in a Node context:
libraryTarget: 'umd'
},
resolve: {
...baseConfig.resolve,
alias: {
...baseConfig.resolve.alias,
buildDir: path.resolve(__dirname, buildDir)
}
},
plugins: [
...baseConfig.plugins,
new StaticSiteGeneratorPlugin('staticbundle', staticSitePaths, locals, scope),
new SitemapPlugin('http://oppgaver.kidsakoder.no' + publicPath, staticSitePaths)
]
};
export default config;