1 | #!/usr/bin/env node
|
2 |
|
3 | import open from 'open';
|
4 | import yargs from 'yargs';
|
5 | import { resolve, extname } from 'path';
|
6 | import interpret from 'interpret';
|
7 | import { createServer } from '../lib';
|
8 |
|
9 | const argv = yargs
|
10 | .option('client', {
|
11 | alias: 'c',
|
12 | description: 'Path to client webpack configuration',
|
13 | requiresArg: true,
|
14 | type: 'string',
|
15 | })
|
16 | .option('server', {
|
17 | alias: 's',
|
18 | description: 'Path to server webpack configuration',
|
19 | requiresArg: true,
|
20 | type: 'string',
|
21 | })
|
22 | .option('hot', {
|
23 | alias: 'H',
|
24 | default: true,
|
25 | description: 'Enable hot-reload.',
|
26 | type: 'boolean',
|
27 | })
|
28 | .argv;
|
29 |
|
30 | function registerCompiler(moduleDescriptor) {
|
31 | if (moduleDescriptor) {
|
32 | if (typeof moduleDescriptor === 'string') {
|
33 | require(moduleDescriptor);
|
34 | } else if (!Array.isArray(moduleDescriptor)) {
|
35 | moduleDescriptor.register(require(moduleDescriptor.module));
|
36 | } else {
|
37 | for (let i = 0; i < moduleDescriptor.length; i++) {
|
38 | try {
|
39 | registerCompiler(moduleDescriptor[i]);
|
40 | break;
|
41 | } catch (e) {
|
42 |
|
43 | }
|
44 | }
|
45 | }
|
46 | }
|
47 | }
|
48 |
|
49 | function load(entry) {
|
50 | const extensions = Object.keys(interpret.extensions).sort(function(a, b) {
|
51 | return a.length - b.length;
|
52 | });
|
53 | const configPath = resolve(entry);
|
54 | let ext;
|
55 | for (let i = extensions.length - 1; i >= 0; i--) {
|
56 | const tmpExt = extensions[i];
|
57 | if (configPath.indexOf(tmpExt, configPath.length - tmpExt.length) > -1) {
|
58 | ext = tmpExt;
|
59 | break;
|
60 | }
|
61 | }
|
62 | if (!ext) {
|
63 | ext = extname(configPath);
|
64 | }
|
65 | registerCompiler(interpret.extensions[ext]);
|
66 | const value = require(configPath);
|
67 | if (value && value.__esModule) {
|
68 | return value.default;
|
69 | }
|
70 | return value;
|
71 | }
|
72 |
|
73 | global.__IN_DEV_SERVER = true;
|
74 | process.env.HOT = argv.hot;
|
75 |
|
76 | const server = createServer({
|
77 | client: load(argv.client),
|
78 | server: load(argv.server),
|
79 | });
|
80 |
|
81 |
|
82 | server.on('ready', () => {
|
83 | console.log('💎 Ready.');
|
84 | });
|
85 |
|
86 | server.listen(process.env.PORT, () => {
|
87 | const url = `http://localhost:${server.address().port}/`;
|
88 | console.log(`💎 Listening: ${url}.`);
|
89 | if (!process.env.PORT) {
|
90 | open(url);
|
91 | }
|
92 | });
|