Skip to content

Commit

Permalink
fix(babel-plugin-remove-graphql-queries): Correct staticQueryDir de…
Browse files Browse the repository at this point in the history
…fault and improved Storybook support (#38267)

Co-authored-by: LekoArts <[email protected]>
  • Loading branch information
dslovinsky and LekoArts authored Jun 28, 2023
1 parent 7534a2b commit 2fd6623
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ exports.default = _default;"
`;

exports[`babel-plugin-remove-graphql-queries Doesn't add data import for non static queries 1`] = `
"import staticQueryData from \\"public/static/d/426988268.json\\";
"import staticQueryData from \\"public/page-data/sq/d/426988268.json\\";
import * as React from 'react';
import { StaticQuery } from \\"gatsby\\";
const Test = () => /*#__PURE__*/React.createElement(StaticQuery, {
Expand All @@ -49,7 +49,7 @@ exports[`babel-plugin-remove-graphql-queries Doesn't add data import for non sta
var _interopRequireDefault = require(\\"@babel/runtime/helpers/interopRequireDefault\\");
exports.__esModule = true;
exports.default = void 0;
var _ = _interopRequireDefault(require(\\"../../public/static/d/426988268.json\\"));
var _ = _interopRequireDefault(require(\\"../../public/page-data/sq/d/426988268.json\\"));
var React = _interopRequireWildcard(require(\\"react\\"));
var _gatsby = require(\\"gatsby\\");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== \\"function\\") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
Expand All @@ -65,7 +65,7 @@ const fragment = \\"4176178832\\";"
`;

exports[`babel-plugin-remove-graphql-queries Handles closing StaticQuery tag 1`] = `
"import staticQueryData from \\"public/static/d/426988268.json\\";
"import staticQueryData from \\"public/page-data/sq/d/426988268.json\\";
import * as React from 'react';
import { StaticQuery } from 'gatsby';
export default (() => /*#__PURE__*/React.createElement(StaticQuery, {
Expand All @@ -80,7 +80,7 @@ exports[`babel-plugin-remove-graphql-queries Handles closing StaticQuery tag 2`]
var _interopRequireDefault = require(\\"@babel/runtime/helpers/interopRequireDefault\\");
exports.__esModule = true;
exports.default = void 0;
var _ = _interopRequireDefault(require(\\"../../public/static/d/426988268.json\\"));
var _ = _interopRequireDefault(require(\\"../../public/page-data/sq/d/426988268.json\\"));
var React = _interopRequireWildcard(require(\\"react\\"));
var _gatsby = require(\\"gatsby\\");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== \\"function\\") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
Expand Down Expand Up @@ -356,7 +356,7 @@ exports.default = _default;"
`;
exports[`babel-plugin-remove-graphql-queries Transforms queries defined in own variable in <StaticQuery> 1`] = `
"import staticQueryData from \\"public/static/d/426988268.json\\";
"import staticQueryData from \\"public/page-data/sq/d/426988268.json\\";
import * as React from 'react';
import { StaticQuery } from 'gatsby';
const query = \\"426988268\\";
Expand All @@ -373,7 +373,7 @@ exports[`babel-plugin-remove-graphql-queries Transforms queries defined in own v
var _interopRequireDefault = require(\\"@babel/runtime/helpers/interopRequireDefault\\");
exports.__esModule = true;
exports.default = void 0;
var _ = _interopRequireDefault(require(\\"../../public/static/d/426988268.json\\"));
var _ = _interopRequireDefault(require(\\"../../public/page-data/sq/d/426988268.json\\"));
var React = _interopRequireWildcard(require(\\"react\\"));
var _gatsby = require(\\"gatsby\\");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== \\"function\\") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
Expand Down Expand Up @@ -416,7 +416,7 @@ exports.default = _default;"
`;
exports[`babel-plugin-remove-graphql-queries Transforms queries in <StaticQuery> 1`] = `
"import staticQueryData from \\"public/static/d/426988268.json\\";
"import staticQueryData from \\"public/page-data/sq/d/426988268.json\\";
import * as React from 'react';
import { StaticQuery } from 'gatsby';
export default (() => /*#__PURE__*/React.createElement(StaticQuery, {
Expand All @@ -432,7 +432,7 @@ exports[`babel-plugin-remove-graphql-queries Transforms queries in <StaticQuery>
var _interopRequireDefault = require(\\"@babel/runtime/helpers/interopRequireDefault\\");
exports.__esModule = true;
exports.default = void 0;
var _ = _interopRequireDefault(require(\\"../../public/static/d/426988268.json\\"));
var _ = _interopRequireDefault(require(\\"../../public/page-data/sq/d/426988268.json\\"));
var React = _interopRequireWildcard(require(\\"react\\"));
var _gatsby = require(\\"gatsby\\");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== \\"function\\") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
Expand Down Expand Up @@ -600,7 +600,7 @@ const query = \\"426988268\\";"
`;
exports[`babel-plugin-remove-graphql-queries transforms exported variable queries in <StaticQuery> 1`] = `
"import staticQueryData from \\"public/static/d/426988268.json\\";
"import staticQueryData from \\"public/page-data/sq/d/426988268.json\\";
import * as React from 'react';
import { StaticQuery } from 'gatsby';
export const query = \\"426988268\\";
Expand All @@ -617,7 +617,7 @@ exports[`babel-plugin-remove-graphql-queries transforms exported variable querie
var _interopRequireDefault = require(\\"@babel/runtime/helpers/interopRequireDefault\\");
exports.__esModule = true;
exports.query = exports.default = void 0;
var _ = _interopRequireDefault(require(\\"../../public/static/d/426988268.json\\"));
var _ = _interopRequireDefault(require(\\"../../public/page-data/sq/d/426988268.json\\"));
var React = _interopRequireWildcard(require(\\"react\\"));
var _gatsby = require(\\"gatsby\\");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== \\"function\\") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
Expand Down
5 changes: 4 additions & 1 deletion packages/babel-plugin-remove-graphql-queries/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,14 +303,17 @@ export default function ({ types: t }): PluginObj {
JSXIdentifier(path2: NodePath<JSXIdentifier>): void {
if (
(process.env.NODE_ENV === `test` ||
// When Storybook is running, we need to process the queries
process.env.npm_lifecycle_script?.includes(`storybook`) ||
state.opts.stage === `develop-html`) &&
path2.isJSXIdentifier({ name: `StaticQuery` }) &&
path2.referencesImport(`gatsby`, ``) &&
path2.parent.type !== `JSXClosingElement`
) {
const identifier = t.identifier(`staticQueryData`)
const filename = state.file.opts.filename
const staticQueryDir = state.opts.staticQueryDir || `static/d`
const staticQueryDir =
state.opts.staticQueryDir || `page-data/sq/d`
const shortResultPath = `public/${staticQueryDir}/${this.queryHash}.json`
const resultPath = nodePath.join(process.cwd(), shortResultPath)
// Add query
Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby/src/utils/babel-loader-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ export const prepareOptions = (
babel.createConfigItem(
[
resolve(`babel-plugin-remove-graphql-queries`),
// packages/babel-plugin-remove-graphql-queries/src/index.ts sets a default value for staticQueryDir
// They should be identical
{ stage, staticQueryDir: `page-data/sq/d` },
],
{
Expand Down

0 comments on commit 2fd6623

Please sign in to comment.