-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
support multiple files for codesandbox import #5619
support multiple files for codesandbox import #5619
Conversation
Deploy preview for gatsbygram ready! Built with commit 4d1a232 |
Deploy preview for using-drupal ready! Built with commit 4d1a232 |
61a4940
to
1126275
Compare
Ah. This is a good point. Here is an initial idea (that I have not thought through fully). Let's say we have the following:
What if the package JSON we sent CodeSandbox:
So given the above example, the JSON we posted would look like: files: {
"package.json": {
content: {
dependencies: {
react: "latest",
"react-dom": "latest"
},
main: "foo.js"
}
},
"foo.js": {
content: <contents-of-foo.js>
},
"bar.js": {
content: <contents-of-bar.js>
},
"baz.css": {
content: <contents-of-baz.css>
},
"index.html": {
content: html
}
} |
1126275
to
819f419
Compare
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.
This needs new tests to cover the new functionality 😄
Non-CodeSandbox REPL targets should also error if given this syntax, since they don't support it. (And we should have tests for this too.)
@@ -114,17 +126,22 @@ module.exports = ( | |||
} | |||
return map | |||
}, {}), | |||
main: filesPaths[0].fileName, |
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 should actually use the full path, not just file name. Might be conflicts otherwise.
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.
Non-CodeSandbox REPL targets should also error if given this syntax, since they don't support it.
My changes are all covered in if (node.url.startsWith(PROTOCOL_CODE_SANDBOX))
so I don't think it'll affect other platforms.
I think we should actually use the full path, not just file name.
I am not sure here. Do you mean we put the normalized path in the main field and the name of each files? I don't think it's gonna work that way bc codesandbox won't be able to recognize the full path in remote.
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.
My changes are all covered in
if (node.url.startsWith(PROTOCOL_CODE_SANDBOX))
so I don't think it'll affect other platforms.
Agreed that your changes don't impact other platforms, but we should warn if someone uses the unsupported syntax for other platforms.
I am not sure here. Do you mean we put the normalized path in the main field and the name of each files?
I mean the relative path people pass (e.g. path/foo.js
)
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.
Sorry I misunderstood what you meant. It makes sense now. Thanks for explanation!
"index.html": { | ||
content: html, | ||
}, | ||
}, | ||
} | ||
|
||
filesPaths.forEach((path, i) => { | ||
const code = fs.readFileSync(path.filePath, `utf8`) | ||
parameters.files[path.fileName] = { |
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 should actually use the full path, not just file name. Might be conflicts otherwise.
Issue with importing code example with global variablesMost code examples don't have the
|
In the below example, how will CodeSandbox be able to properly initialize the component in the Link to example: https://reactjs.org/docs/components-and-props.html#extracting-components |
@washingtonsoares I agree. We might do need to maintain separated examples like this?
and add |
Hey! Just stumbled on this issue, I like the mentioned approaches. I have some extra ideas that may be of use. @Noviny created something called
This is a good one. I generally prefer to keep the {
React: 'react',
ReactDOM: 'react-dom'
} and it will generate code like this at the start of every file: import React from 'react';
import ReactDOM from 'react-dom'; Just some additional ideas, I already like the proposed solution. I'm curious what you think! |
@washingtonsoares @cyan33 I couldn't find where exactly, but I've seen before an example of a runnable snippet of markdown code that, besides having line highlighting as with the So it would be possible to add the necessary |
@hsribei, this seems to solve the problem as a whole. Do you have any examples of how line hiding works? |
While working on reactjs/react.dev/pull/931, I also found myself thinking it would be nice if there was a way that we could specify a one-off dependency for a link (or override a default one- in this case, I found myself wanting to pin to React 16.3 rather than "latest"). Just food for thought. Probably not common, and definitely doesn't have to be part of this PR either. |
I tried to find examples but couldn't. I'm certain I've seen it before in
some docs though. I thought it was the Rust Book but no luck :/
…On Tue, Jun 5, 2018 at 7:01 PM Washington Soares ***@***.***> wrote:
@hsribei <https://github.com/hsribei>, this seems to solve the problem as
a whole.
Do you have any examples about *line hiding* ?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#5619 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAAQ294ey6J1LgIXEvBUytLJs2tBM-pFks5t5w3ngaJpZM4UUbhC>
.
|
@@ -57,25 +57,43 @@ module.exports = ( | |||
|
|||
const getFilePath = (url, protocol, directory) => { | |||
let filePath = url.replace(protocol, ``) | |||
if (!filePath.endsWith(`.js`)) { | |||
if (!filePath.endsWith(`.js`) && !filePath.endsWith(`.css`)) { |
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 we should actually change this to be a check for files without any extension rather than this list?
|
||
const verifyFile = (path, protocol) => { | ||
if (protocol !== PROTOCOL_CODE_SANDBOX && path.split(`,`).length > 1) { | ||
throw Error(`Code example path should only contain a single file, but found more than one: ${path.replace(directory, ``)}`) |
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 might want to mention which REPLs support multi-file and which don't. This error is a little vague and could cause some confusion.
Friendly ping, @cyan33– You up for making those two small tweaks so we can land this? |
@bvaughn Sorry I was occupied at another work and forgot this. I'll do it right now! 👍 |
Deploy preview for using-remark failed. Built with commit 5b9b480 https://app.netlify.com/sites/using-remark/deploys/5b5218dd4ed62f2e54b70dbe |
Deploy preview for using-contentful failed. Built with commit 4d1a232 https://app.netlify.com/sites/using-contentful/deploys/5b5602cddd28ef3de8f7c8f0 |
5b9b480
to
50db7d8
Compare
Deploy preview for gatsbyjs failed. Built with commit 50db7d8 https://app.netlify.com/sites/gatsbyjs/deploys/5b521a423813f06d7affa102 |
) | ||
} | ||
|
||
if (!(/\.js|css|ts|es6$/g).test(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.
This is a backwards breaking change. I don't think we should make it.
I think we should check for a likely file extension (e.g. indexOf(".") > 0
) and if it doesn't exist, append ".js" like we used to before– and call it a day.
Oh... the usual computers breaking and stuff :-) |
50db7d8
to
7197cd8
Compare
@bvaughn Updated. Thanks for the feedback. |
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 ok to me. Assuming CI is happy, let's merge it.
Edit If CI other than Netlify is happy, because Netlify builds seem to be failing for all of the most recent PRs that I checked.
Whoops! Almost forgot to update the docs (gatsbyjs.org/packages/gatsby-remark-code-repls/) to show the newly added functionality. @cyan33, would you like to do this? |
@bvaughn My bad, forgot to update the doc. Will do. |
b7526cd
to
87be5f1
Compare
|
||
<!-- after --> | ||
<a href="/redirect-to-codepen/components-and-props/rendering-a-component"> | ||
Try it on CodePen | ||
</a> | ||
|
||
<!-- before --> | ||
[Try it on CodeSandbox](codesandbox://components-and-props/rendering-a-component) | ||
[Try it on CodeSandbox](codesandbox://components-and-props/rendering-a-component.js) |
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 tell people to add explicit file extensions now that we support multiple types of files, even if we have a fallback mechanism for JavaScript files.
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 is a good suggestion.
@bvaughn Sorry Brian, I accidentally override your commit changes with force push. I've run prettier and replaced Let's see if the document looks good to you. |
e1bb8b6
to
62702c2
Compare
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 absolutely love this! Thanks for the effort in making this support multiple files!
One question that I have (should not block merging): is it possible to define dependencies currently?
CodesandBox supports code example with multiple files. With this plugin, you can do: | ||
|
||
```html | ||
[Try it on CodePen](codesandbox://my-example/index.js,my-example/util.js,my-example/index.css) |
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.
One nit: CodePen
> CodeSandbox
│ └── index.css | ||
``` | ||
|
||
CodesandBox supports code example with multiple files. With this plugin, you can do: |
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.
Small nit: CodesandBox
-> CodeSandbox
@CompuIves Thanks! As for your question, this PR doesn't involve any changes to dependencies. But seems like there's already a way to customize dependencies through config. |
|
||
import "./index.css" | ||
``` | ||
|
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.
Nice overview.
@@ -57,25 +57,52 @@ module.exports = ( | |||
|
|||
const getFilePath = (url, protocol, directory) => { | |||
let filePath = url.replace(protocol, ``) | |||
if (!filePath.endsWith(`.js`)) { | |||
if (!filePath.includes(`.`)) { |
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 making this change! It was nagging me 😁
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
I guess the ball is in your court now, @KyleAMathews / @gatsbot 😁 |
Holy buckets, @cyan33 — we just merged your PR to Gatsby! 💪💜 Gatsby is built by awesome people like you. Let us say “thanks” in two ways:
If there’s anything we can do to help, please don’t hesitate to reach out to us: tweet at @gatsbyjs and we’ll come a-runnin’. Thanks again! |
Thanks for the nice improvement! Published in [email protected] |
Reference: reactjs/react.dev#913
This PR adds the ability when you want to import a CodeSandbox with multiple files (like css files). So you could do something like:
However, I came across an issue when we have more than one JavaScript files and could not decide which one is the entry. Previously we just port whatever content into
index.js
and that becomes the entry. My current quick workaround is to allow only a single JavaScript file in the list and throw an error if it's more than one. The advantage is that it has a good backward compatibility. We don't need to change the name of the file or add extraneous logic.Anyway, I tested with the reactjs.org repo and it works. But I'm not quite sure about the changes of the snapshot test, which causes the fail of the CI build. Do I need to just change the snapshot accordingly?
cc @bvaughn @washingtonsoares