forked from storybookjs/storybook
/
webpack.config.js
99 lines (89 loc) · 2.9 KB
/
webpack.config.js
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
import path from 'path';
import webpack from 'webpack';
import fs from 'fs';
const logger = console;
const config = {
devtool: 'cheap-module-eval-source-map',
entry: {
admin: [
'stack-source-map/register',
path.resolve(__dirname, '../client/init_admin'),
],
preview: [
'stack-source-map/register',
'webpack-hot-middleware/client',
path.resolve(__dirname, '../client/init_preview'),
],
},
output: {
path: path.join(__dirname, 'dist'),
filename: '[name].bundle.js',
publicPath: '/static/',
},
plugins: [
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.HotModuleReplacementPlugin(),
],
resolve: {},
module: {
loaders: [
{
test: /\.jsx?$/,
loader: 'babel',
query: { presets: ['react', 'es2015', 'stage-2'] },
exclude: [path.resolve('./node_modules'), path.resolve(__dirname, 'node_modules')],
include: [path.resolve('./'), __dirname],
},
],
},
};
const configDir = path.resolve('./.storybook');
// load babelrc file.
const babelrcPath = path.resolve('./.babelrc');
if (fs.existsSync(babelrcPath)) {
logger.info('=> Using custom .babelrc configurations.');
const babelrcContent = fs.readFileSync(babelrcPath);
try {
const babelrc = JSON.parse(babelrcContent);
config.module.loaders[0].query = babelrc;
} catch (ex) {
logger.error(`=> Error parsing .babelrc file: ${ex.message}`);
throw ex;
}
}
// add config path to the entry
const storybookConfigPath = path.resolve(configDir, 'config.js');
if (!fs.existsSync(storybookConfigPath)) {
logger.error('=> Create a storybook config file in ".storybook/config.js".\n');
process.exit(0);
}
config.entry.preview.push(storybookConfigPath);
// load custom webpack configurations
const customConfigPath = path.resolve(configDir, 'webpack.config.js');
if (fs.existsSync(customConfigPath)) {
const customConfig = require(customConfigPath);
if (customConfig.module.loaders) {
logger.info('=> Loading custom webpack loaders.');
config.module.loaders =
config.module.loaders.concat(customConfig.module.loaders);
}
if (customConfig.plugins) {
logger.info(' => Loading custom webpack plugins.');
config.plugins = config.plugins.concat(customConfig.plugins);
}
// load resolve.extensions
if (customConfig.resolve && customConfig.resolve.extensions) {
config.resolve.extensions = customConfig.resolve.extensions.slice();
if (config.resolve.extensions.indexOf('.js') === -1) {
config.resolve.extensions.push('.js');
}
}
// load resolve.modulesDirectories
if (customConfig.resolve && customConfig.resolve.modulesDirectories) {
config.resolve.modulesDirectories = customConfig.resolve.modulesDirectories.slice();
if (config.resolve.modulesDirectories.indexOf('node_modules') === -1) {
config.resolve.modulesDirectories.push('node_modules');
}
}
}
export default config;