diff --git a/packages/babel-plugin-relay/__tests__/BabelPluginRelay-path-test.js b/packages/babel-plugin-relay/__tests__/BabelPluginRelay-path-test.js new file mode 100644 index 0000000000000..96836554754c3 --- /dev/null +++ b/packages/babel-plugin-relay/__tests__/BabelPluginRelay-path-test.js @@ -0,0 +1,46 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + * @oncall relay + */ + +'use strict'; + +describe('`development` option', () => { + function transformOnPlatform(platform: string) { + jest.resetModules(); + + Object.defineProperty(process, 'platform', { + value: platform, + }); + + jest.doMock('path', () => { + if (platform === 'win32') { + return jest.requireActual('path').win32; + } else { + return jest.requireActual('path').posix; + } + }); + + const transformerWithOptions = require('./transformerWithOptions'); + + return transformerWithOptions( + { + artifactDirectory: '/test/artifacts', + }, + 'development', + )('graphql`fragment TestFrag on Node { id }`'); + } + + it('tests the handling of file path', () => { + const codeOnPosix = transformOnPlatform('linux'); + const codeOnNonPosix = transformOnPlatform('win32'); + + expect(codeOnNonPosix).toEqual(codeOnPosix); + expect(codeOnPosix).toMatchSnapshot(); + }); +}); diff --git a/packages/babel-plugin-relay/__tests__/__snapshots__/BabelPluginRelay-path-test.js.snap b/packages/babel-plugin-relay/__tests__/__snapshots__/BabelPluginRelay-path-test.js.snap new file mode 100644 index 0000000000000..3e5b8dc2ba838 --- /dev/null +++ b/packages/babel-plugin-relay/__tests__/__snapshots__/BabelPluginRelay-path-test.js.snap @@ -0,0 +1,15 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`\`development\` option tests the handling of file path 1`] = ` +"var _TestFrag; +_TestFrag !== void 0 + ? _TestFrag + : ((_TestFrag = require('./test/artifacts/TestFrag.graphql')), + _TestFrag.hash && + _TestFrag.hash !== '0bb6b7b29bc3e910921551c4ff5b6757' && + console.error( + \\"The definition of 'TestFrag' appears to have changed. Run \`relay-compiler\` to update the generated files to receive the expected data.\\", + ), + _TestFrag); +" +`; diff --git a/packages/babel-plugin-relay/compileGraphQLTag.js b/packages/babel-plugin-relay/compileGraphQLTag.js index 1c51fc271d73b..7280ec8913e85 100644 --- a/packages/babel-plugin-relay/compileGraphQLTag.js +++ b/packages/babel-plugin-relay/compileGraphQLTag.js @@ -29,6 +29,14 @@ const { const GENERATED = './__generated__/'; +/** + * Converts backslashes in a path to forward slashes (POSIX style) for + * cross-platform compatibility. + */ +function posixifyPath(path: string): string { + return process.platform === 'win32' ? path.replace(/\\/g, '/') : path; +} + /** * Given a graphql`` tagged template literal, replace it with the appropriate * runtime artifact. @@ -107,11 +115,13 @@ function createNode( throw new Error('GraphQL operations and fragments must contain names'); } const requiredFile = definitionName + '.graphql'; - const requiredPath = options.isHasteMode - ? requiredFile - : options.artifactDirectory - ? getRelativeImportPath(state, options.artifactDirectory, requiredFile) - : GENERATED + requiredFile; + const requiredPath = posixifyPath( + options.isHasteMode + ? requiredFile + : options.artifactDirectory + ? getRelativeImportPath(state, options.artifactDirectory, requiredFile) + : GENERATED + requiredFile, + ); const hash = crypto .createHash('md5')