zerkalica/zerollup

Resolve node packages

Closed this issue · 18 comments

TS easily resolve paths in format:
[package]/*: [package]/dist/*

Can u improve library to transform [package] or [scope/package]?

What is [package], it's a placeholder or what?
I can't find something about it https://www.typescriptlang.org/docs/handbook/module-resolution.html
Can you provide examples and results?

By [package] I mean external dependency in node_modules.
But sorry I think typescript will not resolve it, I will test it

You can add node_modules to destination, like in ts documentation:

{
  "compilerOptions": {
    "baseUrl": ".", // This must be specified if "paths" is.
    "paths": {
      "jquery": ["node_modules/jquery/dist/jquery"] // This mapping is relative to "baseUrl"
    }
  }
}

What is your case?

In my case it's not possible because I deliver library and try to avoid any node_modules and so on.
I have files in different format and want to include diff dependency format.

Example:
Exported [file].js need to include library myLib/dependecy.js
Exported [file].es.js need to include library myLib/dependecy.es.js
Exported [file].cjs.js need to include library myLib/dependecy.cjs.js

So code in tsconfig.cjs.json:

"baseUrl": "."
"paths": { "myLib/dependecy": ["node_modules/myLib/dependecy.cjs''] }

So path include node_modules and instead of generating:
../node_modules/myLib/dependecy.cjs

we simply can add:
myLib/dependecy.cjs

I think, your don't need paths. Just provide package.json with main: 'dist/some.cjs.js' and module: 'dist/some.mjs'.

Use paths, if you don't want relative imports in your development package.

your_package/src/A.ts

export type A = string

your_package/src/some/B.ts

import {A} from 'your_package/A'

instead of

import {A} from '../A'

tsconfig.json

{
  "compilerOptions": {
    "paths": {
      "your_package/*": [ "./src/*" ]
    }
  }
}

So idea if we resolve something like */node_modules/package/dist/file.js we can generate code 'package/dist/file.js'

paths not for node_modules. packages in node_modules resolved by ts or by rollup/webpack without any configuration.

For multiple packages in repository use lerna, look at zerollup or zerollup-demo as example.

paths not for node_modules.

Anyway it can be used.

When I have file under src folder and path as u mentioned with jquery I will get:
../node_modules/jquery/dist/jquery

And this is one is not very good to have at lib.
So maybe transformer can check path and if it's part of node_module just have:
jquery/dist/jquery

Why do you need alias package in node_modules?
just use

import jquery from 'jquery'

Without any additional configuration.

Simple example in first case I want jquery library, other case substitution almond.

For jquery do.

npm install jquery

index.ts

import jquery from 'jquery'

We discuss totally different topics.
My main question is it possible that transformer instead of ../node_modules/[package] insert just [package]?

What for you need aliasing for packages in node_modules? Show real example.
I try to use something:

tsconfig.json

 "paths": {
    "jsdom/*": [ "jsdom/lib/*" ]
 }
import jsdom from 'jsdom/api'

And got an error: "Module not found"

cevek commented

@oleksandr-dakal You can try classic moduleresolution in your tsconfig.json

    "moduleResolution": "classic",
    "baseUrl": "./",
    "paths": {
      "*": ["node_modules/*"]
    },

This option works like browser module resolution, and it doesn't know anything about node_modules
so, in your code you can write something like

import jquery from 'jquery/dist/jquery'

I will try to describe real example.

Goal:
Library provide files with commonjs and ES6 import syntax.
Any bundler (rollup, webpack) do not used because main target is node.js, so clean tsc usage.

Node.js v8 only support of cjs.
Node.js v9 support cjs and ES6.

Precondition:
Library have dependency External installed in node_modules that export files:
es/file.js (ES6 syntax) - Used in our code
cjs/file.js (commonjs syntax)

tsconfig.json
target: es6 module: es6
Generated file will work ok on node v9.

tsconfig.cjs.json
target: es6 module: commonjs
Generated file will fail on node v8. Because imported es/file.js contain not supported syntax.

This problem can be solved using your transform and ts paths:
External/es/*: ['node_modules/External/cjs/*']

Seems like must work like charm. But generated import path will be ../node_modules/External/cjs/file.js and this will not work.

"module" value in packages.json doesn't work?

{
  "main": "dist/index.cjs.js",
  "module": "dist/index.mjs"
}

Так давай на русском.

"main": "dist/index.cjs.js", "module": "dist/index.mjs"

Это екпорт моего пакета, я же пробую обьяснить что я хочу инклуднуть к себе разные файлы с другой библиотеки.

../node_modules/External/cjs/file.js это же некорректый формат. Я вот не могу взять и зарелизать пакет в npm с таким путем.

Это ответственность нодового ресолва модулей, а не paths в typescript. Достаточно генерить mjs рядом и 10я нода их подхватит.

dist/1.mjs

import {v} from './2'

console.log(v)

dist/1.js

const {v} = require('./2')

console.log(v)

dist/2.mjs

export const v = 123

dist/2.js

module.export.v = 123

node --experimental-modules 1.mjs
node 1.js