refactor: improve tree shaking abilities

Internal refactor completed in order to improve tree shaking and dead
code removal. The public API, with an exception to ion-slides, has
stayed the same. However, internally many changes were required so
bundlers could better exclude modules which should not be bundled.
Ultimately most changes resorted to removing references to `window` or
`document`, or a module that referenced one of those.

BREAKING CHANGES

ion-slides was refactored to remove the external dependencies, and
rewritten in TypeScript/ES6 modules to again improve tree shaking
abilities.
This commit is contained in:
Adam Bradley
2017-01-09 09:51:39 -06:00
parent 13cf6a6cb7
commit 7000b1b173
191 changed files with 9401 additions and 13664 deletions

View File

@@ -36,7 +36,7 @@
}
});
</script>
<script src="../polyfills/polyfills.ng.js"></script>
<script src="../polyfills/polyfills.js"></script>
<script src="../../bundles/ionic.system.js"></script>
</head>

View File

@@ -13,7 +13,7 @@
<ion-app></ion-app>
<script src="../polyfills/polyfills.ng.js"></script>
<script src="../polyfills/polyfills.js"></script>
<script src="./build/main.js"></script>
</body>
</html>

View File

@@ -1,6 +1,6 @@
import { task } from 'gulp';
import { DIST_BUILD_ROOT, DIST_BUILD_ES2015_ROOT, DIST_BUILD_UMD_ROOT, ES5, ES_2015, PROJECT_ROOT, UMD_MODULE } from '../constants';
import { copySourceToDest, copySwiperToPath, createTempTsConfig, deleteFiles, runNgc } from '../util';
import { copySourceToDest, createTempTsConfig, deleteFiles, runNgc } from '../util';
export function buildIonicAngularUmd(excludeSpec: boolean, stripDebug: boolean, done: Function) {
@@ -14,7 +14,6 @@ export function buildIonicAngularUmd(excludeSpec: boolean, stripDebug: boolean,
return;
}
copySwiperToPath(`${DIST_BUILD_UMD_ROOT}/components/slides`, UMD_MODULE);
// clean up any .ts files that remain as well as ngc metadata
deleteFiles([`${DIST_BUILD_UMD_ROOT}/**/*.ts`,
`${DIST_BUILD_UMD_ROOT}/node_modules`,
@@ -34,7 +33,6 @@ export function buildIonicAngularEsm(stripDebug: boolean, done: Function) {
done(err);
return;
}
copySwiperToPath(`${DIST_BUILD_ROOT}/components/slides`, ES_2015);
// clean up any .ts files that remain as well as ngc metadata
deleteFiles([`${DIST_BUILD_ROOT}/**/*.ts`,
`${DIST_BUILD_ROOT}/node_modules`,
@@ -54,7 +52,6 @@ export function buildIonicPureEs6(stripDebug: boolean, done: Function) {
done(err);
return;
}
copySwiperToPath(`${DIST_BUILD_ES2015_ROOT}/components/slides`, ES_2015);
// clean up any .ts files that remain as well as ngc metadata
deleteFiles([`${DIST_BUILD_ES2015_ROOT}/**/*.ts`,
`${DIST_BUILD_ES2015_ROOT}/node_modules`,

View File

@@ -10,7 +10,7 @@ task('clean', (done: Function) => {
});
task('clean.src', (done: Function) => {
del(['src/**/*.js', 'src/**/*.d.ts', '!src/components/slides/swiper-widget.*']).then(() => {
del(['src/**/*.js', 'src/**/*.d.ts']).then(() => {
done();
}).catch(err => {
done(err);

View File

@@ -88,12 +88,8 @@ task('demos.build', function () {
* Creates SystemJS bundle from Ionic source files.
*/
task('demos.bundle', function () {
var tsResult = tsCompile(getTscOptions('es6'), 'system')
.pipe(babel(babelOptions));
var swiper = src('src/components/slides/swiper-widget.system.js');
return merge([tsResult, swiper])
return tsCompile(getTscOptions('es6'), 'system')
.pipe(babel(babelOptions))
.pipe(remember('system'))
.pipe(concat('ionic.system.js'))
.pipe(dest(`${DIST_NAME}/bundles`))

View File

@@ -127,12 +127,8 @@ task('e2e.build', function () {
* Creates SystemJS bundle from Ionic source files.
*/
task('e2e.bundle', function () {
var tsResult = tsCompile(getTscOptions('es6'), 'system')
.pipe(babel(babelOptions));
var swiper = src('src/components/slides/swiper-widget.system.js');
return merge([tsResult, swiper])
return tsCompile(getTscOptions('es6'), 'system')
.pipe(babel(babelOptions))
.pipe(remember('system'))
.pipe(concat('ionic.system.js'))
.pipe(dest(`${DIST_NAME}/bundles`))

View File

@@ -1,4 +1,4 @@
import { UMD_MODULE, ES_2015, NODE_MODULES_ROOT, PROJECT_ROOT, SRC_ROOT, SRC_COMPONENTS_ROOT } from './constants';
import { NODE_MODULES_ROOT, PROJECT_ROOT, SRC_ROOT } from './constants';
import { src, dest } from 'gulp';
import { join } from 'path';
import * as fs from 'fs';
@@ -138,17 +138,6 @@ export function copyFile(srcPath: string, destPath: string) {
fs.writeFileSync(destPath, sourceData);
}
export function copySwiperToPath(distPath: string, moduleType: string) {
copyFile(`${SRC_COMPONENTS_ROOT}/slides/swiper-widget.d.ts`, `${distPath}/swiper-widget.d.ts`);
if (!moduleType || moduleType === UMD_MODULE) {
copyFile(`${SRC_COMPONENTS_ROOT}/slides/swiper-widget.js`, `${distPath}/swiper-widget.js`);
} else if (moduleType === ES_2015) {
copyFile(`${SRC_COMPONENTS_ROOT}/slides/swiper-widget.es2015.js`, `${distPath}/swiper-widget.js`);
} else {
copyFile(`${SRC_COMPONENTS_ROOT}/slides/swiper-widget.system.js`, `${distPath}/swiper-widget.system.js`);
}
}
export function runNgc(pathToConfigFile: string, done: Function) {
let exec = require('child_process').exec;
var shellCommand = `node --max_old_space_size=8096 ${PROJECT_ROOT}/node_modules/.bin/ngc -p ${pathToConfigFile}`;

View File

@@ -12,7 +12,8 @@ export function config(config) {
require('karma-jasmine'),
require('karma-coverage'),
require('karma-sourcemap-loader'),
require('karma-chrome-launcher')
require('karma-chrome-launcher'),
require('karma-spec-reporter')
],
files: [
{pattern: 'dist/vendor/core-js/client/core.js', included: true, watched: false},
@@ -37,8 +38,7 @@ export function config(config) {
customLaunchers: customLaunchers,
exclude: [
'dist/e2e/**/*',
'dist/ionic-angular/components/slides/swiper-widget*'
'dist/e2e/**/*'
],
// Source files that you wanna generate coverage for.
// Do not include tests or libraries (these files will be instrumented by Istanbul)
@@ -46,7 +46,15 @@ export function config(config) {
'dist/ionic-angular/umd/**/!(*spec).js': ['coverage'],
'dist/ionic-angular/**/*.js': ['sourcemap']
},
reporters: ['dots', 'coverage'],
reporters: ['dots', 'coverage', 'spec'],
specReporter: {
maxLogLines: 5, // limit number of lines logged per test
suppressErrorSummary: true, // do not print error summary
suppressFailed: false, // do not print information about failed tests
suppressPassed: false, // do not print information about passed tests
suppressSkipped: true, // do not print information about skipped tests
showSpecTiming: false // print the time elapsed for each spec
},
port: 9876,
colors: true,
logLevel: config.LOG_INFO,

View File

@@ -27,4 +27,33 @@
};
}
// requestAnimationFrame polyfill by Erik Möller. fixes from Paul Irish and Tino Zijdel
// MIT license
var win = window;
if (!win.requestAnimationFrame) {
win.requestAnimationFrame = win.webkitRequestAnimationFrame;
win.cancelAnimationFrame = win.webkitCancelAnimationFrame || win.webkitCancelRequestAnimationFrame;
if (!win.requestAnimationFrame) {
win.requestAnimationFrame = function(callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = win.setTimeout(function() {
callback(currTime + timeToCall);
}, timeToCall);
lastTime = currTime + timeToCall;
return id;
};
}
if (!win.cancelAnimationFrame) {
win.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
}
}
})();