backpack
backpack copied to clipboard
CLI options (--entry)
Hey! I really love how easy backpack makes writing scripts and servers :D
One thing that I would like to see is the ability to provide some options when running backpack. I know it is mainly build to run "a project" now, but when writing scripts I sometimes want to run specific files to test them out or bootstrap something quickly.
I suggest adding a --entry <file>
option, that would... use a different entry file :D
If this is something you think would be cool (😎) I'd love to make a PR for it!
What about using the main
field in package.json?
@benjick sounds like another good option, but isn't really what I am hoping for :-) Because then you still can't "just run a file" without editing your package.json
Related, I'm going to be testing out backpack
with a project that has multiple entry-points, so using package.json
is out...
I did this in my backpack.config.js:
module.exports = {
webpack: (config) => {
// eslint-disable-next-line no-param-reassign
config.entry.main = [
'./src/server/index.js',
];
return config;
},
};
POC: https://github.com/pandada8/backpack
very rough implementation and need improvement
Usage: backpack run xxxx.js -- --extra-options-can-be-pass-to-script
@benjick Your method is feasible, but it cannot support multiple entries.
For example, I have both a server and a client in a project, for some reason.
@yoyo837 You use this for frontend too?
@yoyo837 You should use Razzle if you have both a client and server
@benjick @jaredpalmer No, just http client and server. Two entries.
Here is how I work with multiple entry-points:
package.json:
"scripts": {
"backpackEntry1": "cross-env BACKPACK_ENTRY=entry1 backpack dev",
"backpackEntry2": "cross-env BACKPACK_ENTRY=entry2 backpack dev"
},
backpack.config.js:
module.exports = {
webpack: (config, options, webpack) => {
config.entry.main = './server/entry.js';
return config;
}
}
server/entry.js:
const BACKPACK_ENTRY_MAP = {
entry1: () => {
require('./entry1')();
},
entry2: () => {
require('./entry2')();
}
};
const runEntryCommand = () => {
console.log(process.env.BACKPACK_ENTRY);
const entry = process.env.BACKPACK_ENTRY;
const task = BACKPACK_ENTRY_MAP[entry] || BACKPACK_ENTRY_MAP['entry1'];
task();
}
runEntryCommand();