programming language, superset of JavaScript that compiles to JavaScript
Factsheet
Anders Hejlsberg,
Luke Hoban
Anders Hejlsberg,
Luke Hoban
Videos
As my comment mentions in the original question, from the yarn documentation:
TypeScript uses its own resolver as well. In this case the situation is a bit more complex - the TS team has some concerns about allowing third-party hooks inside the tsc compiler, meaning that we can’t work with it at the moment. That being said, TypeScript isn’t only tsc and as such we’ve been able to add PnP support to the popular ts-loader - meaning that as long as you compile your TypeScript through Webpack, everything works well! Consult the dedicated section about it for more information.
To enable yarn package resolution when using Typescript you have to use webpack with ts-loader. This is how I resolved it.
- install the neccessary dependencies:
npm install webpack webpack-cli pnp-webpack-plugin ts-loader
- Create a 'webpack.config.js' file in your project root directory with the following content:
const path = require('path');
const PnpWebpackPlugin = require('pnp-webpack-plugin');
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin');
const commonConfig = {
mode: 'development',
devtool: 'source-map',
plugins: [
new NodePolyfillPlugin(),
],
output: {
filename: '[name].js',
path: path.resolve(__dirname, 'dist'),
},
node: {
__dirname: false,
},
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
plugins: [PnpWebpackPlugin],
},
resolveLoader: {
plugins: [PnpWebpackPlugin.moduleLoader(module)],
},
};
module.exports = [
Object.assign(
{
target: 'electron-main',
entry: { main: './src/main.ts' },
},
commonConfig
),
Object.assign(
{
target: 'electron-renderer',
entry: { preload: './src/preload.ts' },
},
commonConfig
),
];
- I updated my package.json with scripts to run webpack:
{
"name": "ElectroMega",
"packageManager": "[email protected]",
"private": true,
"scripts": {
"build": "webpack",
"prestart": "yarn run build",
"start": "electron ./dist/main.js"
},
"devDependencies": {
"html-webpack-plugin": "^5.3.2",
"node-polyfill-webpack-plugin": "^1.1.4",
"pnp-webpack-plugin": "^1.7.0",
"ts-loader": "^9.2.6",
"typescript": "^4.4.3",
"webpack": "^5.54.0",
"webpack-cli": "^4.8.0"
},
"dependencies": {
"@tsconfig/node14": "^1.0.1",
"@types/node": "^16.9.6",
"electron": "^14.0.1"
}
}
- Then you should be able to build your code with the command:
yarn build
My solution was a result of following the webpack GettingStarted section that explained much of the troubles I had, and the basics of using webpack.
FWIW, iamkneel's answer from above worked for my. Had an express app that I was using Typescript for and set it up with yarn. Setting nodeLinker: node-modules in the .yarnrc.yml file took care of the compile issue.
» npm install @yarnpkg/plugin-typescript