Skip to content

Commit

Permalink
fix: handle defualt exports properly in secondary exports (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
ForbesLindesay authored May 10, 2019
1 parent 8c3c330 commit 680ed3a
Show file tree
Hide file tree
Showing 13 changed files with 941 additions and 15 deletions.
5 changes: 4 additions & 1 deletion packages/mysql-test/jest/globalSetup.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// @autogenerated

export * from '../lib/jest/globalSetup';
import def from '../lib/jest/globalSetup';

export default def;
export * from '../lib/jest/globalSetup';
4 changes: 3 additions & 1 deletion packages/mysql-test/jest/globalTeardown.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @autogenerated

export * from '../lib/jest/globalTeardown';
import def from '../lib/jest/globalTeardown';

export default def;
2 changes: 1 addition & 1 deletion packages/mysql-test/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@databases/mysql-test",
"version": "1.0.0",
"version": "1.0.1",
"description": "",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down
8 changes: 5 additions & 3 deletions packages/mysql-test/src/jest/globalSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ const DEFAULT_ENV_VAR =

export const killers: Array<() => Promise<void>> = [];

module.exports = async (
export default async function setup(
opts: Partial<Options> & {
environmentVariable?: string;
migrationsScript?: string[];
} = {},
) => {
) {
const envVar: string = opts.environmentVariable || DEFAULT_ENV_VAR;
const migrationsScript =
opts.migrationsScript ||
Expand All @@ -40,6 +40,8 @@ module.exports = async (
delete process.env[envVar];
await kill();
});
};
}

module.exports = setup;
module.exports.default = setup;
module.exports.killers = killers;
7 changes: 5 additions & 2 deletions packages/mysql-test/src/jest/globalTeardown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import {killers} from './globalSetup';

module.exports = async () => {
export default async function teardown() {
await Promise.all(killers.map(async kill => await kill()));
};
}

module.exports = teardown;
module.exports.default = teardown;
5 changes: 4 additions & 1 deletion packages/pg-migrations/build-package.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// @autogenerated

export * from './lib/build-package';
import def from './lib/build-package';

export default def;
export * from './lib/build-package';
4 changes: 3 additions & 1 deletion packages/pg-migrations/getID.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @autogenerated

export * from './lib/getID';
import def from './lib/getID';

export default def;
2 changes: 1 addition & 1 deletion packages/pg-migrations/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@databases/pg-migrations",
"version": "0.0.2",
"version": "0.0.3",
"description": "",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down
5 changes: 4 additions & 1 deletion packages/pg-test/jest/globalSetup.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// @autogenerated

export * from '../lib/jest/globalSetup';
import def from '../lib/jest/globalSetup';

export default def;
export * from '../lib/jest/globalSetup';
4 changes: 3 additions & 1 deletion packages/pg-test/jest/globalTeardown.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @autogenerated

export * from '../lib/jest/globalTeardown';
import def from '../lib/jest/globalTeardown';

export default def;
2 changes: 1 addition & 1 deletion packages/pg-test/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@databases/pg-test",
"version": "1.0.0",
"version": "1.0.1",
"description": "",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down
15 changes: 14 additions & 1 deletion scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ lsrSync(cwd + '/lib').forEach(entry => {
unlinkSync(entry.fullPath);
}
if (isPublic) {
const definition = readFileSync(
entry.fullPath.replace(/\.jsx?$/, '.d.ts'),
'utf8',
);
const publicFilename = resolve(
cwd + '/' + entry.path.substr(2).replace(/\.jsx$/, '.js'),
);
Expand All @@ -119,9 +123,18 @@ lsrSync(cwd + '/lib').forEach(entry => {
publicFilename,
"// @autogenerated\n\nmodule.exports = require('" + requirePath + "');",
);
const hasDefaultExport = /export default/.test(definition);
const hasNamedExport = /export (?:[^d]|d[^e]|de[^f]|def[^a]|defa[^u]|defau[^l]|defaul[^t]|default[^ ])/.test(
definition,
);

writeFileSync(
publicFilename.replace(/\.jsx?$/, '.d.ts'),
"// @autogenerated\n\nexport * from '" + requirePath + "';",
`// @autogenerated\n\n${
hasDefaultExport ? `import def from '${requirePath}';\n\n` : ''
}${hasDefaultExport ? `export default def;\n` : ''}${
hasNamedExport ? `export * from '${requirePath}';\n` : ''
}`,
);
}
}
Expand Down
Loading

0 comments on commit 680ed3a

Please sign in to comment.