Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue in tsConfig options resolving #86

Merged
merged 3 commits into from
Nov 23, 2019
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.idea
22 changes: 9 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

const path = require('path');
const debug = require('debug')('cabinet');
const fs = require('fs');
const decomment = require('decomment');

/*
* most js resolver are lazy-loaded (only required when needed)
Expand Down Expand Up @@ -182,9 +180,7 @@ function jsLookup({dependency, filename, directory, config, webpackConfig, confi
function tsLookup({dependency, filename, tsConfig}) {
debug('performing a typescript lookup');

const defaultTsConfig = {
compilerOptions: {}
};
let compilerOptions = {};

if (!ts) {
ts = require('typescript');
Expand All @@ -193,35 +189,35 @@ function tsLookup({dependency, filename, tsConfig}) {
debug('given typescript config: ', tsConfig);

if (!tsConfig) {
tsConfig = defaultTsConfig;
debug('no tsconfig given, defaulting');

} else if (typeof tsConfig === 'string') {
debug('string tsconfig given, parsing');

try {
tsConfig = JSON.parse(decomment(fs.readFileSync(tsConfig, 'utf8')));
const tsParsedConfig = ts.readJsonConfigFile(tsConfig, ts.sys.readFile);
compilerOptions = ts.parseJsonSourceFileConfigFileContent(tsParsedConfig, ts.sys, path.dirname(tsConfig)).options;
debug('successfully parsed tsconfig');
} catch (e) {
debug('could not parse tsconfig');
throw new Error('could not read tsconfig');
}
} else {
compilerOptions = ts.convertCompilerOptionsFromJson(tsConfig.compilerOptions).options;
}

debug('processed typescript config: ', tsConfig);
debug('processed typescript config type: ', typeof tsConfig);

const {options} = ts.convertCompilerOptionsFromJson(tsConfig.compilerOptions);

// Preserve for backcompat. Consider removing this as a breaking change.
if (!options.module) {
options.module = ts.ModuleKind.AMD;
if (!compilerOptions.module) {
compilerOptions.module = ts.ModuleKind.AMD;
}

const host = ts.createCompilerHost({});
debug('with options: ', options);
debug('with options: ', compilerOptions);

const namedModule = ts.resolveModuleName(dependency, filename, options, host);
const namedModule = ts.resolveModuleName(dependency, filename, compilerOptions, host);
let result = '';

if (namedModule.resolvedModule) {
Expand Down
2 changes: 2 additions & 0 deletions test/mockedJSFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ module.exports = {
'check-nested.ts': 'import {Child} from "./subdir";',
'image.svg': '<svg></svg>',
'.tsconfig': '{ "version": "1.0.0", "compilerOptions": { "module": "commonjs" }\n // comments\n }',
'.tsconfigSpecificModuleResolution': '{ "version": "1.0.0", "compilerOptions": { "module": "commonjs", "moduleResolution": "node" }\n // comments\n }',
'.tsconfigExtending': '{ "extends": "./.tsconfigSpecificModuleResolution" }',
'subdir': {
'index.tsx': 'export Child = () => { return (<div></div>); );',
'subimage.svg': '<svg></svg>'
Expand Down
30 changes: 21 additions & 9 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,9 +456,7 @@ describe('filing-cabinet', function() {
tsConfig: parsedConfig
});

assert.deepEqual(mockTs.resolveModuleName.args[0][2], {
module: ts.ModuleKind.CommonJS,
});
assert.equal(mockTs.resolveModuleName.args[0][2].module, ts.ModuleKind.CommonJS);

revert();
});
Expand All @@ -481,6 +479,24 @@ describe('filing-cabinet', function() {
);
});

it('finds import from child subdirectories when using node module resolution in extended config', function() {
const filename = directory + '/check-nested.ts';

const tsConfigPath = path.join(path.resolve(directory), '.tsconfigExtending');

const result = cabinet({
partial: './subdir',
filename,
directory,
tsConfig: tsConfigPath
});

assert.equal(
result,
path.join(path.resolve(directory), '/subdir/index.tsx')
);
});

it('finds imports of non-typescript files', function() {
const filename = directory + '/index.ts';

Expand Down Expand Up @@ -556,9 +572,7 @@ describe('filing-cabinet', function() {
tsConfig: path.join(path.resolve(directory), '.tsconfig')
});

assert.deepEqual(mockTs.resolveModuleName.args[0][2], {
module: ts.ModuleKind.CommonJS,
});
assert.equal(mockTs.resolveModuleName.args[0][2].module, ts.ModuleKind.CommonJS);

revert();
});
Expand All @@ -581,9 +595,7 @@ describe('filing-cabinet', function() {
directory
});

assert.deepEqual(mockTs.resolveModuleName.args[0][2], {
module: mockTs.ModuleKind.AMD
});
assert.deepEqual(mockTs.resolveModuleName.args[0][2].module, mockTs.ModuleKind.AMD);

revert();
});
Expand Down