|
|
@ -1,3 +1,4 @@
|
|
|
|
|
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
const path = require('path')
|
|
|
|
const withImages = require('next-images')
|
|
|
|
const withImages = require('next-images')
|
|
|
|
|
|
|
|
|
|
|
@ -12,17 +13,46 @@ class CustomResolver {
|
|
|
|
resolver
|
|
|
|
resolver
|
|
|
|
.getHook(this.source)
|
|
|
|
.getHook(this.source)
|
|
|
|
.tapAsync("CustomResolver", (request, resolveContext, callback) => {
|
|
|
|
.tapAsync("CustomResolver", (request, resolveContext, callback) => {
|
|
|
|
|
|
|
|
|
|
|
|
// request.request is the string path we're resolving
|
|
|
|
// request.request is the string path we're resolving
|
|
|
|
// match ~/[dir]/[component]
|
|
|
|
// match ~/[dir]/[path]
|
|
|
|
const match = request.request.match(/^\~\/([^/]*)\/([^/]*)$/)
|
|
|
|
const match = request.request.match(/^(?:\.\/|)\~\/(?<dir>[^/]*)\/(?<path>.+)$/)
|
|
|
|
if(!match)
|
|
|
|
|
|
|
|
|
|
|
|
const allowedShortcutDirs = [
|
|
|
|
|
|
|
|
'styles',
|
|
|
|
|
|
|
|
'hooks',
|
|
|
|
|
|
|
|
'components'
|
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// If it's not the right pattern, or it's outside our list of directories
|
|
|
|
|
|
|
|
// handle normally
|
|
|
|
|
|
|
|
if(!match || !allowedShortcutDirs.includes(match.groups.dir))
|
|
|
|
return callback();
|
|
|
|
return callback();
|
|
|
|
|
|
|
|
|
|
|
|
// Create a new request, and turn it into
|
|
|
|
const dirPath = match.groups.path.split('/')
|
|
|
|
// ./[dir]/[component]/[component].js
|
|
|
|
const componentDir = dirPath.pop()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const resolvedDir =
|
|
|
|
|
|
|
|
path.join(__dirname, match.groups.dir, dirPath.join('/'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let selectedPath = request.request
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const pathsToCheck = [
|
|
|
|
|
|
|
|
path.join(resolvedDir, componentDir, componentDir + '.js'),
|
|
|
|
|
|
|
|
path.join(resolvedDir, componentDir + '.js'),
|
|
|
|
|
|
|
|
path.join(resolvedDir, componentDir)
|
|
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(const path of pathsToCheck)
|
|
|
|
|
|
|
|
if(fs.existsSync(path)){
|
|
|
|
|
|
|
|
selectedPath = path
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Forge new request to pass to the resolver
|
|
|
|
const newRequest = {
|
|
|
|
const newRequest = {
|
|
|
|
...request,
|
|
|
|
...request,
|
|
|
|
request: path.join(__dirname, match[1], match[2], match[2] + '.js')
|
|
|
|
request: selectedPath
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
resolver.doResolve(target, newRequest, null, resolveContext, callback);
|
|
|
|
resolver.doResolve(target, newRequest, null, resolveContext, callback);
|
|
|
@ -36,4 +66,4 @@ module.exports = withImages({
|
|
|
|
|
|
|
|
|
|
|
|
return config
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|