-
Notifications
You must be signed in to change notification settings - Fork 37
Add a tree shaking test that involves the use of Flow types #771
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
assumeNoImportSideEffects: true | ||
}; |
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.
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}; | ||
const usedDepVal2: UsedDependencyOtherType = { alsoShouldBeUsed: usedDependencyValue};; | ||
// - userland | ||
const usedUserlandVal: UsedUserlandType = { shouldBeUsed: usedUserlandValue}; | ||
const usedUserlandVal2: UsedUserlandOtherType = { alsoShouldBeUsed: usedUserlandValue};; | ||
|
||
console.log(usedDepVal, usedDepVal2, usedUserlandVal, usedUserlandVal2); | ||
|
||
export default () => {}; |
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__'; |
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__'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// @flow | ||
export * from './used2.js'; | ||
export * from './unused2.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); |
There was a problem hiding this comment.
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 fromfixture-pckg
(__FIXTURE_DEPENDENCY_UNUSED__
which should be used only in the server bundle):Removing these two lines tree shakes the import for
fixture-pckg
.