Skip to content
This repository has been archived by the owner on Apr 8, 2019. It is now read-only.

Commit

Permalink
Use Cosmiconfig (#41)
Browse files Browse the repository at this point in the history
* add process.env flag on suggestion by @Kovensky

* use cosmiconfig

* update readme with config info
  • Loading branch information
shellscape authored Mar 6, 2018
1 parent 8d160d8 commit f1aad58
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 54 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,23 @@ will display help and usage information. In order to accommodate the zero-config
changes in webpack v4, users of webpack v4 will need to use the `--help` flag to
display the same information.

## Webpack Config `serve` Property
## `webpack-serve` Config

You can store and define configuration / options for `webpack-serve` in a number
of different ways. This module leverages [cosmiconfig](https://github.com/davidtheclark/cosmiconfig),
which allows you to define `webpack-serve` options in the following ways:

- in your package.json file in a `serve` property
- in a `.serverc` or `.serverc.json` file, in either JSON or YML.
- in a `serve.config.js` file which exports a CommonJS module (just like webpack).

It's most common to keep `serve` options in your `webpack.config.js` (see below),
however, you can utilize any of the options above _in tandem_ with
`webpack.config.js`, and the options from the two sources will be merged. This
can be useful for setups with multiple configs that share common options for
`webpack-serve`, but require subtle differences.

### Webpack Config `serve` Property

`webpack-serve` supports the `serve` property in your webpack config file, which
may contain any of the supported [options](#Options).
Expand Down
43 changes: 19 additions & 24 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,11 @@ if (!module.parent) {
}

const chalk = require('chalk');
const cosmiconfig = require('cosmiconfig');
const debug = require('debug')('webpack-serve');
const findUp = require('find-up');
const meow = require('meow');
const merge = require('lodash/merge');
const importLocal = require('import-local'); // eslint-disable-line import/order
const WebpackServeError = require('./lib/WebpackServeError'); // eslint-disable-line import/order

let webpackPackage;
let webpackVersion;

try {
webpackPackage = require('webpack/package.json'); // eslint-disable-line global-require
webpackVersion = parseInt(webpackPackage.version, 10);
} catch (e) {
throw new WebpackServeError('webpack must be installed for webpack-serve to function.\n See: webpack-serve/package.json/peerDependencies');
}


// Prefer the local installation of webpack-serve
/* istanbul ignore if */
Expand Down Expand Up @@ -68,28 +57,34 @@ const cli = meow(chalk`
`);

const flags = Object.assign({}, cli.flags);
const cosmicOptions = {
rcExtensions: true,
sync: true
};
const explorer = cosmiconfig('serve', cosmicOptions);
const { config } = explorer.load() || {};
const options = merge({ flags }, config);

if (cli.input.length) {
[flags.config] = cli.input;
} else if (!flags.config) {
const filePath = findUp.sync('webpack.config.js');
flags.config = filePath;
const webpackExplorer = cosmiconfig('webpack', cosmicOptions);
const webpackConfig = webpackExplorer.load();

if (webpackConfig) {
options.config = webpackConfig.config;
flags.config = webpackConfig.filepath;
}
}

if (flags.help) {
cli.showHelp(0);
}

const options = { flags };

if (!flags.config) {
if (webpackVersion < 4) {
cli.showHelp(0);
} else {
// webpack v4 defaults an empty config to { entry: './src' }. but since we
// need an array, we'll mimic that default config.
options.config = { entry: ['./src'] };
}
// webpack v4 defaults an empty config to { entry: './src' }. but since we
// need an array, we'll mimic that default config.
options.config = { entry: ['./src'] };
}

serve(options)
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const pkg = require('./package.json');
module.exports = (opts) => {
updateNotifier({ pkg }).notify();

process.env.WEBPACK_SERVE = true;

return getOptions(opts)
.then((results) => {
const { options, configs } = results;
Expand Down
2 changes: 1 addition & 1 deletion lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function resolve(options) {
return Promise.resolve([].concat(options.config));
}

const configPath = path.resolve(options.flags.config || options.config);
const configPath = path.resolve(options.flags.config);
// eslint-disable-next-line global-require, import/no-dynamic-require
let config = require(configPath);

Expand Down
39 changes: 23 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@shellscape/koa-static": "^4.0.2",
"chalk": "^2.3.0",
"clipboardy": "^1.2.2",
"cosmiconfig": "^4.0.0",
"debug": "^3.1.0",
"find-up": "^2.1.0",
"get-port": "^3.2.0",
Expand Down
23 changes: 11 additions & 12 deletions test/tests/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function x(fn, ...args) {
const proc = execa(...args);
// webpack@4 has a lot more warnings
const ready = new RegExp('(Compiled successfully)|(Compiled with warnings)');

proc.stdout.on('data', (data) => {
if (ready.test(data.toString())) {
fn(proc);
Expand Down Expand Up @@ -87,18 +88,16 @@ describe('webpack-serve CLI', () => {
}, cliPath, { cwd: path.resolve(__dirname, '../fixtures/basic') });
});

if (webpackVersion > 3) {
t('should run webpack-serve with webpack v4 defaults', (done) => {
x((proc) => {
fetch('http://localhost:8080')
.then((res) => {
assert(res.ok);
proc.kill('SIGINT');
done();
});
}, cliPath, { cwd: path.resolve(__dirname, '../fixtures/webpack-4-defaults') });
});
}
t('should run webpack-serve with webpack v4 defaults', (done) => {
x((proc) => {
fetch('http://localhost:8080')
.then((res) => {
assert(res.ok);
proc.kill('SIGINT');
done();
});
}, cliPath, { cwd: path.resolve(__dirname, '../fixtures/webpack-4-defaults') });
});

t('should use the --content flag', (done) => {
const confPath = path.resolve(__dirname, '../fixtures/content/webpack.config.js');
Expand Down

0 comments on commit f1aad58

Please sign in to comment.