This repository has been archived by the owner on Apr 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add react with elm build support (#155)
* Add react with elm build support
- Loading branch information
Showing
3 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!/usr/bin/env node | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const ElmPlugin = require('esbuild-plugin-elm'); | ||
const EnvFilePlugin = require('./lib/env.js'); | ||
|
||
require('esbuild') | ||
.build({ | ||
entryPoints: ['web/ts/index.ts', 'web/react/index.tsx'], | ||
outdir: 'dist/js', | ||
entryNames: '[dir]/[name]-[hash]', | ||
allowOverwrite: true, | ||
bundle: true, | ||
metafile: true, | ||
minify: true, | ||
plugins: [EnvFilePlugin, ElmPlugin({ pathToElm: 'paack-elm-wrapper' })], | ||
}) | ||
.catch(() => process.exit(1)) | ||
.then((result) => { | ||
const htmlPath = 'dist/index.html'; | ||
|
||
fs.copyFile('web/index.html', htmlPath, console.log); | ||
|
||
fs.readFile(htmlPath, 'utf8', (err, data) => { | ||
if (err) return console.error(err); | ||
|
||
const path = Object.keys(result.metafile.outputs)[0].replace('dist/', ''); | ||
const reactPath = Object.keys(result.metafile.outputs)[1].replace( | ||
'dist/', | ||
'', | ||
); | ||
fs.writeFile( | ||
htmlPath, | ||
data | ||
.replace('/ts/index.js', path) | ||
.replace('/react/index.js', reactPath), | ||
'utf8', | ||
console.log, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!/usr/bin/env node | ||
const ElmPlugin = require('esbuild-plugin-elm'); | ||
const EnvFilePlugin = require('./lib/env'); | ||
const http = require('http'); | ||
|
||
require('esbuild') | ||
.serve( | ||
{ | ||
servedir: 'web', | ||
}, | ||
{ | ||
entryPoints: ['web/ts/index.ts', 'web/react/index.tsx'], | ||
outdir: 'web', | ||
bundle: true, | ||
plugins: [EnvFilePlugin, ElmPlugin({ debug: true })], | ||
}, | ||
) | ||
.then((esbuildServer) => { | ||
const { host, port } = esbuildServer; | ||
|
||
http | ||
.createServer((req, res) => { | ||
const forwardRequest = (path) => { | ||
const options = { | ||
hostname: host, | ||
port, | ||
path, | ||
method: req.method, | ||
headers: req.headers, | ||
}; | ||
|
||
const proxyReq = http.request(options, (proxyRes) => { | ||
if (proxyRes.statusCode === 404) { | ||
// If esbuild 404s the request, assume it's a route needing to | ||
// be handled by the JS bundle, so forward a second attempt to `/`. | ||
// https://gist.github.com/martinrue/2896becdb8a5ed81761e11ff2ea5898e | ||
return forwardRequest('/'); | ||
} | ||
|
||
res.writeHead(proxyRes.statusCode, proxyRes.headers); | ||
proxyRes.pipe(res, { end: true }); | ||
}); | ||
|
||
req.pipe(proxyReq, { end: true }); | ||
}; | ||
|
||
forwardRequest(req.url); | ||
}) | ||
.listen(1234); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters