Update Next config for better imports

main
Ashelyn Dawn 5 years ago
parent 20e1fa74f3
commit 690ab42fb8

@ -1,30 +1,32 @@
require('dotenv').config() const express = require('express');
const express = require('express') const next = require('next');
const nextjs = require('next')
const {promisify} = require('util') const port = parseInt(process.env.PORT, 10) || 3000;
const dev = process.env.NODE_ENV !== 'production';
const dev = process.env.NODE_ENV !== 'production'
const next = nextjs({dev}) // Configure SSR axios defaults
const nextRequestHandler = next.getRequestHandler() const axios = require('axios')
const app = express() axios.defaults.baseURL = `http://localhost:${port}`
app.listen = promisify(app.listen.bind(app))
// Set up Express
;(async ()=>{ const server = express();
const app = next({ dev });
// Allow Next.js to compile its templates const handle = app.getRequestHandler();
console.log('Preparing Next.js templates . . .') server.locals.dev = dev
await next.prepare()
// API Routes
const app = express() server.use('/api/', require('./api/'));
app.use('/api', require('./api')) // Let Next.js render pages
app.use(nextRequestHandler) server.all('*', (req, res) => {
return handle(req, res);
await app.listen(3000) });
console.log('App listening on port 3000') // Start server
server.listen(port, err => {
})().catch((ex) => { if (err) throw err;
console.error(ex.stack) console.log(`> Ready on http://localhost:${port}`);
process.exit(1) });
})
// Make sure Next.js gets its crap together
app.prepare();

@ -1,8 +1,38 @@
const path = require('path') const path = require('path')
module.exports = { class CustomResolver {
webpack(config, options) { constructor(source, target){
config.resolve.alias["components"] = path.join(__dirname, 'components'); this.source = source || 'resolve';
return config; this.target = target || 'resolve';
}
apply(resolver) {
const target = resolver.ensureHook(this.target);
resolver
.getHook(this.source)
.tapAsync("CustomResolver", (request, resolveContext, callback) => {
// request.request is the string path we're resolving
// match ~/[dir]/[component]
const match = request.request.match(/^\~\/([^/]*)\/([^/]*)$/)
if(!match)
return callback();
// Create a new request, and turn it into
// ./[dir]/[component]/[component].js
const newRequest = {
...request,
request: path.join(__dirname, match[1], match[2], match[2] + '.js')
}
resolver.doResolve(target, newRequest, null, resolveContext, callback);
});
} }
} }
module.exports = {
webpack: (config) => {
config.resolve.plugins.push(new CustomResolver())
return config
}
}

49
package-lock.json generated

