Skip to content

Commit 8d9db96

Browse files
Fix yarn build-types on Windows
Three steps in scripts/js-api/build-types read a native file path as /-separated: the default-export rename took the whole Windows path as the module name, the cyclic-import rewrite wrote backslash specifiers, and the api-extractor config embedded the temp directory unescaped. Use path.basename and path.dirname, and write the paths that land in a specifier or the config with /.
1 parent 0bd2cb1 commit 8d9db96

4 files changed

Lines changed: 16 additions & 7 deletions

File tree

‎scripts/js-api/build-types/buildApiSnapshot.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ async function generateConfigFiles(tempDirectory: string) {
296296
);
297297
const adjustedApiExtractorConfig = apiExtractorConfig.replaceAll(
298298
'${typegen_directory}',
299-
tempDirectory,
299+
tempDirectory.split(path.sep).join('/'),
300300
);
301301
await fs.writeFile(
302302
path.join(tempDirectory, API_EXTRACTOR_CONFIG_FILE),

‎scripts/js-api/build-types/transforms/typescript/__tests__/replaceDefaultExportName-test.js‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
const createReplaceDefaultExportName = require('../replaceDefaultExportName');
1212
const babel = require('@babel/core');
1313
const flowApiTranslator = require('flow-api-translator');
14+
const path = require('node:path');
1415

1516
const prettierOptions = {parser: 'babel'};
1617

@@ -32,7 +33,10 @@ async function translate(code: string, fileName: string): Promise<string> {
3233
describe('replaceDefaultExportName', () => {
3334
test('should replace default export name with unambiguous one', async () => {
3435
const code = `export default Foo;`;
35-
const result = await translate(code, 'mock/path/to/module/Foo.js');
36+
const result = await translate(
37+
code,
38+
path.join('mock', 'path', 'to', 'module', 'Foo.js'),
39+
);
3640
expect(result).toMatchInlineSnapshot(`
3741
"declare const $$Foo: typeof Foo;
3842
declare type $$Foo = typeof $$Foo;

‎scripts/js-api/build-types/transforms/typescript/replaceDefaultExportName.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010

1111
import type {PluginObj} from '@babel/core';
1212

13+
const path = require('node:path');
14+
1315
function createDefaultExportNameReplacer(filePath: string): PluginObj<unknown> {
1416
return {
1517
visitor: {
1618
Identifier(node) {
17-
const fileName = filePath.split('/').pop() ?? '';
19+
const fileName = path.basename(filePath);
1820
const moduleName = fileName.split('.')[0];
1921

2022
if (node.node.name === '$$EXPORT_DEFAULT_DECLARATION$$') {

‎scripts/js-api/build-types/utils/resolveCyclicImportsInDefinition.js‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ function getReplacementInfo(
2424
filePath: string,
2525
packages: PackageConfig,
2626
) {
27-
const sourceDirectory = filePath.split('/').slice(0, -1).join('/');
28-
const pathToRoot = path.relative(sourceDirectory, rootPath);
27+
const sourceDirectory = path.dirname(filePath);
28+
const pathToRoot = path
29+
.relative(sourceDirectory, rootPath)
30+
.split(path.sep)
31+
.join('/');
2932

3033
const packageToReplace = packages.find(({name}) => source.startsWith(name));
3134

@@ -50,7 +53,7 @@ const visitor: PluginObj<{
5053

5154
nodePath.node.source.value = nodePath.node.source.value.replace(
5255
packageToReplace.name,
53-
path.join(pathToRoot, packageToReplace.directory),
56+
path.posix.join(pathToRoot, packageToReplace.directory),
5457
);
5558
},
5659
ExportNamedDeclaration(nodePath, state) {
@@ -72,7 +75,7 @@ const visitor: PluginObj<{
7275
// $FlowExpectedError[incompatible-use]
7376
nodePath.node.source.value = nodePath.node.source.value.replace(
7477
packageToReplace.name,
75-
path.join(pathToRoot, packageToReplace.directory),
78+
path.posix.join(pathToRoot, packageToReplace.directory),
7679
);
7780
},
7881
},

0 commit comments

Comments
 (0)