Skip to content
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

Import rewrites #1076

Closed
sapegin opened this issue Aug 7, 2018 · 4 comments
Closed

Import rewrites #1076

sapegin opened this issue Aug 7, 2018 · 4 comments

Comments

@sapegin
Copy link
Member

sapegin commented Aug 7, 2018

The idea is to make example’s code more real-life by allowing you to import components like the user would import them in their app, including all dependencies like React. For example:

import React from 'react'
import Button from 'my-awesome-library/Button'

Technically it's not needed because both React and the current component are available in the example context by default, but it will make example easier to use, because users could copy code directly to their apps.

To make this possible we'll need to add a new config option like:

rewriteRequire(module) {
  return module.replace('my-awesome-library', '../')
}

The only think I'm not sure about is what to do with relative path.

@okonet
Copy link
Member

okonet commented Aug 19, 2018

I’m wondering if we could detect the import and use it instead the provided by the context?

Can you elaborate on relative path?

@sapegin
Copy link
Member Author

sapegin commented Aug 20, 2018

The relative path: we'll need to replace import Button from 'mylib/Button' with something like const Button = require('../../Button') where ../.. is a relative path from our example Markdown file to the module we're importing.

I’m wondering if we could detect the import and use it instead the provided by the context?

Can you elaborate on that? :-)

@okonet
Copy link
Member

okonet commented Aug 20, 2018

Can we use aliases in webpack for that?

I mean: if the import is present, it overwrites what context would give you. Making it fault tolerant but at the same time not required.

@sapegin
Copy link
Member Author

sapegin commented Aug 21, 2018

Yeah, I think that's what we're going to use in the end ;-)

If by context you mean the context config option (and add React to to it by default), then that's how it's supposed to work, yeah.

@sapegin sapegin self-assigned this Sep 3, 2018
sapegin pushed a commit that referenced this issue Sep 19, 2018
Closes #1076

Add new config option `moduleAliases` to define aliases for modules, that you can import in your examples, to make example code more realistic and copypastable:

```javascript
const path = require('path')
module.exports = {
  moduleAliases: {
    'rsg-example': path.resolve(__dirname, 'src')
  }
}
```

````jsx static
// ```jsx inside Markdown
import React from 'react'
import Button from 'rsg-example/components/Button'
import Placeholder from 'rsg-example/components/Placeholder'
````

Check out the [webpack resolve.alias documentation](https://webpack.js.org/configuration/resolve/#resolve-alias) for available syntax.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants