Skip to content
This repository has been archived by the owner on May 17, 2019. It is now read-only.

Add a tree shaking test that involves the use of Flow types #771

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions test/e2e/tree-shaking-flow/fixture/.fusionrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
assumeNoImportSideEffects: true
};
2 changes: 2 additions & 0 deletions test/e2e/tree-shaking-flow/fixture/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
!node_modules
.fusion_babel_cache/

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions test/e2e/tree-shaking-flow/fixture/src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// @flow

/* Includes type imports from both node_modules and userland but as
* standalone type imports and mixed imports:
* import type {AType, BType} from ...
* import {type AType, type BType, C, D} from ...
*/

// Import only types from dependency
import type {
UnusedDependencyType,
UsedDependencyType,
} from 'fixture-pkg';

// Import mixed types and values from dependency
import {
type UnusedDependencyOtherType,
type UsedDependencyOtherType,
unusedDependencyValue,
usedDependencyValue
} from 'fixture-pkg';

// Import only types from userland
import type {
UnusedUserlandType,
UsedUserlandType
} from './user-code.js';

// Import mixed types and values from userland
import {
type UnusedUserlandOtherType,
type UsedUserlandOtherType,
unusedUserlandValue,
usedUserlandValue
} from './user-code.js';

if (__NODE__) {
// unused in the browser
// - node modules
const unusedDepVal: UnusedDependencyType = { shouldBeUnused: unusedDependencyValue };
const unusedDepVal2: UnusedDependencyOtherType = { alsoShouldBeUnused: unusedDependencyValue };
// - userland
const unustedUserlandVal: UnusedUserlandType = { shouldBeUnused: unusedUserlandValue };
const unustedUserlandVal2: UnusedUserlandOtherType = { alsoShouldBeUnused: unusedUserlandValue };

console.log(unusedDepVal, unusedDepVal2, unustedUserlandVal, unustedUserlandVal2);
}

// used in the browser
// - node modules
const usedDepVal: UsedDependencyType = { shouldBeUsed: usedDependencyValue};
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 51-52 here cause the vendor bundle to leak the unused import from fixture-pckg (__FIXTURE_DEPENDENCY_UNUSED__ which should be used only in the server bundle):

/*! no static exports found */function(e,o,s){"use strict";Object.defineProperty(o,"__esModule",{value:!0});o.usedDependencyValue="__FIXTURE_DEPENDENCY_USED__",o.unusedDependencyValue="__FIXTURE_DEPENDENCY_UNUSED__"}}]);

Removing these two lines tree shakes the import for fixture-pckg.

const usedDepVal2: UsedDependencyOtherType = { alsoShouldBeUsed: usedDependencyValue};;
// - userland
const usedUserlandVal: UsedUserlandType = { shouldBeUsed: usedUserlandValue};
const usedUserlandVal2: UsedUserlandOtherType = { alsoShouldBeUsed: usedUserlandValue};;

console.log(usedDepVal, usedDepVal2, usedUserlandVal, usedUserlandVal2);

export default () => {};
5 changes: 5 additions & 0 deletions test/e2e/tree-shaking-flow/fixture/src/unused2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @flow
export type UnusedUserlandType = {| shouldBeUnused: string |};
export type UnusedUserlandOtherType = {| alsoShouldBeUnused: string |};

export const unusedUserlandValue = '__FIXTURE_USERLAND_UNUSED__';
5 changes: 5 additions & 0 deletions test/e2e/tree-shaking-flow/fixture/src/used2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @flow
export type UsedUserlandType = {| shouldBeUsed: string |};
export type UsedUserlandOtherType = {| alsoShouldBeUsed: string |};

export const usedUserlandValue = '__FIXTURE_USERLAND_USED__';
3 changes: 3 additions & 0 deletions test/e2e/tree-shaking-flow/fixture/src/user-code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// @flow
export * from './used2.js';
export * from './unused2.js';
43 changes: 43 additions & 0 deletions test/e2e/tree-shaking-flow/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// @flow
/* eslint-env node */

const t = require('assert');
const path = require('path');
const fs = require('fs');
const {promisify} = require('util');

const readdir = promisify(fs.readdir);

const dir = path.resolve(__dirname, './fixture');

const {cmd} = require('../utils.js');

test('`fusion build` strips unused types in dev and correctly tree shakes remaining imports w/ assumeNoImportSideEffects: true', async () => {
await cmd(`build --dir=${dir}`);

const distFolder = path.resolve(dir, '.fusion/dist/development/client');
const clientFiles = await readdir(distFolder);

clientFiles
.filter(file => path.extname(file) === '.js')
.map(file => path.join(distFolder, file))
.forEach(file => {
const unusedChecks = [
// userland
'UnusedUserlandType',
'UnusedUserlandOtherType',
'__FIXTURE_USERLAND_UNUSED__',
// node modules
'UnusedDependencyType',
'UnusedDependencyOtherType',
'__FIXTURE_DEPENDENCY_UNUSED__',
];

unusedChecks.forEach(name => {
t.ok(
!fs.readFileSync(file, 'utf-8').includes(name),
`should not include unused export in browser: ${name}`
);
});
});
}, 100000);