Skip to content

Commit 9f135b8

Browse files
artolafacebook-github-bot
authored andcommitted
fix path on Windows (#4544)
Summary: - closes #3272 sibelius, here is a PR addressing this long-standing issue. This is a follow-up to your [comment](#3272 (comment)). Pull Request resolved: #4544 Reviewed By: captbaritone, tyao1 Differential Revision: D52567994 Pulled By: alunyov fbshipit-source-id: b5be2c7d70107ba6dca2daf538e94281a1eac826
1 parent 921f2a8 commit 9f135b8

File tree

3 files changed

+76
-5
lines changed

3 files changed

+76
-5
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
* @oncall relay
9+
*/
10+
11+
'use strict';
12+
13+
describe('`development` option', () => {
14+
function transformOnPlatform(platform: string) {
15+
jest.resetModules();
16+
17+
Object.defineProperty(process, 'platform', {
18+
value: platform,
19+
});
20+
21+
jest.doMock('path', () => {
22+
if (platform === 'win32') {
23+
return jest.requireActual('path').win32;
24+
} else {
25+
return jest.requireActual('path').posix;
26+
}
27+
});
28+
29+
const transformerWithOptions = require('./transformerWithOptions');
30+
31+
return transformerWithOptions(
32+
{
33+
artifactDirectory: '/test/artifacts',
34+
},
35+
'development',
36+
)('graphql`fragment TestFrag on Node { id }`');
37+
}
38+
39+
it('tests the handling of file path', () => {
40+
const codeOnPosix = transformOnPlatform('linux');
41+
const codeOnNonPosix = transformOnPlatform('win32');
42+
43+
expect(codeOnNonPosix).toEqual(codeOnPosix);
44+
expect(codeOnPosix).toMatchSnapshot();
45+
});
46+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`\`development\` option tests the handling of file path 1`] = `
4+
"var _TestFrag;
5+
_TestFrag !== void 0
6+
? _TestFrag
7+
: ((_TestFrag = require('./test/artifacts/TestFrag.graphql')),
8+
_TestFrag.hash &&
9+
_TestFrag.hash !== '0bb6b7b29bc3e910921551c4ff5b6757' &&
10+
console.error(
11+
\\"The definition of 'TestFrag' appears to have changed. Run \`relay-compiler\` to update the generated files to receive the expected data.\\",
12+
),
13+
_TestFrag);
14+
"
15+
`;

packages/babel-plugin-relay/compileGraphQLTag.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ const {
2929

3030
const GENERATED = './__generated__/';
3131

32+
/**
33+
* Converts backslashes in a path to forward slashes (POSIX style) for
34+
* cross-platform compatibility.
35+
*/
36+
function posixifyPath(path: string): string {
37+
return process.platform === 'win32' ? path.replace(/\\/g, '/') : path;
38+
}
39+
3240
/**
3341
* Given a graphql`` tagged template literal, replace it with the appropriate
3442
* runtime artifact.
@@ -107,11 +115,13 @@ function createNode(
107115
throw new Error('GraphQL operations and fragments must contain names');
108116
}
109117
const requiredFile = definitionName + '.graphql';
110-
const requiredPath = options.isHasteMode
111-
? requiredFile
112-
: options.artifactDirectory
113-
? getRelativeImportPath(state, options.artifactDirectory, requiredFile)
114-
: GENERATED + requiredFile;
118+
const requiredPath = posixifyPath(
119+
options.isHasteMode
120+
? requiredFile
121+
: options.artifactDirectory
122+
? getRelativeImportPath(state, options.artifactDirectory, requiredFile)
123+
: GENERATED + requiredFile,
124+
);
115125

116126
const hash = crypto
117127
.createHash('md5')

0 commit comments

Comments
 (0)