Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scripts/js-api/build-types/buildApiSnapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ async function generateConfigFiles(tempDirectory: string) {
);
const adjustedApiExtractorConfig = apiExtractorConfig.replaceAll(
'${typegen_directory}',
tempDirectory,
tempDirectory.split(path.sep).join('/'),
);
await fs.writeFile(
path.join(tempDirectory, API_EXTRACTOR_CONFIG_FILE),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
const createReplaceDefaultExportName = require('../replaceDefaultExportName');
const babel = require('@babel/core');
const flowApiTranslator = require('flow-api-translator');
const path = require('node:path');

const prettierOptions = {parser: 'babel'};

Expand All @@ -32,7 +33,10 @@ async function translate(code: string, fileName: string): Promise<string> {
describe('replaceDefaultExportName', () => {
test('should replace default export name with unambiguous one', async () => {
const code = `export default Foo;`;
const result = await translate(code, 'mock/path/to/module/Foo.js');
const result = await translate(
code,
path.join('mock', 'path', 'to', 'module', 'Foo.js'),
);
expect(result).toMatchInlineSnapshot(`
"declare const $$Foo: typeof Foo;
declare type $$Foo = typeof $$Foo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@

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

const path = require('node:path');

function createDefaultExportNameReplacer(filePath: string): PluginObj<unknown> {
return {
visitor: {
Identifier(node) {
const fileName = filePath.split('/').pop() ?? '';
const fileName = path.basename(filePath);
const moduleName = fileName.split('.')[0];

if (node.node.name === '$$EXPORT_DEFAULT_DECLARATION$$') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ function getReplacementInfo(
filePath: string,
packages: PackageConfig,
) {
const sourceDirectory = filePath.split('/').slice(0, -1).join('/');
const pathToRoot = path.relative(sourceDirectory, rootPath);
const sourceDirectory = path.dirname(filePath);
const pathToRoot = path
.relative(sourceDirectory, rootPath)
.split(path.sep)
.join('/');

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

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

nodePath.node.source.value = nodePath.node.source.value.replace(
packageToReplace.name,
path.join(pathToRoot, packageToReplace.directory),
path.posix.join(pathToRoot, packageToReplace.directory),
);
},
ExportNamedDeclaration(nodePath, state) {
Expand All @@ -72,7 +75,7 @@ const visitor: PluginObj<{
// $FlowExpectedError[incompatible-use]
nodePath.node.source.value = nodePath.node.source.value.replace(
packageToReplace.name,
path.join(pathToRoot, packageToReplace.directory),
path.posix.join(pathToRoot, packageToReplace.directory),
);
},
},
Expand Down
Loading