Skip to content

Commit

Permalink
Merge branch 'master' into feat/tsLookup/path-mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
jjangga0214 committed Jan 26, 2022
2 parents 92d22f0 + 56031e9 commit 7e9ef2b
Show file tree
Hide file tree
Showing 71 changed files with 6,401 additions and 2,235 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Node.js CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [10.x, 12.x, 14.x, 15.x, 16.x]

steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: npm ci
- run: npm run build --if-present
- run: npm test
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
/node_modules
.idea
.DS_Store
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
test
wallaby.conf.js
9 changes: 0 additions & 9 deletions .travis.yml

This file was deleted.

38 changes: 31 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const defaultLookups = {
* @param {Object} [options.ast] A preparsed AST for the file identified by filename.
* @param {String|Object} [options.tsConfig] Path to a typescript configuration or an object representing a pre-parsed typescript config.
* @param {String} [options.tsConfigPath] A (virtual) Path to typescript config file when options.tsConfig is given as an object. Needed to calculate [Path Mapping](https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping). If not given when options.tsConfig is an object, Path Mapping is not considered.
* @param {boolean} [options.noTypeDefinitions] Whether to return '.d.ts' files or '.js' files for a dependency
* @param {Boolean} [options.noTypeDefinitions] Whether to return '.d.ts' files or '.js' files for a dependency
*/
module.exports = function cabinet(options) {
const {
Expand Down Expand Up @@ -96,6 +96,16 @@ module.exports.register = function(extension, lookupStrategy) {
}
};

/**
* Unregister a custom lookup resolver for a file extension
*
* @param {String} extension - The file extension whose resolver should be removed
*/
module.exports.unregister = function(extension) {
delete defaultLookups[extension];
module.exports.supportedFileExtensions = Object.keys(defaultLookups);
};

/**
* Exposed for testing
*
Expand Down Expand Up @@ -173,7 +183,8 @@ function getCompilerOptionsFromTsConfig(tsConfig) {
* @param {Object} [options.ast]
* @return {String}
*/
function jsLookup({dependency, filename, directory, config, webpackConfig, configPath, nodeModulesConfig, ast, tsConfig}) {
function jsLookup(options) {
const {dependency, filename, directory, config, webpackConfig, configPath, ast} = options;
const type = module.exports._getJSType({
config: config,
webpackConfig: webpackConfig,
Expand All @@ -199,7 +210,7 @@ function jsLookup({dependency, filename, directory, config, webpackConfig, confi

case 'commonjs':
debug('using commonjs resolver');
return commonJSLookup({dependency, filename, directory, nodeModulesConfig, tsConfig});
return commonJSLookup(options);

case 'webpack':
debug('using webpack resolver for es6');
Expand All @@ -208,7 +219,7 @@ function jsLookup({dependency, filename, directory, config, webpackConfig, confi
case 'es6':
default:
debug('using commonjs resolver for es6');
return commonJSLookup({dependency, filename, directory, nodeModulesConfig, tsConfig});
return commonJSLookup(options);
}
}

Expand Down Expand Up @@ -294,7 +305,9 @@ function tsLookup({dependency, filename, tsConfig, tsConfigPath, noTypeDefinitio
return result ? path.resolve(result) : '';
}

function commonJSLookup({dependency, filename, directory, nodeModulesConfig, tsConfig}) {
function commonJSLookup(options) {
const {filename, directory, nodeModulesConfig, tsConfig} = options;
let {dependency} = options;
if (!resolve) {
resolve = require('resolve');
}
Expand Down Expand Up @@ -326,9 +339,17 @@ function commonJSLookup({dependency, filename, directory, nodeModulesConfig, tsC
}

const tsCompilerOptions = getCompilerOptionsFromTsConfig(tsConfig);
const allowMIxedJsAndTs = tsCompilerOptions.allowJs;
const allowMixedJsAndTs = tsCompilerOptions.allowJs;
let extensions = ['.js', '.jsx'];
if (allowMIxedJsAndTs) {
if (allowMixedJsAndTs) {
// Let the typescript engine take a stab at resolving this one. This lookup will
// respect any custom paths in tsconfig.json
result = tsLookup(options);
if (result) {
debug('typescript successfully resolved commonjs module: ', result);
return result;
}
// Otherwise, let the commonJS resolver look for plain .ts file imports.
extensions = extensions.concat(['.ts', '.tsx']);
}

Expand Down Expand Up @@ -361,6 +382,9 @@ function resolveWebpackPath({dependency, filename, directory, webpackConfig}) {
if (typeof loadedConfig === 'function') {
loadedConfig = loadedConfig();
}
if (Array.isArray(loadedConfig)) {
loadedConfig = loadedConfig[0];
}
} catch (e) {
debug('error loading the webpack config at ' + webpackConfig);
debug(e.message);
Expand Down
Loading

0 comments on commit 7e9ef2b

Please sign in to comment.