This repository has been archived by the owner on Aug 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconfig.js
472 lines (431 loc) · 15.7 KB
/
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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
const _ = require('lodash')
const path = require('path')
const utils = require('./utils')
const shell = require('shelljs')
const fs = require('fs')
const ExtractTextPlugin = require('extract-text-webpack-plugin')
const webpack = require('webpack')
const webpackDevMiddleware = require('webpack-dev-middleware')
const webpackHotMiddleware = require('webpack-hot-middleware')
const BowerWebpackPlugin = require('bower-webpack-plugin')
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin')
const Visualizer = require('webpack-visualizer-plugin')
const yaml = require('js-yaml')
const AssetsPlugin = require('assets-webpack-plugin')
const WebpackMd5Hash = require('webpack-md5-hash')
let runtime = {}
runtime.lanyonDir = path.join(__dirname, '..')
runtime.lanyonEnv = process.env.LANYON_ENV || 'development'
runtime.lanyonPackageFile = path.join(runtime.lanyonDir, 'package.json')
const lanyonPackage = require(runtime.lanyonPackageFile)
runtime.lanyonVersion = lanyonPackage.version
runtime.trace = process.env.LANYON_TRACE === '1'
runtime.publicPath = '/assets/build/'
runtime.rubyProvidersOnly = (process.env.LANYON_ONLY || '')
runtime.rubyProvidersSkip = (process.env.LANYON_SKIP || '').split(/\s+/)
runtime.lanyonReset = process.env.LANYON_RESET === '1'
runtime.onTravis = process.env.TRAVIS === 'true'
runtime.ghPagesEnv = {
GHPAGES_URL : process.env.GHPAGES_URL,
GHPAGES_BOTNAME : process.env.GHPAGES_BOTNAME,
GHPAGES_BOTEMAIL: process.env.GHPAGES_BOTEMAIL,
}
runtime.isDev = runtime.lanyonEnv === 'development'
runtime.isHotLoading = runtime.isDev && ['serve', 'start'].indexOf(process.argv[2]) !== -1
runtime.projectDir = process.env.LANYON_PROJECT || process.env.PWD || process.cwd() // <-- symlinked npm will mess up process.cwd() and point to ~/code/lanyon
runtime.npmRoot = utils.upwardDirContaining('package.json', runtime.projectDir, 'lanyon')
if (!runtime.npmRoot) {
console.error(`--> Unable to determine non-lanyon npmRoot, falling back to ${runtime.projectDir}`)
runtime.npmRoot = runtime.projectDir
}
runtime.gitRoot = utils.upwardDirContaining('.git', runtime.npmRoot)
runtime.projectPackageFile = path.join(runtime.npmRoot, 'package.json')
try {
var projectPackage = require(runtime.projectPackageFile)
} catch (e) {
projectPackage = {}
}
runtime.gems = _.defaults(_.get(projectPackage, 'lanyon.gems') || {}, _.get(lanyonPackage, 'lanyon.gems'))
runtime = _.defaults(projectPackage.lanyon || {}, lanyonPackage.lanyon, runtime)
try {
runtime.projectDir = fs.realpathSync(runtime.projectDir)
} catch (e) {
runtime.projectDir = fs.realpathSync(`${runtime.gitRoot}/${runtime.projectDir}`)
}
runtime.cacheDir = path.join(runtime.projectDir, '.lanyon')
runtime.binDir = path.join(runtime.cacheDir, 'bin')
runtime.recordsPath = path.join(runtime.cacheDir, 'records.json')
runtime.assetsSourceDir = path.join(runtime.projectDir, 'assets')
runtime.assetsBuildDir = path.join(runtime.assetsSourceDir, 'build')
runtime.contentBuildDir = path.join(runtime.projectDir, '_site')
runtime.contentScandir = path.join(runtime.projectDir, runtime.contentScandir || '.')
runtime.contentIgnore = runtime.contentIgnore || []
// Load project's jekyll _config.yml
runtime.jekyllConfig = {}
const jekyllConfigPath = path.join(runtime.projectDir, '_config.yml')
try {
const buf = fs.readFileSync(jekyllConfigPath)
runtime.jekyllConfig = yaml.safeLoad(buf)
} catch (e) {
console.error(`Unable to load ${jekyllConfigPath}`)
}
runtime.themeDir = false
if (runtime.jekyllConfig.theme) {
const cmd = `${path.join(runtime.binDir, 'bundler')} show ${runtime.jekyllConfig.theme}`
const z = shell.exec(cmd).stdout
if (!z) {
console.error(`Unable find defined them "${runtime.jekyllConfig.theme}" via cmd: "${cmd}"`)
} else {
runtime.themeDir = z
}
}
// Set prerequisite defaults
for (const name in runtime.prerequisites) {
if (!runtime.prerequisites[name].exeSuffix) {
runtime.prerequisites[name].exeSuffix = ''
}
if (!runtime.prerequisites[name].exe) {
runtime.prerequisites[name].exe = name
}
if (!runtime.prerequisites[name].versionCheck) {
runtime.prerequisites[name].versionCheck = `${runtime.prerequisites[name].exe} -v`
}
}
// Determine rubyProvider sources to traverse
const allApps = [ 'system', 'docker', 'rbenv', 'rvm', 'ruby-shim' ]
if (runtime.rubyProvidersOnly === 'auto-all') {
runtime.rubyProvidersOnly = ''
}
if (runtime.rubyProvidersOnly) {
runtime.rubyProvidersSkip = []
allApps.forEach(app => {
if (app !== runtime.rubyProvidersOnly) {
runtime.rubyProvidersSkip.push(app)
}
})
}
function getFilename (extension, isChunk, isContent) {
let filename = `[name].${extension}`
if (!runtime.isDev) {
filename = `[name].[chunkhash].${extension}`
if (isContent) {
filename = `[name].[contenthash].${extension}`
}
}
if (isChunk) {
filename = `[name].[chunkhash].[id].chunk.${extension}`
}
return filename
}
const cfg = {
webpack: {
entry: (function entries () {
var entries = {}
runtime.entries.forEach(entry => {
entries[entry] = [ path.join(runtime.assetsSourceDir, `${entry}.js`) ]
if (entry === 'app' && runtime.isDev) {
entries[entry].unshift('webpack-hot-middleware/client')
}
})
if (runtime.common) {
// e.g.: [ "jquery" ]
// https://webpack.github.io/docs/code-splitting.html#split-app-and-vendor-code
entries.common = runtime.common
}
return entries
}()),
node: {
fs: 'empty',
},
target: 'web',
output: {
publicPath : runtime.publicPath,
path : runtime.assetsBuildDir,
filename : getFilename('js'),
chunkFilename: getFilename('js', true),
cssFilename : getFilename('css'),
},
devtool: 'eval-cheap-source-map',
// devtool: 'source-map',
bail : false, // <-- We use our own ReportErrors plugin as with bail errors details are lost. e.g.: `Error at NormalModule.onModuleBuildFailed`
module : {
loaders: (function plugins () {
const loaders = [
{
test : /\.woff(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url?limit=10000&mimetype=application/font-woff',
}, {
test : /\.woff2(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url?limit=10000&mimetype=application/font-woff',
}, {
test : /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url?limit=10000&mimetype=application/octet-stream',
}, {
test : /\.eot(\?v=\d+\.\d+\.\d+)?$/,
loader: 'file',
}, {
test : /\.cur(\?v=\d+\.\d+\.\d+)?$/,
loader: 'file',
}, {
test : /\.svg(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url?limit=10000&mimetype=image/svg+xml',
},
{
test : /\.coffee$/,
loader: 'coffee',
},
{
test : /\.(png|gif|jpe?g)$/,
loader: 'url?limit=8096',
},
{
// https://github.com/webpack/webpack/issues/512
test : /[\\/](bower_components)[\\/]modernizr[\\/]modernizr\.js$/,
loader: 'imports?this=>window!exports?window.Modernizr',
},
{
test : /[\\/](bower_components)[\\/]svgeezy[\\/]svgeezy\.js$/,
loader: 'imports?this=>window!exports?svgeezy',
},
{
// https://www.techchorus.net/blog/using-sass-version-of-bootstrap-with-webpack/
test : /[\\/](bower_components)[\\/]bootstrap-sass[\\/]assets[\\/]javascripts[\\/]/,
loader: 'imports?jQuery=jquery,$=jquery,this=>window',
},
{
test : /[\\/]jquery\..*\.js$/,
loader: 'imports?jQuery=jquery,$=jquery,this=>window',
},
]
if (runtime.isDev) {
loaders.push({
test : /\.css$/,
loader: `style!css?sourceMap!resolve-url?root=${runtime.projectDir}`,
})
loaders.push({
test : /\.scss$/,
loader: `style!css?sourceMap!sass?sourceMap!resolve-url?root=${runtime.projectDir}`,
})
loaders.push({
test : /\.less$/,
// @todo Had to disable resolve-url-loader for less as less currently produces invalid css (in its eyes)
// see: https://travis-ci.org/tus/tus.io/builds/183913229#L1206
loader: 'style!css?sourceMap!less?sourceMap',
})
loaders.push({
test : /\.(js|jsx)$/,
loader: 'babel',
query : {
babelrc: false,
// If we ever want multiple loaders on this test: https://github.com/babel/babel-loader/issues/166#issuecomment-170054444
presets: [
require.resolve('babel-preset-es2015'),
require.resolve('babel-preset-react'),
require.resolve('babel-preset-stage-0'),
],
sourceRoot : `${runtime.projectDir}`,
cacheDirectory: `${runtime.cacheDir}/babelCache`,
},
exclude: /[\\/](node_modules|bower_components|js-untouched)[\\/]/,
})
} else {
loaders.push({
test : /\.css$/,
loader: ExtractTextPlugin.extract(`css?sourceMap!resolve-url?root=${runtime.projectDir}`),
})
loaders.push({
test : /\.scss$/,
loader: ExtractTextPlugin.extract(`css?sourceMap!sass?sourceMap!resolve-url?root=${runtime.projectDir}`),
})
loaders.push({
test : /\.less$/,
// @todo Had to disable resolve-url-loader for less as less currently produces invalid css (in its eyes)
// see: https://travis-ci.org/tus/tus.io/builds/183913229#L1206
loader: ExtractTextPlugin.extract('css?sourceMap!less?sourceMap'),
})
loaders.push({
test : /\.(js|jsx)$/,
loader: 'babel',
query : {
babelrc: false,
// If we ever want multiple loaders on this test: https://github.com/babel/babel-loader/issues/166#issuecomment-170054444
presets: [
require.resolve('babel-preset-es2015'),
require.resolve('babel-preset-react'),
require.resolve('babel-preset-stage-0'),
],
sourceRoot : `${runtime.projectDir}`,
cacheDirectory: `${runtime.cacheDir}/babelCache`,
},
exclude: /[\\/](node_modules|bower_components|js-untouched)[\\/]/,
})
}
return loaders
}()),
},
plugins: (function plugins () {
var plugins = [
new BowerWebpackPlugin(),
new webpack.ProvidePlugin({
_ : 'lodash',
$ : 'jquery',
jQuery: 'jquery',
}),
]
if (runtime.isDev) {
plugins.push(new webpack.HotModuleReplacementPlugin())
} else {
plugins.push(new ExtractTextPlugin(getFilename('css'), {
allChunks: true,
}))
plugins.push(new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: true,
},
mangle : true,
sourceMap: true,
exclude : /[\\/](node_modules|bower_components|js-untouched)[\\/]/,
}))
// plugins.push(new webpack.NoErrorsPlugin())
plugins.push(new OptimizeCssAssetsPlugin())
plugins.push(new webpack.optimize.OccurrenceOrderPlugin())
plugins.push(new webpack.optimize.DedupePlugin())
plugins.push(new webpack.optimize.LimitChunkCountPlugin({maxChunks: 15}))
plugins.push(new webpack.optimize.MinChunkSizePlugin({minChunkSize: 10000}))
plugins.push(function ReportErrors () {
this.plugin('done', ({compilation}) => {
for (const asset in compilation.assets) {
console.log(`--> Wrote ${runtime.assetsBuildDir}/${asset}`)
}
if (compilation.errors && compilation.errors.length) {
console.error(compilation.errors)
if (!runtime.isDev) {
process.exit(1)
}
}
})
})
}
if (runtime.common) {
plugins.push(new webpack.optimize.CommonsChunkPlugin(/* chunkName= */'common', /* filename= */getFilename('js')))
}
if (runtime.statistics) {
const fullpathStatistics = `${runtime.assetsBuildDir}/${runtime.statistics}`
if (runtime.isDev) {
console.log(`--> Cannot write statistics to "${fullpathStatistics}" in dev mode. Create a production build via LANYON_ENV=production. `)
} else {
// @todo: Once Vizualizer supports multiple entries, add support for that here
// https://github.com/chrisbateman/webpack-visualizer/issues/5
// Currently it just shows stats for all entries in one graph
plugins.push(new Visualizer({
filename: runtime.statistics,
}))
}
}
plugins.push(new AssetsPlugin({
filename: 'jekyll.lanyon_assets.yml',
path : runtime.cacheDir,
processOutput (assets) {
console.log(`--> Writing asset manifest to: "${runtime.cacheDir}/jekyll.lanyon_assets.yml"`)
return yaml.safeDump({lanyon_assets: assets})
},
}))
plugins.push(new WebpackMd5Hash())
plugins.push(new webpack.optimize.OccurenceOrderPlugin())
return plugins
}()),
resolveLoader: {
root: [
path.join(runtime.lanyonDir, 'node_modules'),
path.join(runtime.projectDir, 'node_modules'),
],
},
recordsPath: runtime.recordsPath,
resolve : {
root: [
path.resolve(runtime.assetsSourceDir),
`${path.resolve(runtime.assetsSourceDir)}/bower_components`,
`${path.resolve(runtime.projectDir)}/node_modules`,
`${path.resolve(runtime.lanyonDir)}/node_modules`,
],
},
debug: runtime.isDev,
},
}
if (runtime.isHotLoading) {
var bundler = webpack(cfg.webpack)
}
cfg.browsersync = {
server: {
port : runtime.ports.content,
baseDir : runtime.contentBuildDir,
middleware: (function middlewares () {
var middlewares = []
if (runtime.isHotLoading) {
middlewares.push(webpackDevMiddleware(bundler, {
publicPath: runtime.publicPath,
hot : true,
inline : true,
stats : { colors: true },
}))
middlewares.push(webpackHotMiddleware(bundler))
}
if (!middlewares.length) {
return false
}
return middlewares
}()),
// serveStatic: runtime.themeDir
},
watchOptions: {
ignoreInitial: true,
ignored : [
// no need to watch '*.js' here, webpack will take care of it for us,
// including full page reloads if HMR won't work
'*.js',
'.git',
'assets/build',
'.lanyon',
],
},
reloadDelay: 200,
files : runtime.contentBuildDir,
}
cfg.jekyll = {
exclude: [
'node_modules',
'env.sh',
'env.*.sh',
'.env.sh',
'.env.*.sh',
'.lanyon',
],
}
cfg.nodemon = {
onChangeOnly: true,
verbose : true,
watch : runtime.contentScandir,
ignore : [
'_site/**',
'.env.*.sh',
'.env.sh',
'.lanyon/**',
'assets/**',
'env.*.sh',
'env.sh',
'node_modules/**',
'vendor/**',
].concat(runtime.contentIgnore),
ext: [
'htm',
'html',
'jpg',
'json',
'md',
'png',
'sh',
'yml',
].join(','),
}
cfg.runtime = runtime
module.exports = cfg