forked from OpenStackweb/openstacksignage
-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.common.js
181 lines (156 loc) · 5.31 KB
/
webpack.common.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
const path = require('path')
const fs = require('fs');
const webpack = require('webpack')
const dotenv = require('dotenv')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CopyWebpackPlugin = require('copy-webpack-plugin')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const config = dotenv.config()
if (config.error) {
throw config.error
}
const env = config.parsed,
envKeys = Object.keys(env).reduce((prev, next) => {
prev[`process.env.${next}`] = JSON.stringify(env[next]);
return prev;
}, {});
const templatesDir = 'templates';
const templatesMetadataFile = './templates.json';
const defaultEntryPoints = {
'image': './src/entrypoints/entry-image.js',
'image-landscape': './src/entrypoints/entry-image-landscape.js',
'banner': './src/entrypoints/entry-banner.js',
'schedule': './src/entrypoints/entry-schedule.js',
};
const defaultTemplates = [
{'name':'image','file':'image.html'},
{'name':'image-landscape','file':'image-landscape.html'},
// {'name':'index','file':'index.html'},
// {'name':'banner','file':'banner.html'},
];
const templateMetadata = {};
const templateEntryPoints = {};
const templateHTMLEntries = {};
const getHTMLFiles = (dir, files_) => {
files_ = files_ || [];
let files = fs.readdirSync(dir);
for (let i in files) {
const name = `${dir}/${files[i]}`;
if (fs.statSync(name).isDirectory()) {
getHTMLFiles(name, files_);
} else {
if (name.toLowerCase().endsWith('.html'))
files_.push(name);
}
}
return files_;
}
const isNumeric = (value) => {
return /^-?\d+$/.test(value);
}
// post processing
getHTMLFiles(templatesDir).forEach(htmlFile => {
const fileParts = htmlFile.split('/');
const root = fileParts[0];
const tenant = fileParts[1];
if(!isNumeric(fileParts[2])){
console.log(`skipping tenant ${tenant} summit ${fileParts[2]}...`)
return;
}
const summitId = parseInt(fileParts[2]);
const file = fileParts[3]
if (!templateMetadata.hasOwnProperty(tenant)) {
templateMetadata[tenant] = {};
}
if (!templateMetadata[tenant].hasOwnProperty(summitId)) {
templateMetadata[tenant][summitId] = [...defaultTemplates];
}
const fileEntry = file.toLowerCase().replace(".html", "");
templateMetadata[tenant][summitId].push({
'name': fileEntry,
'file': file,
});
// all entrypoints should follow this convention
if(fs.existsSync(`./src/entrypoints/entry-${fileEntry}.js`)) {
console.log(`adding template entry point ${fileEntry} ...`);
templateEntryPoints[fileEntry] = `./src/entrypoints/entry-${fileEntry}`
}
templateHTMLEntries[fileEntry] = htmlFile;
});
// write metadata file
fs.writeFileSync(templatesMetadataFile, JSON.stringify(templateMetadata), 'utf8');
const entry = {
...defaultEntryPoints,
...templateEntryPoints
}
const mapHTMLEntry = ([entryName, template]) => {
let fileName = entryName
let inject = 'body'
let chunks = ['manifest', entryName]
// for entry names that different html name:
if (entryName == 'schedule') fileName = 'index'
if (entryName == 'config-admin') {
inject = 'head'
fileName = 'admin'
}
return new HtmlWebpackPlugin({
inject: inject,
hash: true,
template: Object.keys(defaultEntryPoints).includes(entryName) ? `${fileName}.html` : template,
filename: `${fileName}.html`,
chunks: chunks,
minify:false,
})
}
// inject compiled entry chunk HTML from defaultEntries and templates
const entryHtmlPlugins = [
...Object.entries(defaultEntryPoints).map(mapHTMLEntry),
...Object.entries(templateHTMLEntries).map(mapHTMLEntry)
]
module.exports = {
entry: entry,
output: {
path: path.resolve(__dirname, './dist'),
filename: '[name].build.js',
},
module: {
rules: [
{test: /\.vue$/, loader: 'vue-loader' },
{test: /\.js$/, exclude: /node_modules/, use: ['babel-loader']},
// Styles: Inject CSS into the head with source maps
{
test: /\.(scss|css)$/,
use: [
'vue-style-loader',
'style-loader',
{loader: 'css-loader', options: {sourceMap: true, importLoaders: 1}},
{loader: 'postcss-loader', options: {sourceMap: true}},
{loader: 'sass-loader', options: {sourceMap: true}},
],
},
// Images: Copy image files to build folder
{test: /\.(?:ico|gif|png|jpg|jpeg)$/i, type: 'asset/resource'},
// Fonts and SVGs: Inline files
{test: /\.(woff(2)?|eot|ttf|otf|svg|)$/, type: 'asset/inline'},
]
},
resolve: {
alias: {
'vue$': 'vue/dist/vue.esm.js',
assets: path.resolve(__dirname, 'assets/')
}
},
performance: {
hints: false
},
plugins: [
// Vue plugin for the magic
new VueLoaderPlugin(),
new webpack.DefinePlugin(envKeys),
new CopyWebpackPlugin({ patterns: [
{ from: 'assets', to: 'assets' },
// metadata
{ from: templatesMetadataFile, to: templatesMetadataFile}
]})
].concat(entryHtmlPlugins),
}