@teambit/ui 0.0.566 → 0.0.570

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,338 @@
1
+ import { ProvidePlugin } from 'webpack';
2
+ import * as stylesRegexps from '@teambit/webpack.modules.style-regexps';
3
+ import { pathNormalizeToLinux } from '@teambit/legacy/dist/utils';
4
+ import { WebpackConfigWithDevServer } from '@teambit/webpack';
5
+
6
+ import HtmlWebpackPlugin from 'html-webpack-plugin';
7
+ import ReactRefreshWebpackPlugin from '@pmmmwh/react-refresh-webpack-plugin';
8
+ import errorOverlayMiddleware from 'react-dev-utils/errorOverlayMiddleware';
9
+ import evalSourceMapMiddleware from 'react-dev-utils/evalSourceMapMiddleware';
10
+ import noopServiceWorkerMiddleware from 'react-dev-utils/noopServiceWorkerMiddleware';
11
+ import redirectServedPath from 'react-dev-utils/redirectServedPathMiddleware';
12
+ import getPublicUrlOrPath from 'react-dev-utils/getPublicUrlOrPath';
13
+ import path from 'path';
14
+ import { html } from './html';
15
+
16
+ /*
17
+ * Webpack config for the bit ui
18
+ * i.e. `bit start --dev`,
19
+ */
20
+
21
+ const matchNothingRegex = 'a^';
22
+ const clientHost = process.env.WDS_SOCKET_HOST;
23
+ const clientPath = process.env.WDS_SOCKET_PATH; // default is '/sockjs-node';
24
+ const port = process.env.WDS_SOCKET_PORT;
25
+
26
+ // const reactRefreshRuntimeEntry = require.resolve('react-refresh/runtime');
27
+ // const reactRefreshWebpackPluginRuntimeEntry = require.resolve(
28
+ // '@pmmmwh/react-refresh-webpack-plugin/lib/runtime/RefreshUtils'
29
+ // );
30
+
31
+ const publicUrlOrPath = getPublicUrlOrPath(process.env.NODE_ENV === 'development', '/', '/public');
32
+
33
+ const moduleFileExtensions = [
34
+ 'web.mjs',
35
+ 'mjs',
36
+ 'web.js',
37
+ 'js',
38
+ 'web.ts',
39
+ 'ts',
40
+ 'web.tsx',
41
+ 'tsx',
42
+ 'json',
43
+ 'web.jsx',
44
+ 'jsx',
45
+ ];
46
+
47
+ export function devConfig(workspaceDir, entryFiles, title): WebpackConfigWithDevServer {
48
+ const resolveWorkspacePath = (relativePath) => path.resolve(workspaceDir, relativePath);
49
+
50
+ // Host
51
+ const host = process.env.HOST || 'localhost';
52
+
53
+ // Required for babel-preset-react-app
54
+ process.env.NODE_ENV = 'development';
55
+
56
+ return {
57
+ // Environment mode
58
+ mode: 'development',
59
+ // improves HMR - assume node_modules might change
60
+ snapshot: { managedPaths: [] },
61
+
62
+ devtool: 'inline-source-map',
63
+
64
+ // Entry point of app
65
+ entry: {
66
+ main: entryFiles,
67
+ },
68
+
69
+ output: {
70
+ // Development filename output
71
+ filename: 'static/js/[name].bundle.js',
72
+
73
+ pathinfo: true,
74
+
75
+ path: resolveWorkspacePath('/'),
76
+
77
+ publicPath: publicUrlOrPath,
78
+
79
+ chunkFilename: 'static/js/[name].chunk.js',
80
+
81
+ // point sourcemap entries to original disk locations (format as URL on windows)
82
+ devtoolModuleFilenameTemplate: (info) => pathNormalizeToLinux(path.resolve(info.absoluteResourcePath)),
83
+
84
+ // this defaults to 'window', but by setting it to 'this' then
85
+ // module chunks which are built will work in web workers as well.
86
+ // Commented out to use the default (self) as according to tobias with webpack5 self is working with workers as well
87
+ // globalObject: 'this',
88
+ },
89
+
90
+ infrastructureLogging: {
91
+ level: 'error',
92
+ },
93
+
94
+ stats: 'errors-only',
95
+
96
+ devServer: {
97
+ allowedHosts: 'all',
98
+
99
+ static: [
100
+ {
101
+ directory: resolveWorkspacePath(publicUrlOrPath),
102
+ staticOptions: {},
103
+ // Don't be confused with `dev.publicPath`, it is `publicPath` for static directory
104
+ // Can be:
105
+ // publicPath: ['/static-public-path-one/', '/static-public-path-two/'],
106
+ publicPath: publicUrlOrPath,
107
+ // Can be:
108
+ // serveIndex: {} (options for the `serveIndex` option you can find https://github.com/expressjs/serve-index)
109
+ serveIndex: true,
110
+ // Can be:
111
+ // watch: {} (options for the `watch` option you can find https://github.com/paulmillr/chokidar)
112
+ watch: true,
113
+ },
114
+ ],
115
+
116
+ // Enable compression
117
+ compress: true,
118
+
119
+ // Enable hot reloading
120
+ hot: true,
121
+
122
+ host,
123
+
124
+ historyApiFallback: {
125
+ disableDotRule: true,
126
+ index: publicUrlOrPath,
127
+ },
128
+
129
+ client: {
130
+ webSocketURL: {
131
+ hostname: clientHost,
132
+ pathname: clientPath,
133
+ port,
134
+ },
135
+ },
136
+
137
+ onBeforeSetupMiddleware({ app, server }) {
138
+ // Keep `evalSourceMapMiddleware` and `errorOverlayMiddleware`
139
+ // middlewares before `redirectServedPath` otherwise will not have any effect
140
+ // This lets us fetch source contents from webpack for the error overlay
141
+ app.use(evalSourceMapMiddleware(server));
142
+ // This lets us open files from the runtime error overlay.
143
+ app.use(errorOverlayMiddleware());
144
+ },
145
+
146
+ onAfterSetupMiddleware({ app }) {
147
+ // Redirect to `PUBLIC_URL` or `homepage` from `package.json` if url not match
148
+ app.use(redirectServedPath(publicUrlOrPath));
149
+
150
+ // This service worker file is effectively a 'no-op' that will reset any
151
+ // previous service worker registered for the same host:port combination.
152
+ // We do this in development to avoid hitting the production cache if
153
+ // it used the same host and port.
154
+ // https://github.com/facebook/create-react-app/issues/2272#issuecomment-302832432
155
+ app.use(noopServiceWorkerMiddleware(publicUrlOrPath));
156
+ },
157
+
158
+ devMiddleware: {
159
+ // forward static files
160
+ publicPath: publicUrlOrPath.slice(0, -1),
161
+ },
162
+ },
163
+
164
+ resolve: {
165
+ // These are the reasonable defaults supported by the Node ecosystem.
166
+ // We also include JSX as a common component filename extension to support
167
+ // some tools, although we do not recommend using it, see:
168
+ // https://github.com/facebook/create-react-app/issues/290
169
+ // `web` extension prefixes have been added for better support
170
+ // for React Native Web.
171
+ extensions: moduleFileExtensions.map((ext) => `.${ext}`),
172
+ alias: {
173
+ react: require.resolve('react'),
174
+ 'react-dom/server': require.resolve('react-dom/server'),
175
+ 'react-dom': require.resolve('react-dom'),
176
+ // 'react-refresh/runtime': require.resolve('react-refresh/runtime'),
177
+ },
178
+ fallback: {
179
+ fs: false,
180
+ path: require.resolve('path-browserify'),
181
+ stream: false,
182
+ process: false,
183
+ },
184
+ },
185
+
186
+ module: {
187
+ // Webpack by default includes node_modules under its managed paths which cause the whole directory to be cached
188
+ // Watch mode requires us to turn off unsafeCache as well
189
+ // this de-optimizes the dev build but ensures hmr works when writing/linking into node modules.
190
+ // However we do not lose the caching entirely like cache: false
191
+ unsafeCache: false,
192
+ rules: [
193
+ {
194
+ test: /\.m?js/,
195
+ resolve: {
196
+ fullySpecified: false,
197
+ },
198
+ },
199
+ {
200
+ test: /\.js$/,
201
+ enforce: 'pre',
202
+ include: /node_modules/,
203
+ // only apply to packages with componentId in their package.json (ie. bit components)
204
+ descriptionData: { componentId: (value) => !!value },
205
+ use: [require.resolve('@pmmmwh/react-refresh-webpack-plugin/loader'), require.resolve('source-map-loader')],
206
+ },
207
+ {
208
+ test: /\.(js|jsx|tsx|ts)$/,
209
+ exclude: /node_modules/,
210
+ include: workspaceDir,
211
+ use: [
212
+ require.resolve('@pmmmwh/react-refresh-webpack-plugin/loader'),
213
+ {
214
+ loader: require.resolve('babel-loader'),
215
+ options: {
216
+ configFile: false,
217
+ babelrc: false,
218
+ presets: [
219
+ // Preset includes JSX, TypeScript, and some ESnext features
220
+ require.resolve('babel-preset-react-app'),
221
+ ],
222
+ plugins: [require.resolve('react-refresh/babel')],
223
+ },
224
+ },
225
+ ],
226
+ },
227
+ {
228
+ test: stylesRegexps.sassModuleRegex,
229
+ use: [
230
+ require.resolve('style-loader'),
231
+ {
232
+ loader: require.resolve('css-loader'),
233
+ options: {
234
+ modules: {
235
+ localIdentName: '[name]__[local]--[hash:base64:5]',
236
+ },
237
+ sourceMap: true,
238
+ },
239
+ },
240
+ {
241
+ loader: require.resolve('sass-loader'),
242
+ options: {
243
+ sourceMap: true,
244
+ },
245
+ },
246
+ ],
247
+ },
248
+ {
249
+ test: stylesRegexps.sassNoModuleRegex,
250
+ use: [
251
+ require.resolve('style-loader'),
252
+ require.resolve('css-loader'),
253
+ {
254
+ loader: require.resolve('sass-loader'),
255
+ options: {
256
+ sourceMap: true,
257
+ },
258
+ },
259
+ ],
260
+ },
261
+ {
262
+ test: stylesRegexps.lessModuleRegex,
263
+ use: [
264
+ require.resolve('style-loader'),
265
+ {
266
+ loader: require.resolve('css-loader'),
267
+ options: {
268
+ modules: {
269
+ localIdentName: '[name]__[local]--[hash:base64:5]',
270
+ },
271
+ sourceMap: true,
272
+ },
273
+ },
274
+ {
275
+ loader: require.resolve('less-loader'),
276
+ options: {
277
+ sourceMap: true,
278
+ },
279
+ },
280
+ ],
281
+ },
282
+ {
283
+ test: stylesRegexps.lessNoModuleRegex,
284
+ use: [
285
+ require.resolve('style-loader'),
286
+ require.resolve('css-loader'),
287
+ {
288
+ loader: require.resolve('less-loader'),
289
+ options: {
290
+ sourceMap: true,
291
+ },
292
+ },
293
+ ],
294
+ },
295
+ {
296
+ test: stylesRegexps.cssModuleRegex,
297
+ use: [
298
+ require.resolve('style-loader'),
299
+ {
300
+ loader: require.resolve('css-loader'),
301
+ options: {
302
+ modules: {
303
+ localIdentName: '[name]__[local]--[hash:base64:5]',
304
+ },
305
+ sourceMap: true,
306
+ },
307
+ },
308
+ ],
309
+ },
310
+ {
311
+ test: stylesRegexps.cssNoModulesRegex,
312
+ use: [require.resolve('style-loader'), require.resolve('css-loader')],
313
+ },
314
+ ],
315
+ },
316
+
317
+ plugins: [
318
+ new ReactRefreshWebpackPlugin({
319
+ // we use '@pmmmwh/react-refresh-webpack-plugin/loader' directly where relevant.
320
+ // FYI, original defaults of the plugin are:
321
+ // include: /\.([cm]js|[jt]sx?|flow)$/i, exclude: /node_modules/,
322
+ include: matchNothingRegex,
323
+ }),
324
+ // Re-generate index.html with injected script tag.
325
+ // The injected script tag contains a src value of the
326
+ // filename output defined above.
327
+ new HtmlWebpackPlugin({
328
+ inject: true,
329
+ templateContent: html(title || 'My component workspace'),
330
+ chunks: ['main'],
331
+ filename: 'index.html',
332
+ }),
333
+ new ProvidePlugin({
334
+ process: require.resolve('process/browser'),
335
+ }),
336
+ ],
337
+ };
338
+ }
@@ -0,0 +1,38 @@
1
+ import { Configuration } from 'webpack';
2
+ import path from 'path';
3
+ import { merge } from 'webpack-merge';
4
+
5
+ import createBaseConfig from './webpack.base.config';
6
+
7
+ export default function createWebpackConfig(
8
+ workspaceDir: string,
9
+ entryFiles: string[],
10
+ publicDir: string
11
+ ): Configuration {
12
+ const baseConfig = createBaseConfig(workspaceDir, entryFiles);
13
+ const ssrConfig = createSsrConfig(workspaceDir, publicDir);
14
+
15
+ const combined = merge(baseConfig, ssrConfig);
16
+
17
+ return combined;
18
+ }
19
+
20
+ function createSsrConfig(workspaceDir: string, publicDir: string) {
21
+ const ssrConfig: Configuration = {
22
+ target: 'node',
23
+ devtool: 'eval-cheap-source-map', // TODO
24
+
25
+ output: {
26
+ path: path.resolve(workspaceDir, publicDir, 'ssr'),
27
+ publicPath: '/public/ssr/',
28
+ libraryTarget: 'commonjs',
29
+ filename: 'index.js',
30
+ },
31
+
32
+ // // no optimizations for ssr at this point,
33
+ // // especially no chunks.
34
+ // optimization: { },
35
+ };
36
+
37
+ return ssrConfig;
38
+ }