Comments (13)
My use-case is I'm using websockets as per the micro examples.
from micro-dev.
Sounds like there's a lack of concrete use cases in this issue, so I'd like to throw in mine.
I currently use micro
to serve a Typescript app. I have a src/index.ts
file which gets compiled to a build/index.js
file, which I point micro toward using the main
key of my package.json
. I have the following scripts set up for development and production:
{
"main": "dist/index.js",
"scripts": {
"start": "micro",
"build": "tsc",
"dev": "ts-node --typeCheck node_modules/.bin/micro-dev src/index.ts",
"postinstall": "npm run build"
},
"dependencies": {
"micro": "^9.1.4"
},
"devDependencies": {
"@types/micro": "^7.3.1",
"@types/node": "9.4.7",
"micro-dev": "^2.2.1",
"ts-node": "^5.0.1",
"typescript": "^2.7.2"
}
}
I've also been testing a version that simply uses ts-node
for production, to avoid an extra compilation step:
{
"main": "index.ts",
"scripts": {
"start": "ts-node node_modules/.bin/micro",
"dev": "ts-node --typeCheck node_modules/.bin/micro-dev"
}
}
Being able to require a module before running the app might make this a bit cleaner, something like mocha
's or nodemon
's --require module
flag: micro-dev --require ts-node/register
. Or, being able to import micro-dev
as a package would allow one to write their own start
script, avoiding a tool like ts-node
altogether:
// script/dev.ts
import microDev from "micro-dev"
import app from ".."
const server = microDev(app)
server.listen(3000)
This is similar to the issue pointed out in vercel/micro#337.
I hope this is helpful — something to keep in mind as you build out the micro ecosystem!
from micro-dev.
@leo I think the doubt is more about using micro-dev
when using micro
programmatically. You can just use micro-dev index.js
if it's not exporting a function.
That mean if you use micro
programmatically you can't get the advantages of using micro-dev
from micro-dev.
The advantages of micro-dev can only be provided over the CLI. micro-dev is only meant to be used as a CLI in development. If you want its advantages, you need to use its CLI. There is no way we can provide this programmatically in a proper way.
from micro-dev.
micro-dev is a development CLI. For programmatic use, take advantage of the micro package.
from micro-dev.
This is not clear. If I use micro in a programmatic way, there should be a way to launch micro-dev from cli and still support that way of launching the application
from micro-dev.
@leo Is there a way to use micro-dev from CLI while using micro programmatically?
Currently receiving this error:
micro: The file "/my...path/index.js" does not export a function.
micro: https://err.sh/micro/no-export
I tried @sergiodxa 's recommendation of appending the filename (ie. micro-dev index.js
) but I received the same export error as just running micro-dev
.
from micro-dev.
I know this is kind of an off-topic question. But I'm wondering why you guys want to use micro programmatically in the first place? I'm just trying to think about this issue another way around.
from micro-dev.
@tungv Good question.
Why programmatically?
I'm attempting to homogenize builds for multiple projects as much as possible.
What about micro CLI makes it difficult to homogenize builds?
- Inability to specify listen port (port exposure is relevant for Docker mapping)
- Needing to call
micro
instead of traditionalnode index.js
equivalent (requires changes in both NPM builds and Dockerfile)
These are by no means hard blockers (I caved yesterday, reverted to non-programmatic build and specialized build for micro), but it's definitely frustrating to not be able to use micro-dev programmatically.
If that's a requirement, why not just use something like Express?
A few soft reasons (in order of high-low weighted relevance)
- Emotionally bought into the zeit ecosystem. I love now & hyper
- Micro-dev is beautiful & simple. Default hot reload and pretty logs save time
- I'm interested in exploring; I've used Express for every microservice prior to this
- Express has always seemed verbose to me
(PS. I see you contributed to hot reloading so thanks for that <3 )
from micro-dev.
Inability to specify listen port (port exposure is relevant for Docker mapping)
You can use micro -p $PORT
to customize the port.
Needing to call micro instead of traditional node index.js equivalent (requires changes in both NPM builds and Dockerfile)
With npm scripts I don't see how this is an issue, you can also use npx micro
to run micro outside an npm script
from micro-dev.
I'm not sure I understand the relevance of npx micro
suggestion. As I pointed out in previous post, I've already configured a specialty build for Micro. The mere fact of needing to alter my standard build/start script in the first place is the problem.
Having said that, given that I do need to specialize the build for micro, the port tip is helpful. Thank you, @sergiodxa
from micro-dev.
I can definitely echo the websockets usecase—need to pass the server instance to socket.io so need to use micro programmatically.
from micro-dev.
So I was making something similar work with tape, which doesn't have built in support for typescript/babel/etc. One of the workarounds to work with typescript (the others include --register
flags like the ones mentioned above) is to call the tape binary from ts-node via ts-node node_modules/tape/bin/tape
. This seems to work fine with micro-dev (as tested with sucrase):
sucrase-node node_modules/micro-dev/bin/micro-dev ./micro.ts -p 3001
Here I'm running a typescript file (types, imports, es7 syntax, etc) with sucrase, and passing args (port) to micro-dev, and it all seems to work as it should. Versions used:
micro: "^9.3.4",
micro-dev: "^3.0.0",
Hopefully that helps someone! Can confirm it's soooooooo much faster and concise than a separate tsc -w
script.
Everybody else gave a use case, mine is that I run barebones mock APIs for my frontend typescript projects. I write my mock data in typescript so I can catch missing fields/typos, which means i can't import my mocks directly into micro without some sort of transpilation. Hence, this.
from micro-dev.
Related Issues (20)
- Restore compatibility with micro@canary
- Update chokidar to v3 HOT 1
- Add cli arguments to documentation
- apollo-server-micro floods console with introspection logs HOT 3
- Provide an option to disable the auto copy to clipboard behaviour
- --ignore option not accepting glob HOT 1
- micro with next js restart every time on next compile
- micro-dev restarts on non-source code changes
- Yargs Parser Security Vulnerability
- Update all dependencies HOT 1
- Add ESLint and move to TypeScript
- print name of module file being used on startup.
- Purpose of SIGUSR2 signal? HOT 1
- Not compatible with `micro` v10.x HOT 3
- Windows node debugger: SyntaxError: missing ) after argument list HOT 1
- micro-dev exits on error HOT 3
- error Command failed with exit code 140 HOT 1
- UnhandledPromiseRejectionWarning thrown when content-type is not sent HOT 1
- add to readme: how to use custom now.json files with now-env
- Disable console logging? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from micro-dev.