Repository navigation
Expand file tree
/
Copy pathwebpack.config.js
More file actions
211 lines (203 loc) · 7.64 KB
/
Copy pathwebpack.config.js
File metadata and controls
211 lines (203 loc) · 7.64 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* eslint-disable @typescript-eslint/no-var-requires */
const path = require('path');
const { BannerPlugin, ProvidePlugin } = require('webpack');
const banner = require('./packages/core-impl/src/fragments/license');
// This is needed for baseUrl to resolve correctly from tsconfig
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
const { createStripLogs } = require('./grunt/webpack/strip-logs');
const { excludeMsgpackCondition } = require('./grunt/esbuild/build');
// Everything webpack builds here is compiled from the private implementation in packages/core-impl
// rather than from this monorepo root. That is also why the tsconfig has to be named explicitly:
// the plugin resolves one from the working directory, which would find the root's, and the root's
// covers scripts/ and carries none of the implementation's `baseUrl`.
const implDir = path.resolve(__dirname, 'packages', 'core-impl');
const tsconfigPathsPlugin = () => new TsconfigPathsPlugin({ configFile: path.resolve(implDir, 'tsconfig.json') });
const baseConfig = {
mode: 'production',
entry: {
index: path.resolve(implDir, 'src', 'common', 'lib', 'index.js'),
},
resolve: {
extensions: ['.js', '.ts'],
plugins: [tsconfigPathsPlugin()],
},
output: {
path: path.resolve(implDir, 'build'),
library: 'Ably',
libraryTarget: 'umd',
libraryExport: 'default',
},
module: {
rules: [
// all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
{ test: /\.ts$/, loader: 'ts-loader' },
],
},
target: ['web', 'es2020'],
externals: {
request: false,
ws: false,
},
plugins: [new BannerPlugin({ banner })],
performance: {
hints: false,
},
stats: {
modules: false,
},
};
// The React Native bundles of `@ably/pubsub-device`. Built with webpack rather than esbuild, as the
// React Native platform always has been. Each bundles the package's own entry point, which reaches
// the React Native implementation through the `react-native` export condition of
// `@ably/pubsub-core-impl/platform`, and leaves `@ably/pubsub-core` external for the same reason as
// the package's esbuild bundles (grunt/esbuild/build.js).
//
// Like the package's other platform bundles, it is built twice: with every log below error level
// stripped for the package root, and with full logging for `@ably/pubsub-device/debug`.
//
// It leaves MessagePack to the `MsgPack` plugin, as the package's other bundles do, by resolving
// with the same export condition (see `excludeMsgpackCondition` in grunt/esbuild/build.js).
//
// packages/README.md lists both bundles, so update it when you change them.
function createReactNativeConfig({ debug }) {
const stripLogs = debug ? null : createStripLogs();
return {
...baseConfig,
entry: {
index: path.resolve(__dirname, 'packages', 'device', 'src', 'index.ts'),
},
output: {
path: path.resolve(__dirname, 'packages', 'device', 'dist'),
filename: debug ? 'debug-react-native.js' : 'react-native.js',
library: { type: 'commonjs2' },
},
module: {
rules: [
...(stripLogs ? [stripLogs.rule] : []),
// Type checking is left to `tsc`: the package sources sit outside the implementation's
// tsconfig, whose `baseUrl` the implementation's own imports need.
{
test: /\.ts$/,
loader: 'ts-loader',
options: { configFile: path.resolve(implDir, 'tsconfig.json'), transpileOnly: true },
},
],
},
plugins: [...baseConfig.plugins, ...(stripLogs ? [stripLogs.plugin] : [])],
resolve: {
extensions: ['.js', '.ts'],
plugins: [tsconfigPathsPlugin()],
conditionNames: [excludeMsgpackCondition, 'react-native', 'require', 'default'],
fallback: {
crypto: false,
},
},
externals: {
request: false,
ws: false,
'@ably/pubsub-core': true,
'react-native': true,
fastestsmallesttextencoderdecoder: 'fastestsmallesttextencoderdecoder',
},
optimization: {
minimize: false,
},
};
}
// The NativeScript bundle of `@ably/pubsub-device`, published as the `@ably/pubsub-device/nativescript`
// subpath rather than behind an export condition, since NativeScript's bundler sets none that would
// tell it apart from a browser. Built like the React Native bundle: it bundles the package's own
// entry point, which reaches the NativeScript implementation through the `nativescript` export
// condition of the private `@ably/pubsub-core-impl/platform`, and leaves `@ably/pubsub-core` external.
//
// Unlike the package root on the web and React Native it keeps every log, as there is no debug
// counterpart to turn to for the full logs, and it includes MessagePack, as the NativeScript bundle
// always has, rather than leaving it to the `MsgPack` plugin.
//
// The app provides `nativescript-websockets` and `@nativescript/core`, so both stay external.
//
// packages/README.md lists this bundle, so update it when you change it.
function createNativeScriptConfig() {
return {
...baseConfig,
entry: {
index: path.resolve(__dirname, 'packages', 'device', 'src', 'index.ts'),
},
output: {
path: path.resolve(__dirname, 'packages', 'device', 'dist'),
filename: 'nativescript.js',
library: { type: 'commonjs2' },
},
module: {
rules: [
// Type checking is left to `tsc`, for the reason given in createReactNativeConfig.
{
test: /\.ts$/,
loader: 'ts-loader',
options: { configFile: path.resolve(implDir, 'tsconfig.json'), transpileOnly: true },
},
],
},
resolve: {
extensions: ['.js', '.ts'],
plugins: [tsconfigPathsPlugin()],
conditionNames: ['nativescript', 'require', 'default'],
fallback: {
crypto: false,
},
},
externals: {
request: false,
ws: false,
'@ably/pubsub-core': true,
'nativescript-websockets': true,
'@nativescript/core/application-settings': true,
},
optimization: {
minimize: false,
},
};
}
/**
* We create a bundle that exposes the mocha-junit-reporter package to be able to use it in the browser. We need to do this for the following reasons:
* - This package is designed for Node only and hence requires polyfills of Node libraries (e.g. `stream`, `path`) — webpack takes care of this for us.
* - The package is not compatible with RequireJS and hence we don’t have any easy way to directly load it in our tests — the webpack bundle exposes it as a global named MochaJUnitReporter.
*/
function createMochaJUnitReporterConfig() {
const dir = path.join(implDir, 'test', 'support', 'mocha_junit_reporter');
return {
mode: 'development',
entry: path.join(dir, 'index.js'),
externals: {
mocha: 'mocha.Mocha',
},
output: {
path: path.join(dir, 'build'),
filename: 'browser.js',
library: 'MochaJUnitReporter',
},
plugins: [
new ProvidePlugin({
process: 'process/browser.js',
}),
],
resolve: {
fallback: {
// These are the modules suggested by webpack, post v5-upgrade, to replace v4’s built-in shims.
path: require.resolve('path-browserify'),
stream: require.resolve('stream-browserify'),
},
modules: [
// Webpack doesn’t suggest any useful shim for the `fs` module, so we provide our own.
path.resolve(dir, 'shims'),
'node_modules',
],
},
};
}
module.exports = {
nativeScript: createNativeScriptConfig(),
reactNative: createReactNativeConfig({ debug: false }),
reactNativeDebug: createReactNativeConfig({ debug: true }),
mochaJUnitReporterBrowser: createMochaJUnitReporterConfig(),
};