Skip to content
This repository was archived by the owner on Oct 16, 2021. It is now read-only.

Commit fcfdc6c

Browse files
committed
feat(baumeister): Enable to define baumeister config in package.json
You are now able to choose to store your settings either in a file called `baumeister.json` (respectively `.baumeister.json`) or in a `baumeister` key in your `package.json` file.
1 parent b042ed5 commit fcfdc6c

File tree

7 files changed

+51
-32
lines changed

7 files changed

+51
-32
lines changed

app/templates/_package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
"cli-error-notifier": "^2.1.0",
9999
"common-tags": "^1.7.2",
100100
"copy-webpack-plugin": "^4.5.1",
101+
"cosmiconfig": "^5.0.7",
101102
"cross-env": "^5.1.4",
102103
"css-loader": "^2.1.0",
103104
"cssnano": "^4.1.8",

app/templates/build/config.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
1-
const configFile = require('../baumeister.json');
1+
import path from 'path';
2+
import cosmiconfig from 'cosmiconfig';
3+
import logSymbols from 'log-symbols';
4+
import chalk from 'chalk';
25

3-
/**
4-
* Boolean flag to set when using handlebars instead of plain HTML files in `src`.
5-
*/
6-
export const { useHandlebars } = configFile;
6+
const explorer = cosmiconfig('baumeister', {
7+
searchPlaces: ['package.json', '.baumeister.json', 'baumeister.json']
8+
});
79

8-
/**
9-
* Flag for generating banners on on top of dist files (CSS & JS).
10-
*/
11-
export const { generateBanners } = configFile;
10+
export const userSettings = explorer.searchSync(path.resolve(__dirname, '../'));
11+
12+
if (userSettings === null) {
13+
console.log(
14+
logSymbols.error,
15+
`${chalk.red.bold('error')} – No Baumeister config found`,
16+
'\n\n',
17+
chalk.yellow(
18+
'Please see the <https://github.com/micromata/Baumeister> for info regarding the configuration file.'
19+
)
20+
);
21+
process.exit(1);
22+
}
1223

