Update Next config for better imports
parent
20e1fa74f3
commit
690ab42fb8
@ -1,30 +1,32 @@
|
||||
require('dotenv').config()
|
||||
const express = require('express')
|
||||
const nextjs = require('next')
|
||||
const {promisify} = require('util')
|
||||
|
||||
const dev = process.env.NODE_ENV !== 'production'
|
||||
const next = nextjs({dev})
|
||||
const nextRequestHandler = next.getRequestHandler()
|
||||
const app = express()
|
||||
app.listen = promisify(app.listen.bind(app))
|
||||
|
||||
;(async ()=>{
|
||||
|
||||
// Allow Next.js to compile its templates
|
||||
console.log('Preparing Next.js templates . . .')
|
||||
await next.prepare()
|
||||
|
||||
const app = express()
|
||||
|
||||
app.use('/api', require('./api'))
|
||||
app.use(nextRequestHandler)
|
||||
|
||||
await app.listen(3000)
|
||||
|
||||
console.log('App listening on port 3000')
|
||||
|
||||
})().catch((ex) => {
|
||||
console.error(ex.stack)
|
||||
process.exit(1)
|
||||
})
|
||||
const express = require('express');
|
||||
const next = require('next');
|
||||
|
||||
const port = parseInt(process.env.PORT, 10) || 3000;
|
||||
const dev = process.env.NODE_ENV !== 'production';
|
||||
|
||||
// Configure SSR axios defaults
|
||||
const axios = require('axios')
|
||||
axios.defaults.baseURL = `http://localhost:${port}`
|
||||
|
||||
// Set up Express
|
||||
const server = express();
|
||||
const app = next({ dev });
|
||||
const handle = app.getRequestHandler();
|
||||
server.locals.dev = dev
|
||||
|
||||
// API Routes
|
||||
server.use('/api/', require('./api/'));
|
||||
|
||||
// Let Next.js render pages
|
||||
server.all('*', (req, res) => {
|
||||
return handle(req, res);
|
||||
});
|
||||
|
||||
// Start server
|
||||
server.listen(port, err => {
|
||||
if (err) throw err;
|
||||
console.log(`> Ready on http://localhost:${port}`);
|
||||
});
|
||||
|
||||
// Make sure Next.js gets its crap together
|
||||
app.prepare();
|
||||
|
@ -1,8 +1,38 @@
|
||||
const path = require('path')
|
||||
|
||||
module.exports = {
|
||||
webpack(config, options) {
|
||||
config.resolve.alias["components"] = path.join(__dirname, 'components');
|
||||
return config;
|
||||
class CustomResolver {
|
||||
constructor(source, target){
|
||||
this.source = source || 'resolve';
|
||||
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