forked from add2cal/add-to-calendar-button
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGruntfile.js
170 lines (167 loc) · 5.08 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
function prepareFinalFile(
content,
stripImport = true,
stripAllImport = false,
newExportPhrase = '',
transformToCommonJS = false
) {
if (stripImport) {
// remove import statements, except for our TimeZones iCal Library
content = content.replace(/^import[\w\s{}\+-_,"`\/\\.]*'((?!timezones-ical-library).)*';$/gim, '');
}
if (stripAllImport) {
// remove all import statements
content = content.replace(/^import[\w\s{}\+-_,'"`\/\\.]*';/gim, '');
}
if (newExportPhrase != '') {
// remove export statements
content = content.replace(
/\/\/ START INIT[\s\S]*?\/\/ END INIT/g,
`${newExportPhrase} { atcb_action, atcb_init };`
);
}
if (transformToCommonJS) {
content = content
.replace(
/^import[\w\s{}\+-_,"`\/\\.]*'timezones\-ical\-library';$/gim,
"// eslint-disable-next-line @typescript-eslint/no-var-requires\r\nconst tzlibActions = require('timezones-ical-library');"
)
.replace(/tzlib_get_offset/gm, 'tzlibActions.tzlib_get_offset')
.replace(/tzlib_get_ical_block/gm, 'tzlibActions.tzlib_get_ical_block')
.replace(/tzlib_get_timezones/gm, 'tzlibActions.tzlib_get_timezones');
}
return content;
}
// The config.
module.exports = function (grunt) {
grunt.initConfig({
// update version. Either use via `npm run release -- patch` (default), `npm run release -- minor`, `npm run release -- major`, or `npm run release -- x.x.x` (with x.x.x being the exact version number)
version: {
package: {
src: ['package.json'],
},
demoHtml: {
options: {
prefix: '.(tiny-version">v|.?v=)',
},
src: ['index.html'],
},
js: {
options: {
prefix: 'Version.=..',
},
src: ['src/*.js'],
},
css: {
options: {
prefix: 'Version:.',
},
src: ['assets/css/*.css'],
},
},
// cleans old built files
clean: {
oldBuildFiles: [
'npm_dist/',
'dist/',
'assets/css/*.min.css',
'assets/css/*.min.css.map',
'demo_assets/css/*.min.css',
'demo_assets/css/*.min.css.map',
'demo_assets/js/*.js.css',
'demo_assets/js/*.min.js.map',
],
},
// minifies the css file
cssmin: {
options: {
sourceMap: true,
},
minify: {
files: [
{
expand: true,
cwd: 'assets/css',
src: ['*.css', '!*.min.css'],
dest: 'assets/css',
ext: '.min.css',
},
{
expand: true,
cwd: 'demo_assets/css',
src: ['*.css', '!*.min.css'],
dest: 'demo_assets/css',
ext: '.min.css',
},
],
},
},
// generate the distributable JavaScript files
// for the npm version supporting CommonJS and ES Module (https://www.sensedeep.com/blog/posts/2021/how-to-create-single-source-npm-module.html)
concat: {
dist: {
src: ['node_modules/timezones-ical-library/dist/tzlib.js', 'src/atcb.js'],
dest: 'dist/atcb.js',
options: { process: (content) => prepareFinalFile(content, false, true) },
},
mjs_dist: {
src: 'src/atcb.js',
dest: 'npm_dist/mjs/index.js',
options: { process: (content) => prepareFinalFile(content, true, false, 'export') },
},
cjs_dist: {
src: 'src/atcb.js',
dest: 'npm_dist/cjs/index.js',
options: { process: (content) => prepareFinalFile(content, true, false, 'module.exports =', true) },
},
},
// creates files to support the npm dist structure
'file-creator': {
'package.json ES Module': {
'npm_dist/mjs/package.json': function (fs, fd, done) {
fs.writeSync(fd, '{ "type": "module" }');
done();
},
},
'package.json commonJS': {
'npm_dist/cjs/package.json': function (fs, fd, done) {
fs.writeSync(fd, '{ "type": "commonjs" }');
done();
},
},
'.eslintrc.json commonJS': {
'npm_dist/cjs/.eslintrc.json': function (fs, fd, done) {
fs.writeSync(
fd,
'{ "extends": "../../.eslintrc.json", "env": { "node": true }, "plugins": ["commonjs"] }'
);
done();
},
},
},
// minifies the main js file
uglify: {
options: {
compress: true,
mangle: true,
sourceMap: true,
},
newBuild: {
files: {
'dist/atcb.min.js': ['dist/atcb.js'],
'demo_assets/js/demopage.min.js': ['demo_assets/js/demopage.js'],
},
},
},
});
// Load the plugins.
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-file-creator');
grunt.loadNpmTasks('grunt-version');
// Register task(s).
grunt.registerTask('default', ['clean', 'cssmin', 'concat:dist', 'uglify']);
grunt.registerTask('npm', ['clean', 'cssmin', 'concat', 'file-creator', 'uglify']);
};