babel-plugin-module-resolver icon indicating copy to clipboard operation
babel-plugin-module-resolver copied to clipboard

cwd: 'babelrc' not working in node_modules

Open rgranger opened this issue 6 years ago • 0 comments

I have a project named 'aw-utils' containing utils functions and another called 'aw-platform' containing the web application. aw-platform has aw-utils as dependency.

Both projects have different aliases. But trying to import an aw-utils function in aw-platform, the aw-utils aliases are not resolved.

Here is the folder structure :

aw-platform __ .babelrc.js __ start.js __ node_modules ____ aw-utils ______ .babelrc.js
______ tmp.js ______ foobar ________ folder __________ test.js

And the content of the files

// .babelrc.js in aw-platform

module.exports = {
  ignore: [
    /node_modules[\\/](?!(aw-utils))/, // ignore node_modules except aw-utils
  ],
  plugins: [
    '@babel/plugin-transform-modules-commonjs',
    '@babel/plugin-transform-runtime',
    ['@babel/plugin-proposal-class-properties', { loose: true }],
    ['module-resolver', {
      cwd: 'babelrc',
      alias: {
        client: './src/client',
        server: './src/server',
      }
    }]
  ],
}
// start.js in aw-platform
const babelrc = require('./babelrc')
require('@babel/register')(babelrc) // necessary because otherwise the ignore option is overriden by babel-register

const { tmp } = require('aw-utils/tmp')
tmp()
// .babelrc.js in aw-utils

module.exports = {
  plugins: [
    '@babel/plugin-transform-modules-commonjs',
    '@babel/plugin-transform-runtime',
    ['@babel/plugin-proposal-class-properties', { loose: true }],
    ["module-resolver", {
      "cwd": "babelrc",
      "root": ["./"],
      "alias": {
        "foobar": "./foobar/folder"
      }
    }]
  ],
}
// tmp.js in aw-utils

import { test } from 'foobar/test'

export function tmp () {
  console.log('tmp')
  test()
}
// test.js in aw-utils/foobar/folder

export function test () {
  console.log('test')
}

Then, when i try to node start.js from aw-platform, i got the following error :

image

Am I doing something wrong, or have I misunderstood the cwd: 'babelrc" option ?

rgranger avatar Feb 21 '19 17:02 rgranger