1324
export const mainDirectories = {
1425
dev: '../server/',

app/templates/build/handlebars.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ import globby from 'globby';
1212
import perfy from 'perfy';
1313
import { stripIndents } from 'common-tags';
1414

15-
import { settings, useHandlebars } from './config';
15+
import { settings, userSettings } from './config';
16+
17+
const {
18+
config: { useHandlebars }
19+
} = userSettings;
1620

1721
perfy.start('build', false);
1822

app/templates/build/webpack/config.entry.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import path from 'path';
22
import globby from 'globby';
3-
import { settings } from '../config';
3+
import { settings, userSettings } from '../config';
44

5-
const configFile = require('../../baumeister.json');
5+
const { config: userConfig } = userSettings;
66

77
export const entry = {
88
app: `${path.join(__dirname, '../../', settings.sources.app)}index.js`,
99
...getVendorCSS()
1010
};
1111

1212
function getVendorCSS() {
13-
// Return flattened array of resolved globs from baumeister.json
13+
// Return flattened array of resolved globs from Baumeister user config.
1414
const vendorCSS = [].concat(
15-
...configFile.vendor.bundleCSS.map(glob =>
15+
...userConfig.vendor.bundleCSS.map(glob =>
1616
globby.sync(`./node_modules/${glob}`)
1717
)
1818
);

app/templates/build/webpack/config.module.rules.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import path from 'path';
22
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
33

4-
import { settings } from '../config';
4+
import { settings, userSettings } from '../config';
55
import { isDevMode } from './helpers';
66

7-
const configFile = require('../../baumeister.json');
7+
const { config: userConfig } = userSettings;
88

99
export const rules = [
1010
{
@@ -31,7 +31,7 @@ export const rules = [
3131
sourceMap: isDevMode(),
3232
config: {
3333
ctx: {
34-
usePurifyCSS: configFile.purifyCSS.usePurifyCSS,
34+
usePurifyCSS: userConfig.purifyCSS.usePurifyCSS,
3535
cssnano: {
3636
discardComments: {
3737
removeAll: true

app/templates/build/webpack/config.output.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import path from 'path';
2-
import { mainDirectories } from '../config';
2+
import { mainDirectories, userSettings } from '../config';
33
import { isDevMode } from './helpers';
44

5-
const configFile = require('../../baumeister.json');
5+
const { config: userConfig } = userSettings;
66

77
export const output = {
88
path: isDevMode()
99
? path.join(__dirname, '../', mainDirectories.dev)
1010
: path.join(__dirname, '../', mainDirectories.prod),
11-
filename: configFile.cacheBusting
11+
filename: userConfig.cacheBusting
1212
? 'app/[name].[chunkhash].bundle.js'
1313
: 'app/[name].bundle.js',
14-
chunkFilename: configFile.cacheBusting
14+
chunkFilename: userConfig.cacheBusting
1515
? 'app/[name].[chunkhash].bundle.js'
1616
: 'app/[name].bundle.js',
1717
publicPath: '/'

app/templates/build/webpack/config.plugins.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,18 @@ import PurifyCSSPlugin from 'purifycss-webpack';
88
import ImageminPlugin from 'imagemin-webpack-plugin';
99
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
1010

11-
import { generateBanners, settings, useHandlebars } from '../config';
11+
import { settings, userSettings } from '../config';
1212
import { isDevMode, isProdMode } from './helpers';
1313

1414
const pkg = require('../../package.json');
15-
const configFile = require('../../baumeister.json');
15+
16+
const { config: userConfig } = userSettings;
1617

1718
const manifest = new WebpackAssetsManifest({
1819
output: path.resolve('.webpack-assets.json')
1920
});
2021

21-
const copyVendorFiles = configFile.vendor.includeStaticFiles.map(glob => {
22+
const copyVendorFiles = userConfig.vendor.includeStaticFiles.map(glob => {
2223
return {
2324
from: glob,
2425
context: 'node_modules',
@@ -31,7 +32,7 @@ const purifyCSSOptions = {
3132
purifyOptions: {
3233
minify: true,
3334
cleanCssOptions: { level: { 1: { specialComments: 0 } } },
34-
whitelist: configFile.purifyCSS.whitelist
35+
whitelist: userConfig.purifyCSS.whitelist
3536
}
3637
};
3738

@@ -42,15 +43,17 @@ const generalPlugins = [
4243
manifest,
4344
new MiniCssExtractPlugin({
4445
filename:
45-
configFile.cacheBusting && isProdMode()
46+
userConfig.cacheBusting && isProdMode()
4647
? 'assets/css/[name].[chunkhash].bundle.css'
4748
: 'assets/css/[name].bundle.css'
4849
}),
49-
new webpack.ProvidePlugin({ ...configFile.webpack.ProvidePlugin }),
50+
new webpack.ProvidePlugin({ ...userConfig.webpack.ProvidePlugin }),
5051
new CopyWebpackPlugin([
5152
{
5253
from: '**/*.html',
53-
context: useHandlebars ? settings.destinations.handlebars : './src',
54+
context: userConfig.useHandlebars
55+
? settings.destinations.handlebars
56+
: './src',
5457
transform(content) {
5558
return content
5659
.toString()
@@ -75,8 +78,8 @@ const generalPlugins = [
7578
]),
7679
new webpack.DefinePlugin(
7780
isDevMode()
78-
? { ...configFile.webpack.DefinePlugin.development }
79-
: { ...configFile.webpack.DefinePlugin.production }
81+
? { ...userConfig.webpack.DefinePlugin.development }
82+
: { ...userConfig.webpack.DefinePlugin.production }
8083
)
8184
];
8285

@@ -91,10 +94,10 @@ const devPlugins = [];
9194
const prodPlugins = [
9295
new webpack.HashedModuleIdsPlugin(),
9396
new ImageminPlugin({ test: /\.(jpe?g|png|gif|svg)$/i }),
94-
configFile.purifyCSS.usePurifyCSS
97+
userConfig.purifyCSS.usePurifyCSS
9598
? new PurifyCSSPlugin(purifyCSSOptions)
9699
: false,
97-
generateBanners
100+
userConfig.generateBanners
98101
? new webpack.BannerPlugin({
99102
banner: stripIndents`${pkg.title} - v${pkg.version}
100103
${pkg.author.email}

0 commit comments

Comments
 (0)