Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: env caching #578

Merged
merged 4 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tame-squids-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@headstartwp/next": patch
---

Fix an annoying bug that would require deleting the .next/cache folder after changing headless.config.js or .env files. Now you only need to restart the next.js server after changing those files.
105 changes: 105 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions packages/next/src/config/withHeadlessConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ConfigError, HeadlessConfig } from '@headstartwp/core';
import { NextConfig } from 'next';
import { ModifySourcePlugin, ConcatOperation } from 'modify-source-webpack-plugin';
import path from 'path';
import fs from 'fs';

const LINARIA_EXTENSION = '.linaria.module.css';

Expand Down Expand Up @@ -156,6 +158,32 @@ export function withHeadlessConfig(
setHeadstartWPConfig(${JSON.stringify(headlessConfig)});
`;

// clear webpack cache whenever headless.config.js changes or one of the env files
if (Array.isArray(config.cache.buildDependencies.config)) {
const [nextConfigPath] = config.cache.buildDependencies.config;

const headlessConfigPath = path.resolve(nextConfigPath, '../headless.config.js');
const envLocalPath = path.resolve(nextConfigPath, '../.env.local');
const envPath = path.resolve(nextConfigPath, '../.env');
const envDevPath = path.resolve(nextConfigPath, '../.env.development');

if (fs.existsSync(headlessConfigPath)) {
config.cache.buildDependencies.config.push(headlessConfigPath);
}

if (fs.existsSync(envLocalPath)) {
config.cache.buildDependencies.config.push(envLocalPath);
}

if (fs.existsSync(envPath)) {
config.cache.buildDependencies.config.push(envPath);
}

if (fs.existsSync(envDevPath)) {
config.cache.buildDependencies.config.push(envDevPath);
}
}

config.plugins.push(
new ModifySourcePlugin({
rules: [
Expand All @@ -164,6 +192,7 @@ export function withHeadlessConfig(
if (!withHeadlessConfigOptions.injectConfig) {
return false;
}

const userRequest = normalModule.userRequest || '';

const startIndex =
Expand All @@ -175,6 +204,11 @@ export function withHeadlessConfig(
.substring(startIndex)
.replace(/\\/g, '/');

// skip next/dist/pages/_app.js
if (/next\/dist\/pages\/_app.js/.test(moduleRequest)) {
return false;
}

const matched =
/_app.(tsx|ts|js|mjs|jsx)$/.test(moduleRequest) ||
/middleware.(ts|js|mjs)$/.test(moduleRequest) ||
Expand Down
Loading