From 4ef6c84377daf18b97b2d36a6ee17e9e17916f2d Mon Sep 17 00:00:00 2001 From: Aleksandar Cakalic Date: Thu, 14 Nov 2024 13:14:36 +0100 Subject: [PATCH 1/2] feat: add script for hot reloading local packages --- next.config.mjs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/next.config.mjs b/next.config.mjs index 62ea1a8..13e2956 100644 --- a/next.config.mjs +++ b/next.config.mjs @@ -1,5 +1,7 @@ /** @type {import('next').NextConfig} */ import p from "./package.json" assert { type: "json" } +import path from "path" +import { fileURLToPath } from "url" const starknetkitNextVersion = Object.entries(p.dependencies) .find((dep) => dep[0] === "starknetkit-next")[1] @@ -24,6 +26,13 @@ const starknetReactNextVersion = Object.entries(p.dependencies) .replace("npm:", "") .split("@")[2] +const localPackages = [ + { + name: "starknetkit-next", + path: "../starknetkit", + }, +] + const nextConfig = { productionBrowserSourceMaps: true, env: { @@ -32,6 +41,44 @@ const nextConfig = { starknetReactVersion, starknetReactNextVersion, }, + webpack: (config, { dev, webpack }) => { + if (!dev) { + return config + } + + const __filename = fileURLToPath(import.meta.url) + const __dirname = path.dirname(__filename) + + config.resolve.alias = { + ...config.resolve.alias, + ...localPackages.reduce( + (aliases, pkg) => ({ + ...aliases, + [pkg.name]: path.resolve(__dirname, pkg.path), + [`${pkg.name}/(.*)`]: path.resolve(__dirname, `${pkg.path}/$1`), + }), + {}, + ), + } + + localPackages.forEach((pkg) => { + 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 From 67be02151813c5067ac85fc891a273cc8f31b284 Mon Sep 17 00:00:00 2001 From: Aleksandar Cakalic Date: Thu, 14 Nov 2024 13:23:29 +0100 Subject: [PATCH 2/2] fix: skip hot reloading packages that are not locally imported --- next.config.mjs | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/next.config.mjs b/next.config.mjs index 13e2956..042df26 100644 --- a/next.config.mjs +++ b/next.config.mjs @@ -2,6 +2,9 @@ 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] @@ -48,20 +51,27 @@ const nextConfig = { 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) => ({ - ...aliases, - [pkg.name]: path.resolve(__dirname, pkg.path), - [`${pkg.name}/(.*)`]: path.resolve(__dirname, `${pkg.path}/$1`), - }), - {}, - ), + ...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}(\\/.*)?$`),