How do I compile Typescript at Heroku postinstall?

前端 未结 2 1452
故里飘歌
故里飘歌 2021-02-19 19:20

Instead of uploading the precompiled dist directory, I want to compile src at server side instead.

Here are my scripts inside package.json:

\"scripts\":          


        
2条回答
  •  盖世英雄少女心
    2021-02-19 19:40

    Spent a while to deploy my simple typescript create-react-app to Heroku. Here what worked for me.

    package.json - does not need postinstall at all

    In the command line install buildpack for your application run: heroku buildpacks:add zidizei/typescript heroku buildpacks:add heroku/nodejs

    You can also search for buildpacks run: heroku buildpacks:search typescript

    My server looks like that (/root/server.js)

    const express = require('express')
    const app = express()
    const PORT = process.env.PORT || 3000
    
    app.use(express.static('build'));
    app.listen(PORT, () => console.log(`Listening on port ${PORT}`))
    

    package.json

     "scripts": {
        "start": "node server.js",
        "build": "react-scripts build",
        "test": "react-scripts test",
        "eject": "react-scripts eject"
      }
    

    Also before pushing to heroku, do run 'npm run build'.

    My solution wont work if you gonna use webpack dev server, it must be custom server, in my case it was EXPRESS.

提交回复
热议问题