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

add invalidateOnEnvChange to resolver #8103

Merged
merged 10 commits into from May 27, 2022
16 changes: 16 additions & 0 deletions packages/core/core/src/requests/PathRequest.js
Expand Up @@ -67,6 +67,12 @@ async function run({input, api, options}: RunOpts) {
});
let result: ResolverResult = await resolverRunner.resolve(input.dependency);

if (result.invalidateOnEnvChange) {
for (let env of result.invalidateOnEnvChange) {
api.invalidateOnEnvChange(env);
}
}

if (result.invalidateOnFileCreate) {
for (let file of result.invalidateOnFileCreate) {
api.invalidateOnFileCreate(
Expand Down Expand Up @@ -105,6 +111,7 @@ type ResolverResult = {|
assetGroup: ?AssetGroup,
invalidateOnFileCreate?: Array<FileCreateInvalidation>,
invalidateOnFileChange?: Array<FilePath>,
invalidateOnEnvChange?: Array<string>,
diagnostics?: Array<Diagnostic>,
|};

Expand Down Expand Up @@ -185,6 +192,7 @@ export class ResolverRunner {
let diagnostics: Array<Diagnostic> = [];
let invalidateOnFileCreate = [];
let invalidateOnFileChange = [];
let invalidateOnEnvChange = [];
for (let resolver of resolvers) {
try {
let result = await resolver.plugin.resolve({
Expand All @@ -208,6 +216,10 @@ export class ResolverRunner {
dependency.priority = Priority[result.priority];
}

if (result.invalidateOnEnvChange) {
invalidateOnEnvChange.push(...result.invalidateOnEnvChange);
}

if (result.invalidateOnFileCreate) {
invalidateOnFileCreate.push(...result.invalidateOnFileCreate);
}
Expand All @@ -221,6 +233,7 @@ export class ResolverRunner {
assetGroup: null,
invalidateOnFileCreate,
invalidateOnFileChange,
invalidateOnEnvChange,
};
}

Expand Down Expand Up @@ -251,6 +264,7 @@ export class ResolverRunner {
},
invalidateOnFileCreate,
invalidateOnFileChange,
invalidateOnEnvChange,
};
}

Expand Down Expand Up @@ -286,6 +300,7 @@ export class ResolverRunner {
assetGroup: null,
invalidateOnFileCreate,
invalidateOnFileChange,
invalidateOnEnvChange,
};
}

Expand All @@ -308,6 +323,7 @@ export class ResolverRunner {
assetGroup: null,
invalidateOnFileCreate,
invalidateOnFileChange,
invalidateOnEnvChange,
diagnostics,
};
}
Expand Down
@@ -0,0 +1,4 @@
{
"extends": "@parcel/config-default",
"resolvers": ["parcel-resolver-can-invalidateonenvchange"]
}
@@ -0,0 +1 @@
const willBeReplaced = true;

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

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

@@ -0,0 +1,4 @@
{
"name": "test-resolver-invalidateonenevchange",
"private": true
}
20 changes: 20 additions & 0 deletions packages/core/integration-tests/test/plugin.js
Expand Up @@ -190,4 +190,24 @@ parcel-transformer-b`,

assert.equal(await run(b), 2);
});

it('should allow resolvers to invalidateOnEnvChange', async () => {
async function assertAsset(replacedCode) {
let b = await bundle(
path.join(
__dirname,
'/integration/resolver-can-invalidateonenvchange/index.js',
),
{
shouldDisableCache: false,
inputFS: overlayFS,
env: {replacedCode},
},
);
let code = await b.getBundles()[0].getEntryAssets()[0].getCode();
assert(code.indexOf(replacedCode) !== -1);
}
await assertAsset('const replaced = 1;');
await assertAsset('const replaced = 2;');
});
});
2 changes: 2 additions & 0 deletions packages/core/types/index.js
Expand Up @@ -1499,6 +1499,8 @@ export type ResolveResult = {|
+invalidateOnFileCreate?: Array<FileCreateInvalidation>,
/** A list of files that should invalidate the resolution if modified or deleted. */
+invalidateOnFileChange?: Array<FilePath>,
/** Invalidates the resolution when the given environment variable changes.*/
+invalidateOnEnvChange?: Array<string>,
|};

/**
Expand Down