From b746c6422957fd56e0485fa8942899bd1e4b20ac Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Wed, 8 Jun 2016 12:15:09 -0700 Subject: [PATCH] WIP: test: add tree-shaking test (#8979) * test: add tree-shaking test currently this doesn't throw or break the build, first we need to resolve all of the existing issues. to run execute: ./tools/tree-shaking-test/test.sh then inspect dist/tree-shaking/test/**/*.bundle.js * fix(http): remove peerDep on @angular/common it is not needed there because it will get transitively installed by @angular/platform-browser we only need to declare this dependency in tsconfig.json because tsconfig.json's do not support transitive dependencies in this way. --- modules/@angular/http/package.json | 1 - tools/tree-shaking-test/README.md | 17 ++++++++++++ tools/tree-shaking-test/rollup.config.js | 20 ++++++++++++++ tools/tree-shaking-test/test.sh | 35 ++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 tools/tree-shaking-test/README.md create mode 100644 tools/tree-shaking-test/rollup.config.js create mode 100755 tools/tree-shaking-test/test.sh diff --git a/modules/@angular/http/package.json b/modules/@angular/http/package.json index d3af3f8c06..920c7a2696 100644 --- a/modules/@angular/http/package.json +++ b/modules/@angular/http/package.json @@ -10,7 +10,6 @@ "peerDependencies": { "rxjs": "5.0.0-beta.6", "@angular/core": "0.0.0-PLACEHOLDER", - "@angular/common": "0.0.0-PLACEHOLDER", "@angular/platform-browser": "0.0.0-PLACEHOLDER" }, "repository": { diff --git a/tools/tree-shaking-test/README.md b/tools/tree-shaking-test/README.md new file mode 100644 index 0000000000..99b4da8e26 --- /dev/null +++ b/tools/tree-shaking-test/README.md @@ -0,0 +1,17 @@ +Rollup tree-shaking test +------------------------ + +The purpose of this test is to verify how much stuff can be tree-shaken from our packages. + +The test generates an empty js file that imports everything from a particular package but doesn't +use any of the imported references. + +In the ideal scenario Rollup should detect that none of the references are being used and should +create an empty bundle file. + +In reality there is a lot of stuff preserved in the bundle because Rollup is currently not able to +make a safe decision to remove many of the unused symbols. + +To run execute: `./tools/tree-shaking-test/test.sh` + +then inspect `dist/tree-shaking/test/**/*.bundle.js` diff --git a/tools/tree-shaking-test/rollup.config.js b/tools/tree-shaking-test/rollup.config.js new file mode 100644 index 0000000000..b027594f50 --- /dev/null +++ b/tools/tree-shaking-test/rollup.config.js @@ -0,0 +1,20 @@ +class RollupNG2 { + resolveId(id, from){ + if(id.startsWith('@angular/')){ + return `${__dirname}/../../packages-dist/${id.split('/')[1]}/esm/index.js`; + } + + // if(id.startsWith('rxjs/')){ + // return `${__dirname}/../../../node_modules/rxjs-es/${id.replace('rxjs/', '')}.js`; + // } + } +} + + +export default { + entry: 'test.js', + format: 'es6', + plugins: [ + new RollupNG2(), + ] +} diff --git a/tools/tree-shaking-test/test.sh b/tools/tree-shaking-test/test.sh new file mode 100755 index 0000000000..a0792090e0 --- /dev/null +++ b/tools/tree-shaking-test/test.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash + +set -ex -o pipefail + +cd `dirname $0` +cd ../.. + +export NODE_PATH=${NODE_PATH}:$(pwd)/dist-packages/ + +DEST_DIR=./dist/trees-shaking-test + +rm -rf ${DEST_DIR} + +for PACKAGE in \ + core \ + compiler \ + common \ + platform-browser \ + platform-server \ + http \ + router \ + upgrade +do + echo "======= Tree-shaking TEST: ${SRCDIR} =====" + TEST_DIR=${DEST_DIR}/${PACKAGE} + TEST_ENTRY_POINT=${TEST_DIR}/test.js + mkdir -p ${TEST_DIR} + cp ./tools/tree-shaking-test/rollup.config.js ${TEST_DIR}/ + echo "import * as x from '@angular/${PACKAGE}'" > ${TEST_ENTRY_POINT} + ( + cd ${TEST_DIR} + $(npm bin)/rollup --config rollup.config.js --output ${PACKAGE}.bundle.js + ) + +done