forked from retorquere/zotero-better-bibtex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.ts
219 lines (199 loc) · 6.78 KB
/
webpack.config.ts
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
212
213
214
215
216
217
218
219
// tslint:disable:no-console
declare const OS: any
import * as webpack from 'webpack'
import * as path from 'path'
// import CircularDependencyPlugin = require('circular-dependency-plugin')
import WrapperPlugin = require('wrapper-webpack-plugin')
import * as translators from './gen/translators.json'
const _ = require('lodash')
const common = {
mode: 'development',
devtool: false,
optimization: {
flagIncludedChunks: true,
usedExports: true,
minimize: false,
concatenateModules: false,
emitOnErrors: false,
moduleIds: 'named',
chunkIds: 'named',
// runtimeChunk: false,
},
resolve: {
extensions: ['.ts', '.js'],
// https://github.com/webpack/webpack/pull/8460/commits/a68426e9255edcce7822480b78416837617ab065
fallback: {
fs: false,
assert: require.resolve('assert'),
util: require.resolve('util'),
},
alias: {
'path': path.join(__dirname, 'setup/shims/path.js')
},
},
// node: { fs: 'empty' },
resolveLoader: {
alias: {
'pegjs-loader': 'zotero-plugin/loader/pegjs',
// 'json-jsesc-loader': 'zotero-plugin/loader/json',
'bcf-loader': path.join(__dirname, './setup/loaders/bcf.ts'),
'trace-loader': path.join(__dirname, './setup/loaders/trace.ts'),
},
},
module: {
rules: [
{ test: /\.pegjs$/, use: [ 'pegjs-loader' ] },
// { test: /\.json$/, type: 'javascript/auto', use: [ 'json-jsesc-loader' ] }, // https://github.com/webpack/webpack/issues/6572
{ test: /\.bcf$/, use: [ 'bcf-loader' ] },
{ test: /\.ts$/, exclude: [ /node_modules/ ], use: [ 'trace-loader', 'ts-loader' ] },
],
},
}
const config: webpack.Configuration[] = []
if (!process.env.MINITESTS) {
config.push(
// main app logic
_.merge({}, common, {
optimization: {
splitChunks: {
// name: true,
cacheGroups: {
common: {
name: 'common',
chunks: 'all',
// minSize: 1,
minChunks: 2,
enforce: true,
priority: 10,
},
},
},
runtimeChunk: {
name: 'webpack'
},
},
plugins: [
new webpack.ProvidePlugin({ process: 'process/browser', }),
],
context: path.resolve(__dirname, './content'),
entry: {
BetterBibTeX: './BetterBibTeX.ts',
'BetterBibTeX.ZoteroPane': './ZoteroPane.ts',
'BetterBibTeX.KeyManager': './KeyManager.ts',
'BetterBibTeX.TestSupport': './TestSupport.ts',
'BetterBibTeX.Preferences': './Preferences.ts',
'BetterBibTeX.ErrorReport': './ErrorReport.ts',
'BetterBibTeX.FirstRun': './FirstRun.ts',
'BetterBibTeX.ItemPane': './ItemPane.ts',
'BetterBibTeX.ExportOptions': './ExportOptions.ts',
},
// devtool: '#source-map',
output: {
globalObject: 'Zotero',
path: path.resolve(__dirname, './build/content'),
filename: '[name].js',
// jsonpFunction: 'WebPackedBetterBibTeX',
uniqueName: 'BetterBibTeX',
// chunkFilename: "[id].chunk.js",
// sourceMapFilename: "./[name].js.map",
pathinfo: true,
library: 'Zotero.[name]',
libraryTarget: 'assign',
},
})
)
for (const [label, header] of Object.entries(translators.byName)) {
const vars = ['Translator']
.concat((header.translatorType & 1) ? ['detectImport', 'doImport'] : [])
.concat((header.translatorType & 2) ? ['doExport'] : [])
.join(', ')
config.push(
_.merge({}, common, {
plugins: [
new webpack.ProvidePlugin({ process: 'process/browser', }),
// new CircularDependencyPlugin({ failOnError: true }),
new webpack.DefinePlugin({
ZOTERO_TRANSLATOR_INFO: JSON.stringify(header),
}),
// new LogUsedFilesPlugin(label, 'translator'),
],
context: path.resolve(__dirname, './translators'),
entry: { [label]: `./${label}.ts` },
output: {
// jsonpFunction: `WebPacked${label.replace(/ /g, '')}`,
uniqueName: `Translator${label}`.replace(/ /g, ''),
path: path.resolve(__dirname, './build/resource'),
filename: '[name].js',
pathinfo: true,
library: `var {${vars}}`,
libraryTarget: 'assign',
},
})
)
}
config.push(
_.merge({}, common, {
plugins: [
new webpack.ProvidePlugin({ process: 'process/browser', }),
// new CircularDependencyPlugin({ failOnError: true }),
new WrapperPlugin({
test: /\.js$/,
// otherwise these would be contained in the webpack IIFE
header: 'importScripts("resource://zotero/config.js") // import ZOTERO_CONFIG\n\n',
footer: '\nimportScripts(`resource://zotero-better-bibtex/${params.translator}.js`);\n',
})
],
context: path.resolve(__dirname, './translators'),
entry: { Zotero: './worker/zotero.ts' },
output: {
// jsonpFunction: 'WebPackedZoteroShim',
uniqueName: 'BetterBibTeXZoteroShim',
path: path.resolve(__dirname, './build/resource/worker'),
filename: '[name].js',
pathinfo: true,
library: 'var { Zotero, onmessage, params }',
libraryTarget: 'assign',
},
})
)
}
if (process.env.MINITESTS) {
config.length = 0
for (const minitest of process.env.MINITESTS.split(' ')) {
config.push(
_.merge({}, common, {
plugins: [
// new CircularDependencyPlugin({ failOnError: true }),
new webpack.NormalModuleReplacementPlugin(/.*/, function(resource) {
resource.request = resource.request
.replace(/\/prefs.ts$/, '/minitests/prefs.ts')
.replace(/\/title-case.ts$/, '/minitests/title-case.ts')
}),
new webpack.DefinePlugin({
'Zotero.debug': 'console.log',
'Zotero.Debug.enabled': 'true',
'Components.utils.import': 'console.log',
'ZOTERO_CONFIG': '{}',
'Services.appinfo.name': '"Zotero"',
'Services.appinfo.version': '"0.0"',
'Zotero.version': '"0.0"',
'Zotero.platform': '"node"',
'Zotero.oscpu': '"node"',
'Zotero.locale': '"locale"',
'Zotero.logError': 'undefined',
}),
new webpack.NormalModuleReplacementPlugin(/\.\/prefs/, '../minitests/prefs.js'),
new webpack.NormalModuleReplacementPlugin(/\.\/title-case/, '../minitests/title-case.js'),
],
context: path.resolve(__dirname, './minitests'),
entry: { [minitest]: `./${minitest}.ts` },
output: {
path: path.resolve(__dirname, './build/minitest'),
filename: '[name].js',
pathinfo: true,
},
})
)
}
}
export default config