Skip to content

Commit

Permalink
fix: Move murmurhash to gatsby-core-utils (#36882)
Browse files Browse the repository at this point in the history
  • Loading branch information
LekoArts committed Oct 25, 2022
1 parent 7f1b57c commit aaaa8c5
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 30 deletions.
2 changes: 1 addition & 1 deletion integration-tests/artifacts/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { spawn } = require(`child_process`)
const path = require(`path`)
const { murmurhash } = require(`babel-plugin-remove-graphql-queries`)
const { murmurhash } = require(`gatsby-core-utils/murmurhash`)
const { readPageData } = require(`gatsby/dist/utils/page-data`)
const { stripIgnoredCharacters } = require(`gatsby/graphql`)
const fs = require(`fs-extra`)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const babel = require(`@babel/core`)
const plugin = require(`../`)
import { transform as babelTransform } from "@babel/core"
import plugin from "../"

function matchesSnapshot(query) {
const { code: codeWithoutFileName } = babel.transform(query, {
function matchesSnapshot(query): void {
// @ts-ignore - code exists
const { code: codeWithoutFileName } = babelTransform(query, {
presets: [`@babel/preset-react`],
plugins: [plugin],
})
const { code: codeWithFileName } = babel.transform(query, {
// @ts-ignore - code exists
const { code: codeWithFileName } = babelTransform(query, {
presets: [`@babel/preset-react`],
plugins: [plugin],
filename: `src/components/test.js`,
Expand All @@ -16,11 +18,11 @@ function matchesSnapshot(query) {
expect(codeWithFileName).toMatchSnapshot()
}

function transform(query, filename) {
const { code } = babel.transform(query, {
function transform(query): string | null | undefined {
// @ts-ignore - code exists
const { code } = babelTransform(query, {
presets: [`@babel/preset-react`],
plugins: [plugin],
filename,
})
return code
}
Expand Down Expand Up @@ -499,8 +501,9 @@ describe(`babel-plugin-remove-graphql-queries`, () => {
})

it(`validates that config export is async`, () => {
const run = () =>
transform(`
const run = (): any =>
transform(
`
import * as React from 'react'
import { graphql } from "gatsby"
Expand All @@ -519,7 +522,8 @@ describe(`babel-plugin-remove-graphql-queries`, () => {
}
}
}
`)
`
)
expect(run).toThrowErrorMatchingInlineSnapshot(
`"unknown file: BabelPluginRemoveGraphQLQueries: the \\"config\\" export must be async when using it with graphql"`
)
Expand Down
5 changes: 2 additions & 3 deletions packages/babel-plugin-remove-graphql-queries/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-disable no-unused-expressions */
/* eslint-disable new-cap */
import graphql from "gatsby/graphql"
import { murmurhash } from "./murmur"
import nodePath from "path"
import { NodePath, PluginObj } from "@babel/core"
import { slash } from "gatsby-core-utils"
import { slash } from "gatsby-core-utils/path"
import { murmurhash } from "gatsby-core-utils/murmurhash"
import { Binding } from "babel__traverse"
import {
CallExpression,
Expand Down Expand Up @@ -639,5 +639,4 @@ export {
GraphQLSyntaxError,
ExportIsNotAsyncError,
isWithinConfigExport,
murmurhash,
}
17 changes: 17 additions & 0 deletions packages/gatsby-core-utils/src/__tests__/murmurhash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { murmurhash } from "../murmurhash"

describe(`murmurhash`, () => {
it(`returns a hash for a string`, () => {
expect(murmurhash(`foo`, 0)).toEqual(2414502773)
})
it(`returns same hash for same input string and seed`, () => {
const result = murmurhash(`foo`, 0)
expect(murmurhash(`foo`, 0)).toEqual(result)
})
it(`returns different hashes for same input string and different seed`, () => {
const one = murmurhash(`foo`, 1)
const two = murmurhash(`foo`, 2)
expect(two).not.toEqual(one)
expect(two).toEqual(1907694736)
})
})
1 change: 1 addition & 0 deletions packages/gatsby-core-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ export { listPlugins } from "./list-plugins"
export { createFilePath } from "./filename-utils"
export { readConfigFile, getConfigPath } from "./utils"
export { lock } from "./lock"
export { murmurhash } from "./murmurhash"

export type { IFetchRemoteFileOptions } from "./fetch-remote-file"
4 changes: 2 additions & 2 deletions packages/gatsby-plugin-image/src/babel-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { murmurhash } from "babel-plugin-remove-graphql-queries/murmur"
import { murmurhash } from "gatsby-core-utils/murmurhash"
import { JSXOpeningElement } from "@babel/types"
import { NodePath } from "@babel/core"
import { getAttributeValues } from "babel-jsx-utils"
Expand Down Expand Up @@ -60,5 +60,5 @@ export function evaluateImageAttributes(
}

export function hashOptions(options: unknown): string {
return `${murmurhash(JSON.stringify(options))}`
return `${murmurhash(JSON.stringify(options), 0)}`
}
4 changes: 2 additions & 2 deletions packages/gatsby/src/redux/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const path = require(`path`)
const v8 = require(`v8`)
const telemetry = require(`gatsby-telemetry`)
const reporter = require(`gatsby-cli/lib/reporter`)
const { murmurhash } = require(`babel-plugin-remove-graphql-queries/murmur`)
const { murmurhash } = require(`gatsby-core-utils/murmurhash`)
const writeToCache = jest.spyOn(require(`../persist`), `writeToCache`)
const v8Serialize = jest.spyOn(v8, `serialize`)
const v8Deserialize = jest.spyOn(v8, `deserialize`)
Expand Down Expand Up @@ -87,7 +87,7 @@ jest.mock(`glob`, () => {
}),
}
})
jest.mock(`babel-plugin-remove-graphql-queries/murmur`)
jest.mock(`gatsby-core-utils/murmurhash`)

function getFakeNodes() {
// Set nodes to something or the cache will fail because it asserts this
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby/src/redux/__tests__/pages.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { readFile } from "fs-extra"
import { murmurhash } from "babel-plugin-remove-graphql-queries/murmur"
import { murmurhash } from "gatsby-core-utils/murmurhash"

jest.mock(`fs-extra`, () => {
return {
readFile: jest.fn(() => `contents`),
readFileSync: jest.fn(() => `foo`), // createPage action reads the page template file trying to find `getServerData`
}
})
jest.mock(`babel-plugin-remove-graphql-queries/murmur`)
jest.mock(`gatsby-core-utils/murmurhash`)
import glob from "glob"

import { pagesReducer as reducer } from "../reducers/pages"
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby/src/utils/__tests__/js-chunk-names.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { murmurhash } from "babel-plugin-remove-graphql-queries/murmur"
import { murmurhash } from "gatsby-core-utils/murmurhash"
import { generateComponentChunkName } from "../js-chunk-names"

jest.mock(`../../redux`, () => {
Expand All @@ -13,7 +13,7 @@ jest.mock(`../../redux`, () => {
}
})

jest.mock(`babel-plugin-remove-graphql-queries/murmur`)
jest.mock(`gatsby-core-utils/murmurhash`)

describe(`js-chunk-names`, () => {
beforeEach(() => {
Expand Down
12 changes: 7 additions & 5 deletions packages/gatsby/src/utils/js-chunk-names.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import memoize from "memoizee"
import { kebabCase as _kebabCase } from "lodash"
import { murmurhash as _murmurhash } from "babel-plugin-remove-graphql-queries/murmur"
import { murmurhash as _murmurhash } from "gatsby-core-utils/murmurhash"
import path from "path"
import { store } from "../redux"

const kebabCase = memoize(_kebabCase)
const pathRelative = memoize(path.relative)
const murmurhash = memoize(_murmurhash)
const kebabCase: (string?: string) => string = memoize(_kebabCase)
const pathRelative: (from: string, to: string) => string = memoize(
path.relative
)
const murmurhash: (str: string, seed: number) => number = memoize(_murmurhash)

// unified routes adds support for files with [] and {},
// the problem with our generateComponentChunkName is that when you
Expand Down Expand Up @@ -64,7 +66,7 @@ export function generateComponentChunkName(
* To prevent long file name errors, we truncate the name to a maximum of 60 characters.
*/
if (shouldTruncate) {
const hash = murmurhash(name)
const hash = murmurhash(name, 0)
name = `${hash}-${name.substring(name.length - 60)}`
}

Expand Down
5 changes: 3 additions & 2 deletions packages/gatsby/src/utils/sample-site-for-experiment.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { getRepositoryId } from "gatsby-telemetry/lib/repository-id"
import { murmurhash } from "babel-plugin-remove-graphql-queries"
import { murmurhash } from "gatsby-core-utils/murmurhash"

const sampleSite = (experimentName: string, percentage: number): boolean => {
const bucketNumber =
murmurhash(
experimentName + `` + JSON.stringify(getRepositoryId().repositoryId)
experimentName + `` + JSON.stringify(getRepositoryId().repositoryId),
0
) % 100

return bucketNumber < percentage
Expand Down

0 comments on commit aaaa8c5

Please sign in to comment.