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

Query data from any file #72

Closed
egoist opened this issue Apr 27, 2019 · 6 comments
Closed

Query data from any file #72

egoist opened this issue Apr 27, 2019 · 6 comments
Assignees

Comments

@egoist
Copy link
Collaborator

egoist commented Apr 27, 2019

Feature request

What problem does this feature solve?

Allow users to pull data in components when they actually need it.

What does the proposed API look like?

Introducing the saber-data.js (supported in project-level and theme-level):

exports.allTagNames = () => ['foo', 'bar']

In a component:

<script>
import { allTagNames } from 'saber/data'

export default {
  data() {
    return { allTagNames }
  }
}
</script>

How should this be implemented in your opinion?

Convert this with a babel plugin:

import { allTagNames } from 'saber/data'

to:

import allTagNames from '#cache/data.js?name=allTagNames'

The content of #cache/data.js:

const saberData = Object.assign(
  require('/path/to/project/saber-data.js'),
  require('/path/to/theme/saber-data.js')
)

module.exports = name => {
  return saberData[name] && saberData[name]()
}

Add a webpack loader just for #cache/data.js:

const devalue = require('devalue')

module.exports = async function(source) {
    const done = this.async()
	const mod = runCodeInSandbox(source, this.resourcePath)
    const result = await mod(this.resourceQuery.name)
    return `export default ${devalue(result)}`
}

Are you willing to work on this yourself?

Yes.

@egoist egoist changed the title Query data from anywhere Query data from any file Apr 27, 2019
@IssueHuntBot
Copy link

@IssueHunt has funded $80.00 to this issue.


@egoist
Copy link
Collaborator Author

egoist commented Jun 25, 2019

Maybe close this in favor of #183

@nblthree
Copy link
Member

@egoist Yes #183 seems much better if I am not mistaken you can simply do

exports.handler = { allTagNames: someValue }

inside ./functions/sharedData.js
then requesting the data with

import { embedFunction } from 'saber/functions'

const posts = embedFunction('/sharedData.js')

@egoist
Copy link
Collaborator Author

egoist commented Jun 25, 2019

@MarchWorks should be embedFunction('/sharedData') 😄, and exports.handler should be a function that returns the data.

#183 will also make #191 possible.

@nblthree
Copy link
Member

@egoist It's absolutely great just need to be implemented 🎉

@krmax44
Copy link
Contributor

krmax44 commented Jun 25, 2019

moved to #183

@krmax44 krmax44 closed this as completed Jun 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants