Skip to content

Commit

Permalink
Merge pull request moment#3545 from ichernev:babel-redo
Browse files Browse the repository at this point in the history
[build] Use rollup and babel instead of esperanto for transpiling
  • Loading branch information
ichernev committed Nov 6, 2016
2 parents 7873743 + 257791d commit a4de1ca
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 106 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"grunt": "~0.4",
"grunt-cli": "latest",
"benchmark": "latest",
"esperanto": "latest",
"grunt-contrib-clean": "latest",
"grunt-contrib-concat": "latest",
"grunt-contrib-copy": "latest",
Expand All @@ -64,6 +63,8 @@
"karma-sauce-launcher": "latest",
"qunit": "^0.7.5",
"qunit-cli": "^0.1.4",
"rollup": "latest",
"rollup-plugin-babel": "latest",
"spacejam": "latest",
"typescript": "^1.8.10",
"coveralls": "^2.11.2",
Expand Down
2 changes: 1 addition & 1 deletion src/test/locale/ms.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {localeModule, test} from '../qunit';
import {moment} from '../../moment';
import moment from '../../moment';
localeModule('ms');

test('parse', function (assert) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/locale/se.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {localeModule, test} from '../qunit';
import {moment} from '../../moment';
import moment from '../../moment';
localeModule('se');

test('parse', function (assert) {
Expand Down
12 changes: 6 additions & 6 deletions src/test/moment/relative_time.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,23 +151,23 @@ test('custom rounding', function (assert) {
moment.relativeTimeThreshold('d', 31);
moment.relativeTimeThreshold('M', 12);

var a = moment();
var a = moment.utc();
a.subtract({minutes: 59, seconds: 59});
assert.equal(a.toNow(), 'in 59 minutes', 'Round down towards the nearest minute');

a = moment();
a = moment.utc();
a.subtract({hours: 23, minutes: 59, seconds: 59});
assert.equal(a.toNow(), 'in 23 hours', 'Round down towards the nearest hour');

a = moment();
a = moment.utc();
a.subtract({days: 30, hours: 23, minutes: 59});
assert.equal(a.toNow(), 'in 30 days', 'Round down towards the nearest day');

a = moment();
a = moment.utc();
a.subtract({days: 364});
assert.equal(a.toNow(), 'in 11 months', 'Round down towards the nearest month');

a = moment();
a = moment.utc();
a.subtract({years: 1, days: 364});
assert.equal(a.toNow(), 'in a year', 'Round down towards the nearest year');

Expand All @@ -177,7 +177,7 @@ test('custom rounding', function (assert) {
};
moment.relativeTimeRounding(retainValue);

a = moment();
a = moment.utc();
a.subtract({hours: 39});
assert.equal(a.toNow(), 'in 1.625 days', 'Round down towards the nearest year');

Expand Down
165 changes: 90 additions & 75 deletions tasks/transpile.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module.exports = function (grunt) {
var esperanto = require('esperanto');
// var esperanto = require('esperanto');
var rollup = require('rollup').rollup;
var babel = require('rollup-plugin-babel');
var path = require('path');
var Promise = require('es6-promise').Promise;
var TMP_DIR = 'build/tmp';
Expand Down Expand Up @@ -32,26 +34,61 @@ module.exports = function (grunt) {

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

function rollupBundle(opts) {
// entry, umdName, skipMoment

var rollupOpts = {
entry: opts.entry,
plugins: [
babel({})
]
}, bundleOpts = {
format: 'umd',
moduleName: opts.umdName != null ? opts.umdName : 'not_used'
};

if (opts.skipMoment) {
// And this is what people call progress?
rollupOpts.external = [
'./moment',
'../moment',
'../../moment',
path.resolve('src/moment'),
path.resolve('build/tmp/moment')
];
bundleOpts.globals = {};
bundleOpts.globals[path.resolve('src/moment')] = 'moment';
bundleOpts.globals[path.resolve('build/tmp/moment')] = 'moment';
}

return rollup(rollupOpts).then(function (bundle) {
var result = bundle.generate(bundleOpts);
return result.code;
});
}

function transpile(opts) {
// base, entry, skip, headerFile, skipLines, target
var umdName = opts.headerFile ? 'not_used' : opts.umdName,
// base, entry, skipMoment, headerFile, skipLines, target
var umdName = opts.headerFile != null && opts.headerFile !== 'none' ? 'not_used' : opts.umdName,
headerFile = opts.headerFile ? opts.headerFile : 'templates/default.js',
header = getHeaderByFile(headerFile),
skipLines = opts.skipLines ? opts.skipLines : 5;
skipLines = opts.skipLines != null ? opts.skipLines : 5;

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');
return rollupBundle({
entry: path.join(opts.base, opts.entry),
skipMoment: opts.skipMoment != null ? opts.skipMoment : false,
umdName: umdName
}).then(function (code) {
var fixed = header + code.split('\n').slice(skipLines).join('\n');
if (opts.moveComments) {
fixed = moveComments(fixed, opts.moveComments);
}
Expand All @@ -71,7 +108,7 @@ module.exports = function (grunt) {
base: opts.base,
entry: file,
headerFile: opts.headerFile,
skip: opts.skip,
skipMoment: opts.skipMoment,
skipLines: opts.skipLines,
moveComments: opts.moveComments,
target: path.join(opts.targetDir, file)
Expand Down Expand Up @@ -110,16 +147,19 @@ module.exports = function (grunt) {
skipLines: opts.skipLines,
moveComments: opts.moveComments,
target: opts.target,
skip: opts.skip
skipMoment: opts.skipMoment
});
}

function generateLocales(target, localeFiles) {
function generateLocales(target, localeFiles, opts) {
var files = localeFiles,
code = files.map(function (file) {
code = [
'import moment from "./moment";',
'export default moment;'
].concat(files.map(function (file) {
var identifier = path.basename(file, '.js').replace('-', '_');
return 'import ' + identifier + ' from "./' + file + '";';
}).concat([
})).concat([
// Reset the language back to 'en', because every defineLocale
// also sets it.
'moment.locale(\'en\');'
Expand All @@ -128,52 +168,9 @@ module.exports = function (grunt) {
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,
moveComments: 'main-only'
}).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);
skipMoment: opts.skipMoment,
headerFile: opts.skipMoment === true ? 'templates/locale-header.js' : 'templates/default.js',
skipLines: opts.skipMoment === true ? 7 : 5
});
}

Expand All @@ -185,6 +182,7 @@ module.exports = function (grunt) {
entry: 'moment.js',
umdName: 'moment',
target: 'build/umd/moment.js',
skipLines: 5,
moveComments: true
}).then(function () {
grunt.log.ok('build/umd/moment.js');
Expand All @@ -193,10 +191,10 @@ module.exports = function (grunt) {
base: 'src',
pattern: 'locale/*.js',
headerFile: 'templates/locale-header.js',
skipLines: 5,
skipLines: 7,
moveComments: true,
targetDir: 'build/umd',
skip: ['moment']
skipMoment: true
});
}).then(function () {
grunt.log.ok('build/umd/locale/*.js');
Expand All @@ -205,10 +203,10 @@ module.exports = function (grunt) {
base: 'src',
pattern: 'test/moment/*.js',
headerFile: 'templates/test-header.js',
skipLines: 5,
skipLines: 7,
moveComments: true,
targetDir: 'build/umd',
skip: ['moment']
skipMoment: true
});
}).then(function () {
grunt.log.ok('build/umd/test/moment/*.js');
Expand All @@ -217,21 +215,27 @@ module.exports = function (grunt) {
base: 'src',
pattern: 'test/locale/*.js',
headerFile: 'templates/test-header.js',
skipLines: 5,
skipLines: 7,
moveComments: true,
targetDir: 'build/umd',
skip: ['moment']
skipMoment: true
});
}).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'));
return generateLocales(
'build/umd/min/locales.js',
grunt.file.expand({cwd: 'src'}, 'locale/*.js'),
{skipMoment: true}
);
}).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'));
return generateLocales(
'build/umd/min/moment-with-locales.js',
grunt.file.expand({cwd: 'src'}, 'locale/*.js'),
{skipMoment: false}
);
}).then(function () {
grunt.log.ok('build/umd/min/moment-with-locales.js');
}).then(done, function (e) {
Expand Down Expand Up @@ -262,14 +266,25 @@ module.exports = function (grunt) {
}

return generateLocales(
'build/umd/min/locales.custom.js', localeFiles
'build/umd/min/locales.custom.js',
localeFiles,
{skipMoment: true}
).then(function () {
grunt.log.ok('build/umd/min/locales.custom.js');
}).then(function () {
return generateMomentWithLocales('build/umd/min/moment-with-locales.custom.js',
localeFiles);
return generateLocales(
'build/umd/min/moment-with-locales.custom.js',
localeFiles,
{skipMoment: false});
}).then(function () {
grunt.log.ok('build/umd/min/moment-with-locales.custom.js');
}).then(function () {
var moment = require('../build/umd/min/moment-with-locales.custom.js');
if (moment.locales().length !== localeFiles.length) {
throw new Error(
'You probably specified locales requiring ' +
'parent locale, but didn\'t specify parent');
}
}).then(done, function (e) {
grunt.log.error('error transpiling-custom', e);
done(e);
Expand Down
7 changes: 0 additions & 7 deletions templates/amd-named.js

This file was deleted.

7 changes: 0 additions & 7 deletions templates/amd.js

This file was deleted.

2 changes: 1 addition & 1 deletion templates/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
global.moment = factory()
}(this, function () { 'use strict';
}(this, (function () { 'use strict';
5 changes: 0 additions & 5 deletions templates/globals.js

This file was deleted.

2 changes: 1 addition & 1 deletion templates/locale-header.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
&& typeof require === 'function' ? factory(require('../moment')) :
typeof define === 'function' && define.amd ? define(['../moment'], factory) :
factory(global.moment)
}(this, function (moment) { 'use strict';
}(this, (function (moment) { 'use strict';
2 changes: 1 addition & 1 deletion templates/test-header.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
&& typeof require === 'function' ? factory(require('../../moment')) :
typeof define === 'function' && define.amd ? define(['../../moment'], factory) :
factory(global.moment)
}(this, function (moment) { 'use strict';
}(this, (function (moment) { 'use strict';

0 comments on commit a4de1ca

Please sign in to comment.