-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
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
WIP: Add a 'run' script #245
Conversation
// We don't want to compile the node_modules, but we still want to | ||
// do a commonjs require for them. | ||
var nodeModules = {}; | ||
fs.readdirSync(paths.appNodeModules).filter(function(x) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't cover eg require("lodash/fp/extend")
(it'll get bundled instead of marked as an external).
If you want, you could just use webpack-node-externals to handle this part.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I hadn't tested requires with a /
in them. Thanks for pointing me to webpack-node-externals - I'll take a look at that.
Can you offer a few use cases of how you’d use this? |
Some use cases this should be good for:
|
var outputPath = os.tmpDir(); | ||
var outputFile = 'entry.js'; | ||
|
||
// Configure webpack. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was there a specific reason why you chose to write the webpack config inline rather than breaking it out into a separate file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
scriptName
is different depending on where you choose the entry point to be, so you need a slightly different webpack config for each execution, so you can't just have a webpack config created in its own file like the other webpack configs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could do:
// webpack.config.js
module.exports = (scriptName) => {
return {
// adapt based on scriptName here
};
};
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True. Is that aesthetically superior to just leaving it all in one file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It has the benefit of matching the new syntax allowed in Webpack 2, where configs can actually be functions that return the config object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've always grouped them because it makes it easy to sync the different webpack configs. (i.e. have a webpack.base.babel.js
that has all the common stuff)
Another use case this would be helpful for - the Relay starter kit has a script to automatically generate a graphql file. https://github.com/relayjs/relay-starter-kit/blob/master/scripts/updateSchema.js It would be nice if all the FB open source React examples used the |
We can definitely leverage |
I thought about this a bit and your goal is to run ES2015+ code in older/current node engines, right? I don't understand why you need a webpack config for that. Is there a reason why this cannot be as simple as: scripts/run.js require('babel-register')(require('../config/babel.dev'));
require(scriptPath); ? If however your goal for the run script is to actually require and run application code (like "App.js") and we end up merging #250, I'm happy to make some changes to jest-runtime to work with less configuration just like jest-cli. |
@cpojer The main reason that Do you know if there is a babel-based non-jest way to do the css and image stubbing you had in your PR? |
I still don't understand why you need this. Can you explain what the point If that is your intention however, I'd propose using jest-runtime after
|
There is no real use for running a script that imports a css or image file, at least not with these null stubs. I just thought it is frustrating to have two slightly different ways something can run, and you have files that you can import in one environment but not in others. The real use case for this however is using shared library functions, not shared React components. You might inadvertently be exporting some useful functions in a file you want to require for your script, but it's probably poor style. OK so I am convinced the webpack-based way is not the way to go. We should probably just start with something like the simple babel wrapper, see if the lack of handling css and image imports is actually a big problem, and we can always enhance the script runtime later. I will close this one and open a new PR |
This adds a
run
script so that you can invoke a script in the same ES6-and-React-compatible environment you get when you runnpm start
, but in node.To run a script:
src
, like atsrc/foobar.js
react-scripts run foobar.js
I wanted to get feedback if this seems like it's headed in the right direction. In particular are these decisions the right behavior:
fetch
.Things that I'm pretty sure are not the ideal behavior but can be improved later:
It seems like a lot of the behavior will be similar between "run a one-off script" and "run unit tests" so perhaps we should have some underlying library that is shared between this and testing like #216 .