Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-26117] Use polyfill #32

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 35 additions & 3 deletions lib/jsanalyze.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@

var appc = require('node-appc'),
fs = require('fs'),
path = require('path'),
DOMParser = require('xmldom').DOMParser,
babel = require('babel-core'),
babylon = require('babylon'),
types = require('babel-types'),
traverse = require('babel-traverse').default,
babili = require.resolve('babel-preset-babili'),
minify = require('babel-preset-minify'),
env = require('babel-preset-env'),
__ = appc.i18n(__dirname).__,
apiUsage = {};
Expand Down Expand Up @@ -171,7 +172,38 @@ exports.analyzeJs = function analyzeJs(contents, opts) {
// transpile
if (opts.transpile) {
options.plugins.push(require.resolve('./global-this'));
options.presets.push([ env, { targets: opts.targets } ]);
options.plugins.push(require.resolve('babel-plugin-transform-async-to-generator'));
options.presets.push([ env, { targets: opts.targets, useBuiltIns: true } ]);

// install polyfill
if (opts.resourcesDir) {
const modulesDir = path.join(opts.resourcesDir, 'node_modules');

// make sure our 'node_modules' directory exists
if (!fs.existsSync(modulesDir)) {
fs.mkdirSync(modulesDir);
}

// copy over polyfill and its dependencies
// WARNING: REMEMBER TO UPDATE THIS IF 'babel-polyfill' DEPENDENCIES CHANGE!
[ 'babel-polyfill', 'core-js', 'regenerator-runtime' ].forEach((moduleName) => {
const moduleSrcDir = path.dirname(require.resolve(path.join(moduleName, 'package.json'))),
moduleDstDir = path.join(modulesDir, moduleName);

// copy over module if it does not exist
if (!fs.existsSync(moduleDstDir)) {

// on windows copy over the module
if (process.platform === 'win32') {
appc.fs.copyDirSyncRecursive(moduleSrcDir, moduleDstDir);

// on osx we can optimize by creating symbolic links
} else {
fs.symlinkSync(moduleSrcDir, moduleDstDir, 'dir');
}
}
});
}
}

// minify
Expand All @@ -182,7 +214,7 @@ exports.analyzeJs = function analyzeJs(contents, opts) {
comments: false
});

options.presets.push([ babili, {
options.presets.push([ minify, {
mangle: false,
deadcode: false
} ]);
Expand Down