-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(common): pass manifest json to sandbox #273
feat(common): pass manifest json to sandbox #273
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
@@ -0,0 +1,12 @@ | |||
const styles = { | |||
reset: '\u001b[0m', |
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.
There is also a library for having colorful outputs in your console logs. I believe it is this one (https://www.npmjs.com/package/chalk) we are also using in the CLI
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.
I'll check it out @BibiSebi. I saw the CLI uses it but have never tried it on my own. 🤔
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.
Yeah tbh I'd prefer using the library instead of keeping the color codes here. And it's not increasing our bundle size either (only for development mode).
But if you add chalk
, you should add it to the vite config's externals
as well. (Let me know if you don't understand what that is for)
Side note: just randomly found this library that is even used by prettier https://github.com/cosmiconfig/cosmiconfig that helps loading configurations. Maybe we can check it out |
|
||
export const load = (): Manifest => { | ||
try { | ||
const content: string = readFileSync(MANIFEST_FULL_PATH, { |
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 here we can do some kind of validation ? to check if the schema is ok ? it might be that the user types in something weird. What do you think?
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.
It would be nice Bibi. You mean using some library such as joi
, right?
It'd be a great improvement o/
Thanks for the suggestion @BibiSebi. 🚀
Interesting. It's similar to https://github.com/unjs/c12 I guess those packages are specifically useful when we want to support different types of configs (e.g. an object within package.json, something.json, something.js, something.ts, ...) |
Thanks for sharing @BibiSebi. |
@@ -0,0 +1,12 @@ | |||
{ |
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.
I think this will lead to users having unexpected options (option1, option2) by default (probably without knowing it).
What if we have an empty array for options
in the templates?
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.
Makes sense @eunjae-lee.
It should be enough once we work on the documentation.
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.
Then could you remove these options from this branch? I know it might be annoying for you to test with actual values, but they shouldn't be committed here any more.
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.
Done @eunjae-lee.
export const load = (): Manifest => { | ||
try { | ||
const content: string = readFileSync(MANIFEST_FULL_PATH, { | ||
encoding: 'utf-8', |
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.
I think we can drop this as it's by default.
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.
You mean without making use of other libraries such as cosmiconfig
, right?
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.
Oh I meant "utf-8" here
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.
I've tried without informing the encoding as suggested, @eunjae-lee, but when it's not informed, the readFileSync
returns a buffer instead of a string. As I'd need to convert it into a string in order to call JSON.parse
, I think the easiest way would be to keep the encoding part but in its shortcut way:
const content: string = readFileSync(MANIFEST_FULL_PATH, 'utf-8')
I'll commit this way and if you may have another option, let me know, please.
@@ -0,0 +1,12 @@ | |||
const styles = { | |||
reset: '\u001b[0m', |
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.
Yeah tbh I'd prefer using the library instead of keeping the color codes here. And it's not increasing our bundle size either (only for development mode).
But if you add chalk
, you should add it to the vite config's externals
as well. (Let me know if you don't understand what that is for)
…toryblok/field-plugin into EXT-1877-pass-manifest-json-to-sandbox
ping me here when you're ready for another review :) |
Co-authored-by: Eunjae Lee <hey@eunjae.dev>
…toryblok/field-plugin into EXT-1877-pass-manifest-json-to-sandbox
@@ -0,0 +1,12 @@ | |||
{ |
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.
Done @eunjae-lee.
} | ||
} | ||
|
||
const validateSchema = (manifest: Manifest): void => { |
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.
I've added this simple validation, without using any external packages, here since we decided not to go too far on this.
|
||
export const load = (): Manifest => { | ||
try { | ||
const content: string = readFileSync(MANIFEST_FULL_PATH, 'utf8') |
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.
Here I kept informing the encoding
since otherwise it would return me a Buffer instead of a string.
So, this way is the simplest one, I guess. If there is any other better way, let me know, please.
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.
We've had some other code like readFileSync(...).toString()
. I don't know what's better, though.
|
||
const chalk = new Chalk({ level: 1 }) | ||
|
||
export const green = (text: string) => chalk.green(text) |
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.
I've decided to keep this file here, even after using the Chalk package, as it serves as an abstract layer and allows us to easily switch between libraries in the future (e.g. kleur). Such as it was done now. I've just needed to change it here and for the rest of the helper was transparent.
It's ready for another review round @eunjae-lee. 🙌 |
throw new Error(`When declared, the 'options' property should be an array`) | ||
} | ||
|
||
manifest.options?.forEach((o) => { |
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.
let's rename this o
to option
to be more informative. Other than that, it looks good!
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.
Thanks for the suggestion here, @eunjae-lee. 🙌
I've just renamed it.
If there is something else, let me know, please.
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.
Looks good to me @demetriusfeijoo !
Could you also create another PR to add some tests? I should've noticed it earlier but now I think we could add some tests for some functions in manifest.ts
and sandbox.ts
. However I don't want to delay the merge of this PR. So let's do it separately :)
Thanks for the effort in reviewing it @eunjae-lee. I've created a task for adding the tests, thanks for pointing it out. 🙌 |
What?
It allows the Sandbox/Container to bootstrap options from a manifest file located within a field-plugin root folder.
Why?
JIRA: EXT-1877
see the RFC
When our Vite
printDev
plugin finds the manifest file:When our Vite
printDev
plugin doesn't find the manifest file:When our Vite
printDev
plugin finds the manifest file but it's not valid:How to test? (optional)
From the root folder of this repository, run:
yarn dev:container
yarn dev:react
It should load the options automatically (from the manifest file) like below:
If you want to change the manifest options locally, edit this file here React Manifest