Skip to content

Commit

Permalink
fix: resolve plugins from dts-cli
Browse files Browse the repository at this point in the history
fixes #186
  • Loading branch information
aladdin-add committed Apr 6, 2023
1 parent 5be0864 commit c5c0ca4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
15 changes: 8 additions & 7 deletions src/babelPluginDts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createConfigItem } from '@babel/core';
import { createBabelInputPluginFactory } from '@rollup/plugin-babel';
import merge from 'lodash.merge';
import { resolve } from './utils';

export const isTruthy = (obj?: any) => {
if (!obj) {
Expand Down Expand Up @@ -68,20 +69,20 @@ export const babelPluginDts = createBabelInputPluginFactory(() => ({
// pragma: customOptions.jsx || 'h',
// pragmaFrag: customOptions.jsxFragment || 'Fragment',
// },
{ name: 'babel-plugin-macros' },
{ name: 'babel-plugin-annotate-pure-calls' },
{ name: 'babel-plugin-dev-expression' },
{ name: resolve('babel-plugin-macros') },
{ name: resolve('babel-plugin-annotate-pure-calls') },
{ name: resolve('babel-plugin-dev-expression') },
customOptions.format !== 'cjs' && {
name: 'babel-plugin-transform-rename-import',
name: resolve('babel-plugin-transform-rename-import'),
replacements,
},
{
name: 'babel-plugin-polyfill-regenerator',
name: resolve('babel-plugin-polyfill-regenerator'),
// don't pollute global env as this is being used in a library
method: 'usage-pure',
},
{
name: '@babel/plugin-proposal-class-properties',
name: resolve('@babel/plugin-proposal-class-properties'),
loose: true,
},
isTruthy(customOptions.extractErrors) && {
Expand Down Expand Up @@ -122,7 +123,7 @@ export const babelPluginDts = createBabelInputPluginFactory(() => ({
// if no preset-env, add it & merge with their presets
const defaultPresets = createConfigItems('preset', [
{
name: '@babel/preset-env',
name: resolve('@babel/preset-env'),
targets: customOptions.targets,
modules: false,
loose: true,
Expand Down
6 changes: 4 additions & 2 deletions src/createEslintConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import fs from 'fs-extra';
import path from 'path';
import { Linter } from 'eslint';
import { PackageJson } from './types';
import { getReactVersion } from './utils';
import { getReactVersion, resolve } from './utils';

interface CreateEslintConfigArgs {
pkg: PackageJson;
rootDir: string;
Expand All @@ -18,7 +19,7 @@ export async function createEslintConfig({
const config = {
extends: [
path.join(__dirname, '../conf/eslint-config-react-app/index.js'),
'prettier',
resolve('eslint-config-prettier'),
'plugin:prettier/recommended',
],
settings: {
Expand All @@ -37,6 +38,7 @@ export async function createEslintConfig({
// if the path is an abs path(e.g. "/Users/user/my-project/.eslintrc.js"),
// need to convert a rel path to app root.
config.extends[0] = './' + path.relative(rootDir, config.extends[0]);
config.extends[1] = './' + path.relative(rootDir, config.extends[1]);
try {
await fs.writeFile(
file,
Expand Down
5 changes: 3 additions & 2 deletions src/createJestConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Config } from '@jest/types';
import { resolve } from './utils';

export type JestConfigOptions = Partial<Config.InitialOptions>;

Expand All @@ -8,8 +9,8 @@ export function createJestConfig(
): JestConfigOptions {
const config: JestConfigOptions = {
transform: {
'.(ts|tsx)$': 'ts-jest/dist',
'.(js|jsx)$': 'babel-jest', // jest's default
'.(ts|tsx)$': resolve('ts-jest'),
'.(js|jsx)$': resolve('babel-jest'), // jest's default
},
transformIgnorePatterns: ['[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$'],
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
Expand Down
7 changes: 7 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,10 @@ export function getNodeEngineRequirement({ engines }: PackageJson) {
export function interopRequireDefault(obj: any): any {
return obj && obj.__esModule ? obj : { default: obj };
}

// resolve a module from where dts was installed
// this is needed as some package managers failed to hoist the modules
// e.g. https://github.com/weiran-zsd/dts-cli/issues/186
export function resolve(mod: string) {
return require.resolve(mod, { paths: [__dirname] });
}

0 comments on commit c5c0ca4

Please sign in to comment.