Using the --ignore cli option with babel-node

73 Views Asked by At

I am trying to set up babel-node for developement, in a monorepo that we are creating. The base folder structure of the monorepo, looks something like this:

+ apps
    + app1
      ...
      - package.json
      - tsconfig.json
      - babel.config.js
    + app2
      ...
      - package.json
      - tsconfig.json
      - babel.config.js
    ...
+ packages
    + package1
      ...
      - package.json
      - tsconfig.json
    + package2
      ...
      - package.json
      - tsconfig.json
    ...
- package.json
- babel.config.js

Where each app and package has their separate package.json files, and the main package.json file shown in the above directory structure, sets the workspaces (I am using npm).

All the apps and packages are written in typescript, and each have their own separate tsconfig.json file. What I am trying to achieve, is to be able to run one of the apps in developement mode, by transforming and transpiling the code that was written in typescript, using a JIT transformation. For this, I've chose to use babel-node.

Each app has a babel.config.js file, that loads the core babel.config.js, which sets up the following presets and plugins for use:

/** This is a plugin written by us, that used with the babel-plugin-module-resolver, should load the tsconfig file appropiate of the package that is importing something, and if finds an appropiate alias, should change the module resolution on the fly **/
const babelResolvePath = require('@yellow-utils/babel-resolve-path-typescript');
const path = require('path');

const [presets, plugins] = [[], []];

presets.push(["@babel/preset-env", {
    targets: {
        node: 'current'
    }
}]);
presets.push("@babel/preset-typescript");

/** We start from the apps tsconfig file that was being loaded **/
const usedTsConfig =path.resolve(process.cwd(), './tsconfig.json');

plugins.push([
    require.resolve('babel-plugin-module-resolver'),
    {
        root: ["./src/"],
        resolvePath: babelResolvePath.createPathResolver({
            tsconfigPath: usedTsConfig, 
            extensions: ['.ts', '.js', '.mustache']
        }),
        extensions: [".js", ".ts"],
        stripExtensions: [".ts", ".js"]
    }
]);

module.exports = { 
    presets,
    plugins
};

My problem is the following: all of our tsconfig files are set up, so that when I am importing something in app1 for example, from package1, the import is aliased, so that the file is not loaded from the node_modules folder, but directly from the repositories file structure. So basically, if I do in app1/src/index.ts

import {...} from 'package1';

it should be rewritten to

import {...} from '../../../packages/package1/src/index.ts`

According to the documentation, by default, if you do not provide either --ignore, nor --only parameters for babel-node on the command line, all files from node_modules will be ignored and not transpiled, and ONLY files from the current working directory will be transpiled. This does not work for us, as files that are imported from the packages folder should also be transpiled.

I've tried to set an ignore to the babel-node then, so that the only rule will be disregarded, but I am failing to achieve any success with this.

These are the commands that I've tried, and the reasons they failed. All commands are run from the scope of one of the app directories, for example, from apps/app1/

// with this command, babel will try to transpile all node_modules aswell, but the script runs as expected otherwise
babel-node -x .ts,.js --ignore node_modules src/index.ts

// other variations on the above command, trying with "regexp", as the documentation states, achieves the same outcome as before - the files imported from node_modules folder are also transpiled
babel-node -x .ts,.js --ignore=\".*node_modules.*\" src/index.ts

// also tried by supplying a glob pattern, but the results are the same
babel-node -x .ts,.js --ignore **/node_modules/**

The only way I was able to achieve my desired result, is by adding the relative path to the node_modules folder that I want ignored:

babel-node -x .ts,.js --ignore ../../node_modules src/index.ts

However, I feel this to be not portable at all, as if my folder structure changes, I need to constantly update the dev command as well.

What am I missing here? Why is neither the regexp, or the glob pattern working, and why does babel ignore the node_modules folder, when I supply to it neither ignore nor only cli parameters? According the documentation, the default value for the ignore cli parameter is node_modules, and the only parameter is empty, unless neither the ignore, nor the only parameter was supplied.

I am not even sure, that this error is originating from babel-node, and not from babel-plugin-module-resolver, but I couldn't try it out if I am right or not.

0

There are 0 best solutions below