@ -2392,6 +2392,24 @@
"sha.js": "^2.4.8" "sha.js": "^2.4.8"
} }
}, },
"cross-env": {
"version": "7.0.0",
"resolved": "https://registry.npmjs.org/cross-env/-/cross-env-7.0.0.tgz",
"integrity": "sha512-rV6M9ldNgmwP7bx5u6rZsTbYidzwvrwIYZnT08hSGLcQCcggofgFW+sNe7IhA1SRauPS0QuLbbX+wdNtpqE5CQ==",
"requires": {
"cross-spawn": "^7.0.1"
}
},
"cross-spawn": {
"version": "7.0.1",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.1.tgz",
"integrity": "sha512-u7v4o84SwFpD32Z8IIcPZ6z1/ie24O6RU3RbtL5Y316l3KuHVPx9ItBgWQ6VlfAFnRnTtMUrsQ9MUUTuEZjogg==",
"requires": {
"path-key": "^3.1.0",
"shebang-command": "^2.0.0",
"which": "^2.0.1"
}
},
"crypto-browserify": { "crypto-browserify": {
"version": "3.12.0", "version": "3.12.0",
"resolved": "https://registry.npmjs.org/crypto-browserify/-/crypto-browserify-3.12.0.tgz", "resolved": "https://registry.npmjs.org/crypto-browserify/-/crypto-browserify-3.12.0.tgz",
@ -3816,6 +3834,11 @@
"resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz",
"integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE="
}, },
"isexe": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz",
"integrity": "sha1-6PvzdNxVb/iUehDcsFctYz8s+hA="
},
"isobject": { "isobject": {
"version": "3.0.1", "version": "3.0.1",
"resolved": "https://registry.npmjs.org/isobject/-/isobject-3.0.1.tgz", "resolved": "https://registry.npmjs.org/isobject/-/isobject-3.0.1.tgz",
@ -4936,6 +4959,11 @@
"resolved": "https://registry.npmjs.org/path-is-inside/-/path-is-inside-1.0.2.tgz", "resolved": "https://registry.npmjs.org/path-is-inside/-/path-is-inside-1.0.2.tgz",
"integrity": "sha1-NlQX3t5EQw0cEa9hAn+s8HS9/FM=" "integrity": "sha1-NlQX3t5EQw0cEa9hAn+s8HS9/FM="
}, },
"path-key": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/path-key/-/path-key-3.1.1.tgz",
"integrity": "sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q=="
},
"path-parse": { "path-parse": {
"version": "1.0.6", "version": "1.0.6",
"resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.6.tgz", "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.6.tgz",
@ -6276,6 +6304,19 @@
"kind-of": "^6.0.2" "kind-of": "^6.0.2"
} }
}, },
"shebang-command": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz",
"integrity": "sha512-kHxr2zZpYtdmrN1qDjrrX/Z1rR1kG8Dx+gkpK1G4eXmvXswmcE1hTWBWYUzlraYw1/yZp6YuDY77YtvbN0dmDA==",
"requires": {
"shebang-regex": "^3.0.0"
}
},
"shebang-regex": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/shebang-regex/-/shebang-regex-3.0.0.tgz",
"integrity": "sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A=="
},
"shell-quote": { "shell-quote": {
"version": "1.7.2", "version": "1.7.2",
"resolved": "https://registry.npmjs.org/shell-quote/-/shell-quote-1.7.2.tgz", "resolved": "https://registry.npmjs.org/shell-quote/-/shell-quote-1.7.2.tgz",
@ -7914,6 +7955,14 @@
"resolved": "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-3.0.0.tgz", "resolved": "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-3.0.0.tgz",
"integrity": "sha512-9GSJUgz1D4MfyKU7KRqwOjXCXTqWdFNvEr7eUBYchQiVc744mqK/MzXPNR2WsPkmkOa4ywfg8C2n8h+13Bey1Q==" "integrity": "sha512-9GSJUgz1D4MfyKU7KRqwOjXCXTqWdFNvEr7eUBYchQiVc744mqK/MzXPNR2WsPkmkOa4ywfg8C2n8h+13Bey1Q=="
}, },
"which": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz",
"integrity": "sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==",
"requires": {
"isexe": "^2.0.0"
}
},
"wide-align": { "wide-align": {
"version": "1.1.3", "version": "1.1.3",
"resolved": "https://registry.npmjs.org/wide-align/-/wide-align-1.1.3.tgz", "resolved": "https://registry.npmjs.org/wide-align/-/wide-align-1.1.3.tgz",

@ -4,9 +4,9 @@
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"start": "node index.js", "dev": "cross-env NODE_ENV=development node index.js",
"dev": "next dev", "build": "next build",
"build": "next build" "start": "cross-env NODE_ENV=production node index.js"
}, },
"author": "", "author": "",
"license": "ISC", "license": "ISC",
@ -15,6 +15,7 @@
"axios": "^0.19.2", "axios": "^0.19.2",
"bcrypt": "^3.0.8", "bcrypt": "^3.0.8",
"body-parser": "^1.19.0", "body-parser": "^1.19.0",
"cross-env": "^7.0.0",
"debug": "^4.1.1", "debug": "^4.1.1",
"dotenv": "^8.2.0", "dotenv": "^8.2.0",
"express": "^4.17.1", "express": "^4.17.1",

@ -1,10 +1,9 @@
import React from "react" import React from "react"
import PropTypes from "prop-types" import PropTypes from "prop-types"
import Header from 'components/header' import Header from '~/components/header'
import Footer from 'components/footer' import Footer from '~/components/footer'
// import "./layout.css" import "../styles/layout.css"
// import useUserSession from "../hooks/useUserSession";
const Layout = ({ Component, pageProps }) => { const Layout = ({ Component, pageProps }) => {
// Retrieve user information // Retrieve user information

@ -1,24 +0,0 @@
import Document, { Html, Head, Main, NextScript } from 'next/document'
class MyDocument extends Document {
static async getInitialProps(ctx) {
const initialProps = await Document.getInitialProps(ctx)
return { ...initialProps }
}
render() {
return (
<Html>
<Head>
<link rel="stylesheet" href="/layout.css" />
</Head>
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}
export default MyDocument
Loading…
Cancel
Save