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 Webpack cache invalidation #230

Merged
merged 1 commit into from
Nov 29, 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
18 changes: 16 additions & 2 deletions packages/cli/src/config/webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import type { NonUndefined } from 'utility-types';
import { GojiWebpackPluginOptions, GojiWebpackPlugin } from '@goji/webpack-plugin';
import nodeLibsBrowser from 'node-libs-browser';
import resolve from 'resolve';
// eslint-disable-next-line import/no-extraneous-dependencies
import { version as gojiCliVersion } from '@goji/cli/package.json';
import { version as babelCoreVersion } from '@babel/core/package.json';
import { version as babelLoaderVersion } from 'babel-loader/package.json';
import postcssConfig from './postcssConfig';
Expand Down Expand Up @@ -36,6 +38,7 @@ const getNodeLibsFallback = () => {

export const getWebpackConfig = ({
basedir,
gojiConfigFile,
outputPath,
target,
nodeEnv,
Expand All @@ -46,6 +49,7 @@ export const getWebpackConfig = ({
parallel,
}: {
basedir: string;
gojiConfigFile?: string;
outputPath?: string;
target: GojiTarget;
nodeEnv: string;
Expand Down Expand Up @@ -121,8 +125,18 @@ export const getWebpackConfig = ({
type: 'filesystem',
idleTimeout: 0,
idleTimeoutForInitialStore: 0,
// prevent re-using cache for different target
version: target,
// prevent re-using cache for different target / GojiJS versions
version: [target, gojiCliVersion].join('-'),
buildDependencies: {
config: [
// `webpack.config.js` should be included
// https://webpack.js.org/configuration/cache/#cachebuilddependencies
// https://github.com/webpack/webpack-cli/blob/ef09fee7185e5c80efd3de1c98260e61f1e63ba0/packages/webpack-cli/src/webpack-cli.ts#L2279-L2300
__filename,
// `goji.config.js` should be included if exists
...(gojiConfigFile ? [gojiConfigFile] : []),
],
},
},
stats: {
preset: 'minimal',
Expand Down
9 changes: 5 additions & 4 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ interface GojiConfig {
}

const GOJI_CONFIG_FILE_NAME = 'goji.config';
const requireGojiConfig = (basedir: string): GojiConfig => {
const requireGojiConfig = (basedir: string): [string | undefined, GojiConfig] => {
let resolvedPath: string;
try {
resolvedPath = resolve.sync(path.join(basedir, GOJI_CONFIG_FILE_NAME));
} catch (error) {
console.info(`\`goji.config.js\` not found in folder ${basedir}, using default config.`);
return {};
return [undefined, {}];
}

// eslint-disable-next-line global-require, import/no-dynamic-require
return require(resolvedPath);
return [resolvedPath, require(resolvedPath)];
};

const main = async () => {
Expand All @@ -49,7 +49,7 @@ const main = async () => {
process.env.NODE_ENV = cliConfig.production ? 'production' : 'development';
process.env.GOJI_TARGET = cliConfig.target;

const gojiConfig = requireGojiConfig(basedir);
const [gojiConfigFile, gojiConfig] = requireGojiConfig(basedir);
// eslint-disable-next-line global-require
const babelConfig = require('./config/babel.config');
if (gojiConfig.configureBabel) {
Expand All @@ -59,6 +59,7 @@ const main = async () => {
const watch = cliConfig.watch ?? gojiConfig.watch ?? !cliConfig.production;
const webpackConfig = getWebpackConfig({
basedir,
gojiConfigFile,
outputPath: gojiConfig.outputPath,
target: cliConfig.target,
nodeEnv: cliConfig.production ? 'production' : 'development',
Expand Down
Loading