Coder Social home page Coder Social logo

Comments (6)

GCheung55 avatar GCheung55 commented on June 20, 2024

I think it's related to the change in https://github.com/requirejs/cajon/blob/master/tools/c.js#L41. Using global.eval vs eval.

When eval is used, global is provided via the scope chain, but global.eval doesn't.

from cajon.

jrburke avatar jrburke commented on June 20, 2024

If you have an example or test case that shows what kind of module loading breaks, that would help figure out a solution.

from cajon.

GCheung55 avatar GCheung55 commented on June 20, 2024

Here's a CommonJS module to test global.

https://gist.github.com/GCheung55/5116931959a24afc1b2a

I just tried [email protected] and the issue is reproducible.

from cajon.

jrburke avatar jrburke commented on June 20, 2024

Looking more at this, while the behavior changed in #8, it was for correctness -- best not to leak the private variables inside the loader to the evaluated text, and to provide a consistent eval environment. So I would expect the behavior as typeof global being undefined.

Is this more about compatibility with modules from node? If so, that is a trickier issue. Node has other implied available variables, like process, but I was not planning on simulating a node environment with cajon, just providing an "unwrapped" modules in the style of CommonJS/Node. But just on the style of module APIs, not on their execution environment.

If this was needed, for a particular project, it can be provided outside the loader:

<script src="cajon.js"></script>
<script>
var global = this;
require(['app']);
</script>

Does that meet the goals?

from cajon.

GCheung55 avatar GCheung55 commented on June 20, 2024

True, I could add var global = this;. I don't expect a complete simulation of a node environment. process doesn't make sense to simulate, especially since there isn't an equivalent in a browser environment. But I think a global equivalent in a browser environment would be window.

How do you feel about exposing global as an argument to the module's callback?

define(function(require, module, exports, global){})

I ask because this would be a tiny bit more performant than looking up the scope chain in order to find global set as a global variable.

from cajon.

GCheung55 avatar GCheung55 commented on June 20, 2024

Also, there are node modules that are written for both browser and server environments. The one I'm currently using is https://github.com/kamicane/prime/blob/master/defer.js which makes use of checking global for properties/methods. It's treating global as window.

from cajon.

Related Issues (15)

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.