Search code examples
javascripttypescriptwebpackbabeljsbabel-loader

Unable to compile TypeScript of symlinked package using babel-loader


I have 2 packages: 'shared' and 'web'. They both use TypeScript. I'm using webpack with babel-loader in 'web'. When I symlink 'shared', it's unable to compile its TypeScript.

This issue suggests using symlinks: false in webpack.config.js, but doesn't work: https://github.com/webpack/webpack/issues/1643

Folder structure:

root
│
└───shared
│   │   package.json
│   │   UseMe.ts
│   
└───web
    │   .babelrc
    │   index.ts
    │   package.json
    │   tsconfig.json
    │   webpack.config.js

shared\package.json

{
  "name": "shared",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "typescript": "^3.6.4"
  }
}

shared\UseMe.ts

class UseMe {
    prop: string
}

export default UseMe;

web\.babelrc

{
    "presets": [
        "@babel/preset-typescript"
    ]
}

web\index.ts

import UseMe from 'shared/UseMe';
const x = new UseMe();

web\package.json

{
  "name": "web",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "author": "",
  "license": "ISC",
  "scripts": {
    "build": "webpack"
  },
  "dependencies": {
    "@babel/core": "^7.2.2",
    "@babel/preset-typescript": "^7.1.0",
    "babel-loader": "^8.0.5",
    "webpack": "^4.29.3",
    "webpack-cli": "^3.2.3"
  },
  "devDependencies": {
    "typescript": "^3.3.3"
  }
}

web\tsconfig.json

{
  "compilerOptions": {
    "outDir": "./dist/",
    "noImplicitAny": true,
    "module": "commonjs",
    "esModuleInterop": true,
    "target": "es6",
  }
}

web\webpack.config.js

const path = require('path');

module.exports = {
    entry: './index.ts',
    output: {
        path: path.join(__dirname, './dist'),
        filename: 'bundle.js'
    },
    resolve: {
        extensions: ['.ts', '.tsx', '.js'],
        symlinks: false
    },
    module: {
        rules: [
            {
                test: /\.(ts|js|tsx)?$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader'
                },
            }
        ]
    }
};

Commands:

cd shared
npm install
npm link
cd ..\web
npm install
npm link shared
npm run build

Error:

ERROR in ./node_modules/shared/UseMe.ts 2:8
Module parse failed: Unexpected token (2:8)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| class UseMe {
>     prop: string
| }
|

Solution

  • Solution

    1. Delete root\web\.babelrc and replace it with root\web\babel.config.js:
    module.exports = function (api) {
        api.cache(true);
    
        const presets = ["@babel/preset-typescript"];
        const plugins = [];
    
        return {
            presets,
            plugins
        };
    }
    
    1. Remove line exclude: /node_modules/ from root\web\webpack.config.js

    More Information

    The first problem was that when using Babel, .babelrc limits its scope to the the same folder as package.json. So even if I manually moved the 'shared' folder into root\web it still wouldn't work, unless I deleted shared\package.json.

    The second problem was that when a package is symlinked, it winds up in your 'node_module', therefore exclude: /node_modules/ would exclude it from its transpiling.

    See this page for more information: https://babeljs.io/docs/en/config-files

    Thanks to @ford04 for coming up with the answer, I just wrote up the steps.