-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathwebpack-server.js
More file actions
104 lines (86 loc) · 2.82 KB
/
Copy pathwebpack-server.js
File metadata and controls
104 lines (86 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env node
/* eslint-env node */
/* eslint-disable no-console */
const path = require('path');
const args = require('./build-arguments');
const webpack = require('webpack');
const webpackMerge = require('webpack-merge');
const middleware = require('webpack-dev-middleware');
const hotMiddleware = require('webpack-hot-middleware');
const getWebpackConfig = require('./get-webpack-config');
const express = require('express');
if (!args.config) {
throw new Error('Please provide config path --config=PATH_TO_CONFIG.js');
}
let isCompiling = false;
if (args.buildDir) {
isCompiling = true;
}
const configPath = path.resolve(process.cwd(), args.config);
const config = require(configPath);
const PORT = args.port || 8080;
const HOST = args.host || 'localhost';
const webpackConfigFromProject = config.getWebpackConfig({
nodeRequire: require,
path,
webpack,
});
if (
webpackConfigFromProject &&
webpackConfigFromProject.module &&
webpackConfigFromProject.module.rules
) {
throw new Error(
`Please don't use module.rules in your webpack config, pass getLoaderForModule() instead. See the documentation for more info.`
);
}
const tsConfigPath = config.tsConfigPath
? config.tsConfigPath
: path.resolve(process.cwd(), './tsconfig.json');
const publicPath = `/${
config.getPublicPath && typeof config.getPublicPath === 'function' ? config.getPublicPath() : ''
}`;
const ourWebpackConfig = getWebpackConfig({
buildDir: args.buildDir,
configPath,
publicPath,
getSections: config.getSections,
getComponentRoots: config.getComponentRoots,
getExceptionForLoaders: config.getExceptionForLoaders,
getBabelConfig: config.getBabelConfig,
getBabelParserOptions: config.getBabelParserOptions,
getLoadersForComponents: config.getLoadersForComponents,
getLoaderForModule: config.getLoaderForModule,
getTypescriptCompilerOptions: config.getTypescriptCompilerOptions,
applyBabelToTypescriptCode: config.applyBabelToTypescriptCode,
tsConfigPath,
});
const mergedConfig = webpackMerge.smart(ourWebpackConfig, webpackConfigFromProject);
const compiler = webpack(mergedConfig);
if (isCompiling) {
compiler.run((err, stats) => {
// Stats Object
if (err) {
throw err;
}
if (stats.hasErrors()) {
throw stats.toString({
// Add console colors
colors: true,
});
}
console.log('Styleguide compiled');
});
} else {
const server = express();
server
.use(
middleware(compiler, {
publicPath: mergedConfig.output.publicPath,
})
)
.use(hotMiddleware(compiler));
server.listen(PORT, HOST, () => {
console.log(`Starting server on http://${HOST}:${PORT}${publicPath}`);
});
}