Skip to content

Commit

Permalink
Merge pull request #57 from argentlabs/feat/hot-reload-local-packages
Browse files Browse the repository at this point in the history
Feat: Hot reload local packages
  • Loading branch information
Cussone authored Nov 14, 2024
2 parents 6efdafa + 67be021 commit 4149d8b
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
/** @type {import('next').NextConfig} */
import p from "./package.json" assert { type: "json" }
import path from "path"
import { fileURLToPath } from "url"
import { createRequire } from "module"

const require = createRequire(import.meta.url)

const starknetkitNextVersion = Object.entries(p.dependencies)
.find((dep) => dep[0] === "starknetkit-next")[1]
Expand All @@ -24,6 +29,13 @@ const starknetReactNextVersion = Object.entries(p.dependencies)
.replace("npm:", "")
.split("@")[2]

const localPackages = [
{
name: "starknetkit-next",
path: "../starknetkit",
},
]

const nextConfig = {
productionBrowserSourceMaps: true,
env: {
Expand All @@ -32,6 +44,51 @@ const nextConfig = {
starknetReactVersion,
starknetReactNextVersion,
},
webpack: (config, { dev, webpack }) => {
if (!dev) {
return config
}

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const packageJson = require(path.join(__dirname, "./package.json"))

config.resolve.alias = {
...config.resolve.alias,
...localPackages.reduce((aliases, pkg) => {
if (packageJson.dependencies[pkg.name].startsWith("file:")) {
return {
...aliases,
[pkg.name]: path.resolve(__dirname, pkg.path),
[`${pkg.name}/(.*)`]: path.resolve(__dirname, `${pkg.path}/$1`),
}
}

return { ...aliases }
}, {}),
}

localPackages.forEach((pkg) => {
if (!packageJson.dependencies[pkg.name].startsWith("file:")) {
return
}
config.plugins.push(
new webpack.NormalModuleReplacementPlugin(
new RegExp(`^${pkg.name}(\\/.*)?$`),
(resource) => {
const requestPath = resource.request.replace(pkg.name, "")
resource.request = path.resolve(
__dirname,
`${pkg.path}${requestPath}`,
)
resource.context = path.dirname(resource.request)
},
),
)
})

return config
},
}

export default nextConfig

0 comments on commit 4149d8b

Please sign in to comment.