This repository was archived by the owner on Nov 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
143 lines (129 loc) · 3.92 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const webpack = require('webpack')
const fs = require('fs')
const reactDocs = require('react-docgen')
// This will attempt to:
// 1. find the target component in the current directory
// 2. find the target component in the src directory
// 3. look at the package.json main entry to find the file if a target component name isn't provided
let filePath
const fileName = process.argv[2]
if (process.argv[2]) {
const currDirPath = path.resolve('.', `${fileName}.js`)
const srcDirPath = path.resolve('.', 'src', `${fileName}.js`)
const isInCurrentDir = fs.existsSync(currDirPath)
const isInSrcDir = fs.existsSync(srcDirPath)
filePath = (isInCurrentDir && currDirPath) || (isInSrcDir && srcDirPath)
} else {
const packageJson = require(path.resolve('.', 'package.json'))
filePath = path.resolve(packageJson.main)
}
if (!filePath) console.error(`No file available on path: ${fileName}`)
const fileContents = fs.readFileSync(filePath, 'utf8')
const componentInfo = reactDocs.parse(fileContents)
module.exports = {
context: path.resolve('.', 'src'),
mode: 'production',
cache: true,
// Enables source maps
devtool: 'source-map',
entry: {
'render-demo': path.resolve(__dirname, 'src/DemoRenderer.js'),
},
plugins: [
// Passes on the info about the component
new webpack.DefinePlugin({
'process.env.COMPONENT_INFO': JSON.stringify(componentInfo),
}),
// Generates the HTML file for the demo
new HtmlWebpackPlugin({ title: componentInfo.displayName }),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin(),
],
resolve: {
symlinks: false,
alias: {
// Resolve the path to the target demo
DemoFile: filePath,
// Resolve the path to React so we don't import multiple react versions
react: path.resolve(__dirname, './node_modules/react'),
'parse-prop-types': path.resolve(__dirname, 'node_modules', 'parse-prop-types'),
},
},
// This needed so svg-inline-loader (and others) will work correctly
resolveLoader: {
modules: [path.join(__dirname, 'node_modules')],
},
output: {
path: path.resolve(__dirname, '/'),
publicPath: '/',
filename: '[name].js', // Has to be name for some reason? Anything else doesn't load render-demo
libraryTarget: 'umd', // make the bundle export
},
optimization: {
runtimeChunk: 'multiple',
moduleIds: 'hashed',
minimize: false,
splitChunks: {
minSize: 1000,
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/](?!@fs)/,
name: 'vendors',
chunks: 'all',
},
fs: {
test: /[\\/]node_modules[\\/]@fs[\\/]/,
name: 'fs',
chunks: 'all',
},
targetComponent: {
test: path.resolve('.', 'src'),
name: 'target-component',
chunks: 'all',
},
},
chunks: 'all',
},
},
module: {
rules: [
{
test: /\.js$/,
include: [
path.resolve('.'),
path.resolve(__dirname, 'src'),
path.resolve('node_modules/@fs'),
],
exclude: /node_modules\/(?!@fs)/,
use: [
'cache-loader',
{
loader: 'babel-loader',
options: {
cacheDirectory: true,
presets: [
'@babel/preset-react',
['@babel/preset-env', { targets: { node: 'current' } }],
'@emotion/babel-preset-css-prop',
],
plugins: [
['babel-plugin-emotion', { sourceMaps: true }],
'@babel/plugin-syntax-dynamic-import',
],
},
},
],
},
{
test: /\.(html|png)$/,
loader: 'file-loader?name=[name].[ext]',
},
{
test: /\.svg$/,
loader: 'svg-inline-loader',
},
],
},
}