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

Can this work in the browser? #4

Open
serapath opened this issue May 11, 2017 · 3 comments
Open

Can this work in the browser? #4

serapath opened this issue May 11, 2017 · 3 comments

Comments

@serapath
Copy link

Not sure if it makes sense at all, but it would be awesome if it could work in the browser too :-)

I am currently using a sync require function in the browser that downloads modules and caches them in localStorage. I plan to extend it somehow utilizing an iframe to share the modules cross domain, but figured I would actually also like to have some kind of window['package.json'] or localStorage['package.json'] and maybe even a way to prompt the user for a token to be able to do a npm publish directly from the browser or the devtools :-)

I'm actually right now working on refining this and even though it utilizes http://wzrd.in by default, it's possible to provide an alternative like for example a forked version wizardamigos-browserify-cdn.herokuapp.com and ideally it would be kind of neat to provide something ssb and/or hyperdrive based?

What do you think about this?

@hackergrrl
Copy link
Owner

hackergrrl commented May 12, 2017 via email

@hackergrrl
Copy link
Owner

You should also check out Everything Stays. You should be able to use stay with js-ipfs right now.

@serapath
Copy link
Author

interesting. I will check out "Everything Stays". thx :-)

I am not sure about the details, but the initial page load should be as small and optimized as possible.
Probably it could even be less. All dependencies are loaded once and then cached indefinitely...

Maybe the iframe that manages the storing of modules to make them available cross domains for other tabs that also use the "https://wzrd.in/standalone/npmrequire" script could later even be used to share modules in a p2p fashion with other npmrequire users :-)

I would love to get some feedback and inspiration of what would actually make the most sense...
I'm still a lot in the brainstorming phase and have 2 goals in mind:

  1. just require an app and automatically start it (maybe just provide some configuration)
  2. receive all the source connected to a module to actually edit/change it to later run npm publish,
    • it would prompt for an npm token to succeed so the next visitor sees the updated version already

I'm actually trying to build something minimal, so that every page eventually becomes something like:

<meta charset=“utf-8”>
<body>
  <!-- <script name=".npmrc" type="application/json"> ... </script> -->
  <script name="package.json" type="application/json">
    {
      version: '1.0.0',
      name: 'hellobrowser',
      main: 'hello.js',
      // (dev-)dependencies can specify version if any 'cached' or 'latest' is not good enough...
      scripts: {
        start: ''
      }
    }
  </script>
  <script name="src/hello.js" type="text/plain">
    var bel = require('bel')
    var world = require('./world')
    var browser = require('_browser')
    module.exports = bel`
      <h1> hello ${browser} ${world} </h1>
    `
  </script>
  <script name="src/world.js" type="text/plain">
    module.exports = 'world'
  </script>
  <script name="src/node_modules/_browser.js" type="text/plain">
    module.exports = 'browser'
  </script>
  <script src="https://wzrd.in/standalone/npmrequire">
    /* set's `window['npm']` and `window['require']` and then maybe:

      1. searches for a script tag with the name `package.json`
         and of type "application/json" and turns it into:
         => window['package.json'] = { /* ... content of above script tag ... */ }

      2. maybe searches also for a `.npmrc` named script tag?
         and turns it into:
         => window['.npmrc'] = `...`

      3. it also executes the content of this script tag
    */

    // that would probably be run automatically when a normal user visits the page
    var main = window['package.json'].main
    var hellobrowser = require(main)
    document.body.appendChild(hellobrowser)

    // that would probably be run from the "devtools" and not here when in "dev mode"
    npm.start()

  </script>
</body>

I went through all the npm cli commands and was wondering if it's actually smart to do it like npm or if also less features would do. What I'm using for quite some time already is a script on codepen https://codepen.io/serapath/pen/yMpGXr/ and I have one version that includes the basic iframe logic but it's not published yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants