-
Notifications
You must be signed in to change notification settings - Fork 34
/
webpack.netlify.config.js
90 lines (84 loc) · 2.68 KB
/
webpack.netlify.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
const webpack = require('webpack')
const path = require('path')
const fs = require('fs')
const environment = require('./node/environment')
const config = require('./node/config')
const fm = require('front-matter')
const articles = {}
config.availableLanguages.forEach(({ key: language }) => {
articles[language] = fs
.readdirSync(
path.join(__dirname, `resources/content/articles/${language}`),
{ encoding: 'utf8' }
)
.map(filename => {
const data = fs.readFileSync(
path.join(
__dirname,
`resources/content/articles/${language}`,
filename
),
{ encoding: 'utf8' }
)
const content = fm(data)
const slug = filename.replace(new RegExp(`^(.*?)-${language}.md$`), '$1')
return {
label: `${content.attributes.title} - ${content.attributes.description}`,
value: slug
}
})
})
process.env.AVAILABLE_ARTICLES = JSON.stringify(articles)
const getRandomString = () => {
let s = ''
const characterRange = new Array(123 - 97).fill(null).map((_, i) => i + 97).concat(...new Array(58 - 48).fill(null).map((_, i) => i + 48)).map(n => String.fromCharCode(n))
while (s.length < 16) {
const index = Math.round(Math.random() * (characterRange.length - 1))
s += characterRange[index]
}
return s
}
const bundleOutput = `netlify.bundle.${getRandomString()}.js`
const adminIndex = path.join(__dirname, 'public/admin/index.html')
const adminTemplate = path.join(__dirname, 'static/admin/index.html')
const adminContents = fs.readFileSync(adminTemplate, { encoding: 'utf8' })
fs.writeFileSync(adminIndex, adminContents.replace('{{bundle}}', bundleOutput), { encoding: 'utf8' })
module.exports = {
entry: {
netlify: path.join(__dirname, 'netlify/index.js')
},
output: {
path: path.join(__dirname, 'public/admin'),
filename: bundleOutput
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [ '@babel/preset-env', '@babel/preset-react' ]
}
}
},
{
test: /\.md$/,
loader: 'frontmatter-markdown-loader'
}
]
},
devtool: !environment.isProduction() && 'inline-source-map',
mode: environment.isProduction() ? 'production' : 'development',
plugins: [
new webpack.EnvironmentPlugin({
NODE_ENV: 'development',
// Branch exposed by netlify build https://www.netlify.com/docs/continuous-deployment/#environment-variables
HEAD: 'staging',
UPLOADCARE_PUBLIC_KEY: 'demopublickey',
GATSBY_IOHK_STARTER_CONFIG: JSON.stringify(config),
AVAILABLE_ARTICLES: ''
})
]
}