This repository has been archived by the owner on Jan 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 479
[RFC] esbuild support #3659
Closed
Closed
[RFC] esbuild support #3659
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6efc52a
[WIP]
EvanBacon 7f0e834
Update service.ts
EvanBacon 4a5e4dc
Support platform extensions
EvanBacon 0c527ab
Added multi-platform support
EvanBacon 3c74206
Merge branch 'master' into @evanbacon/expo-cli/esbuild
EvanBacon 820a8a0
Merge branch 'master' into @evanbacon/expo-cli/esbuild
EvanBacon 3addacb
fixup command
EvanBacon f5c3339
Merge branch 'master' into @evanbacon/expo-cli/esbuild
EvanBacon f70e2f2
Added dev server support
EvanBacon 36f7bef
PR Feedback
EvanBacon 783e7be
Merge branch 'master' into @evanbacon/expo-cli/esbuild
EvanBacon 3ce2fac
lazy load esbuild
EvanBacon d367bdf
Added better shim for bundling
EvanBacon d3de4d9
Merge branch 'master' into @evanbacon/expo-cli/esbuild
EvanBacon 116803e
added hardcoded path
EvanBacon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
import Log from '@expo/bunyan'; | ||
import { getBareExtensions } from '@expo/config/paths'; | ||
import { BuildOptions } from 'esbuild'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import resolveFrom from 'resolve-from'; | ||
import { resolveEntryPoint } from 'xdl/build/tools/resolveEntryPoint'; | ||
|
||
import { | ||
getAssetExtensions, | ||
getBundleEnvironment, | ||
getMainFields, | ||
isDebug, | ||
} from './bundlerSettings'; | ||
import aliasPlugin from './plugins/aliasPlugin'; | ||
import loggingPlugin from './plugins/loggingPlugin'; | ||
import patchPlugin from './plugins/patchPlugin'; | ||
import reactNativeAssetsPlugin from './plugins/reactNativeAssetsPlugin'; | ||
import stripFlowTypesPlugin from './plugins/stripFlowTypesPlugin'; | ||
|
||
function setAssetLoaders(assetExts: string[]) { | ||
return assetExts.reduce<Record<string, string>>( | ||
(loaders, ext) => ({ ...loaders, ['.' + ext]: 'file' }), | ||
{} | ||
); | ||
} | ||
|
||
export function loadConfig( | ||
projectRoot: string, | ||
{ | ||
logger, | ||
platform, | ||
isDev, | ||
cleanCache, | ||
config, | ||
}: { | ||
logger: Log; | ||
platform: 'ios' | 'android' | 'web'; | ||
isDev: boolean; | ||
cleanCache?: boolean; | ||
config?: Partial<BuildOptions>; | ||
} | ||
) { | ||
const distFolder = path.resolve(projectRoot, `dist`); | ||
const outputPath = path.resolve(`dist/index.${platform}.js`); | ||
fs.mkdirSync(path.dirname(outputPath), { recursive: true }); | ||
|
||
const assetExtensions = getAssetExtensions(); | ||
const buildOptions: BuildOptions = { | ||
...(config || {}), | ||
entryPoints: [resolveEntryPoint(projectRoot, platform)], | ||
outfile: outputPath, | ||
assetNames: 'assets/[name]', | ||
publicPath: '/', | ||
minify: !isDev, | ||
write: true, | ||
bundle: true, | ||
// For now, remove all comments so the client can download the bundle faster. | ||
legalComments: isDev ? 'none' : 'eof', | ||
// This helps the source maps be located in the correct place. | ||
// Without it, tapping the stack traces will open to the wrong place. | ||
sourceRoot: distFolder, | ||
sourcemap: true, | ||
incremental: true, | ||
logLevel: isDebug ? 'verbose' : 'debug', | ||
mainFields: getMainFields(platform), | ||
define: getBundleEnvironment({ isDev }), | ||
loader: { '.js': 'jsx', ...setAssetLoaders(assetExtensions) }, | ||
}; | ||
if (!buildOptions.plugins) { | ||
buildOptions.plugins = []; | ||
} | ||
|
||
if (platform !== 'web') { | ||
buildOptions.target = 'esnext'; | ||
buildOptions.format = 'iife'; | ||
|
||
buildOptions.resolveExtensions = getBareExtensions([platform, 'native'], { | ||
isModern: false, | ||
isTS: true, | ||
isReact: true, | ||
}).map(value => '.' + value); | ||
|
||
buildOptions.plugins.push( | ||
stripFlowTypesPlugin( | ||
projectRoot, | ||
[ | ||
'react-native', | ||
'@react-native-community/masked-view', | ||
'expo-asset-utils', | ||
'@react-native-picker/picker', | ||
'@react-native-segmented-control/segmented-control', | ||
'@react-native-community/datetimepicker', | ||
'@react-native-async-storage/async-storage', | ||
'react-native-view-shot', | ||
'react-native-gesture-handler', | ||
'@react-native-community/toolbar-android', | ||
'@react-native/normalize-color', | ||
'@react-native/assets', | ||
'@react-native/polyfills', | ||
], | ||
cleanCache | ||
), | ||
loggingPlugin(logger), | ||
reactNativeAssetsPlugin(projectRoot, platform, assetExtensions), | ||
patchPlugin(), | ||
aliasPlugin({ | ||
// TODO: Make this interface more like { 'react-native-vector-icons': '@expo/vector-icons' } | ||
// TODO: Make optional | ||
'react-native-vector-icons/': resolveFrom(projectRoot, '@expo/vector-icons'), | ||
}) | ||
); | ||
|
||
buildOptions.inject = [ | ||
resolveRelative(projectRoot, 'react-native/Libraries/polyfills/console.js'), | ||
resolveRelative(projectRoot, 'react-native/Libraries/polyfills/error-guard.js'), | ||
resolveRelative(projectRoot, 'react-native/Libraries/polyfills/Object.es7.js'), | ||
// Sets up React DevTools | ||
resolveRelative(projectRoot, 'react-native/Libraries/Core/InitializeCore.js'), | ||
]; | ||
} else { | ||
buildOptions.target = 'es2020'; | ||
buildOptions.format = 'esm'; | ||
|
||
buildOptions.resolveExtensions = getBareExtensions([platform], { | ||
isModern: false, | ||
isTS: true, | ||
isReact: true, | ||
}).map(value => '.' + value); | ||
|
||
buildOptions.plugins.push( | ||
loggingPlugin(logger), | ||
aliasPlugin({ | ||
'react-native-vector-icons/': resolveFrom(projectRoot, '@expo/vector-icons'), | ||
}) | ||
); | ||
|
||
buildOptions.inject = [resolveFrom(projectRoot, 'setimmediate/setImmediate.js')]; | ||
} | ||
|
||
// Append to the top of the bundle | ||
if (!buildOptions.banner) { | ||
buildOptions.banner = { js: '' }; | ||
} | ||
|
||
if (platform === 'web' && isDev) { | ||
buildOptions.banner.js = | ||
`(() => new EventSource("/esbuild").onmessage = () => location.reload())();\n` + | ||
buildOptions.banner.js; | ||
} | ||
if (platform !== 'web') { | ||
if (!buildOptions.banner.js) { | ||
buildOptions.banner.js = ``; | ||
} | ||
buildOptions.banner.js += `\nvar __BUNDLE_START_TIME__=this.nativePerformanceNow?nativePerformanceNow():Date.now(); | ||
var window = typeof globalThis !== 'undefined' ? globalThis : typeof global !== 'undefined' ? global : typeof window !== 'undefined' ? window : this;`; | ||
} | ||
|
||
return buildOptions; | ||
} | ||
|
||
function resolveRelative(projectRoot: string, moduleId: string): string { | ||
const _path = path.relative(projectRoot, resolveFrom(projectRoot, moduleId)); | ||
if (_path.startsWith('.')) return _path; | ||
return './' + _path; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Maybe drop this one