Installations
npm install @stavalfi/babel-plugin-module-resolver-loader
Releases
Unable to fetch releases
Developer
stavalfi
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
No
Node Version
8.11.3
NPM Version
6.9.0
Statistics
12 Stars
15 Commits
1 Forks
1 Watching
31 Branches
1 Contributors
Updated on 17 Sept 2024
Languages
JavaScript (95.31%)
TypeScript (4.69%)
Total Downloads
Cumulative downloads
Total Downloads
33,053
Last day
-70%
3
Compared to previous day
Last week
0%
28
Compared to previous week
Last month
-31.9%
280
Compared to previous month
Last year
-62.2%
4,823
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
2
Peer Dependencies
2
Dev Dependencies
37
babel-plugin-module-resolver-loader
Webpack loader for the plugin: babel-plugin-module-resolver by @Tommy
- Installation
- Who Needs It
- How To Use
- Typescript Won't Fix It (Soon)
- Alternative solutions
- Implementation Notes
Installation
webpack 4+
There shouldn't be any reason to lock you on specific versions on the following packages. As a result, you must have under devDependencies:
"@babel/core": "^7.4.4",
"@babel/parser": "^7.4.4"
yarn add --dev @babel/core @babel/parser @stavalfi/babel-plugin-module-resolver-loader
Who Needs It
Library creators who uses Typescript will probably want to generate *.d.ts
files. Incase you are using absolute paths in your code like this:
1// src/some-file-under-src-folder.ts 2export { myType } from 'file-under-src'
1// src/file-under-src.ts 2export const myType = number
Will encounter a serious problem when they will use 'ts-loader' to generate the *.d.ts
files because typescript won't transform back those absolute paths to relative paths when generating all the d.ts
files.
The genrated *.d.ts
files will be:
1// dist/some-file-under-src-folder.d.ts 2export { myType } from 'file-under-src' // BAD - absolute (invalid path)
1// dist/file-under-src.d.ts 2export declare type myType = number
Instead of
1// dist/some-file-under-src-folder.d.ts 2export { myType } from './file-under-src' // GOOD - relative (valid path)
1// dist/file-under-src.d.ts 2export declare type myType = number
Q: Why is this a problem?
A: Your library consumers can't resolve the given absolute path: file-under-src
because when typescript compiler will load the file dist/some-file-under-src-folder.d.ts
, he won't be able to find the module file-under-src
. Error!
How To Use
I advise to use this plugin only in production build because it's not so fast. For development builds, I recommend to use a build system that is similar as https://github.com/stavalfi/jstream.
You have two options:
- You can use the example-project folder in this reporisotry to see how a real-minimal project use it.
- Lerna project with lib that produces
*.d.ts
files and a consumer who uses them: https://github.com/stavalfi/jstream - [must be done in any option] Add this loader before ts-loader:
1{ 2 module: { 3 rules: [ 4 { 5 test: /\.(ts|js)x?$/, 6 exclude: /(node_module|dist)/, 7 use: [ 8 { 9 loader: 'babel-loader', 10 options: { 11 cacheDirectory: true, 12 }, 13 }, 14 { 15 loader: 'ts-loader', 16 }, 17 { 18 loader: '@stavalfi/babel-plugin-module-resolver-loader', 19 options: { 20 // all those options will go directly to babel-plugin-module-resolver plugin. 21 // Read babel-plugin-module-resolver DOCS to see all options: 22 // https://github.com/tleunen/babel-plugin-module-resolver/blob/master/DOCS.md 23 root: ['./src'], 24 extensions: ['.js', '.jsx', '.d.ts', '.ts', '.tsx'], 25 }, 26 }, 27 ], 28 }, 29 ], 30 }, 31}
Note1: You ONLY want to pass the paths of the modules you built WITHOUT passing the location of node_modules and node_modules/@types. Why?
- Because if you pass
node_modules
, the plugin I use will find the external module there and replace the path with a relative path to there. if that external module has types file innode_module/@types
, then typescript won't find it. - if you pass
node_modules/@types
, the plugin I use will find the external module there and replace the path with a relative path to there. There are no source files of that library undernode_module/@types/your_external_lib
so every loader you will ever use will fail to find the source files of the external modules you want to use.
1{ 2 baseUrl: '.', 3 paths: { 4 '*': ['src', 'node_modules'], 5 }, 6}
Note2: The default extensions
doesn't ionclude typescript extentions so we overrire them.
Typescript Won't Fix It (Soon)
From https://github.com/Microsoft/TypeScript/issues/15479:
Our general take on this is that you should write the import path that works at runtime, and set your TS flags to satisfy the compiler's module resolution step, rather than writing the import that works out-of-the-box for TS and then trying to have some other step "fix" the paths to what works at runtime.
We have about a billion flags that affect module resolutions already (baseUrl, path mapping, rootDir, outDir, etc). Trying to rewrite import paths "correctly" under all these schemes would be an endless nightmare.
Alternative solutions
-
You can use
ttypescript
transformers so the paths will be converted when typescript compiles the code (before generating the*.d.ts
files): There are some libraries for that. you can look at here: https://github.com/Microsoft/TypeScript/issues/15479 -
generate one big
*.d.ts
file. There won't be any imports there so all will be good. I couldn't find a working library that does that. -
Writing multiple/single
*.d.ts
files by your self and copy them to the dist folder after every webpack build. You can use plugins to run some cli commands after every build. it's easy. -
Use relative imports when you import types in your
*.ts
files undersrc
folder.
Implementation Notes
- I couldn't use
babel-loader
becuause it uses@babel/core
's transform function.
Q: Why is it a problem?
A: transform function accepts a code and return the transpiled code. This function create AST from the code, change the AST and then create code from the new AST.
To create the first AST, it needs to recognize typescript code. I couldn't teach him typescript code without also changing the typescript code to js code at the end.
so If you run ts-loader after that, ts-loader will get ts files with pure-js code and complain that everything has any type.
So that leaved me with out much options. I couldn't use @babel/core
's transform function also. I must create the AST by my self and do th
The solution is to first create an AST from the code webpack gave me:
1const ast = parser.parse(source, { 2 // source ==== code of a file 3 sourceFilename: this.resourcePath, // webpack gives me absolute path of the source file. Maybe I don't need to set this param here. 4 babelrc: false, 5 sourceType: 'module', 6 plugins: ['jsx', 'typescript'], // these plugins are not available in @babel/core.transform function 7})
so there won't be any transpilation involved.
Then I send this AST directly to the transpilation fucntion (without any extra plugins to transpile anything else bu twhat I want):
1const coreResult = core.transformFromAstSync(ast, source, { 2 filename: this.resourcePath, 3 babelrc: false, 4 plugins: [['module-resolver', options]], 5})
The result (the original source code of this loader):
1const { getOptions } = require('loader-utils') 2const core = require('@babel/core') 3const parser = require('@babel/parser') 4 5module.exports = function(source) { 6 const options = getOptions(this) 7 8 const ast = parser.parse(source, { 9 sourceFilename: this.resourcePath, 10 babelrc: false, 11 sourceType: 'module', 12 plugins: ['jsx', 'typescript'], 13 }) 14 15 const coreResult = core.transformFromAstSync(ast, source, { 16 filename: this.resourcePath, 17 babelrc: false, 18 plugins: [['module-resolver', options]], 19 }) 20 21 return coreResult.code 22}
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
Found 0/10 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
license file not detected
Details
- Warn: project does not have a license file
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 10 are checked with a SAST tool
Reason
51 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-6chw-6frg-f759
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-x9w5-v3q2-3rhw
- Warn: Project is vulnerable to: GHSA-w8qv-6jwh-64r5
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-vh7m-p724-62c2
- Warn: Project is vulnerable to: GHSA-r9p9-mrjm-926w
- Warn: Project is vulnerable to: GHSA-434g-2637-qmqr
- Warn: Project is vulnerable to: GHSA-49q7-c7j4-3p7m
- Warn: Project is vulnerable to: GHSA-977x-g7h5-7qgw
- Warn: Project is vulnerable to: GHSA-f7q4-pwc6-w24p
- Warn: Project is vulnerable to: GHSA-fc9h-whq2-v747
- Warn: Project is vulnerable to: GHSA-8r6j-v8pm-fqw3
- Warn: Project is vulnerable to: MAL-2023-462
- Warn: Project is vulnerable to: GHSA-qqgx-2p2h-9c37
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-6c8f-qphg-qjgp
- Warn: Project is vulnerable to: GHSA-76p3-8jx3-jpfq
- Warn: Project is vulnerable to: GHSA-3rfm-jhwj-7488
- Warn: Project is vulnerable to: GHSA-hhq3-ff78-jv3g
- Warn: Project is vulnerable to: GHSA-jf85-cpcp-j695
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m / GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-fhjf-83wg-r2j9
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-h9rv-jmmf-4pgx
- Warn: Project is vulnerable to: GHSA-hxcc-f52p-wc94
- Warn: Project is vulnerable to: GHSA-4g88-fppr-53pp
- Warn: Project is vulnerable to: GHSA-4jqc-8m5r-9rpr
- Warn: Project is vulnerable to: GHSA-vx3p-948g-6vhq
- Warn: Project is vulnerable to: GHSA-3jfq-g458-7qm9
- Warn: Project is vulnerable to: GHSA-r628-mhmh-qjhw
- Warn: Project is vulnerable to: GHSA-9r2w-394v-53qc
- Warn: Project is vulnerable to: GHSA-5955-9wpr-37jh
- Warn: Project is vulnerable to: GHSA-qq89-hq3f-393p
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-4wf5-vphf-c2xc
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
- Warn: Project is vulnerable to: GHSA-p9pc-299p-vxgp
- Warn: Project is vulnerable to: GHSA-3gx7-xhv7-5mx3
- Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj
Score
1.3
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More