Code Monkey home page Code Monkey logo

Comments (5)

jonschlinkert avatar jonschlinkert commented on May 12, 2024

Sounds like this just needs to be updated to the latest lazy-cache format.

Is lazy-cache really necessary?

Is webpack necessary? You could hand-code all of that yourself, but that's silly. What you're really asking is: "Is it really necessary to try to get the best possible performance from your modules?". To which my answer is, "No, but I'll try my hardest, and I'll base my decisions on facts, data and experience."

I don't think it's a good idea to wrap require in any way, ever.

If wrapping require bothers you, then you probably shouldn't look at what browserify and webpack do to your code.

it's not really a solution, but a hack.

Isn't that semantics? Without the hack neither webpack nor browserify can find lazy-requires, with the hack ~~~they~~~ browserify can.

This hack has turned out to be a pretty good solution. Instead of thinking that lazy-evaluation is a bad solution because the tools you use fail when it's used, perhaps you should be looking for ways to solve that problem. I do, because I know how big the benefits are.

lazy-cache has had something like 8m downloads in the past 4 months, with two issues - including this one, both related to webpack. Materially, both issues were on projects that use the old-style lazy-cache, and we've had zero actual issues on lazy-cache itself. browserify does not have this issue.

IMO this is something that can be solved in a build step, or with a webpack plugin.

from markdown-toc.

alecmev avatar alecmev commented on May 12, 2024

I'm okay with Browserify / Webpack doing crazy things to my code, because that's expected of them. They're pretty much transpilers, making require / import work in browsers.

This, however, is a regular library. Lazy loading does make sense here, but, in the end, on the client-side, where it matters the most, gray-matter still ends up being in the same bundle as markdown-toc (with Browserify; correct me if I'm wrong).

Webpack has its own mechanism for on-demand loading - require.ensure - but it doesn't work anywhere else (well, it did, but seems like nobody cared enough about server-side lazy-loading to maintain it; can be shimmed, with the benefits gone). lazy-cache is admittedly more convenient, but, on top of breaking the convention of having imported modules as variables, it also works only in Node, and nowhere else (it has no benefits in Browserify).

You're saying that this issue should be resolved in Webpack, but why should lazy-cache be preferred over the several-years-old require.ensure?

Sorry for challenging your design choices like this, I don't mind being proven wrong.

from markdown-toc.

jonschlinkert avatar jonschlinkert commented on May 12, 2024

You're saying that this issue should be resolved in Webpack

Well, I'm saying it's "meet in the middle". We can use the hack that got this working for browserify, and we could create a plugin for webpack to convert the lazy-requires to standard requires. I don't know webpack, it seems like it shouldn't be too difficult.

I get your point about how the benefits of lazy-evaluation are lost in non-node usage, but this is, after all, a node.js package. But again, I'd like to find a solution that allows us to take advantage of lazy-caching for node.js, but also make it work with browserify and webpack. I think the community in general would/should see great benefit in that

from markdown-toc.

alecmev avatar alecmev commented on May 12, 2024

Out of curiosity, what was the reason for not doing something like this?

if (file) {
  return require('gray-matter').stringify(res, file.data);
}

Lazy-loading + caching in Node, while working out of the box in Browserify / Webpack / some other lib we could be unaware of.

Just in case, I'm using this library as a client-side patch for some missing backend functionality, which isn't going into a public release, so there's no urgent need for a fix.

from markdown-toc.

doowb avatar doowb commented on May 12, 2024

@jeremejevs try using the unlazy loader in your webpack build.

from markdown-toc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.