-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(repack): fixed asset inlining for newer React Native versions (#422)
* fix(repack): fix inlining assets in react-native >= 0.72 * chore: add changeset * fix: remove redundant .default * test(repack): added tests for inlining assets in newer React Native versions
- Loading branch information
Showing
3 changed files
with
111 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@callstack/repack": patch | ||
--- | ||
|
||
Fix inlining assets in react-native versions >= 0.72 |
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 |
---|---|---|
|
@@ -207,73 +207,110 @@ describe('assetLoader', () => { | |
}); | ||
|
||
describe('should inline asset', () => { | ||
it('without scales', async () => { | ||
const { code } = await compileBundle( | ||
'android', | ||
{ | ||
'./index.js': "export { default } from './__fixtures__/logo.png';", | ||
}, | ||
true | ||
); | ||
it.each([ | ||
{ reactNativeVersion: '<0.72', moduleExportSyntax: 'module.exports =' }, | ||
{ reactNativeVersion: '>=0.72', moduleExportSyntax: 'export default ' }, | ||
])( | ||
'without scales - React Native $reactNativeVersion', | ||
async ({ moduleExportSyntax }) => { | ||
const { code } = await compileBundle( | ||
'android', | ||
{ | ||
'node_modules/react-native/Libraries/Utilities/PixelRatio.js': `${moduleExportSyntax} { get: () => 1 };`, | ||
'./index.js': "export { default } from './__fixtures__/logo.png';", | ||
}, | ||
true | ||
); | ||
|
||
const context: { Export?: { default: Record<string, any> } } = {}; | ||
vm.runInNewContext(code, context); | ||
const context: { Export?: { default: Record<string, any> } } = {}; | ||
vm.runInNewContext(code, context); | ||
|
||
const logo = ( | ||
await fs.promises.readFile( | ||
path.join(__dirname, './__fixtures__/logo.png') | ||
) | ||
).toString('base64'); | ||
expect(context.Export?.default).toEqual({ | ||
uri: `data:image/png;base64,${logo}`, | ||
width: 2292, | ||
height: 393, | ||
scale: 1, | ||
}); | ||
}); | ||
const logo = ( | ||
await fs.promises.readFile( | ||
path.join(__dirname, './__fixtures__/logo.png') | ||
) | ||
).toString('base64'); | ||
expect(context.Export?.default).toEqual({ | ||
uri: `data:image/png;base64,${logo}`, | ||
width: 2292, | ||
height: 393, | ||
scale: 1, | ||
}); | ||
} | ||
); | ||
|
||
it.each([ | ||
{ prefferedScale: 1 }, | ||
{ prefferedScale: 2 }, | ||
{ prefferedScale: 3 }, | ||
])('with scales', async ({ prefferedScale }) => { | ||
const { code } = await compileBundle( | ||
'android', | ||
{ | ||
'node_modules/react-native/Libraries/Utilities/PixelRatio.js': `module.exports = { get: () => ${prefferedScale} };`, | ||
'./index.js': "export { default } from './__fixtures__/star.png';", | ||
}, | ||
true | ||
); | ||
{ | ||
prefferedScale: 1, | ||
reactNativeVersion: '<0.72', | ||
moduleExportSyntax: 'module.exports =', | ||
}, | ||
{ | ||
prefferedScale: 2, | ||
reactNativeVersion: '<0.72', | ||
moduleExportSyntax: 'module.exports =', | ||
}, | ||
{ | ||
prefferedScale: 3, | ||
reactNativeVersion: '<0.72', | ||
moduleExportSyntax: 'module.exports =', | ||
}, | ||
{ | ||
prefferedScale: 1, | ||
reactNativeVersion: '>=0.72', | ||
moduleExportSyntax: 'export default ', | ||
}, | ||
{ | ||
prefferedScale: 2, | ||
reactNativeVersion: '>=0.72', | ||
moduleExportSyntax: 'export default ', | ||
}, | ||
{ | ||
prefferedScale: 3, | ||
reactNativeVersion: '>=0.72', | ||
moduleExportSyntax: 'export default ', | ||
}, | ||
])( | ||
'with scales ($prefferedScale) - React Native $reactNativeVersion', | ||
async ({ prefferedScale, moduleExportSyntax }) => { | ||
const { code } = await compileBundle( | ||
'android', | ||
{ | ||
'node_modules/react-native/Libraries/Utilities/PixelRatio.js': `${moduleExportSyntax} { get: () => ${prefferedScale} };`, | ||
'./index.js': "export { default } from './__fixtures__/star.png';", | ||
}, | ||
true | ||
); | ||
|
||
const context: { Export?: { default: Record<string, any> } } = {}; | ||
vm.runInNewContext(code, context); | ||
const context: { Export?: { default: Record<string, any> } } = {}; | ||
vm.runInNewContext(code, context); | ||
|
||
const logos = await Promise.all([ | ||
( | ||
await fs.promises.readFile( | ||
path.join(__dirname, './__fixtures__/[email protected]') | ||
) | ||
).toString('base64'), | ||
( | ||
await fs.promises.readFile( | ||
path.join(__dirname, './__fixtures__/[email protected]') | ||
) | ||
).toString('base64'), | ||
( | ||
await fs.promises.readFile( | ||
path.join(__dirname, './__fixtures__/[email protected]') | ||
) | ||
).toString('base64'), | ||
]); | ||
const logos = await Promise.all([ | ||
( | ||
await fs.promises.readFile( | ||
path.join(__dirname, './__fixtures__/[email protected]') | ||
) | ||
).toString('base64'), | ||
( | ||
await fs.promises.readFile( | ||
path.join(__dirname, './__fixtures__/[email protected]') | ||
) | ||
).toString('base64'), | ||
( | ||
await fs.promises.readFile( | ||
path.join(__dirname, './__fixtures__/[email protected]') | ||
) | ||
).toString('base64'), | ||
]); | ||
|
||
expect(context.Export?.default).toEqual({ | ||
uri: `data:image/png;base64,${logos[prefferedScale - 1]}`, | ||
width: 286, | ||
height: 272, | ||
scale: prefferedScale, | ||
}); | ||
}); | ||
expect(context.Export?.default).toEqual({ | ||
uri: `data:image/png;base64,${logos[prefferedScale - 1]}`, | ||
width: 286, | ||
height: 272, | ||
scale: prefferedScale, | ||
}); | ||
} | ||
); | ||
}); | ||
|
||
describe('should convert to remote-asset', () => { | ||
|
@@ -306,7 +343,7 @@ describe('assetLoader', () => { | |
{ prefferedScale: 1 }, | ||
{ prefferedScale: 2 }, | ||
{ prefferedScale: 3 }, | ||
])('with scales', async ({ prefferedScale }) => { | ||
])('with scales $prefferedScale', async ({ prefferedScale }) => { | ||
const { code } = await compileBundle( | ||
'ios', // platform doesn't matter for remote-assets | ||
{ | ||
|
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