aboutsummaryrefslogtreecommitdiffstats
path: root/library/moment/tasks/transpile.js
blob: d253407d1dc0bdebf57ee64a8bcc37bec51a6add (plain) (blame)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
module.exports = function (grunt) {
    var esperanto = require('esperanto');
    var path = require('path');
    var Promise = require('es6-promise').Promise;
    var TMP_DIR = 'build/tmp';

    function moveComments(code) {
        var comments = [], rest = [];
        code.split('\n').forEach(function (line) {
            if (line.trim().slice(0, 3) === '//!') {
                comments.push(line.trim());
            } else {
                rest.push(line);
            }
        });

        return comments.concat([''], rest).join('\n');
    }

    var headerCache = {};
    function getHeaderByFile(headerFile) {
        if (!(headerFile in headerCache)) {
            headerCache[headerFile] = grunt.file.read(headerFile);
        }
        return headerCache[headerFile];
    }

    function transpile(opts) {
        // base, entry, skip, headerFile, skipLines, target
        var umdName = opts.headerFile ? 'not_used' : opts.umdName,
            header = opts.headerFile ? getHeaderByFile(opts.headerFile) : '',
            skipLines = opts.skipLines ? opts.skipLines : 0;

        return esperanto.bundle({
            base: opts.base,
            entry: opts.entry,
            skip: opts.skip || []
        }).then(function (bundle) {
            var umd = bundle.toUmd({name: umdName}),
                fixed = header + umd.code.split('\n').slice(skipLines).join('\n');
            if (opts.moveComments) {
                fixed = moveComments(fixed);
            }
            grunt.file.write(opts.target, fixed);
        });
    }

    function transpileMany(opts) {
        var batchSize = 50,
            promise = Promise.resolve(null),
            files = grunt.file.expand({cwd: opts.base}, opts.pattern),
            i,
            transpileOne = function (i) {
                promise = promise.then(function () {
                    return Promise.all(files.slice(i, i + batchSize).map(function (file) {
                        return transpile({
                            base: opts.base,
                            entry: file,
                            headerFile: opts.headerFile,
                            skip: opts.skip,
                            skipLines: opts.skipLines,
                            moveComments: opts.moveComments,
                            target: path.join(opts.targetDir, file)
                        });
                    }));
                });
            };

        for (i = 0; i < files.length; i += batchSize) {
            transpileOne(i);
        }

        return promise;
    }

    function prepareTemp(base) {
        var files = grunt.file.expand({cwd: base}, '**/*.js'),
            tmpDir = TMP_DIR;
        if (grunt.file.exists(tmpDir)) {
            return;
        }
        files.forEach(function (file) {
            grunt.file.copy(path.join(base, file), path.join(tmpDir, file));
        });
    }

    function transpileCode(opts) {
        var entry = opts.entry || path.basename(opts.target);
        prepareTemp(opts.base);
        grunt.file.write(path.join(TMP_DIR, entry), opts.code);
        return transpile({
            base: TMP_DIR,
            entry: entry,
            umdName: opts.umdName || 'not_used',
            headerFile: opts.headerFile,
            skipLines: opts.skipLines,
            moveComments: opts.moveComments,
            target: opts.target,
            skip: opts.skip
        });
    }

    function generateLocales(target, localeFiles) {
        var files = localeFiles,
            code = files.map(function (file) {
                var identifier = path.basename(file, '.js').replace('-', '_');
                return 'import ' + identifier + ' from "./' + file + '";';
            }).join('\n');
        return transpileCode({
            base: 'src',
            code: code,
            target: target,
            skip: ['moment'],
            headerFile: 'templates/locale-header.js',
            skipLines: 5
        });
    }

    function generateMomentWithLocales(target, localeFiles) {
        var files = localeFiles,
            importCode = files.map(function (file) {
                var identifier = path.basename(file, '.js').replace('-', '_');
                var fileNoExt = file.replace('.js', '');
                return 'import ' + identifier + ' from "./' + fileNoExt + '";';
            }).join('\n'),
            code = 'import * as moment_export from "./moment";\n\n' +
                importCode + '\n\n' +
                'export default moment_export;';

        return transpileCode({
            base: 'src',
            code: code,
            umdName: 'moment',
            target: target
        }).then(function () {
            var code = grunt.file.read(target);
            var getDefaultRegExp = new RegExp('var ([a-z$_]+) =\\s+{[^]\\s+get default \\(\\) { return ([a-z$_]+); }[^]\\s+}', '');
            var crap = code.match(getDefaultRegExp);
            if (crap.length !== 3) {
                grunt.file.write('/tmp/crap.js', code);
                throw new Error('Failed to detect get default crap, check /tmp/crap.js');
            }
            code = code.replace(getDefaultRegExp, '');

            var buildExportVars = ['moment_with_locales', 'moment_with_locales_custom'];
            buildExportVars.forEach(function (buildExportVar) {
                var languageReset = buildExportVar  + '.locale(\'en\');';
                code = code.replace('var ' + buildExportVar + ' = ' + crap[1] + ';',
                                    'var ' + buildExportVar + ' = ' + crap[2] + ';\n' +
                                    '    ' + languageReset);
            });

            if (code.match('get default')) {
                grunt.file.write('/tmp/crap.js', code);
                throw new Error('Stupid shit es6 get default plaguing the code, check /tmp/crap.js');
            }
            grunt.file.write(target, code);
        });
    }

    grunt.task.registerTask('transpile-raw', 'convert es6 to umd', function () {
        var done = this.async();

        transpile({
            base: 'src',
            entry: 'moment.js',
            umdName: 'moment',
            target: 'build/umd/moment.js',
            moveComments: true
        }).then(function () {
            grunt.log.ok('build/umd/moment.js');
        }).then(function () {
            return transpileMany({
                base: 'src',
                pattern: 'locale/*.js',
                headerFile: 'templates/locale-header.js',
                skipLines: 5,
                moveComments: true,
                targetDir: 'build/umd',
                skip: ['moment']
            });
        }).then(function () {
            grunt.log.ok('build/umd/locale/*.js');
        }).then(function () {
            return transpileMany({
                base: 'src',
                pattern: 'test/moment/*.js',
                headerFile: 'templates/test-header.js',
                skipLines: 5,
                moveComments: true,
                targetDir: 'build/umd',
                skip: ['moment']
            });
        }).then(function () {
            grunt.log.ok('build/umd/test/moment/*.js');
        }).then(function () {
            return transpileMany({
                base: 'src',
                pattern: 'test/locale/*.js',
                headerFile: 'templates/test-header.js',
                skipLines: 5,
                moveComments: true,
                targetDir: 'build/umd',
                skip: ['moment']
            });
        }).then(function () {
            grunt.log.ok('build/umd/test/locale/*.js');
        }).then(function () {
            return generateLocales('build/umd/min/locales.js',
                grunt.file.expand({cwd: 'src'}, 'locale/*.js'));
        }).then(function () {
            grunt.log.ok('build/umd/min/locales.js');
        }).then(function () {
            return generateMomentWithLocales('build/umd/min/moment-with-locales.js',
                grunt.file.expand({cwd: 'src'}, 'locale/*.js'));
        }).then(function () {
            grunt.log.ok('build/umd/min/moment-with-locales.js');
        }).then(done, function (e) {
            grunt.log.error('error transpiling', e);
            done(e);
        });
    });

    grunt.task.registerTask('transpile-custom-raw',
            'build just custom language bundles',
            function (locales) {
        var done = this.async();

        var localeFiles = locales.split(',').map(function (locale) {
            var file = grunt.file.expand({cwd: 'src'}, 'locale/' + locale + '.js');
            if (file.length !== 1) {
                // we failed to find a locale
                done(new Error('could not find locale: ' + locale));
                done = null;
            } else {
                return file[0];
            }
        });

        // There was an issue with a locale
        if (done == null) {
            return;
        }

        return generateLocales(
            'build/umd/min/locales.custom.js', localeFiles
        ).then(function () {
            grunt.log.ok('build/umd/min/locales.custom.js');
        }).then(function () {
            return generateMomentWithLocales('build/umd/min/moment-with-locales.custom.js',
                localeFiles);
        }).then(function () {
            grunt.log.ok('build/umd/min/moment-with-locales.custom.js');
        }).then(done, function (e) {
            grunt.log.error('error transpiling-custom', e);
            done(e);
        });
    });

    grunt.config('clean.build', [
        'build'
    ]);

    grunt.config('concat.tests', {
        src: 'build/umd/test/**/*.js',
        dest: 'build/umd/min/tests.js'
    });

    grunt.task.registerTask('transpile',
            'builds all es5 files, optinally creating custom locales',
            function (locales) {
        var tasks = [
            'clean:build',
            'transpile-raw',
            'concat:tests'
        ];

        if (locales) {
            tasks.push('transpile-custom-raw:' + locales);
        }

        grunt.task.run(tasks);
    });
};