-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
176 lines (156 loc) · 6.01 KB
/
index.js
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
import path from 'path'
import { hostChecker } from './helpers/checkHosts.js'
import checkForDynamicRoutes from './helpers/checkForDynamicRoutes.js'
import checkForBackground from './helpers/checkForBackground.js'
// import checkExternalizedScriptsFolderName from './helpers/checkExternalizedScriptsFolderName.js'
// import checkAssetsFolderName from './helpers/checkAssetsFolderName.js'
import checkCSFolderName from './helpers/checkCSFolderName.js'
import replacePlaceholderExpressionAndMoveSW from './helpers/replacePlaceholderExpressionAndMoveSW.js'
import checkPrerenderedPaths from './helpers/checkPrerenderedPaths.js'
import createContentScriptsFolder from './helpers/createContentScriptsFolder.js'
import buildManifest from './helpers/buildManifest.js'
import createManifest from './helpers/createManifest.js'
import buildContentScripts from './helpers/buildContentScripts.js'
import externalizeScripts from './helpers/externalizeScripts.js'
const contentScriptsFolderNameDefault = 'content_scripts';
const externalizedScriptsPrefixDefault = 'osabe_';
const manifestFilename = 'manifest.json';
const serviceWorkerFileName = 'service-worker.js';
const getOtionsObject = function getOtionsObject(
/** @type {import('.').AdapterOptions | undefined | null} */ options
) {
const {
output,
input,
strict = true,
fallback,
platforms = ['chrome', 'mozilla'],
platformPlaceholder,
manifestBuilder = null
} = options ?? /** @type {import('./index').AdapterOptions} */ ({});
const root = output?.root || 'build';
const contentScriptsFolderName = output?.contentScriptsFolderName || contentScriptsFolderNameDefault;
const externalizedScriptsPrefix = output?.externalizedScriptsPrefix || externalizedScriptsPrefixDefault;
const webAccessibleResources = input?.webAccessibleResources || 'web_accessible_ressources';
const optionsPage = input?.optionsPage || 'options_page';
const popup = input?.popup || 'popup';
const replace = platformPlaceholder?.replace ?? true;
const expression = platformPlaceholder?.expression || `Math.random() > 0.5 || "OSABE-PLATEFORM"`;
return {
output: {
root,
contentScriptsFolderName,
externalizedScriptsPrefix
},
input: {
webAccessibleResources,
optionsPage,
popup
},
fallback,
strict,
platforms,
platformPlaceholder: {
replace,
expression
},
manifestBuilder
}
}
const buildForPlatform = async function buildForPlateform(
/** @type {import('.').Struct} */ struct,
) {
checkForBackground(struct.builder, struct.adapterName, struct.platform);
struct.builder.writeClient(struct.assetsPath.absolute);
struct.builder.writePrerendered(struct.pagesPath.absolute);
await createContentScriptsFolder(struct);
await replacePlaceholderExpressionAndMoveSW(struct);
await buildContentScripts(struct);
await externalizeScripts(struct);
const manifestContent = await buildManifest(struct);
await createManifest(manifestContent, struct.pagesPath.absolute, manifestFilename);
if (struct.options.fallback) {
struct.builder.generateFallback(path.join(struct.pagesPath.absolute, struct.options.fallback));
}
if (struct.pagesPath.absolute === struct.assetsPath.absolute) {
struct.builder.log(`Wrote files to "${struct.pagesPath.absolute}"`);
} else {
struct.builder.log(`Wrote pages to "${struct.pagesPath.absolute}" and assets to "${struct.assetsPath.absolute}"`);
}
};
/** @type {import('.').AdapterPlugin} */
const adapterPlugin = function adapterPlugin(options) {
const adapterName = '@remotal-io/opinionated-sveltekit-adapter-browser-extension';
/** @type {import('.').Adapter} */
const adapter = {
name: adapterName,
async adapt(builder) {
if (!options?.fallback) {
checkForDynamicRoutes(
options?.strict ?? true,
builder.config.kit.files.routes,
builder.config.kit.prerender.entries,
builder.log,
builder,
);
}
const host = hostChecker.find(platform => platform.test());
if (host) {
builder.log.error(
`Detected ${host.name}. ${name} can not run on this plateform.`
);
}
/** @type {import('.').AdapterOptions} */
const validOptions = getOtionsObject(options);
/** @type {import('.').Struct} */
const struct = {
builder: null,
adapterName,
// @ts-expect-error platform is initialized later
platform: '',
serviceWorkerFileName,
options: validOptions,
tmpPath: path.join(validOptions.output.root, 'tmp'),
appPath: builder.getAppPath(),
assetsPath: {
absolute: '',
relative: '',
},
pagesPath: {
absolute: '',
relative: '',
},
contentScriptPath: {
absolute: '',
relative: '',
},
};
builder.rimraf(struct.tmpPath);
builder.rimraf(validOptions.output.root);
checkPrerenderedPaths(builder.prerendered, builder.log, validOptions.input.webAccessibleResources, adapterName);
// checkExternalizedScriptsFolderName(builder.log, validOptions.out.externalizedScriptsFolderName, externalizedScriptsFolderNameDefault)
const validCSFolderName = checkCSFolderName(builder.log, validOptions.output.contentScriptsFolderName, contentScriptsFolderNameDefault)
builder.writeClient(struct.tmpPath);
const tmpStructStr = JSON.stringify(struct);
const promises = validOptions.platforms.map((platform) => {
/** @type {import('.').Struct} */
const tmpStruct = JSON.parse(tmpStructStr);
tmpStruct.builder = builder;
tmpStruct.platform = platform;
tmpStruct.options = validOptions;
tmpStruct.assetsPath.absolute = path.join(validOptions.output.root, platform);
tmpStruct.pagesPath.absolute = path.join(validOptions.output.root, platform);
tmpStruct.contentScriptPath.absolute = path.join(tmpStruct.pagesPath.absolute, validCSFolderName);
tmpStruct.assetsPath.relative = '.' + path.sep;
tmpStruct.pagesPath.relative = '.' + path.sep;
tmpStruct.contentScriptPath.relative = '.' + path.sep + path.join(tmpStruct.pagesPath.relative, validCSFolderName);
return buildForPlatform(tmpStruct);
})
await Promise.all(promises);
builder.rimraf(struct.tmpPath);
return;
}
}
return adapter;
};
export default adapterPlugin;