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

Commit

Permalink
refactor: use config-loader (#126)
Browse files Browse the repository at this point in the history
* refactor: use config-loader

+ update to latest eslint-config-webpack

* fix: linting errors

* chore: clean up comments

* test: add webpack init failure test

* fix: remove webpack < 4 hook

* refactor: setting content defaults handled by options.js

* refactor: --help is managed by meow

* test: add additional coverage

* test: don't run http2 tests < node 9

* chore: fix lint errors after merge
  • Loading branch information
shellscape committed May 10, 2018
1 parent 7b7f9ea commit a7a682e
Show file tree
Hide file tree
Showing 61 changed files with 779 additions and 646 deletions.
13 changes: 0 additions & 13 deletions .eslintrc

This file was deleted.

11 changes: 11 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
root: true,
plugins: ['prettier'],
extends: ['@webpack-contrib/eslint-config-webpack'],
rules: {
'prettier/prettier': [
'error',
{ singleQuote: true, trailingComma: 'es5', arrowParens: 'always' },
],
},
};
73 changes: 19 additions & 54 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/usr/bin/env node

'use strict';

if (!module.parent) {
// eslint-disable-next-line global-require
const { register } = require('./lib/global');
Expand All @@ -14,7 +12,6 @@ const cosmiconfig = require('cosmiconfig');
const debug = require('debug')('webpack-serve');
const meow = require('meow');
const merge = require('lodash/merge');
const resolveModule = require('resolve').sync;
const importLocal = require('import-local'); // eslint-disable-line import/order

// Prefer the local installation of webpack-serve
Expand All @@ -25,7 +22,8 @@ if (importLocal(__filename)) {

const serve = require('./');

const cli = meow(chalk`
const cli = meow(
chalk`
{underline Usage}
$ webpack-serve <config> [...options]
Expand Down Expand Up @@ -59,61 +57,28 @@ const cli = meow(chalk`
$ webpack-serve ./webpack.config.js --no-reload
$ webpack-serve --config ./webpack.config.js --port 1337
$ webpack-serve # config can be omitted for webpack v4+ only
`, {
flags: {
require: {
alias: 'r',
type: 'string'
}
`,
{
flags: {
require: {
alias: 'r',
type: 'string',
},
},
}
});
);

const flags = Object.assign({}, cli.flags);

if (flags.require) {
if (typeof flags.require === 'string') {
flags.require = [flags.require];
}
const cwd = process.cwd();
for (const module of flags.require) {
if (module) {
// eslint-disable-next-line global-require, import/no-dynamic-require
require(resolveModule(module, { basedir: cwd }));
}
}
}

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

if (cli.input.length) {
[flags.config] = cli.input;
} else if (!flags.config) {
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);
[options.config] = cli.input;
} else if (flags.config) {
options.config = flags.config;
}

if (!flags.config) {
// 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)
.catch(() => {
process.exit(1);
});
serve(options).catch(() => {
process.exit(1);
});
16 changes: 7 additions & 9 deletions docs/addons/bonjour.config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
'use strict';

const path = require('path');

function broadcast(port) {
const bonjour = require('bonjour')(); // eslint-disable-line
bonjour.publish({
name: 'Some Bitchin\' App',
name: `Some Bitchin' App`,
port,
type: 'http',
subtypes: ['webpack']
subtypes: ['webpack'],
});
process.on('exit', () => {
bonjour.unpublishAll(() => {
Expand All @@ -19,19 +17,19 @@ function broadcast(port) {

module.exports = {
entry: {
index: [path.resolve(__dirname, 'app.js')]
index: [path.resolve(__dirname, 'app.js')],
},
mode: 'development',
output: {
filename: 'output.js'
}
filename: 'output.js',
},
};

module.exports.serve = {
content: [__dirname],
on: {
listen(server) {
broadcast(server.address().port);
}
}
},
},
};
11 changes: 5 additions & 6 deletions docs/addons/compress/express-compress.config.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
'use strict';

const path = require('path');

const compress = require('compression');
const convert = require('koa-connect');

module.exports = {
entry: {
index: [path.resolve(__dirname, 'app.js')]
index: [path.resolve(__dirname, 'app.js')],
},
mode: 'development',
output: {
filename: 'output.js'
}
filename: 'output.js',
},
};

module.exports.serve = {
content: [__dirname],
add: (app, middleware, options) => {
app.use(convert(compress()));
}
},
};
10 changes: 4 additions & 6 deletions docs/addons/compress/koa-compress.config.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
'use strict';

const path = require('path');
const compress = require('koa-compress');

module.exports = {
entry: {
index: [path.resolve(__dirname, 'app.js')]
index: [path.resolve(__dirname, 'app.js')],
},
mode: 'development',
output: {
filename: 'output.js'
}
filename: 'output.js',
},
};

module.exports.serve = {
content: [__dirname],
add: (app, middleware, options) => {
app.use(compress());
}
},
};
11 changes: 5 additions & 6 deletions docs/addons/history-fallback.config.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
'use strict';

const path = require('path');

const history = require('connect-history-api-fallback');
const convert = require('koa-connect');

module.exports = {
entry: {
index: [path.resolve(__dirname, 'app.js')]
index: [path.resolve(__dirname, 'app.js')],
},
mode: 'development',
output: {
filename: 'output.js'
}
filename: 'output.js',
},
};

module.exports.serve = {
Expand All @@ -22,5 +21,5 @@ module.exports.serve = {
};

app.use(convert(history(historyOptions)));
}
},
};
11 changes: 5 additions & 6 deletions docs/addons/local-ip.config.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
'use strict';

