From c4e5297ba0882a4cc5a14583b74243d357245bbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Tue, 21 Apr 2020 12:00:06 +0200 Subject: [PATCH] More stuff according to article --- packages/backend/.eslintrc.js | 5 ++ packages/backend/src/index.ts | 80 ++++++++++++++++++++++++++++++ packages/backend/webpack.config.ts | 48 ++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 packages/backend/.eslintrc.js create mode 100644 packages/backend/src/index.ts create mode 100644 packages/backend/webpack.config.ts diff --git a/packages/backend/.eslintrc.js b/packages/backend/.eslintrc.js new file mode 100644 index 0000000000..182a6df30a --- /dev/null +++ b/packages/backend/.eslintrc.js @@ -0,0 +1,5 @@ +module.exports = { + rules: { + 'no-console': 0, + }, +}; diff --git a/packages/backend/src/index.ts b/packages/backend/src/index.ts new file mode 100644 index 0000000000..879f762893 --- /dev/null +++ b/packages/backend/src/index.ts @@ -0,0 +1,80 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Required External Modules + */ + +import * as dotenv from 'dotenv'; +import express from 'express'; +import cors from 'cors'; +import helmet from 'helmet'; + +dotenv.config(); + +/** + * App Variables + */ + +if (!process.env.PORT) { + process.exit(1); +} + +const PORT: number = parseInt(process.env.PORT as string, 10); + +const app = express(); + +/** + * App Configuration + */ + +app.use(helmet()); +app.use(cors()); +app.use(express.json()); + +/** + * Server Activation + */ + +const server = app.listen(PORT, () => { + console.log(`Listening on port ${PORT}`); +}); + +/** + * Webpack HMR Activation + */ + +type ModuleId = string | number; + +interface WebpackHotModule { + hot?: { + data: any; + accept( + dependencies: string[], + callback?: (updatedDependencies: ModuleId[]) => void, + ): void; + accept(dependency: string, callback?: () => void): void; + accept(errHandler?: (err: Error) => void): void; + dispose(callback: (data: any) => void): void; + }; +} + +declare const module: WebpackHotModule; + +if (module.hot) { + module.hot.accept(); + module.hot.dispose(() => server.close()); +} diff --git a/packages/backend/webpack.config.ts b/packages/backend/webpack.config.ts new file mode 100644 index 0000000000..d1bf16a809 --- /dev/null +++ b/packages/backend/webpack.config.ts @@ -0,0 +1,48 @@ +/* + * Copyright 2020 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const webpack = require('webpack'); +const path = require('path'); +const nodeExternals = require('webpack-node-externals'); + +module.exports = { + entry: ['webpack/hot/poll?100', './src/index.ts'], + watch: true, + target: 'node', + externals: [ + nodeExternals({ + whitelist: ['webpack/hot/poll?100'], + }), + ], + module: { + rules: [ + { + test: /.tsx?$/, + use: 'ts-loader', + exclude: /node_modules/, + }, + ], + }, + mode: 'development', + resolve: { + extensions: ['.tsx', '.ts', '.js'], + }, + plugins: [new webpack.HotModuleReplacementPlugin()], + output: { + path: path.join(__dirname, 'dist'), + filename: 'index.js', + }, +};