-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(sdk): force browser compatibility
- Loading branch information
1 parent
2388c82
commit f8ee2ff
Showing
3 changed files
with
69 additions
and
1 deletion.
There are no files selected for viewing
Binary file not shown.
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,67 @@ | ||
// README | ||
// This script is used to ensure that all isomorphic packages are browser compatible. | ||
// Even if the browser build has server code (e.g. NextJS APIs), they will import the browser compatible versions of the isomorphic packages. | ||
|
||
// scripts/postbuild.js | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
const isomorphicPackages = ['grpc']; | ||
|
||
const BROWSER_ENVIRONMENT = 'browser'; | ||
|
||
const environments = ['node', BROWSER_ENVIRONMENT]; | ||
|
||
environments.forEach((environment) => { | ||
console.log(`Running ${environment} environment postbuild script`); | ||
console.log(``); | ||
|
||
isomorphicPackages.forEach((package) => { | ||
const isomorphPath = path.join( | ||
__dirname, | ||
'..', | ||
'lib', | ||
environment, | ||
'isomorphic', | ||
package + '.js' | ||
); | ||
|
||
const targetPath = path.join( | ||
__dirname, | ||
'..', | ||
'lib', | ||
environment, | ||
'isomorphic', | ||
`${package}.${BROWSER_ENVIRONMENT}.js` // force all isomorphic packages to be browser compatible | ||
); | ||
|
||
try { | ||
const content = fs.readFileSync(targetPath, 'utf8'); | ||
fs.writeFileSync(isomorphPath, content); | ||
} catch (error) { | ||
console.error( | ||
`Error processing isomophic package : ${package} :: ${error.message}` | ||
); | ||
} | ||
|
||
// Delete other environment files for safety | ||
environments.forEach((otherEnvironment) => { | ||
if (otherEnvironment === environment) { | ||
return; | ||
} | ||
|
||
const otherTargetPath = path.join( | ||
__dirname, | ||
'..', | ||
'lib', | ||
environment, | ||
'isomorphic', | ||
`${package}.${otherEnvironment}.js` | ||
); | ||
|
||
if (fs.existsSync(otherTargetPath)) { | ||
fs.unlinkSync(otherTargetPath); | ||
} | ||
}); | ||
}); | ||
}); |