forked from uswds/uswds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.twig.config.js
149 lines (132 loc) · 4 KB
/
webpack.twig.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
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
const fs = require('fs');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const merge = require('lodash.merge');
const outputDir = path.resolve(__dirname, './html-templates');
const entryFile = path.resolve(__dirname, `${outputDir}/main.js`);
const contentDir = "content"
if (!fs.existsSync(outputDir)){
fs.mkdirSync(outputDir)
fs.writeFile(entryFile, "", (err) => {
if (err) throw err
});
}
// syncronously check if a file exists
function checkFileExistsSync(filepath){
let flag = true;
try{
fs.accessSync(filepath, fs.constants.F_OK);
}catch(e){
flag = false;
}
return flag && filepath;
}
// builds the file object for html page gen
function buildFileObj(dir, file, dataFile){
const templateFile = file
const dataFilePath = `${dir}/${dataFile}`
const name = !dataFile
? `${file.substr(file.lastIndexOf('/') + 1).replace('.twig', '.html')}`
: `${dataFile.replace('.json', '.html')}`
function buildModifierData(dataSource) {
const regexDashes = /--([\s\S]*)$/
const regexTilda = /~([\s\S]*)$/
let modifiedData;
if (dataSource.indexOf('--') > -1) {
const rootDataFile = dataSource.replace(regexDashes, '.json')
modifiedData = merge(
JSON.parse(fs.readFileSync(rootDataFile, 'utf-8')),
JSON.parse(fs.readFileSync(dataSource, 'utf-8'))
)
}
if (dataSource.indexOf('~') > -1) {
const rootDataFile = dataSource.replace(regexTilda, '.json')
modifiedData = merge(
JSON.parse(fs.readFileSync(rootDataFile, 'utf-8')),
JSON.parse(fs.readFileSync(dataSource, 'utf-8'))
)
}
return modifiedData || JSON.parse(fs.readFileSync(dataSource, 'utf-8'))
}
const data = checkFileExistsSync(dataFilePath)
? buildModifierData(dataFilePath)
: {}
const fileObj = {
filename: name,
template: templateFile,
templateParameters: data,
}
return fileObj
}
// filter out anything that starts with an underscore or is not a twig file
function walk(dir, ext) {
let results = [];
const list = fs.readdirSync(dir);
list.forEach(file => {
// eslint-disable-next-line no-param-reassign
file = `${dir}/${file}`;
const stat = fs.statSync(file);
if (stat && stat.isDirectory() && path.basename(file).indexOf('_') !== 0) {
/* Recurse into a subdirectory */
results = results.concat(walk(file, ext));
} else if (
stat &&
!stat.isDirectory() &&
path.extname(file) === `${ext}` &&
path.basename(file).indexOf('_') !== 0
) {
/* Is a file */
// in each directory, we need to build modifers
// if a directory contains a content directory we update dir here
// once we are done migrating components, we should update since we won't
// have as many use cases
const dataDir = list.indexOf(contentDir) > -1 ? `${dir}/${contentDir}/` : dir
const allData = fs.readdirSync(dataDir).filter( f => f.indexOf('.json') > -1)
if (allData.length > 0) {
allData.forEach((d) => {
results.push(buildFileObj(dataDir, file, d));
})
} else {
results.push(buildFileObj(dataDir, file))
}
}
});
return results;
}
const components = walk('./packages', '.twig');
const templates = walk('./packages/templates', '.twig');
const files = [].concat(
components,
templates
)
const htmlPlugins = files.map(file =>
new HtmlWebpackPlugin({
inject: false,
filename: file.filename,
template: file.template,
templateParameters: file.templateParameters,
}))
module.exports = {
mode: 'production',
entry: {
main: entryFile,
},
output: {
path: outputDir,
},
module: {
rules: [
{
test: /\.twig$/,
use: "twigjs-loader",
resolve: {
alias: {
"@components": path.resolve(__dirname, './packages'),
"@templates": path.resolve(__dirname, './packages/templates'),
},
},
},
],
},
plugins: [].concat(htmlPlugins),
}