UNPKG

16.2 kBSource Map (JSON)View Raw
1{"version":3,"sources":["../../src/query/query-watcher.js"],"names":["_","require","chokidar","path","slash","store","emitter","boundActionCreators","queryCompiler","default","report","queryUtil","debug","getGatsbyDependents","getQueriesSnapshot","state","getState","snapshot","components","Map","staticQueryComponents","handleComponentsWithRemovedQueries","queries","forEach","c","query","has","componentPath","dispatch","type","payload","id","deleteComponentsDependencies","handleQuery","component","isStaticQuery","oldQuery","get","isNewQuery","hash","text","replaceStaticQuery","name","enqueueExtractedQueryId","updateStateAndRunQueries","isFirstRun","parentSpan","then","size","queryExtracted","queriesWillNotRun","queryWillRun","watchComponent","warn","log","stripIndent","runQueuedQueries","clearInactiveComponents","pages","activeTemplates","Set","page","add","exports","extractQueries","process","env","NODE_ENV","watch","program","directory","filesToWatch","watcher","debounceCompile","debounce","rootDir","modulesThatUseGatsby","packagePaths","map","module","filesRegex","pathRegex","join","on","pendingActivity","filePath","startWatchDeletePage","action","otherPageWithTemplateExists","values"],"mappings":";;AAAA;;;;;;;;;AAUA,MAAMA,CAAC,GAAGC,OAAO,CAAE,QAAF,CAAjB;;AACA,MAAMC,QAAQ,GAAGD,OAAO,CAAE,UAAF,CAAxB;;AAEA,MAAME,IAAI,GAAGF,OAAO,CAAE,MAAF,CAApB;;AACA,MAAM;AAAEG,EAAAA;AAAF,IAAYH,OAAO,CAAE,mBAAF,CAAzB;;AAEA,MAAM;AAAEI,EAAAA,KAAF;AAASC,EAAAA;AAAT,IAAqBL,OAAO,CAAE,WAAF,CAAlC;;AACA,MAAM;AAAEM,EAAAA;AAAF,IAA0BN,OAAO,CAAE,kBAAF,CAAvC;;AACA,MAAMO,aAAa,GAAGP,OAAO,CAAE,kBAAF,CAAP,CAA4BQ,OAAlD;;AACA,MAAMC,MAAM,GAAGT,OAAO,CAAE,yBAAF,CAAtB;;AACA,MAAMU,SAAS,GAAGV,OAAO,CAAE,SAAF,CAAzB;;AACA,MAAMW,KAAK,GAAGX,OAAO,CAAE,OAAF,CAA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,CAACW,QAAN,EAAlB;AACA,QAAIwE,2BAA2B,GAAG,KAAlC;;AACA,SAAK,IAAI3B,IAAT,IAAiBH,KAAK,CAAC+B,MAAN,EAAjB,EAAiC;AAC/B,UAAIrF,KAAK,CAACyD,IAAI,CAAC3B,SAAN,CAAL,KAA0BP,aAA9B,EAA6C;AAC3C6D,QAAAA,2BAA2B,GAAG,IAA9B;AACA;AACD;AACF;;AACD,QAAI,CAACA,2BAAL,EAAkC;AAChCnF,MAAAA,KAAK,CAACuB,QAAN,CAAe;AACbC,QAAAA,IAAI,EAAG,2BADM;AAEbC,QAAAA,OAAO,EAAE;AACPH,UAAAA;AADO;AAFI,OAAf;AAMD;AACF,GAlBD;AAmBD,CApBD;;AAsBAoC,OAAO,CAACnB,wBAAR,GAAmCA,wBAAnC","sourcesContent":["/** *\n * Jobs of this module\n * - Maintain the list of components in the Redux store. So monitor new components\n * and add/remove components.\n * - Watch components for query changes and extract these and update the store.\n * - Ensure all page queries are run as part of bootstrap and report back when\n * this is done\n * - Whenever a query changes, re-run all pages that rely on this query.\n ***/\n\nconst _ = require(`lodash`)\nconst chokidar = require(`chokidar`)\n\nconst path = require(`path`)\nconst { slash } = require(`gatsby-core-utils`)\n\nconst { store, emitter } = require(`../redux/`)\nconst { boundActionCreators } = require(`../redux/actions`)\nconst queryCompiler = require(`./query-compiler`).default\nconst report = require(`gatsby-cli/lib/reporter`)\nconst queryUtil = require(`./index`)\nconst debug = require(`debug`)(`gatsby:query-watcher`)\nconst getGatsbyDependents = require(`../utils/gatsby-dependents`)\n\nconst getQueriesSnapshot = () => {\n const state = store.getState()\n\n const snapshot = {\n components: new Map(state.components),\n staticQueryComponents: new Map(state.staticQueryComponents),\n }\n\n return snapshot\n}\n\nconst handleComponentsWithRemovedQueries = (\n { components, staticQueryComponents },\n queries\n) => {\n // If a component had static query and it doesn't have it\n // anymore - update the store\n staticQueryComponents.forEach(c => {\n if (c.query !== `` && !queries.has(c.componentPath)) {\n debug(`Static query was removed from ${c.componentPath}`)\n store.dispatch({\n type: `REMOVE_STATIC_QUERY`,\n payload: c.id,\n })\n boundActionCreators.deleteComponentsDependencies([c.id])\n }\n })\n}\n\nconst handleQuery = (\n { components, staticQueryComponents },\n query,\n component\n) => {\n // If this is a static query\n // Add action / reducer + watch staticquery files\n if (query.isStaticQuery) {\n const oldQuery = staticQueryComponents.get(query.id)\n const isNewQuery = !oldQuery\n\n // Compare query text because text is compiled query with any attached\n // fragments and we want to rerun queries if fragments are edited.\n // Compare hash because hash is used for identyfing query and\n // passing data to component in development. Hash can change if user will\n // format query text, but it doesn't mean that compiled text will change.\n if (\n isNewQuery ||\n oldQuery.hash !== query.hash ||\n oldQuery.query !== query.text\n ) {\n boundActionCreators.replaceStaticQuery({\n name: query.name,\n componentPath: query.path,\n id: query.id,\n query: query.text,\n hash: query.hash,\n })\n\n debug(\n `Static query in ${component} ${\n isNewQuery ? `was added` : `has changed`\n }.`\n )\n\n boundActionCreators.deleteComponentsDependencies([query.id])\n queryUtil.enqueueExtractedQueryId(query.id)\n }\n return true\n }\n\n return false\n}\n\nconst updateStateAndRunQueries = (isFirstRun, { parentSpan } = {}) => {\n const snapshot = getQueriesSnapshot()\n return queryCompiler({ parentSpan }).then(queries => {\n // If there's an error while extracting queries, the queryCompiler returns false\n // or zero results.\n // Yeah, should probably be an error but don't feel like threading the error\n // all the way here.\n if (!queries || queries.size === 0) {\n return null\n }\n handleComponentsWithRemovedQueries(snapshot, queries)\n\n // Run action for each component\n const { components } = snapshot\n components.forEach(c =>\n boundActionCreators.queryExtracted({\n componentPath: c.componentPath,\n query: queries.get(c.componentPath)?.text || ``,\n })\n )\n\n let queriesWillNotRun = false\n queries.forEach((query, component) => {\n const queryWillRun = handleQuery(snapshot, query, component)\n\n if (queryWillRun) {\n watchComponent(component)\n // Check if this is a page component.\n // If it is and this is our first run during bootstrap,\n // show a warning about having a query in a non-page component.\n } else if (isFirstRun && !snapshot.components.has(component)) {\n report.warn(\n `The GraphQL query in the non-page component \"${component}\" will not be run.`\n )\n queriesWillNotRun = true\n }\n })\n\n if (queriesWillNotRun) {\n report.log(report.stripIndent`\n\n Exported queries are only executed for Page components. It's possible you're\n trying to create pages in your gatsby-node.js and that's failing for some\n reason.\n\n If the failing component(s) is a regular component and not intended to be a page\n component, you generally want to use a <StaticQuery> (https://gatsbyjs.org/docs/static-query)\n instead of exporting a page query.\n\n If you're more experienced with GraphQL, you can also export GraphQL\n fragments from components and compose the fragments in the Page component\n query and pass data down into the child component — http://graphql.org/learn/queries/#fragments\n\n `)\n }\n\n queryUtil.runQueuedQueries()\n\n return null\n })\n}\n\n/**\n * Removes components templates that aren't used by any page from redux store.\n */\nconst clearInactiveComponents = () => {\n const { components, pages } = store.getState()\n\n const activeTemplates = new Set()\n pages.forEach(page => {\n // Set will guarantee uniqeness of entires\n activeTemplates.add(slash(page.component))\n })\n\n components.forEach(component => {\n if (!activeTemplates.has(component.componentPath)) {\n debug(\n `${component.componentPath} component was removed because it isn't used by any page`\n )\n store.dispatch({\n type: `REMOVE_TEMPLATE_COMPONENT`,\n payload: component,\n })\n }\n })\n}\n\nexports.extractQueries = ({ parentSpan } = {}) => {\n // Remove template components that point to not existing page templates.\n // We need to do this, because components data is cached and there might\n // be changes applied when development server isn't running. This is needed\n // only in initial run, because during development state will be adjusted.\n clearInactiveComponents()\n\n return updateStateAndRunQueries(true, { parentSpan }).then(() => {\n // During development start watching files to recompile & run\n // queries on the fly.\n if (process.env.NODE_ENV !== `production`) {\n watch(store.getState().program.directory)\n }\n })\n}\n\nconst filesToWatch = new Set()\nlet watcher\nconst watchComponent = componentPath => {\n // We don't start watching until mid-way through the bootstrap so ignore\n // new components being added until then. This doesn't affect anything as\n // when extractQueries is called from bootstrap, we make sure that all\n // components are being watched.\n if (\n process.env.NODE_ENV !== `production` &&\n !filesToWatch.has(componentPath)\n ) {\n filesToWatch.add(componentPath)\n if (watcher) {\n watcher.add(componentPath)\n }\n }\n}\n\nconst debounceCompile = _.debounce(() => {\n updateStateAndRunQueries()\n}, 100)\n\nconst watch = async rootDir => {\n if (watcher) return\n\n const modulesThatUseGatsby = await getGatsbyDependents()\n\n const packagePaths = modulesThatUseGatsby.map(module => {\n const filesRegex = `*.+(t|j)s?(x)`\n const pathRegex = `/{${filesRegex},!(node_modules)/**/${filesRegex}}`\n return slash(path.join(module.path, pathRegex))\n })\n\n watcher = chokidar\n .watch([\n slash(path.join(rootDir, `/src/**/*.{js,jsx,ts,tsx}`)),\n ...packagePaths,\n ])\n .on(`change`, path => {\n report.pendingActivity({ id: `query-extraction` })\n debounceCompile()\n })\n\n filesToWatch.forEach(filePath => watcher.add(filePath))\n}\n\nexports.startWatchDeletePage = () => {\n emitter.on(`DELETE_PAGE`, action => {\n const componentPath = slash(action.payload.component)\n const { pages } = store.getState()\n let otherPageWithTemplateExists = false\n for (let page of pages.values()) {\n if (slash(page.component) === componentPath) {\n otherPageWithTemplateExists = true\n break\n }\n }\n if (!otherPageWithTemplateExists) {\n store.dispatch({\n type: `REMOVE_TEMPLATE_COMPONENT`,\n payload: {\n componentPath,\n },\n })\n }\n })\n}\n\nexports.updateStateAndRunQueries = updateStateAndRunQueries\n"],"file":"query-watcher.js"}
\No newline at end of file