Skip to content

Commit 51d3d37

Browse files
committed
chore: transpile async, fix compiler references
1 parent 3c6dc24 commit 51d3d37

File tree

5 files changed

+69
-51
lines changed

5 files changed

+69
-51
lines changed

.babelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
],
1717
"plugins": [
1818
[
19-
"async-to-promises",
19+
"fast-async",
2020
"transform-object-rest-spread",
2121
{
2222
"useBuiltIns": true

package-lock.json

Lines changed: 40 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"dependencies": {
4545
"@webpack-contrib/schema-utils": "^1.0.0-beta.0",
4646
"app-root-path": "^2.0.1",
47+
"loud-rejection": "^1.6.0",
4748
"merge-options": "^1.0.0",
4849
"webpack-dev-middleware": "^3.0.0",
4950
"webpack-hot-client": "^3.0.0",
@@ -68,13 +69,13 @@
6869
"eslint-config-webpack": "^1.2.5",
6970
"eslint-plugin-import": "^2.12.0",
7071
"eslint-plugin-prettier": "^2.6.0",
72+
"fast-async": "^6.3.7",
7173
"husky": "^0.14.3",
7274
"jest": "^23.1.0",
7375
"killable": "^1.0.0",
7476
"koa": "^2.2.0",
7577
"koa-compose": "^4.0.0",
7678
"lint-staged": "^7.1.3",
77-
"loud-rejection": "^1.6.0",
7879
"memory-fs": "^0.4.1",
7980
"nsp": "^3.2.1",
8081
"pre-commit": "^1.2.2",

src/index.js

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,6 @@ const schema = require('../schemas/options.json');
1010

1111
const defaults = { devMiddleware: {}, hotClient: {} };
1212

13-
const getDevMiddleware = (options) => {
14-
/* eslint-disable import/no-dynamic-require, global-require, no-param-reassign */
15-
const { compiler, config } = options;
16-
17-
if (!compiler) {
18-
if (!config) {
19-
options.config = require(join(root.path, 'webpack.config.js'));
20-
}
21-
22-
options.compiler = webpack(config);
23-
}
24-
25-
if (!options.devMiddleware.publicPath) {
26-
const { publicPath } = compiler.options.output;
27-
28-
if (!publicPath) {
29-
throw new Error(
30-
"koa-webpack: publicPath must be set on `dev` options, or in a compiler's `output` configuration."
31-
);
32-
}
33-
34-
options.devMiddleware.publicPath = publicPath;
35-
}
36-
37-
return devMiddleware(options.compiler, options.devMiddleware);
38-
};
39-
4013
const getClient = (compiler, options) => {
4114
if (!options.hotClient) {
4215
return Promise.resolve(null);
@@ -72,6 +45,7 @@ const getMiddleware = (devWare, options) => async (context, next) => {
7245
context.req,
7346
{
7447
end: (content) => {
48+
// eslint-disable-next-line no-param-reassign
7549
context.body = content;
7650
resolve();
7751
},
@@ -88,8 +62,31 @@ module.exports = async (opts) => {
8862

8963
validate({ name: 'koa-webpack', schema, target: options });
9064

91-
const devWare = getDevMiddleware(options);
92-
const client = await getClient(options.compiler, options);
65+
let { compiler, config } = options;
66+
67+
if (!compiler) {
68+
if (!config) {
69+
// eslint-disable-next-line import/no-dynamic-require, global-require
70+
config = require(join(root.path, 'webpack.config.js'));
71+
}
72+
73+
compiler = webpack(config);
74+
}
75+
76+
if (!options.devMiddleware.publicPath) {
77+
const { publicPath } = compiler.options.output;
78+
79+
if (!publicPath) {
80+
throw new Error(
81+
"koa-webpack: publicPath must be set on `dev` options, or in a compiler's `output` configuration."
82+
);
83+
}
84+
85+
options.devMiddleware.publicPath = publicPath;
86+
}
87+
88+
const client = await getClient(compiler, options);
89+
const devWare = devMiddleware(compiler, options.devMiddleware);
9390
const middleware = getMiddleware(devWare, options);
9491
const close = (callback) => {
9592
const next = client ? () => client.close(callback) : callback;

test/index.test.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ async function setup(opts, setupMiddleware = defaultApp) {
4444
const options = buildOptions(opts);
4545
const middleware = await koaWebpack(options);
4646

47-
// console.log(middleware);
48-
4947
app.use(setupMiddleware(middleware));
5048

5149
const server = app.listen();

0 commit comments

Comments
 (0)