Skip to content

Commit

Permalink
fix(custom-webpack): register ts-node only once (#1035)
Browse files Browse the repository at this point in the history
* test(custom-webpack): add failing test for #1031

* fix(custom-webpack): actual fix for #1031

Closes #1031
  • Loading branch information
Gerkin authored Sep 7, 2021
1 parent 64e5e27 commit 359a4c4
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 8 deletions.
43 changes: 43 additions & 0 deletions packages/custom-webpack/src/transform-factories.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
jest.mock('ts-node', () => ({
register: jest.fn(),
}));
jest.mock('tsconfig-paths', () => ({
loadConfig: jest.fn().mockReturnValue({}),
}));
import * as utils from './utils';
jest.spyOn(utils, 'tsNodeRegister');

import { getTransforms } from './transform-factories';

describe('getTransforms', () => {
beforeEach(() => {
jest.resetModules();
});
it('should call ts-node register once with typescript index-html-transform & custom-webpack-config', () => {
jest.mock('test/index-transform.test.ts', () => jest.fn(), { virtual: true });
jest.mock('test/webpack.test.config.ts', () => jest.fn(), { virtual: true });
const tsNode = require('ts-node') as jest.Mocked<typeof import('ts-node')>;

const transforms = getTransforms(
{
customWebpackConfig: {
path: 'webpack.test.config.ts',
},
indexTransform: 'index-transform.test.ts',
tsConfig: 'tsconfig.test.json',
},
{ workspaceRoot: './test' } as any
);
transforms.webpackConfiguration({});

expect(utils.tsNodeRegister).toHaveBeenCalledWith(
'test/webpack.test.config.ts',
'test/tsconfig.test.json'
);
expect(utils.tsNodeRegister).toHaveBeenCalledWith(
'index-transform.test.ts',
'test/tsconfig.test.json'
);
expect(tsNode.register).toHaveBeenCalledTimes(1);
});
});
33 changes: 25 additions & 8 deletions packages/custom-webpack/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
/**
* check for TS node registration
* @param file: file name or file directory are allowed
* @todo tsNodeRegistration: require ts-node if file extension is TypeScript
*/
export function tsNodeRegister(file: string = '', tsConfig?: string) {
if (file && file.endsWith('.ts')) {
// Register TS compiler lazily
const _tsNodeRegister = (() => {
let lastTsConfig: string | null | undefined;
return (tsConfig?: string) => {
// Check if the function was previously called with the same tsconfig
if (typeof lastTsConfig !== 'undefined' && (tsConfig ?? null) !== lastTsConfig) {
throw new Error('Called with multiple tsconfigs.');
}
if (lastTsConfig) {
return;
}
lastTsConfig = tsConfig ?? null;

// Register ts-node
require('ts-node').register({
project: tsConfig,
compilerOptions: {
Expand All @@ -22,5 +27,17 @@ export function tsNodeRegister(file: string = '', tsConfig?: string) {
if (baseUrl && paths) {
tsconfigPaths.register({ baseUrl, paths });
}
};
})();

/**
* check for TS node registration
* @param file: file name or file directory are allowed
* @todo tsNodeRegistration: require ts-node if file extension is TypeScript
*/
export function tsNodeRegister(file: string = '', tsConfig?: string) {
if (file && file.endsWith('.ts')) {
// Register TS compiler lazily
_tsNodeRegister(tsConfig);
}
}

0 comments on commit 359a4c4

Please sign in to comment.