const path = require('path');

const internalIp = require('internal-ip');

module.exports = {
entry: {
index: [path.resolve(__dirname, 'app.js')]
index: [path.resolve(__dirname, 'app.js')],
},
mode: 'development',
output: {
filename: 'output.js'
}
filename: 'output.js',
},
};

module.exports.serve = {
content: [__dirname],
host: internalIp.v4.sync()
host: internalIp.v4.sync(),
};
11 changes: 5 additions & 6 deletions docs/addons/proxy-history-fallback.config.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
'use strict';

const path = require('path');

const convert = require('koa-connect');
const history = require('connect-history-api-fallback');
const proxy = require('http-proxy-middleware');

module.exports = {
entry: {
index: [path.resolve(__dirname, 'app.js')]
index: [path.resolve(__dirname, 'app.js')],
},
mode: 'development',
output: {
filename: 'output.js'
}
filename: 'output.js',
},
};

module.exports.serve = {
content: [__dirname],
add: (app, middleware, options) => {
app.use(convert(proxy('/api', { target: 'http://localhost:8081' })));
app.use(convert(history()));
}
},
};

// This add-on will route all incoming requests for
Expand Down
13 changes: 6 additions & 7 deletions docs/addons/proxy-router.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

const path = require('path');

const proxy = require('http-proxy-middleware');
const convert = require('koa-connect');
const Router = require('koa-router');
Expand All @@ -9,20 +8,20 @@ const router = new Router();

const proxyOptions = {
target: 'http://api.github.com',
changeOrigin: true
changeOrigin: true,
// ... see: https://github.com/chimurai/http-proxy-middleware#options
};

router.get('*', convert(proxy(proxyOptions)));

module.exports = {
entry: {
index: [path.resolve(__dirname, 'app.js')]
index: [path.resolve(__dirname, 'app.js')],
},
mode: 'development',
output: {
filename: 'output.js'
}
filename: 'output.js',
},
};

module.exports.serve = {
Expand All @@ -35,5 +34,5 @@ module.exports.serve = {

// router *must* be the last middleware added
app.use(router.routes());
}
},
};
12 changes: 5 additions & 7 deletions docs/addons/static-content-options.config.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
'use strict';

const path = require('path');

module.exports = {
entry: {
index: [path.resolve(__dirname, 'app.js')]
index: [path.resolve(__dirname, 'app.js')],
},
mode: 'development',
output: {
filename: 'output.js'
}
filename: 'output.js',
},
};

module.exports.serve = {
Expand All @@ -22,8 +20,8 @@ module.exports.serve = {

// pass desired options here. eg.
middleware.content({
index: 'index.aspx'
index: 'index.aspx',
// see: https://github.com/koajs/static#options
});
}
},
};
Loading

0 comments on commit a7a682e

Please sign in to comment.