forked from FrankerFaceZ/Add-Ons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebpack.config.js
More file actions
173 lines (160 loc) · 4.1 KB
/
webpack.config.js
File metadata and controls
173 lines (160 loc) · 4.1 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
/* global module __dirname */
const path = require('path');
const glob = require('glob');
const fs = require('fs');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const { WebpackManifestPlugin } = require('webpack-manifest-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const {
DefinePlugin
} = require('webpack');
const StringReplacePlugin = require('string-replace-webpack-plugin');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const getFolderName = file => path.basename(path.dirname(file));
const config = {
mode: 'production',
devtool: 'source-map',
resolve: {
extensions: ['.js', '.jsx']
},
entry: glob.sync('./src/**/index.{js,jsx}').reduce((entries, entry) => Object.assign(entries, {
[`${getFolderName(entry)}/script`]: entry
}), {}),
externals: [
function(context, request, callback) {
if ( request === 'vue' )
return callback(null, 'root ffzVue');
callback();
}
],
output: {
publicPath: '//cdn.frankerfacez.com/static/addons/',
path: path.resolve(__dirname, 'dist/addons'),
filename: '[name].[hash].js',
jsonpFunction: 'ffzAddonsWebpackJsonp'
},
plugins: [
new VueLoaderPlugin(),
new CleanWebpackPlugin(),
new DefinePlugin({
Addon: 'FrankerFaceZ.utilities.addon.Addon',
}),
new WebpackManifestPlugin({
serialize: thing => JSON.stringify(thing),
filter: data => ! data.name.endsWith('.map'),
basePath: 'addons/',
publicPath: 'addons/'
}),
new CopyPlugin([
{
from: 'src/**/logo.png',
to: '[1]/logo.png',
toType: 'template',
test: /src(?:\\|\/)([^\\\/]+)(?:\\|\/)logo\.png$/
}
]),
new CopyPlugin([
{
from: 'src/**/logo.jpg',
to: '[1]/logo.jpg',
toType: 'template',
test: /src(?:\\|\/)([^\\\/]+)(?:\\|\/)logo\.jpg$/
}
]),
new WebpackManifestPlugin({
fileName: path.resolve(__dirname, 'dist', 'addons.json'),
serialize: thing => JSON.stringify(thing, null, '\t'),
generate: () => {
const addons = [];
for (const manifest of glob.sync('./src/**/manifest.json')) {
const json = JSON.parse(fs.readFileSync(manifest));
if ( ! json.enabled )
continue;
delete json.enabled;
json.id = getFolderName(manifest);
if ( ! json.icon && fs.existsSync(path.join(path.dirname(manifest), 'logo.png')) )
json.icon = `//cdn.frankerfacez.com/static/addons/${json.id}/logo.png`;
if ( ! json.icon && fs.existsSync(path.join(path.dirname(manifest), 'logo.jpg')) )
json.icon = `//cdn.frankerfacez.com/static/addons/${json.id}/logo.jpg`;
addons.push(json);
}
return addons;
}
}),
new StringReplacePlugin()
],
module: {
rules: [{
test: /index\.jsx?$/,
exclude: /node_modules/,
loader: StringReplacePlugin.replace({
replacements: [{
pattern: /\.register\(\);/ig,
replacement() {
const folder = path.dirname(this._module.rawRequest).substring(6);
const modString = `.register('${folder}');`;
return modString;
}
}]
})
},
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: StringReplacePlugin.replace({
replacements: [{
pattern: /import\('\.\/(.*)\.(.*)'\)/ig,
replacement(match, fileName, extension) {
const folder = path.dirname(this._module.rawRequest).substring(6);
const modString = `import(/* webpackChunkName: "${folder}/${extension}" */ './${fileName}.${extension}')`;
return modString;
}
}]
})
},
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader',
options: {
cacheDirectory: true,
plugins: [
['@babel/plugin-transform-react-jsx', {
pragma: 'createElement'
}]
]
}
},
{
test: /\.(graphql|gql)$/,
exclude: /node_modules/,
loader: 'graphql-tag/loader'
},
{
test: /\.vue$/,
loader: 'vue-loader'
},
{
test: /\.s?css$/,
use: [{
loader: 'file-loader',
options: {
name: '[folder]/[name].[hash].css'
}
}, {
loader: 'extract-loader'
}, {
loader: 'css-loader',
options: {
sourceMap: true
}
}, {
loader: 'sass-loader',
options: {
sourceMap: true
}
}]
}]
},
};
module.exports = config;