Permalink
Browse files

fix: check port when devServer is enabled (#10)

  • Loading branch information...
popomore committed Mar 31, 2018
1 parent 5397894 commit f58d766907f6e18627a44d42b3513e41b71bcf09
@@ -1,9 +1,13 @@
'use strict';

const assert = require('assert');
const DevServer = require('./lib/dev_server');

module.exports = agent => {
if (!agent.config.assets.isLocal) return;
if (!agent.config.assets.devServer.enable) return;

assert(agent.config.assets.devServer.port, 'devServer.port is required when devServer is enabled');

const server = new DevServer(agent);
server.ready(err => {
3 app.js
@@ -7,7 +7,8 @@ const AssetsView = require('./lib/assets_view');

module.exports = app => {
const assetsConfig = app.config.assets;
if (assetsConfig.isLocal && !assetsConfig.url && assetsConfig.devServer.port) {

if (assetsConfig.devServer.enable && assetsConfig.isLocal) {
assetsConfig.url = 'http://127.0.0.1:' + assetsConfig.devServer.port;
}

@@ -25,6 +25,7 @@ module.exports = appInfo => ({
templateViewEngine: '',
contextKey: 'context',
devServer: {
enable: true,
command: '',
port: null,
env: {},
@@ -200,7 +200,7 @@ describe('test/assets.test.js', () => {
let app;

before(() => {
mock.env('local');
mock.env('prod');
app = mock.cluster({
baseDir: 'apps/custom-assets-url',
});
@@ -211,7 +211,7 @@ describe('test/assets.test.js', () => {
it('should GET /', () => {
return app.httpRequest()
.get('/')
.expect(/<link rel="stylesheet" href="http:\/\/localhost\/index.css"><\/link>/)
.expect(/<link rel="stylesheet" href="http:\/\/localhost\/index.b8e2efea.css"><\/link>/)
.expect(200);
});
});
@@ -136,4 +136,29 @@ describe('test/dev_server.test.js', () => {
app.expect('stderr', /spawn unknown ENOENT/);
app.expect('stderr', /Run "unknown command" failed after 5s/);
});

it('should check port when devServer is enabled', async () => {
mock.env('local');
app = mock.cluster({
baseDir: 'apps/dev-server-no-port',
});
// app.debug();
await app.ready();

app.expect('code', 1);
app.expect('stderr', /devServer.port is required when devServer is enabled/);
});

it('should not check port when devServer is disabled', async () => {
mock.env('local');
mock(process.env, 'DEV_SERVER_ENABLE', 'false');
app = mock.cluster({
baseDir: 'apps/dev-server-no-port',
});
app.debug();
await app.ready();

app.expect('code', 0);
app.expect('stdout', /egg started/);
});
});
@@ -7,4 +7,7 @@ exports.view = {
},
};
exports.assets = {
devServer: {
enable: false,
},
};
@@ -0,0 +1,11 @@
'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
async index() {
await this.ctx.render('index.js');
}
}

module.exports = HomeController;
@@ -0,0 +1,7 @@
'use strict';

module.exports = app => {
const { router, controller } = app;

router.get('/', controller.home.index);
};
@@ -0,0 +1,21 @@
'use strict';

exports.keys = '123456';
exports.view = {
mapping: {
'.js': 'assets',
},
};
exports.assets = {
devServer: {
enable: process.env.DEV_SERVER_ENABLE !== 'false',
waitStart: true,
command: 'node ' + require.resolve('./server.js'),
cwd: __dirname,
env: {
DEBUG: true,
},
timeout: 5000,
debug: process.env.DEV_SERVER_DEBUG === 'true',
},
};
@@ -0,0 +1,10 @@
#!/usr/bin/env node

'use strict';

console.log('[server] cwd:', process.cwd());
console.log('[server] DEBUG:', process.env.DEBUG);

if (process.env.EXIT) {
process.exit(1);
}
@@ -0,0 +1,3 @@
{
"name": "egg-view-assets"
}

0 comments on commit f58d766

Please sign in to comment.