-
Notifications
You must be signed in to change notification settings - Fork 8
/
Gruntfile.js
152 lines (139 loc) · 4.43 KB
/
Gruntfile.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
const webpack = require('webpack');
const TerserPlugin = require('terser-webpack-plugin');
const { execSync } = require('child_process');
const args = require('minimist')(process.argv.slice(2));
const OUTPUT_DIR = './build';
const OUTPUT_FILE = './build/twinkle.js';
function isGitWorkDirClean() {
try {
execSync('git diff-index --quiet HEAD --');
return true;
} catch (e) {
return false;
}
}
function makeHeader() {
// This header comment is accurate only if grunt build is run with a clean
// working directory.
let includeCommitHashInComment = isGitWorkDirClean() || console.warn('\x1b[31m%s\x1b[0m', // red
'[WARN] Git working directory is not clean.');
let header =
`/* _______________________________________________________________________________
* | |
* | === WARNING: GLOBAL GADGET FILE === |
* | Changes to this page affect many users. |
* | Please discuss changes on the talk page or on [[WT:Gadget]] before editing. |
* |_______________________________________________________________________________|
*
* Built from source code at GitHub repository [https://github.com/#{USER_OR_ORG}#/#{REPO_NAME}#]
* All changes should be made in the repository. Please do not attempt to edit this file directly.
`;
if (includeCommitHashInComment) {
const commitSHA = execSync('git rev-parse HEAD').toString().trim();
header +=
` * This build was generated from the source files at the repository as of the commit
* ${commitSHA}. You can browse the repo at that point in time using this link:
* https://github.com/#{USER_OR_ORG}#/#{REPO_NAME}#/tree/${commitSHA}
* Changes between two commits of Twinkle can be compared using
* https://github.com/#{USER_OR_ORG}#/#{REPO_NAME}#/compare/COMMIT_HASH_1..COMMIT_HASH_2
`;
}
header +=
` */
/* <nowiki> */
`;
return header;
}
const footer = `
/* </nowiki> */`;
module.exports = function (grunt) {
grunt.initConfig({
// Clean build directory first
clean: [OUTPUT_DIR],
webpack: {
myConfig: {
...require('./webpack.config'),
devtool: undefined,
devServer: undefined,
mode: 'production',
plugins: [
// specify --excludeEnglishMessages to exclude English messages (about 20 kb) in build
// Do this ONLY if you are sure all messages have been translated into your local language,
// otherwise users will see message keys
new webpack.DefinePlugin({
EXCLUDE_ENGLISH_MESSAGES: Boolean(args.excludeEnglishMessages)
})
],
optimization: {
minimizer: [
new TerserPlugin({
extractComments: /@preserve/,
terserOptions: { output: { ascii_only: true } }
}),
],
},
performance: {
hints: false,
},
},
},
// Escape any nowiki tags in code so they don't break the on-wiki gadget file
// There's no point in writing nowiki tags as "<no" + "wiki>" in source files
// as Webpack's Terser plugin will optimise away the string concatenation giving
// a functional nowiki tag. So we must do this *after* webpack minimisation.
replace: {
nowiki: {
options: {
patterns: [
{
match: /<nowiki>/g,
replacement: '<no"+"wiki>',
},
{
match: /<\/nowiki>/g,
replacement: '</no"+"wiki>',
},
],
},
files: [
{
src: [OUTPUT_FILE],
dest: OUTPUT_FILE,
},
],
},
},
// Concatenate the header and footer comments
concat: {
options: {
separator: '\n',
banner: makeHeader(),
footer: footer,
stripBanners: {
block: true,
},
},
dist: {
src: [OUTPUT_FILE],
dest: OUTPUT_FILE,
},
},
// Copy other files to build directory (which don't need to be compiled)
copy: {
main: {
files: [
{ src: './node_modules/twinkle-core/morebits/morebits.js', dest: 'build/morebits.js' },
{ src: './node_modules/twinkle-core/morebits/morebits.css', dest: 'build/morebits.css' },
{ src: './css/twinkle.css', dest: 'build/twinkle.css' },
{ src: './css/twinkle-pagestyles.css', dest: 'build/twinkle-pagestyles.css' },
],
},
},
});
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-webpack');
grunt.loadNpmTasks('grunt-replace');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.registerTask('build', ['clean', 'webpack', 'replace', 'concat', 'copy']);
};