Update Next config for better imports
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
|
||||||
|
}
|
||||||
|
}
|
@ -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…
Reference in New Issue