stealjs / steal

Gets JavaScript
https://stealjs.com
MIT License
1.37k stars 522 forks source link

Steal uses `browser` field in package.json even when running on Node #868

Open stucox opened 7 years ago

stucox commented 7 years ago

Test case — using the facts that:

— so when loading nunjucks on Node (with or without Steal), we’d expect nunjucks.FileSystemLoader to be available.

$ npm install -DE steal
$ npm install -DE nunjucks
$ node
> var nunjucks = require('nunjucks')
> var steal = require('steal')
> !!nunjucks.FileSystemLoader
true
> steal.import('nunjucks').then(nunjucks => console.log(!!nunjucks.FileSystemLoader));
[...]
false

I’ve checked with a bit of debugging that Steal is indeed loading nunjucks/browser/nunjucks.js, rather than nunjucks/index.js.

Removing the browser field from Nunjucks’ package.json and repeating the above throws an error (within Nunjucks) re. fs not being available, which at least implies it’s loading the version intended for Node (which hopefully I can solve that with steal-builtins or similar… anyway, that’s my problem to deal with).

matthewp commented 7 years ago

Running Steal code in Node is a general problem. Some times you want it to use the browser field, such as when you are doing a build, and then some times you want to run the code in a Node context.

I'm not sure how to fix this, to be honest. I plan on making some larger architecture changes and want to outline those in https://github.com/stealjs/rfcs. Leaving this open so I'm sure to consider this case.

stucox commented 7 years ago

Having thought about this a bit, I settled on the following (fairly non-revolutionary) mindset:

P.S. I like the rfcs repo idea!