-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcraco.config.ts
More file actions
95 lines (85 loc) · 2.82 KB
/
Copy pathcraco.config.ts
File metadata and controls
95 lines (85 loc) · 2.82 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
import CopyPlugin from 'copy-webpack-plugin';
import WebExtPlugin from 'web-ext-plugin-cjs';
import { CleanWebpackPlugin } from 'clean-webpack-plugin';
import path from 'path';
const generateManifest = (build: string, content: Buffer) => {
const buildKey = '__build'
const template = JSON.parse(content.toString());
const nodePackage = require('./package.json');
const replaceBuildVariables = (jsObject: Object, buildKey: string, build: string) => {
for (const [key, value] of Object.entries(jsObject)) {
const isValueObject = typeof value === 'object' && value !== null
if (key === buildKey) {
if (isValueObject) {
if (Object.keys(value).includes(build)) {
return value[build];
} else {
return undefined;
}
} else {
throw new Error(`Invalid manifest-template file. Expected an object for key ${buildKey}, got something else instead.`)
}
} else if (isValueObject) {
const replacedValue = replaceBuildVariables(value, buildKey, build);
if (typeof replacedValue !== 'undefined') {
jsObject[key as keyof Object] = replacedValue;
} else {
delete jsObject[key as keyof Object];
if (Array.isArray(jsObject)) {
jsObject = jsObject.filter(Boolean);
}
}
}
}
return jsObject;
}
template.version = nodePackage.version;
return JSON.stringify(replaceBuildVariables(template, buildKey, build), null, 2);
}
module.exports = async () => {
const build = process.env.REACT_APP_EXT_BUILD || 'chrome';
const target = build === 'chrome' ? 'chromium' : 'firefox-desktop';
const copyPatterns = [
{
from: 'public/manifest-template.json', to: 'manifest.json', transform(content: Buffer) {
return generateManifest(build, content);
},
},
{
from: '!(index.html|manifest-template.json|background.js)/**', context: 'public/'
}
]
if (build !== 'chrome') {
copyPatterns.push({ from: 'background.js', context: 'public' });
}
return {
devServer: {
devMiddleware: {
writeToDisk: true
}
},
webpack: {
configure: (webpackConfig: any, { paths }: any) => {
paths.appBuild = webpackConfig.output.path = path.resolve(`build/${build}`);
return webpackConfig;
},
plugins: [
new CleanWebpackPlugin({
cleanOnceBeforeBuildPatterns: ['**/*']
}),
new CopyPlugin({
patterns: copyPatterns
}),
new WebExtPlugin({
sourceDir: path.resolve(`build/${build}`),
target: target,
runLint: false,
chromiumProfile: `profile/${target}`,
firefoxProfile: `profile/${target}`,
keepProfileChanges: true,
profileCreateIfMissing: true
})
]
}
}
}