@@ -21,7 +21,7 @@ import alias from '@rollup/plugin-alias';
2121import { createRequire } from 'module' ;
2222import { fileURLToPath } from 'url' ;
2323import { readdirSync } from 'fs' ;
24- import { dirname , join } from 'path' ;
24+ import { dirname , join , relative , resolve } from 'path' ;
2525
2626const __filename = fileURLToPath ( import . meta. url ) ;
2727const __dirname = dirname ( __filename ) ;
@@ -59,31 +59,46 @@ const externalDeps = ['https', 'http', 'url'].concat(Object.keys({ ...dependenci
5959// into the CJS output instead of leaving it as an external dependency.
6060const externalDepsWithoutUuid = externalDeps . filter ( dep => dep !== 'uuid' ) ;
6161
62- // React Native optional-dependency loaders (imported as 'react_native_optional/<name>', see the tsconfig
63- // paths) are not bundled into the main bundles. Each is shipped as its own module in
64- // dist/react_native_optional and required from the main bundle by relative path, because Metro before
65- // 0.82.5 shifts every later require in a module when an optional dependency is missing .
66- // See lib/utils/import.react_native/optional/fast_text_encoding.ts.
67- const RN_OPTIONAL = 'react_native_optional' ;
68- const RN_OPTIONAL_LOADERS = readdirSync ( join ( __dirname , 'lib/utils/import.react_native/optional' ) )
62+ // React Native optional-dependency loaders (lib/react_native_optional) are not bundled into the
63+ // main bundles. Each is shipped as its own module at the same relative location in dist, because Metro
64+ // before 0.82.5 shifts every later require in a module when an optional dependency is missing.
65+ // See lib/react_native_optional/fast_text_encoding.ts .
66+ const BUILD_DIR = join ( __dirname , '.build' ) ;
67+ const RN_OPTIONAL_DIR = 'react_native_optional' ;
68+ const RN_OPTIONAL_LOADERS = readdirSync ( join ( __dirname , 'lib' , RN_OPTIONAL_DIR ) )
6969 . filter ( file => file . endsWith ( '.ts' ) && ! file . endsWith ( '.spec.ts' ) )
7070 . map ( file => file . replace ( / \. t s $ / , '' ) ) ;
7171
72- const isRnOptionalLoader = id => id . startsWith ( `${ RN_OPTIONAL } /` ) ;
73-
74- const externalWithRnOptionalLoaders = deps => id => deps . includes ( id ) || isRnOptionalLoader ( id ) ;
75-
76- const rnOptionalLoaderPaths = id => ( isRnOptionalLoader ( id ) ? `./${ id } .js` : id ) ;
72+ // Leaves loader imports out of the main bundles and writes them as a path relative to the bundle, e.g.
73+ // import '../../../react_native_optional/async_storage' (from a file deep in .build)
74+ // -> require("./react_native_optional/async_storage.js") (in dist/index.react_native.min.js)
75+ // The bundles are at the root of dist and the loaders are built to dist/react_native_optional,
76+ // so that path points to the built loader.
77+ // Marking the imports external via the `external` option is not enough: Rollup would resolve them to the
78+ // file in .build and write a path from the bundle back into .build (../.build/react_native_optional/...).
79+ // Returning a relative id from resolveId makes Rollup write it exactly as given.
80+ const rnOptionalExternalPlugin = {
81+ name : 'rn-optional-external' ,
82+ resolveId ( source , importer ) {
83+ if ( ! importer || ! source . startsWith ( '.' ) ) return null ;
84+ const pathInBuild = relative ( BUILD_DIR , resolve ( dirname ( importer ) , source ) ) . replace ( / \. j s $ / , '' ) ;
85+ if ( ! pathInBuild . startsWith ( `${ RN_OPTIONAL_DIR } /` ) ) return null ;
86+ return { id : `./${ pathInBuild } .js` , external : true } ;
87+ } ,
88+ } ;
7789
78- const rnOptionalLoaderBundle = name => ( {
90+ const rnOptionalLoadersBundle = {
7991 external : externalDeps ,
80- input : join ( __dirname , `.build/utils/import.react_native/optional/${ name } .js` ) ,
92+ input : Object . fromEntries (
93+ RN_OPTIONAL_LOADERS . map ( name => [ name , join ( BUILD_DIR , RN_OPTIONAL_DIR , `${ name } .js` ) ] )
94+ ) ,
8195 output : {
8296 exports : 'named' ,
8397 format : 'cjs' ,
84- file : `dist/${ RN_OPTIONAL } /${ name } .js` ,
98+ dir : join ( 'dist' , RN_OPTIONAL_DIR ) ,
99+ entryFileNames : '[name].js' ,
85100 } ,
86- } ) ;
101+ } ;
87102
88103const cjsBundleFor = ( platform , opt = { } ) => {
89104 const { minify, ext } = {
@@ -95,14 +110,13 @@ const cjsBundleFor = (platform, opt = {}) => {
95110 const min = minify ? '.min' : '' ;
96111
97112 return {
98- plugins : [ aliasPlugin , resolvePluginByPlatform [ platform ] || resolvePlugin , commonjs ( ) ] ,
99- external : externalWithRnOptionalLoaders ( externalDepsWithoutUuid ) ,
113+ plugins : [ rnOptionalExternalPlugin , aliasPlugin , resolvePluginByPlatform [ platform ] || resolvePlugin , commonjs ( ) ] ,
114+ external : externalDepsWithoutUuid ,
100115 input : `.build/index.${ platform } .js` ,
101116 output : {
102117 exports : 'named' ,
103118 format : 'cjs' ,
104119 file : `dist/index.${ platform } ${ min } ${ ext } ` ,
105- paths : rnOptionalLoaderPaths ,
106120 plugins : minify ? [ terser ( ) ] : undefined ,
107121 sourcemap : true ,
108122 } ,
@@ -120,12 +134,11 @@ const esmBundleFor = (platform, opt) => {
120134
121135 return {
122136 ...cjsBundleFor ( platform ) ,
123- external : externalWithRnOptionalLoaders ( externalDeps ) ,
137+ external : externalDeps ,
124138 output : [
125139 {
126140 format : 'es' ,
127141 file : `dist/index.${ platform } .es${ min } ${ ext } ` ,
128- paths : rnOptionalLoaderPaths ,
129142 plugins : minify ? [ terser ( ) ] : undefined ,
130143 sourcemap : true ,
131144 } ,
@@ -227,9 +240,7 @@ const bundles = {
227240 'esm-node-min' : esmBundleFor ( 'node' , { ext : '.mjs' } ) ,
228241 'esm-react-native-min' : esmBundleFor ( 'react_native' ) ,
229242 'esm-universal' : esmBundleFor ( 'universal' ) ,
230- ...Object . fromEntries (
231- RN_OPTIONAL_LOADERS . map ( name => [ `cjs-react-native-optional-${ name } ` , rnOptionalLoaderBundle ( name ) ] )
232- ) ,
243+ 'cjs-react-native-optional' : rnOptionalLoadersBundle ,
233244 'json-schema' : jsonSchemaBundle ,
234245 'cjs-ua-parser-min' : cjsBundleForUAParser ( ) ,
235246 'esm-ua-parser-min' : esmBundleForUAParser ( ) ,
0 commit comments