-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.js
More file actions
128 lines (100 loc) · 4.09 KB
/
build.js
File metadata and controls
128 lines (100 loc) · 4.09 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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
console.log('Building Cite Unseen bundled version...');
// Read all source files
const styles = fs.readFileSync('styles.css', 'utf8');
const sources = fs.readFileSync('sources.js', 'utf8');
const main = fs.readFileSync('main.js', 'utf8');
// Read i18n JSON files and create i18n object
const i18nDir = 'i18n';
const i18nFiles = fs.readdirSync(i18nDir).filter(file => file.endsWith('.json'));
const i18nData = {};
for (const file of i18nFiles) {
const lang = path.basename(file, '.json');
const content = fs.readFileSync(path.join(i18nDir, file), 'utf8');
const langKey = lang.startsWith('zh-') ? lang.substring(3) : lang; // 'zh-hans' -> 'hans', 'zh-hant' -> 'hant'
i18nData[langKey] = JSON.parse(content);
}
// Convert flat i18n structure back to nested structure
const nestedI18n = {};
for (const [lang, translations] of Object.entries(i18nData)) {
for (const [key, value] of Object.entries(translations)) {
const keyParts = key.split('.');
let current = nestedI18n;
for (let i = 0; i < keyParts.length - 1; i++) {
const part = keyParts[i];
if (!current[part]) {
current[part] = {};
}
current = current[part];
}
const finalKey = keyParts[keyParts.length - 1];
if (!current[finalKey]) {
current[finalKey] = {};
}
current[finalKey][lang] = value;
}
}
const i18nJs = `window.CiteUnseenI18n = ${JSON.stringify(nestedI18n, null, 4)};`;
// Create the bundled content
let bundled = `// Cite Unseen - Bundled Version
// Maintainers: SuperHamster and SuperGrey
// Repository: https://gitlab.wikimedia.org/kevinpayravi/cite-unseen
// Release: ${process.env.CI_COMMIT_TAG}
// Timestamp: ${new Date().toISOString()}
(function() {
'use strict';
// Inject CSS styles
const css = \`${styles.replace(/`/g, '\\`').replace(/\$/g, '\\$')}\`;
const style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = css;
document.head.appendChild(style);
// Load i18n data
${i18nJs}
// Load sources data
${sources}
// Main script with modified importDependencies function
`;
// Modify the main.js content to remove external dependencies
const modifiedMain = main.replace(
/await mw\.loader\.load\('\/\/gitlab-content\.toolforge\.org\/kevinpayravi\/cite-unseen\/-\/raw\/main\/styles\.css[^']*',\s*'text\/css'\);?\s*/g,
'// CSS already injected above\n '
).replace(
/await mw\.loader\.getScript\('\/\/gitlab-content\.toolforge\.org\/kevinpayravi\/cite-unseen\/-\/raw\/main\/i18n\.js[^']*'\);?\s*/g,
'// i18n.js already loaded above\n '
).replace(
/await mw\.loader\.getScript\('\/\/gitlab-content\.toolforge\.org\/kevinpayravi\/cite-unseen\/-\/raw\/main\/sources\.js[^']*'\);?\s*/g,
'// sources.js already loaded above\n '
);
bundled += modifiedMain;
bundled += '\n})();';
// Create build directory
if (!fs.existsSync('build')) {
fs.mkdirSync('build');
}
// Write bundled file
fs.writeFileSync('build/cite-unseen-bundled.js', bundled);
const bundledSize = fs.statSync('build/cite-unseen-bundled.js').size;
console.log('Build completed!');
console.log(`Bundled file: ${bundledSize} bytes`);
// Create deployment README
const deployReadme = `# Cite Unseen - Deploy Branch
This branch contains the bundled version of Cite Unseen.
## Usage
The bundled file is called \`cite-unseen-bundled.js\`.
The file can be pulled directly using \`gitlab-content.toolforge.org\`:
\`\`\`javascript
await mw.loader.getScript('//gitlab-content.toolforge.org/kevinpayravi/cite-unseen/-/raw/deploy/cite-unseen-bundled.js?mime=text/javascript');
\`\`\`
## Build Info
- Release: ${process.env.CI_COMMIT_TAG}
- Built from commit: ${process.env.CI_COMMIT_SHA || 'local'}
- Build timestamp: ${new Date().toISOString()}
- Bundled size: ${bundledSize} bytes
`;
fs.writeFileSync('build/README.md', deployReadme);
console.log('Files created:');
console.log('- build/cite-unseen-bundled.js');
console.log('- build/README.md');