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

Commit

Permalink
feat(baumeister): Enable to define baumeister config in package.json
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mischah committed Feb 20, 2019
1 parent d455382 commit bb7656d
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 32 deletions.
1 change: 1 addition & 0 deletions app/templates/_package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
"cli-error-notifier": "^2.1.0",
"common-tags": "^1.7.2",
"copy-webpack-plugin": "^4.5.1",
"cosmiconfig": "^5.0.7",
"cross-env": "^5.1.4",
"css-loader": "^2.1.0",
"cssnano": "^4.1.8",
Expand Down
29 changes: 20 additions & 9 deletions app/templates/build/config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
const configFile = require('../baumeister.json');
import path from 'path';
import cosmiconfig from 'cosmiconfig';
import logSymbols from 'log-symbols';
import chalk from 'chalk';

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

/**
* Flag for generating banners on on top of dist files (CSS & JS).
*/
export const { generateBanners } = configFile;
export const userSettings = explorer.searchSync(path.resolve(__dirname, '../'));

if (userSettings === null) {
console.log(
logSymbols.error,
`${chalk.red.bold('error')} – No Baumeister config found`,
'\n\n',
chalk.yellow(
'Please see the <https://github.com/micromata/Baumeister> for info regarding the configuration file.'
)
);
process.exit(1);
}

export const mainDirectories = {
dev: '../server/',
Expand Down
6 changes: 5 additions & 1 deletion app/templates/build/handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import globby from 'globby';
import perfy from 'perfy';
import { stripIndents } from 'common-tags';

import { settings, useHandlebars } from './config';
import { settings, userSettings } from './config';

const {
config: { useHandlebars }
} = userSettings;

perfy.start('build', false);

Expand Down
8 changes: 4 additions & 4 deletions app/templates/build/webpack/config.entry.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import path from 'path';
import globby from 'globby';
import { settings } from '../config';
import { settings, userSettings } from '../config';

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

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

function getVendorCSS() {
// Return flattened array of resolved globs from baumeister.json
// Return flattened array of resolved globs from Baumeister user config.
const vendorCSS = [].concat(
...configFile.vendor.bundleCSS.map(glob =>
...userConfig.vendor.bundleCSS.map(glob =>
globby.sync(`./node_modules/${glob}`)
)
);
Expand Down
6 changes: 3 additions & 3 deletions app/templates/build/webpack/config.module.rules.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import path from 'path';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';

import { settings } from '../config';
import { settings, userSettings } from '../config';
import { isDevMode } from './helpers';

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

export const rules = [
{
Expand All @@ -31,7 +31,7 @@ export const rules = [
sourceMap: isDevMode(),
config: {
ctx: {
usePurifyCSS: configFile.purifyCSS.usePurifyCSS,
usePurifyCSS: userConfig.purifyCSS.usePurifyCSS,
cssnano: {
discardComments: {
removeAll: true
Expand Down
8 changes: 4 additions & 4 deletions app/templates/build/webpack/config.output.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import path from 'path';
import { mainDirectories } from '../config';
import { mainDirectories, userSettings } from '../config';
import { isDevMode } from './helpers';

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

export const output = {
path: isDevMode()
? path.join(__dirname, '../', mainDirectories.dev)
: path.join(__dirname, '../', mainDirectories.prod),
filename: configFile.cacheBusting
filename: userConfig.cacheBusting
? 'app/[name].[chunkhash].bundle.js'
: 'app/[name].bundle.js',
chunkFilename: configFile.cacheBusting
chunkFilename: userConfig.cacheBusting
? 'app/[name].[chunkhash].bundle.js'
: 'app/[name].bundle.js',
publicPath: '/'
Expand Down
25 changes: 14 additions & 11 deletions app/templates/build/webpack/config.plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ import PurifyCSSPlugin from 'purifycss-webpack';
import ImageminPlugin from 'imagemin-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';

import { generateBanners, settings, useHandlebars } from '../config';
import { settings, userSettings } from '../config';
import { isDevMode, isProdMode } from './helpers';

const pkg = require('../../package.json');
const configFile = require('../../baumeister.json');

const { config: userConfig } = userSettings;

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

const copyVendorFiles = configFile.vendor.includeStaticFiles.map(glob => {
const copyVendorFiles = userConfig.vendor.includeStaticFiles.map(glob => {
return {
from: glob,
context: 'node_modules',
Expand All @@ -31,7 +32,7 @@ const purifyCSSOptions = {
purifyOptions: {
minify: true,
cleanCssOptions: { level: { 1: { specialComments: 0 } } },
whitelist: configFile.purifyCSS.whitelist
whitelist: userConfig.purifyCSS.whitelist
}
};

Expand All @@ -42,15 +43,17 @@ const generalPlugins = [
manifest,
new MiniCssExtractPlugin({
filename:
configFile.cacheBusting && isProdMode()
userConfig.cacheBusting && isProdMode()
? 'assets/css/[name].[chunkhash].bundle.css'
: 'assets/css/[name].bundle.css'
}),
new webpack.ProvidePlugin({ ...configFile.webpack.ProvidePlugin }),
new webpack.ProvidePlugin({ ...userConfig.webpack.ProvidePlugin }),
new CopyWebpackPlugin([
{
from: '**/*.html',
context: useHandlebars ? settings.destinations.handlebars : './src',
context: userConfig.useHandlebars
? settings.destinations.handlebars
: './src',
transform(content) {
return content
.toString()
Expand All @@ -75,8 +78,8 @@ const generalPlugins = [
]),
new webpack.DefinePlugin(
isDevMode()
? { ...configFile.webpack.DefinePlugin.development }
: { ...configFile.webpack.DefinePlugin.production }
? { ...userConfig.webpack.DefinePlugin.development }
: { ...userConfig.webpack.DefinePlugin.production }
)
];

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

0 comments on commit bb7656d

Please sign in to comment.