forked from redwoodjs/redwood
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
164 lines (138 loc) · 5.03 KB
/
main.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const fs = require('fs')
const path = require('path')
const { merge } = require('webpack-merge')
const { getSharedPlugins } = require('@redwoodjs/core/config/webpack.common.js')
const {
importStatementPath,
getConfig,
getPaths,
} = require('@redwoodjs/internal')
const { getProject } = require('@redwoodjs/structure')
const config = getConfig()
const rwjsPaths = getPaths()
const staticAssetsFolder = path.join(getPaths().web.base, 'public')
function isPackageInstalled(alias) {
try {
return Boolean(require(alias))
} catch (e) {
return false
}
}
function withEmotionVersionFallback(config) {
const alias = Object.entries({
'@emotion/core': '@emotion/core',
'@emotion/styled': '@emotion/styled',
'emotion-theming': '@emotion/react',
}).reduce((acc, [packageName, alias]) => {
if (isPackageInstalled(alias)) {
acc[packageName] = require.resolve(alias)
}
return acc
}, {})
return merge(config, { resolve: { alias } })
}
const baseConfig = {
core: {
builder: 'webpack5',
},
stories: [
`${importStatementPath(rwjsPaths.web.src)}/**/*.stories.{tsx,jsx,js}`,
],
addons: [config.web.a11y && '@storybook/addon-a11y'].filter(Boolean),
// Storybook's UI uses a seperate Webpack configuration
managerWebpack: (sbConfig) => {
const userManagerPath = fs.existsSync(rwjsPaths.web.storybookManagerConfig)
? rwjsPaths.web.storybookManagerConfig
: './manager.example.js'
sbConfig.resolve.alias['~__REDWOOD__USER_STORYBOOK_MANAGER_CONFIG'] =
userManagerPath
return sbConfig
},
webpackFinal: (sbConfig, { configType }) => {
// configType is 'PRODUCTION' or 'DEVELOPMENT', why shout?
const isEnvProduction =
configType && configType.toLowerCase() === 'production'
const rwConfig = isEnvProduction
? require('@redwoodjs/core/config/webpack.production')
: require('@redwoodjs/core/config/webpack.development')
// We replace imports to "@redwoodjs/router" with our own implementation in "@redwoodjs/testing"
sbConfig.resolve.alias['@redwoodjs/router$'] = require.resolve(
'@redwoodjs/testing/dist/web/MockRouter.js'
)
sbConfig.resolve.alias['~__REDWOOD__USER_ROUTES_FOR_MOCK'] =
rwjsPaths.web.routes
sbConfig.resolve.alias['~__REDWOOD__USER_WEB_SRC'] = rwjsPaths.web.src
// Determine the default storybook style file to use.
const supportedStyleIndexFiles = ['index.scss', 'index.sass', 'index.css']
for (let file of supportedStyleIndexFiles) {
const filePath = path.join(rwjsPaths.web.src, file)
if (fs.existsSync(filePath)) {
sbConfig.resolve.alias['~__REDWOOD__USER_WEB_DEFAULT_CSS'] = filePath
break
}
}
const userPreviewPath = fs.existsSync(rwjsPaths.web.storybookPreviewConfig)
? rwjsPaths.web.storybookPreviewConfig
: './preview.example.js'
sbConfig.resolve.alias['~__REDWOOD__USER_STORYBOOK_PREVIEW_CONFIG'] =
userPreviewPath
sbConfig.resolve.extensions = rwConfig.resolve.extensions
sbConfig.resolve.plugins = rwConfig.resolve.plugins // Directory Named Plugin
// Webpack v5 does not include polyfills. Will error without these:
sbConfig.resolve.fallback = {
http: false,
https: false,
timers: false,
os: false,
tty: false,
crypto: false,
stream: false,
zlib: false,
path: false,
}
// ** PLUGINS **
sbConfig.plugins = [
...sbConfig.plugins,
...getSharedPlugins(isEnvProduction),
]
// ** LOADERS **
sbConfig.module.rules = rwConfig.module.rules
// ** NODE **
sbConfig.node = rwConfig.node
// Performance Improvements:
// https://webpack.js.org/guides/build-performance/#avoid-extra-optimization-steps
sbConfig.optimization = {
removeAvailableModules: false,
removeEmptyChunks: false,
splitChunks: false,
}
// https://webpack.js.org/guides/build-performance/#output-without-path-info
sbConfig.output.pathinfo = false
sbConfig = withEmotionVersionFallback(sbConfig)
return sbConfig
},
// only set staticDirs when running Storybook process; will fail if set for SB --build
...(process.env.NODE_ENV !== 'production' && {
staticDirs: [`${staticAssetsFolder}`],
}),
// only set up type checking for typescript projects
...(getProject().isTypeScriptProject && {
// https://storybook.js.org/docs/react/configure/typescript#mainjs-configuration
typescript: {
check: true,
// By default, the checker runs asynchronously in dev mode. Force it to run synchronously.
checkOptions: { async: false },
},
}),
}
const mergeUserStorybookConfig = (baseConfig) => {
const redwoodPaths = getPaths()
const hasCustomConfig = fs.existsSync(redwoodPaths.web.storybookConfig)
if (!hasCustomConfig) {
return baseConfig
}
const userStorybookConfig = require(redwoodPaths.web.storybookConfig)
return merge(baseConfig, userStorybookConfig)
}
/** @returns {import('webpack').Configuration} Webpack Configuration with storybook config */
module.exports = mergeUserStorybookConfig(baseConfig)