-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable code splitting for device code
Signed-off-by: Liam McLoughlin <lmcloughlin@fitbit.com>
- Loading branch information
Showing
11 changed files
with
406 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import('./chunkB') | ||
.then(({ chunkB }) => { | ||
chunkB(); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function chunkB() { | ||
console.log('Hello code splitting!'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import MagicString from 'magic-string'; | ||
import { Plugin } from 'rollup'; | ||
|
||
// require is bugged in FbOS such that the loaded module has its scope | ||
// merged with the global scope, which is bad news (and particularly | ||
// so if you use a minifier as we do). In order to workaround this, | ||
// this plugin wraps all non-entry chunks in an IIFE to maintain | ||
// isolation of scopes. | ||
|
||
export default function workaroundChunkScope(): Plugin { | ||
return { | ||
name: 'workaroundChunkScope', | ||
|
||
renderChunk(code, chunk) { | ||
// Only non-entry chunks need this fix (since those are loaded using require) | ||
if (chunk.isEntry) return null; | ||
|
||
const magic = new MagicString(code); | ||
magic.prepend('(function(){'); | ||
magic.append('})()'); | ||
|
||
return { | ||
code: magic.toString(), | ||
map: magic.generateMap({ | ||
hires: true, | ||
}), | ||
}; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import stream from 'stream'; | ||
|
||
import Vinyl from 'vinyl'; | ||
|
||
export default function getFilesFromStream( | ||
stream: stream.Readable, | ||
numFiles = 1, | ||
) { | ||
const files: Vinyl[] = []; | ||
return new Promise<Vinyl[]>((resolve, reject) => { | ||
stream.on('data', (file: Vinyl) => { | ||
if (file.contents) { | ||
files.push(file); | ||
if (files.length === numFiles) resolve(files); | ||
} | ||
}); | ||
stream.on('finish', () => reject(new Error('No file in stream matched'))); | ||
stream.on('error', reject); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.