Skip to content

Commit

Permalink
fix: Resolve TypeScript when tsConfigPath option is set
Browse files Browse the repository at this point in the history
  • Loading branch information
ntucker committed Apr 21, 2021
1 parent 8d5a15c commit 9381313
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 20 deletions.
62 changes: 43 additions & 19 deletions packages/babel-preset-anansi/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const path = require('path');
const semver = require('semver');
const { readTsConfig } = require('@anansi/ts-utils');
var globToRegExp = require('glob-to-regexp');

/*
Expand Down Expand Up @@ -44,7 +43,7 @@ function buildPreset(api, options = {}) {
hotReloader: false,
reactConstantElementsOptions: {},
nodeTarget,
resolver: {root: [], alias: {}},
resolver: { root: [], alias: {} },
...options,
};
const modules =
Expand Down Expand Up @@ -81,33 +80,54 @@ function buildPreset(api, options = {}) {
options.tsConfigPath = process.env.TS_CONFIG_PATH;
}
if (options.tsConfigPath) {
const { dir, base } = path.parse(options.tsConfigPath)
const tsconfig = base !== '.' && base !== '..' ? readTsConfig(dir, base) : readTsConfig(dir)
let readTsConfig;
try {
readTsConfig = require('@anansi/ts-utils').readTsConfig;
} catch (e) {
throw new Error('tsConfigPath set, but typescript module not found');
}
const { dir, base } = path.parse(options.tsConfigPath);
const tsconfig =
base !== '.' && base !== '..'
? readTsConfig(dir, base)
: readTsConfig(dir);
if (tsconfig.options.paths) {
for (const k in tsconfig.options.paths) {
const key = globToRegExp(k).toString().replace('.*', '(.*)')
options.resolver.alias[key.substr(1, key.length - 2)] = './' +tsconfig.options.paths[k][0].replace('*', '\\1')
const key = globToRegExp(k).toString().replace('.*', '(.*)');
options.resolver.alias[key.substr(1, key.length - 2)] =
'./' + tsconfig.options.paths[k][0].replace('*', '\\1');
}
options.resolver.root = [path.resolve(tsconfig.options.baseUrl)]
options.resolver.root = [path.resolve(tsconfig.options.baseUrl)];
options.resolver.root = [
...(tsconfig.options.baseUrl ? [tsconfig.options.baseUrl] : []),
...(tsconfig.options.rootDir ? [tsconfig.options.rootDir] : tsconfig.options.rootDirs || []),
...(tsconfig.options.rootDir
? [tsconfig.options.rootDir]
: tsconfig.options.rootDirs || []),
...options.resolver.root,
]
];
}
}
options.resolver.extensions= ['.ts.', '.tsx', ".js", ".jsx", ".es", ".es6", ".mjs"]
options.resolver.extensions = [
'.ts.',
'.tsx',
'.js',
'.jsx',
'.es',
'.es6',
'.mjs',
];
options.resolver.alias = {
...options.resolver.alias,
...((process.env.RESOLVER_ALIAS &&
JSON.parse(process.env.RESOLVER_ALIAS)) || options.resolverAlias)
JSON.parse(process.env.RESOLVER_ALIAS)) ||
options.resolverAlias),
};
options.resolver.root = [
...options.resolver.root,
...(process.env.RESOLVER_ROOT
? [process.env.RESOLVER_ROOT]
: options.resolverRoot || [])
]
? [process.env.RESOLVER_ROOT]
: options.resolverRoot || []),
];

const preset = {
presets: [
Expand All @@ -122,7 +142,8 @@ function buildPreset(api, options = {}) {
],
],
plugins: [
(Object.keys(options.resolver.alias).length || Object.keys(options.resolver.root).length) && [
(Object.keys(options.resolver.alias).length ||
Object.keys(options.resolver.root).length) && [
require('babel-plugin-module-resolver').default,
options.resolver,
],
Expand Down Expand Up @@ -152,10 +173,13 @@ function buildPreset(api, options = {}) {
require('@babel/plugin-proposal-export-default-from').default,
require('@babel/plugin-proposal-export-namespace-from').default,
// stage 2
[require('@babel/plugin-proposal-record-and-tuple').default, {
"importPolyfill": true,
"syntaxType": "hash"
}],
[
require('@babel/plugin-proposal-record-and-tuple').default,
{
importPolyfill: true,
syntaxType: 'hash',
},
],
//stage 3
require('@babel/plugin-syntax-top-level-await').default,
// Get "Module parse failed: Unexpected token" when targetting newer browsers without this
Expand Down
6 changes: 5 additions & 1 deletion packages/babel-preset-anansi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@
"@babel/runtime": "^7.7.0",
"babel-minify": "^0.5.1",
"react-hot-loader": "^4.12.0",
"react-refresh": "^0.8.0"
"react-refresh": "^0.8.0",
"typescript": "^3.0.0 | ^4.0.0"
},
"peerDependenciesMeta": {
"babel-minify": {
Expand All @@ -84,6 +85,9 @@
},
"@babel/runtime": {
"optional": true
},
"typescript": {
"optional": true
}
}
}

0 comments on commit 9381313

Please sign in to comment.