Repository navigation
Expand file tree
/
Copy pathwebpack.config.js
More file actions
184 lines (159 loc) · 5.77 KB
/
Copy pathwebpack.config.js
File metadata and controls
184 lines (159 loc) · 5.77 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
// @ts-check
import path from 'path';
import webpack from 'webpack';
import { fileURLToPath } from 'url';
import PACKAGE_JSON from './package.json' with { type: 'json' };
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// The ESM build needs ES modules and import() (for chunks) anyway.
const ESM_TARGETS = 'supports es6-module-dynamic-import';
/** @type {webpack.Configuration} */
const shared = {
mode: 'production',
watchOptions: {
ignored: ['BookReader/**', 'dist/**', 'node_modules/**', 'tests/**'],
},
target: ['web', 'es5'],
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules[/\\](?!(lit-element|lit-html|lit|@lit)[/\\]).*/,
loader: "babel-loader",
},
{
// Allow importing scss as plain strings. Only used for text selection plugin,
// since it needs to performantly load the CSS in an iframe to determine word
// sizings
test: /\.scss$/,
use: [
{ loader: 'css-loader', options: { exportType: 'string' } },
'sass-loader',
],
},
],
},
output: {
filename: '[name]',
path: path.resolve(__dirname, 'BookReader'),
},
};
/** Output file -> srcfile, for the classic and ESM builds */
const bookreaderEntries = {
// BookReader (core + <ia-bookreader> shell)
'ia-bookreader.js': './src/ia-bookreader/ia-bookreader.js',
// Plugins (sorted!)
'plugins/plugin.archive_analytics.js': { import: './src/plugins/plugin.archive_analytics.js', dependOn: 'ia-bookreader.js' },
'plugins/plugin.autoplay.js': { import: './src/plugins/plugin.autoplay.js', dependOn: 'ia-bookreader.js' },
'plugins/plugin.chapters.js': { import: './src/plugins/plugin.chapters.js', dependOn: 'ia-bookreader.js' },
'plugins/plugin.experiments.js': { import: './src/plugins/plugin.experiments.js', dependOn: 'ia-bookreader.js' },
'plugins/plugin.iframe.js': { import: './src/plugins/plugin.iframe.js', dependOn: 'ia-bookreader.js' },
'plugins/plugin.iiif.js': { import: './src/plugins/plugin.iiif.js', dependOn: 'ia-bookreader.js' },
'plugins/plugin.resume.js': { import: './src/plugins/plugin.resume.js', dependOn: 'ia-bookreader.js' },
'plugins/plugin.search.js': { import: './src/plugins/search/plugin.search.js', dependOn: 'ia-bookreader.js' },
'plugins/plugin.text_selection.js': { import: './src/plugins/plugin.text_selection.js', dependOn: 'ia-bookreader.js' },
'plugins/plugin.translate.js': { import: './src/plugins/translate/plugin.translate.js', dependOn: 'ia-bookreader.js' },
'plugins/plugin.tts.js': { import: './src/plugins/tts/plugin.tts.js', dependOn: 'ia-bookreader.js' },
'plugins/plugin.url.js': { import: './src/plugins/url/plugin.url.js', dependOn: 'ia-bookreader.js' },
'plugins/plugin.vendor-fullscreen.js': { import: './src/plugins/plugin.vendor-fullscreen.js', dependOn: 'ia-bookreader.js' },
};
/** @type {webpack.Configuration[]} */
export default [
{
...shared,
// Output file -> srcfile
entry: {
// Polyfill bundles
'webcomponents-bundle.js': { import: '@webcomponents/webcomponentsjs/webcomponents-bundle.js' },
...bookreaderEntries,
'plugins/translator-worker.js': { import: '@internetarchive/bergamot-translator/worker/translator-worker.js' },
},
externals: {
// Anytime 'jquery' is imported, use the at runtime globally defined jQuery
// instead of bundling a copy of jquery at compile-time.
jquery: 'jQuery',
},
plugins: [
new webpack.ProvidePlugin({
// Make $ and jQuery available without importing
$: 'jquery',
jQuery: 'jquery',
}),
],
output: {
filename: '[name]',
chunkFilename: '[id].js',
path: path.resolve(__dirname, 'BookReader'),
},
// Accurate source maps at the expense of build time.
// The source map is intentionally exposed
// to users via sourceMapFilename for prod debugging.
devtool: 'source-map',
},
// ESM build of the same entries, for consumers that bundle BookReader
// themselves (e.g. with Vite) rather than loading it via <script> tags.
{
...shared,
name: 'esm',
target: `browserslist:${ESM_TARGETS}`,
module: {
rules: [
{
...shared.module.rules[0],
options: {
presets: [
['@babel/preset-env', {
targets: ESM_TARGETS,
useBuiltIns: 'usage',
corejs: 3,
}],
],
},
},
shared.module.rules[1],
],
},
entry: bookreaderEntries,
// Dependencies stay bare imports so the consumer can dedupe them; e.g. a
// second copy of a web component throws re-registering its element name.
externals: [
{ jquery: 'var jQuery' },
({ request }, callback) => {
const pkg = request?.match(/^(@[^/]+\/)?[^/.][^/]*/)?.[0];
// lit is bundled: our 2018-09 decorators need lit 2's decorators, and
// the consumer may be on lit 3.
if (pkg && pkg in PACKAGE_JSON.dependencies && pkg !== 'lit') {
return callback(null, `module ${request}`);
}
callback();
},
],
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
}),
],
experiments: {
outputModule: true,
},
output: {
module: true,
library: { type: 'module' },
chunkFormat: 'module',
chunkLoading: 'import',
filename: '[name]',
chunkFilename: 'chunks/[id].js',
path: path.resolve(__dirname, 'dist/esm'),
},
devtool: 'source-map',
},
// jQuery gets its own build, so that it can be used as an "external" in
// everything else.
{
...shared,
entry: {
'jquery-3.js': { import: './src/jquery-wrapper.js' },
},
},
];