Initial commit

This commit is contained in:
2025-03-07 19:22:02 +01:00
commit 4a98255d83
55743 changed files with 5280367 additions and 0 deletions
+3
View File
@@ -0,0 +1,3 @@
/.test/**
/dist/**
/test/**
+37
View File
@@ -0,0 +1,37 @@
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
extends: ['plugin:node/recommended'/*, 'plugin:prettier/recommended'*/],
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
},
settings: {
node: {
tryExtensions: ['.js', '.json', '.ts', '.d.ts'],
},
},
rules: {
// 'no-process-exit': 'off', // to investigate if we should throw an error instead of process.exit()
// 'node/no-unsupported-features/es-builtins': 'off',
},
overrides: [
{
files: ['*.ts'],
extends: [
'plugin:@typescript-eslint/recommended', // Uses the recommended rules from the @typescript-eslint/eslint-plugin
'prettier', // Uses eslint-config-prettier to disable ESLint rules from @typescript-eslint/eslint-plugin that would conflict with prettier
],
rules: {
'node/no-unsupported-features/es-syntax': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-unused-vars': 'off',
// '@typescript-eslint/explicit-function-return-type': 'off',
// '@typescript-eslint/no-namespace': 'off' // maybe we should consider enabling it in the future
'@typescript-eslint/consistent-type-imports': 'error', // the replacement of "importsNotUsedAsValues": "error"
},
},
],
};
+1
View File
@@ -0,0 +1 @@
/test/**
+8
View File
@@ -0,0 +1,8 @@
{
"arrowParens": "avoid",
"singleQuote": true,
"semi": true,
"tabWidth": 2,
"useTabs": false,
"printWidth": 80
}
+750
View File
@@ -0,0 +1,750 @@
# Changelog
## 9.5.2
* [fix: add more detailed error messages](https://github.com/TypeStrong/ts-loader/pull/1665) - thanks @hai-x
## 9.5.1
* [fix: inputSourceMap can be null](https://github.com/TypeStrong/ts-loader/pull/1639) [#1638] - thanks @johnnyreilly and @michaeltford
## 9.5.0
* [Feature: map the input source map in case ts-loader is used in a loader pipeline](https://github.com/TypeStrong/ts-loader/pull/1626) - thanks @Ka0o0 and @bojanv55
## 9.4.4
* [Bug fix: let users override skipLibCheck](https://github.com/TypeStrong/ts-loader/pull/1617) - thanks @haakonflatval-cognite
## 9.4.3
* [Bug fix: add config file as build dependency](https://github.com/TypeStrong/ts-loader/pull/1611) - thanks @alexander-akait
## 9.4.2
* [Bug fix: Use custom transformer when building solution references](https://github.com/TypeStrong/ts-loader/pull/1550) [#1025] - thanks @feosuna1
## 9.4.1
* [Hotfix: Disable `enhanced-resolve`](https://github.com/TypeStrong/ts-loader/pull/1505) - thanks @manuth
## v9.4.0
* [Add Support for Resolving `.cjs`, `.mjs`, `.cts` and `.mts` Files](https://github.com/TypeStrong/ts-loader/pull/1503) [#1503] - thanks @manuth
## v9.3.1
* [Bug fix: Generate declaration files for js files if allowJs is set to true](https://github.com/TypeStrong/ts-loader/pull/1483) [#1260] - thanks @hediet and @mvilliger
## v9.3.0
* [simplify configuration for fork-ts-checker-webpack-plugin](https://github.com/TypeStrong/ts-loader/pull/1451) - thanks @piotr-oles
## v9.2.9
* [make v9 latest following v8 release](https://github.com/TypeStrong/ts-loader/pull/1447) - thanks @johnnyreilly
## v9.2.8
* [Bug fix: support webpack 5 in ts-loader](https://github.com/TypeStrong/ts-loader/pull/1439) [#1438] - thanks @einatbar
## v9.2.7
* [cater for change in resolveTypeReferenceDirective API in TypeScript 4.7](https://github.com/TypeStrong/ts-loader/pull/1422) [#1421] - thanks @johnny_reilly and @cspotcode for inspiration in ts-node work here: https://github.com/TypeStrong/ts-node/pull/1648
## v9.2.6
* [Docs fix for thread-loader / history](https://github.com/TypeStrong/ts-loader/pull/1377) - thanks @johnnyreilly
## v9.2.5
* [Add function to get the latest program](https://github.com/TypeStrong/ts-loader/pull/1352) - thanks @Zn4rK
## v9.2.4
* [Fix undefined configPath now falls back to default](https://github.com/TypeStrong/ts-loader/pull/1346) - thanks @johnnyreilly
## v9.2.3
* [Fix error message for invalid getCustomTransformers modules](https://github.com/TypeStrong/ts-loader/issues/1334) - thanks @blaky
## v9.2.2
* [Start consuming webpack loader types](https://github.com/TypeStrong/ts-loader/issues/1325) - thanks @johnnyreilly
* [Add webpack minimum version in peerDependencies](https://github.com/TypeStrong/ts-loader/issues/1324) - thanks @afdev82
## v9.2.1
* [Make v9 latest in npm again](https://github.com/TypeStrong/ts-loader/issues/1320) - thanks @johnnyreilly
## v9.2.0
* [Fixed impossibility to have several instances of ts-loader with different compiler options](https://github.com/TypeStrong/ts-loader/issues/1316) - thanks @timocov
## v9.1.2
* [Fix removed files handling in watch mode](https://github.com/TypeStrong/ts-loader/pull/1293) - thanks @gasnier
## v9.1.1
* [update CHANGELOG.md for 8.2.0 release](https://github.com/TypeStrong/ts-loader/pull/1291) - thanks @johnnyreilly
## v9.1.0
* [Use caches for module resolution and type reference directives when using compiler default functions](https://github.com/TypeStrong/ts-loader/pull/1287) - thanks @sheetalkamat - uses: https://github.com/microsoft/TypeScript/pull/43700
## v9.0.2
* [Remove usage of loader-utils](https://github.com/TypeStrong/ts-loader/pull/1288) - thanks @jonwallsten
## v9.0.1
* [Use correct hook for emitting additional assets during compilation](https://github.com/TypeStrong/ts-loader/pull/1286) - thanks @jonwallsten
## v9.0.0
Breaking changes:
- minimum webpack version: 5
- minimum node version: 12
Changes:
* [webpack 5 migration](https://github.com/TypeStrong/ts-loader/pull/1251) - thanks @johnnyreilly, @jonwallsten, @sokra, @appzuka, @alexander-akait
## v8.4.0
* [fix: cater for change in resolveTypeReferenceDirective API in 4.7](https://github.com/TypeStrong/ts-loader/pull/1446) - thanks @dragomirtitian
* This is a backport from v9.2.7 for webpack 4 compatibility
## v8.3.0
* [Fixed impossibility to have several instances of ts-loader with different compiler options](https://github.com/TypeStrong/ts-loader/issues/1316) - thanks @timocov
* This is a backport from v9.2.0 for webpack 4 compatibility
## v8.2.0
* [Use caches for module resolution and type reference directives when using compiler default functions](https://github.com/TypeStrong/ts-loader/pull/1287) - thanks @sheetalkamat - uses: https://github.com/microsoft/TypeScript/pull/43700
* This is a backport from v9.1.0 for webpack 4 compatibility
## v8.1.0
* [feat: remove top-level typescript import statements](https://github.com/TypeStrong/ts-loader/pull/1259) - thanks @ulivz
## v8.0.18
* [Perf: Optimize fileExists callback path](https://github.com/TypeStrong/ts-loader/issues/1266) - thanks @berickson1
## v8.0.17
* [Included correct webpack source location in emitted errors](https://github.com/TypeStrong/ts-loader/issues/1199) - thanks @lorenzodallavecchia
## v8.0.16
* [Re-Fixed missing errors in watch mode in webpack5](https://github.com/TypeStrong/ts-loader/issues/1204) - thanks @appzuka
## v8.0.15
* [Update definition files in watch mode in webpack@5](https://github.com/TypeStrong/ts-loader/pull/1249) - thanks @appzuka,@JonWallsten,@alexander-akait
* [Add afterDeclarations to getCustomTransformers in README.md](https://github.com/TypeStrong/ts-loader/pull/1248) - thanks @appzuka
## v8.0.14
* [Upgrade `chalk`, `loader-utils`, and `semver` to latest stable versions](https://github.com/TypeStrong/ts-loader/pull/1237) - thanks Avi Vahl
## v8.0.13
* [Speed up builds by adding an in-memory cache to file path lookups](https://github.com/TypeStrong/ts-loader/pull/1228) - thanks @berickson1
## v8.0.12
* [Instead of checking date, check time thats more accurate to see if something has changed](https://github.com/TypeStrong/ts-loader/pull/1217) - thanks @sheetalkamat
## v8.0.11
* [Fixed build failing in yarn v2 pnp](https://github.com/TypeStrong/ts-loader/pull/1209) - thanks @aicest
## v8.0.10
* [Fixed missing errors in watch mode in webpack5](https://github.com/TypeStrong/ts-loader/issues/1204) - thanks @appzuka
## v8.0.9
* [Fixed build failing when using thread-loader](https://github.com/TypeStrong/ts-loader/pull/1207) - thanks @valerio
## v8.0.8
* [Fixed memory leak when using multiple webpack instances](https://github.com/TypeStrong/ts-loader/pull/1205) - thanks @valerio
## v8.0.7
* [Speeds up project reference build and doesnt store the result in memory](https://github.com/TypeStrong/ts-loader/pull/1202) - thanks @sheetalkamat
## v8.0.6
* [Fixed further deprecation warning on webpack@5](https://github.com/TypeStrong/ts-loader/issues/1196) - thanks @appzuka
## v8.0.5
* [Fixed deprecation warnings on webpack@5](https://github.com/TypeStrong/ts-loader/issues/1194) - thanks @sanex3339
## v8.0.4
* [Uses existing instance if config file is same as already built solution](https://github.com/TypeStrong/ts-loader/pull/1177) - thanks @sheetalkamat
## v8.0.3
* [Fix the wrong instance caching when using `appendTsSuffixTo` and `appendTsxSuffixTo` together](https://github.com/TypeStrong/ts-loader/pull/1170) - thanks @meowtec
## v8.0.2
* [Fix 2 issues with experimentalWatchApi](https://github.com/TypeStrong/ts-loader/pull/1159) - thanks @appzuka
## v8.0.1
* [Fix webpack deprecations](https://github.com/TypeStrong/ts-loader/pull/1135) - thanks @g-plane
## v8.0.0
* [Support for symlinks in project references](https://github.com/TypeStrong/ts-loader/pull/1136) - thanks @sheetalkamat!
* `ts-loader` now supports TypeScript 3.6 and greater **BREAKING CHANGE**
## v7.0.5
* [Add a delay before starting the comparison tests to avoid failures under WSL](https://github.com/TypeStrong/ts-loader/pull/1109) - thanks @appzuka
* [Apply other loaders when updating files in watch mode](https://github.com/TypeStrong/ts-loader/pull/1115) - thanks @iorate
## v7.0.4
* [Ensure a separate webpack instance is created for different loader options](https://github.com/TypeStrong/ts-loader/pull/1104) - thanks @appzuka
## v7.0.3
* [Ensure that JSON files are included in build module resolution](https://github.com/TypeStrong/ts-loader/pull/1101) - thanks @berickson1
## v7.0.2
* [Make content hash consistent across machines](https://github.com/TypeStrong/ts-loader/pull/1085) - thanks @elyalvarado
## v7.0.1
* [fix: watch-run](https://github.com/TypeStrong/ts-loader/pull/1083) - thanks @zn4rk
## v7.0.0
* [Project reference support enhancements](https://github.com/TypeStrong/ts-loader/pull/1076) - thanks @sheetalkamat!
* Following the end of life of Node 8, `ts-loader` no longer supports Node 8 **BREAKING CHANGE**
## v6.2.2
* [Enable typescript 3.8.3 support when using `webpack.config.ts` files](https://github.com/TypeStrong/ts-loader/issues/1072) - thanks @vladimiry!
## v6.2.1
* [Output types alongside JS files, enable declaration maps](https://github.com/TypeStrong/ts-loader/pull/1026) - thanks @meyer!
## v6.2.0
* [Emitting .tsbuildinfo when using watch api](https://github.com/TypeStrong/ts-loader/pull/1017) - thanks @sheetalkamat!
## v6.1.2
* [don't emit declaration files for a declaration file](https://github.com/TypeStrong/ts-loader/pull/1015) (#1014) - thanks @gvinaccia!
* [Consume typescript apis from typescript nightly](https://github.com/TypeStrong/ts-loader/pull/1016) - thanks @sheetalkamat!
## v6.1.1
* [Fix SolutionBuilder watches](https://github.com/TypeStrong/ts-loader/pull/1003) and [related fixes](https://github.com/TypeStrong/ts-loader/pull/1011) (#998) - thanks @sheetalkamat!
* [fix: no errors reported if flagged with @ts-check](https://github.com/TypeStrong/ts-loader/pull/1008) (#1004) - thanks @reinholdk!
## v6.1.0
* [Build upstream project references with SolutionBuilder](https://github.com/TypeStrong/ts-loader/pull/935) (#851, #913) - thanks @sheetalkamat!
## v6.0.4
* [Fix issue when handling files not included in tsconfig.json](https://github.com/TypeStrong/ts-loader/issues/943) (#934) - thanks @davazp!
## v6.0.3
* [Upgrade typescript version to 3.5.2](https://github.com/TypeStrong/ts-loader/pull/954) (#954) - thanks @fa93hws
## v6.0.2
* [Set configFilePath when reading config file](https://github.com/TypeStrong/ts-loader/pull/942) (#939) - thanks @konpikwastaken!
## v6.0.1
* [Fix issue with `resolveTypeReferenceDirective` causing errors like `Cannot find name 'it'` with Jest](https://github.com/TypeStrong/ts-loader/pull/936) (#934) (#919) - thanks @andrewbranch!
* [Fix TypeScript diagnostics not being printed to console when using project references](https://github.com/TypeStrong/ts-loader/pull/937) (#932) - thanks @andrewbranch!
## v6.0.0
* [Drop support for node < 8.6 related to micromatch upgrade to 4](https://github.com/TypeStrong/ts-loader/pull/930); see: https://github.com/TypeStrong/ts-loader/issues/929
* [Update dependencies](https://github.com/TypeStrong/ts-loader/pull/928) - thanks @johnnyreilly!
## v5.4.5
* [use @types/webpack for loader typings](https://github.com/TypeStrong/ts-loader/pull/927) - thanks @LukeSheard!
## v5.4.4
* [refactor: add common appendTsTsxSuffixesIfRequired function to instance](https://github.com/TypeStrong/ts-loader/pull/924) - thanks @johnnyreilly!
## v5.4.3
* [feat: resolveTypeReferenceDirective support for yarn PnP](https://github.com/TypeStrong/ts-loader/pull/921) - thanks @johnnyreilly!
* [fix: don't include anything apart from ts-loader in publish](https://github.com/TypeStrong/ts-loader/pull/923) - thanks @johnnyreilly!
## v5.3.3
* [fix: Pass ts.Program to getCustomTransformers](https://github.com/TypeStrong/ts-loader/pull/889) (#860) - thanks @andersekdahl!
## v5.3.2
* [feat: enable experimentalFileCaching by default](https://github.com/TypeStrong/ts-loader/pull/885) (#868) - thanks @timocov!
## v5.3.1
* [fix: projectReferences with rootDir](https://github.com/TypeStrong/ts-loader/pull/871) (#868) - thanks @andrewbranch!
## v5.3.0
* [feat: Exposes a `resolveNodeModule` option](https://github.com/TypeStrong/ts-loader/pull/862) - thanks @arcanis!
## v5.2.2
* [feat: Micro-optimizations](https://github.com/TypeStrong/ts-loader/pull/855) - thanks @johnnyreilly
## v5.2.1
* [feat: Lists typescript as a peer dependency](https://github.com/TypeStrong/ts-loader/pull/841) - thanks @arcanis!
## v5.2.0
* [feat: Initial support for project references - `projectReferences`](https://github.com/TypeStrong/ts-loader/pull/817) - thanks @andrewbranch!
## v5.1.1
* [fix(getTranspilationEmit): pass the raw path to transpileModule](https://github.com/TypeStrong/ts-loader/pull/835) - thanks @Brooooooklyn
## v5.1.0
* [feat: Added cache for some FS operations while compiling - `experimentalFileCaching`](https://github.com/TypeStrong/ts-loader/pull/829) - thanks @timocov!
## v5.0.0
* [feat: Fixed issue with incorrect output path for declaration files](https://github.com/TypeStrong/ts-loader/pull/822) - thanks @JonWallsten! **BREAKING CHANGE**
## v4.5.0
* [feat: Added support for TypeScript declaration map](https://github.com/TypeStrong/ts-loader/pull/821) - thanks @JonWallsten!
## v4.4.2
* [fix(loader): new Error to webpack when errors occured in the loader function](https://github.com/TypeStrong/ts-loader/pull/792) - thanks @linxiaowu66 and @systemmetaphor!
## v4.4.1
* [fix(types): expose public interfaces from root index.d.ts](https://github.com/TypeStrong/ts-loader/pull/790) - thanks @Hotell!
## v4.4.0
* [feat: generate ambient types from implementation](https://github.com/TypeStrong/ts-loader/pull/788) - thanks @Hotell!
* [error when not using webpack 4](https://github.com/TypeStrong/ts-loader/pull/786) - thanks @johnnyreilly
## v4.3.1
* [Fix options caching when ts-loader is used in multiple rules](https://github.com/TypeStrong/ts-loader/pull/782) - thanks @yyx990803!
Please note, this bug fix requires that vue-loader users still using v14 should either upgrade to v15 or explicitly pass the same ts-loader options via v14's loaders option. [See more details here](https://github.com/TypeStrong/ts-loader/pull/782#issuecomment-394406093)
## v4.3.0
* [Fix dependency resolution when using pnpm](https://github.com/TypeStrong/ts-loader/pull/774) - thanks @xbtsw and @zkochan!
* [Add `allowTsInNodeModules` option for importing .ts files from node_modules](https://github.com/TypeStrong/ts-loader/pull/773) - thanks @aelawson!
## v4.2.0
* [Pass `context' to error formatters](https://github.com/TypeStrong/ts-loader/pull/756) - thanks @gustavderdrache!
## v4.1.0
* [Fix slow `experimentalWatchApi`](https://github.com/TypeStrong/ts-loader/pull/747) (#746) - thanks @sheetalkamat and @MLoughry!
* [feat: `getCustomTransformers` support path string for a module](https://github.com/TypeStrong/ts-loader/pull/745) - thanks @vagusX and @s-panferov (upon whose work this is based I believe)
## v4.0.1
* [Fix name collision in experimentalWatchApi code](https://github.com/TypeStrong/ts-loader/pull/737) - thanks @MLoughry!
## v4.0.0
* Support webpack 4
* Drop support for webpack 2/3 **BREAKING CHANGE** - use ts-loader 3.x if you need webpack 2/3 support
* Minimum TypeScript version is now 2.4.1 **BREAKING CHANGE**
* Deprecated option `entryFileCannotBeJs` removed' **BREAKING CHANGE**
* Start using [prettier](https://prettier.io/) for the codebase
## v3.5.0
* [Add trace for traceResolution](https://github.com/TypeStrong/ts-loader/pull/721) - thanks @onigoetz!
## v3.4.0
* [local .d.ts files now marked as changed when watch is triggered](https://github.com/TypeStrong/ts-loader/pull/698) - thanks @KnisterPeter!
## v3.3.1
* [Fixes to support watch api for compiling - lib support etc](https://github.com/TypeStrong/ts-loader/pull/715) - thanks @sheetalkamat!
## v3.3.0
* [Report diagnostics only on certain files with `reportFiles` option](https://github.com/TypeStrong/ts-loader/pull/701) - thanks @freeman!
* [Replaced option `contextAsConfigBasePath` with `context` option.](https://github.com/TypeStrong/ts-loader/pull/688/) Strictly speaking a breaking change. However, given the original option was never able to fulfil its intended purpose I've decided to treat this as just a new feature; there seems no possibility that anyone can be using `contextAsConfigBasePath` - thanks @christiantinauer!
* [Added support for the new watch api of TypeScript compiler.](https://github.com/TypeStrong/ts-loader/pull/685) nb This feature has been placed behind a new `experimentalWatchApi` option until it has been thoroughly tested. All being well it is likely to become the default behaviour for ts-loader in future - thanks @sheetalkamat!
## v3.2.0
* [Add new loader option `contextAsConfigBasePath`](https://github.com/TypeStrong/ts-loader/pull/681) - thanks @christiantinauer
## v3.1.1
* [Fix error importing buildt ts files with allowJs](https://github.com/TypeStrong/ts-loader/pull/674) (#667) - thanks @Pajn!
## v3.1.0
* [Add `onlyCompileBundledFiles` option which modifies behaviour to load only those files that are actually bundled by webpack](https://github.com/TypeStrong/ts-loader/pull/671) #267 - thanks @maier49!
* [Chore release; upgraded chalk dependency in `package.json` to 2.3, as 2.3 is another breaking changes release (from a TypeScript perspective).](https://github.com/TypeStrong/ts-loader/issues/664), see [here](https://github.com/chalk/chalk/issues/215) for context - thanks @johnnyreilly
## v3.0.5
* [Chore release; upgraded chalk dependency in `package.json` to 2.2, as 2.2 appears to be a breaking changes release.](https://github.com/TypeStrong/ts-loader/issues/664) - thanks @lmk123 for reporting
## v3.0.4
* [Chore release; upgraded chalk dependency.](https://github.com/TypeStrong/ts-loader/pull/662) - thanks @johnnyreilly
## v3.0.3
* [Fix allowJs @types resolution error](https://github.com/TypeStrong/ts-loader/pull/658) (#657, #655) - thanks @johnnyreilly and @roddypratt + @ldrick for providing minimal repro repos which allowed me to fix this long standing bug!
This fix resolves the issue for TypeScript 2.4+ (which is likely 95% of users). For those people stuck on 2.3 or below and impacted by this issue, you should be able to workaround this by setting `entryFileCannotBeJs: true` in your ts-loader options. This option should be considered deprecated as of this release. The option will likely disappear with the next major version of ts-loader which will drop support for TypeScript 2.3 and below, thus removing the need for this option.
## v3.0.0
All changes were made with this [PR](https://github.com/TypeStrong/ts-loader/pull/643) - thanks @johnnyreilly
([Published to npm as v3.0.2 due to npm publishing issues](https://github.com/TypeStrong/ts-loader/issues/654)) thanks @mattlewis92 for noticing!
* drop support for typescript < 2.0 (no-one seems to be using it and we can simplify the code) **BREAKING CHANGE**
* remove `entryFileIsJs` option; it can be inferred from whether the `allowJs` TypeScript compiler option has been set.
* move to webpack 3.0 for test harness
* drop `configFileName` support [(replaced by `configFile`)](https://github.com/TypeStrong/ts-loader/pull/607) **BREAKING CHANGE**
* add support for a custom formatter for output - drop visual studio format (this can be added back if there's clamour for it and people can supply their own formatters in the interim) **BREAKING CHANGE**
* make loglevel warn by default (stop outputting typescript version number by default). Fixes [#488](https://github.com/TypeStrong/ts-loader/issues/488)
* fix [tsc has "module" default to "es2015"when targetting es2015+, but ts-loader does not](https://github.com/TypeStrong/ts-loader/issues/570) - thanks [@Venryx](https://github.com/Venryx) for the suggestion!
* [switch to build ts-loader / run tests with yarn](https://github.com/TypeStrong/ts-loader/issues/369) because of [this](https://stackoverflow.com/questions/45022048/why-does-npm-install-rewrite-package-lock-json/45566871#45566871)
* allow controlling whether the output can contain colours
## v2.3.7
* [Start validating the options supplied to the loader](https://github.com/TypeStrong/ts-loader/pull/630) (#629) - thanks @johnnyreilly!
## v2.3.6
* [Fix kills ts-loader dependant builds issue](https://github.com/TypeStrong/ts-loader/pull/627) (#626) - thanks @Loilo!
## v2.3.5
* [Add an additional check for js files before reusing isExternalLibaryImport](https://github.com/TypeStrong/ts-loader/pull/622) (#620) - thanks @WillMartin!
* [Make TypeScript `basePath` configurable](https://github.com/TypeStrong/ts-loader/pull/621) (#618) - thanks @Loilo!
* [Fix relative configFile path](https://github.com/TypeStrong/ts-loader/pull/618) (#617) - thanks @Loilo!
## v2.3.4
* [Add `configFile` option](https://github.com/TypeStrong/ts-loader/pull/607) - thanks @Loilo!
## v2.3.3
* [fix(tsconfig): stop passing rootDir option to TypeScript compiler](https://github.com/TypeStrong/ts-loader/pull/598) (#597) - thanks @Brooooooklyn
* [Fix findConfigFile in Windows](https://github.com/TypeStrong/ts-loader/pull/605) (#604) - thanks @mengxy
## v2.3.2
* [Move to use strictNullChecks](https://github.com/TypeStrong/ts-loader/pull/589) - thanks @johnnyreilly
* [`allowJs` supports importing types from external libraries](https://github.com/TypeStrong/ts-loader/pull/590) (#586, #577) - thanks @bsouthga!
## v2.3.1
* [Fix undefined watcher in watch-run causes error](https://github.com/TypeStrong/ts-loader/pull/587) (#585) - thanks @zinserjan and @sokra!
## v2.3.0
* [add appendTsxSuffixTo option to support using tsx with Vue](https://github.com/TypeStrong/ts-loader/pull/581) - lots of discussion went into this PR. Thanks to @vhqtvn (author) and @HerringtonDarkholme, @johnnyreilly, @jbrantly, @octref, @rhyek and others for helping us land on our final implementation.
* [refactor: Use chalk instead of colors](https://github.com/TypeStrong/ts-loader/pull/579) - thanks @develar!
## v2.2.2
* [Remove default of setting isolatedModules to true when in transpileOnly mode](https://github.com/TypeStrong/ts-loader/pull/569) - thanks @johnnyreilly and @donaldpipowitch
## v2.2.1
* [Report errors in JS(X) files when CheckJS is enabled](https://github.com/TypeStrong/ts-loader/pull/564) - thanks @schmuli!
* [Cater for change to @types acquisition strategy in TypeScript 2.4.1](https://github.com/TypeStrong/ts-loader/pull/566) - thanks @johnnyreilly
## v2.2.0
* [Support custom transformers for ts](https://github.com/TypeStrong/ts-loader/pull/535) - thanks @longlho and @Igorbek!
## v2.1.0
* [Add happypack compatibility mode](https://github.com/TypeStrong/ts-loader/pull/547) - thanks @aindlq!
## v2.0.3
* [Don't include appended TS extension in webpack dependencies](https://github.com/TypeStrong/ts-loader/pull/497) - thanks again @wearymonkey!
## v2.0.2
* [Fix performance regression related to using getTimes() by tracking timestamps](https://github.com/TypeStrong/ts-loader/pull/500) - thanks @wearymonkey
## v2.0.1
* [make watch resilient to no watcher / watcher.mtimes](https://github.com/TypeStrong/ts-loader/pull/482) - thanks @bancek and @mredbishop
* [move to using loader-utils 1.0](https://github.com/TypeStrong/ts-loader/pull/475)
## v2.0.0
* [Add support for IgnoringWatchFileSystem](https://github.com/TypeStrong/ts-loader/pull/444) - thanks @herschel666
* [Use native Object.assign()](https://github.com/TypeStrong/ts-loader/pull/418) - thanks @arusakov
Breaking changes:
* ts-loader now officially only supports webpack 2. ts-loader 2.x may work with webpack 1 but it is not supported. Related to that, all continuous integration tests now run against webpack 2.
* as webpack 2 does not support node 0.12 neither does ts-loader from now. node 4 at least is required.
## v1.3.3
* [Fix bug when "extend"ing a tsconfig that specifies "allowJs"](https://github.com/TypeStrong/ts-loader/pull/415) Thanks @cspotcode
* [Minor perf optimisations](https://github.com/TypeStrong/ts-loader/pull/412)
## v1.3.2
* [Upgrade enhanced-resolve to v3](https://github.com/TypeStrong/ts-loader/pull/411)
* [Remove arrify dependency](https://github.com/TypeStrong/ts-loader/pull/410)
## v1.3.1
* [Rolled back re-exported const enums no longer break emit in watch mode as performance cost was too high](https://github.com/TypeStrong/ts-loader/pull/406) resolves #393
## v1.3.0
* [Introduce meaningful error when importing TypeScript from `node_modules`](https://github.com/TypeStrong/ts-loader/pull/399)
* [Introduce `entryFileIsJs` loader option which allows having an entry file which is js.](https://github.com/TypeStrong/ts-loader/pull/399) resolves #388 and #401 - thanks @Wykks and @pqr.
NB Previously the `entryFileIsJs` option was on by default when `allowJs` was true. Now it has to be specified directly. Strictly speaking this is a breaking change; however given this is a rarely used option which exists for what is arguably an edge case this is being added without moving to 2.0. If this breaks people then we'll never do this again; I'd be surprised if anyone is relying on this though so we're taking a chance. Related tests have been suffixed "-entryFileIsJs" in the test name.
## v1.2.2
* [Re-exported const enums no longer break emit in watch mode](https://github.com/TypeStrong/ts-loader/pull/377) [#376] - thanks @smphhh
* [typescript.sys should be compiler.sys](https://github.com/TypeStrong/ts-loader/pull/380) [#379] - thanks @johnnyreilly and @jbrantly
## v1.2.1
* [Fix TS module resolution paths on Windows - watch mode becomes faster](https://github.com/TypeStrong/ts-loader/pull/373) [#372] - thanks @smphhh
## v1.2.0
* [Crash when adding/removing files in watch-mode](https://github.com/TypeStrong/ts-loader/pull/364) [#358] - thanks @jbbr for the suggested fix
* [Provided an option to produce Visual Studio compatible error output](https://github.com/TypeStrong/ts-loader/pull/356) [#355] - thanks @gamli
## v1.1.0
* [Added support for vuejs via `appendTsSuffixTo` option](https://github.com/TypeStrong/ts-loader/pull/354) [#270] - thanks @HerringtonDarkholme
## v1.0.0
* [General refactor of ts-loader; some performance improvements](https://github.com/TypeStrong/ts-loader/pull/343) [#335] - thanks @johnnyreilly
* [Make the loader resilient to watched declaration files being removed.](https://github.com/TypeStrong/ts-loader/pull/281) - thanks @opichals
## v0.9.5
* [Improve performance for watch mode / `after-compile` plugin](https://github.com/TypeStrong/ts-loader/pull/187) - thanks @Strate
## v0.9.4
* [Make logging to stderr or stdout configurable; introduce logging levels](https://github.com/TypeStrong/ts-loader/pull/313) [#214] - thanks @ThYpHo0n
* [Fix regression that broke hot module replacement](https://github.com/TypeStrong/ts-loader/pull/322) [#321] - thanks @dopare
## v0.9.3
* [Added support for allowJs](https://github.com/TypeStrong/ts-loader/pull/320) (#316) - thanks @dschnare
## v0.9.2
* [Added support for @types](https://github.com/TypeStrong/ts-loader/pull/318) (#247) -thanks @basarat for the ideas
## v0.9.1
* [Normalize dependency graph paths - Fix broken dependencies on Windows ](https://github.com/TypeStrong/ts-loader/pull/286) - thanks @pzavolinsky
* [Fixed the declaration issue](https://github.com/TypeStrong/ts-loader/pull/307) (#214 part deux) - thanks @dizel3d
## v0.9.0
* [Made ts-loader compatible with node v6](https://github.com/TypeStrong/ts-loader/commit/a4f835345e495f45b40365f025afce72d1817996) - thanks @Blechhirn
* [Fixed the declaration issue](https://github.com/TypeStrong/ts-loader/commit/3bb0fec73a2fab47953b51d256f0f5378f236ad1) (#214) - thanks @17cupsofcoffee
* [Declarations update independent of compiler.watchFileSystem](https://github.com/TypeStrong/ts-loader/pull/167/commits/ae824b2676b226bdd0c860a787754a4ae28e339c) (#155) - thanks @opichals
Now built using TypeScript v2.0
## v0.8.2
* Elided imports are now watched (#156, #169)
* Declaration files for `.d.ts` files are now emitted (thanks @rob-bateman) (#174, #175)
## v0.8.1
* Add better error messaging when a file in tsconfig.json can not be loaded (#117, #145)
* Fix incompatibility with html-webpack-plugin (#152, #154)
## v0.8.0
* Add support for emitting declaration files when `declaration: true` is set (#48, #128)
* Fix bug with specifying `target: es6` and `module: commonjs` at the same time when using
TS 1.7+ (#111, #132, #140).
* Fix bug with resolving dependencies which are linked using `npm link` (#134, #141)
## v0.7.2
* Fix regression with watching definition files (#109, #110)
## v0.7.1
* Fix regression with Windows that was introduced in v0.7.0 (#92)
## v0.7.0
* Fix bug with webpack resolution that could sometimes cause TypeScript to not find modules (#92, #102)
* Loader output is now written to stderr instead of stdout. (#95, #103)
## v0.6.1
* Improve initial build performance significantly for larger projects (#100)
* Fix issue with nightly (#96)
## v0.6.0
* Remove support for 1.5 and 1.6-beta. TypeScript 1.6 (stable) is the now the lowest version
supported.
* Fix issue when using source maps and Babel in certain situations (#81)
* Fix issue with nightly (#83)
## v0.5.6
* Add ignoreDiagnostics feature
* Fix issue with node resolution and `noEmitOnError` (#71)
## v0.5.5
* Fix issue with nightly (Microsoft/TypeScript#4738)
* Add support for the NoErrorsPlugin
## v0.5.4
* Fix issue with nightly (Microsoft/TypeScript#4497)
## v0.5.3
* Utilize TypeScript's new custom module resolution logic to integrate with webpack. This essentially
means that TypeScript will resolve files exactly the same as webpack does (supporting aliases, etc).
See the [aliasResolution test](test/aliasResolution) for an example. Only supported in TS 1.6 and
above.
* Rework error reporting to resolve certain edge cases with dependencies. In general errors should
be much more consistent now in watch mode.
* Fix issue with targeting ES6 and transpile mode (#36)
## v0.5.2
* Fix issue with TypeScript nightly and new node module resolution strategy (#34)
## v0.5.1
* Tweaked error message output to include error code (#32)
* Add helpful messages around the TypeScript dependency
* Suggest how to install TypeScript if it hasn't been installed
* Show TypeScript version when compiling
* Warn if TypeScript version is incompatible
## v0.5.0
* Add support for `transpileOnly` loader option. See README for more information.
* TypeScript is no longer a dependency of the loader and must be installed separately
* Loader options can now be set as a property in `webpack.config.js`
* TypeScript options can be set through the loader option `compilerOptions`
* Improved error reporting
* Errors from all files in the TypeScript application are now reported in watch mode instead of
from just those files that changed. This means that making a breaking change in a dependency
will now be correctly reported as an error in the dependent file.
* Errors with TypeScript options are now reported as webpack errors instead of logged to console
* Error output no longer contains the filename once from webpack and again in the error message.
Instead, the filename is only reported by webpack
* Fixed issue with latest version of webpack where filenames could be reported twice for the same
error in certain situations
* Using the `declaration` TypeScript option no longer results in errors
* Add support for the `newLine` TypeScript option
* Tests have been revamped to be full integration tests with nightly builds against the current stable
and nightly TypeScript. Many new tests have been added.
## v0.4.7
* Update TypeScript dependency to 1.5 release (1.5.3)
## v0.4.6
* Improve error reporting related to tsconfig.json
* Fix bug that reported the wrong errors
* Errors are now reported as webpack errors instead of logged to console
* Add support for latest TypeScript nightly (#24)
## v0.4.5
* Add `silent` flag (#22)
## v0.4.4
* Add support for "noLib" compiler option (#19)
* Make errors easier to parse programmatically (#20)
* Errors in declaration files are now added to the stats object instead of written to console
* Errors now include `file`, `rawMessage`, and `location` properties
* Make --watch option more robust
* Fix issue where changes to entry file were not detected
* Fix issue where changes to typing information only did not result in a rebuild (#21)
## v0.4.3
* Fix error locations to be 1-based instead of 0-based (#18)
## v0.4.2
* Rework the way dependencies are loaded (#14)
* Fix NPM dependency on TypeScript (#15, #16)
## v0.4.1
* Fix Windows issue with paths (#14)
## v0.4.0
* TypeScript 1.5 support! (#14)
* tsconfig.json support (#2, #9)
* ES6 target support
* Remove TS-related options in favor of specifying them in tsconfig.json
* Add `configFileName` option for custom tsconfig files
## v0.3.4
* Exclude TS 1.5 as a dependency since there are breaking changes
## v0.3.3
* Add support for reporting errors in declaration files (#10)
* Add support for watch mode for declaration files (#11)
* Fix issue with extra `sourceMappingURL` in output files (#12)
## v0.3.2
* Add support for manually adding files (#6)
* Add paths to source maps (#8)
## v0.3.1
* Add support for specifying a custom TypeScript compiler
## v0.3.0
* Change how modules are resolved. Imports and declaration file references are
now resolved through TypeScript instead of being resolved through webpack's
`resolve` API. This fixes a number of issues and better aligns the loader to
work as a replacement for the `tsc` command. (#3, #4, #5)
## v0.2.3
* Add noImplicitAny option (#2)
## v0.2.2
* Fix issue with source maps
## v0.2.1
* Add colors to error output
## v0.2.0
* Add new configuration options (#1)
* target, module, sourceMap, instance
* sourceMap default changed from `true` to `false`
* Workaround issue with TypeScript always emitting Windows-style new lines
* Add tests
## v0.1.0
* Initial version
+22
View File
@@ -0,0 +1,22 @@
The MIT License (MIT)
Copyright (c) 2015-present TypeStrong
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+773
View File
@@ -0,0 +1,773 @@
# TypeScript loader for webpack
[![npm version](https://img.shields.io/npm/v/ts-loader.svg)](https://www.npmjs.com/package/ts-loader)
[![build and test](https://github.com/TypeStrong/ts-loader/actions/workflows/push.yml/badge.svg)](https://github.com/TypeStrong/ts-loader/actions/workflows/push.yml)
[![Downloads](https://img.shields.io/npm/dm/ts-loader.svg)](https://npmjs.org/package/ts-loader)
[![node version](https://img.shields.io/node/v/ts-loader.svg)](https://www.npmjs.com/package/ts-loader)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
<br />
<p align="center">
<h3 align="center">ts-loader</h3>
<p align="center">
This is the TypeScript loader for webpack.
<br />
<br />
<a href="https://github.com/TypeStrong/ts-loader#installation">Installation</a>
·
<a href="https://github.com/TypeStrong/ts-loader/issues">Report Bug</a>
·
<a href="https://github.com/TypeStrong/ts-loader/issues">Request Feature</a>
</p>
</p>
## Table of Contents
<!-- toc -->
- [Getting Started](#getting-started)
* [Installation](#installation)
* [Running](#running)
* [Examples](#examples)
* [Faster Builds](#faster-builds)
* [Yarn PlugnPlay](#yarn-plugnplay)
* [Babel](#babel)
* [Compatibility](#compatibility)
* [Configuration](#configuration)
+ [`devtool` / sourcemaps](#devtool--sourcemaps)
* [Code Splitting and Loading Other Resources](#code-splitting-and-loading-other-resources)
* [Declarations (.d.ts)](#declaration-files-dts)
* [Failing the build on TypeScript compilation error](#failing-the-build-on-typescript-compilation-error)
* [`baseUrl` / `paths` module resolution](#baseurl--paths-module-resolution)
* [Options](#options)
* [Loader Options](#loader-options)
+ [transpileOnly](#transpileonly)
+ [happyPackMode](#happypackmode)
+ [resolveModuleName and resolveTypeReferenceDirective](#resolvemodulename-and-resolvetypereferencedirective)
+ [getCustomTransformers](#getcustomtransformers)
+ [logInfoToStdOut](#loginfotostdout)
+ [logLevel](#loglevel)
+ [silent](#silent)
+ [ignoreDiagnostics](#ignorediagnostics)
+ [reportFiles](#reportfiles)
+ [compiler](#compiler)
+ [configFile](#configfile)
+ [colors](#colors)
+ [errorFormatter](#errorformatter)
+ [compilerOptions](#compileroptions)
+ [instance](#instance)
+ [appendTsSuffixTo](#appendtssuffixto)
+ [appendTsxSuffixTo](#appendtsxsuffixto)
+ [onlyCompileBundledFiles](#onlycompilebundledfiles)
+ [useCaseSensitiveFileNames](#useCaseSensitiveFileNames)
+ [allowTsInNodeModules](#allowtsinnodemodules)
+ [context](#context)
+ [experimentalFileCaching](#experimentalfilecaching)
+ [projectReferences](#projectreferences)
* [Usage with webpack watch](#usage-with-webpack-watch)
* [Hot Module replacement](#hot-module-replacement)
- [Contributing](#contributing)
- [License](#license)
<!-- tocstop -->
## Getting Started
### Installation
```
yarn add ts-loader --dev
```
or
```
npm install ts-loader --save-dev
```
You will also need to install TypeScript if you have not already.
```
yarn add typescript --dev
```
or
```
npm install typescript --save-dev
```
### Running
Use webpack like normal, including `webpack --watch` and `webpack-dev-server`, or through another
build system using the [Node.js API](https://webpack.js.org/api/node/).
### Examples
We have a number of example setups to accommodate different workflows. Our examples can be found [here](examples/).
We probably have more examples than we need. That said, here's a good way to get started:
- I want the simplest setup going. Use "[vanilla](examples/vanilla)" `ts-loader`
- I want the fastest compilation that's available. Use [fork-ts-checker-webpack-plugin](https://github.com/TypeStrong/fork-ts-checker-webpack-plugin). It performs type checking in a separate process with `ts-loader` just handling transpilation.
### Faster Builds
As your project becomes bigger, compilation time increases linearly. It's because typescript's semantic checker has to inspect all files on every rebuild.
The simple solution is to disable it by using the `transpileOnly: true` option, but doing so leaves you without type checking and *will not output declaration files*.
You probably don't want to give up type checking; that's rather the point of TypeScript. So what you can do is use the [fork-ts-checker-webpack-plugin](https://github.com/TypeStrong/fork-ts-checker-webpack-plugin).
It runs the type checker on a separate process, so your build remains fast thanks to `transpileOnly: true` but you still have the type checking.
If you'd like to see a simple setup take a look at [our example](examples/fork-ts-checker-webpack-plugin/).
### Yarn PlugnPlay
`ts-loader` supports [Yarn PlugnPlay](https://yarnpkg.com/en/docs/pnp). The recommended way to integrate is using the [pnp-webpack-plugin](https://github.com/arcanis/pnp-webpack-plugin#ts-loader-integration).
### Babel
`ts-loader` works very well in combination with [babel](https://babeljs.io/) and [babel-loader](https://github.com/babel/babel-loader). There is an [example](https://github.com/Microsoft/TypeScriptSamples/tree/master/react-flux-babel-karma) of this in the official [TypeScript Samples](https://github.com/Microsoft/TypeScriptSamples).
### Compatibility
* TypeScript: 3.6.3+
* webpack: 5.x+ (please use `ts-loader` 8.x if you need webpack 4 support)
* node: 12.x+
A full test suite runs each night (and on each pull request). It runs both on Linux and Windows, testing `ts-loader` against major releases of TypeScript. The test suite also runs against TypeScript@next (because we want to use it as much as you do).
If you become aware of issues not caught by the test suite then please let us know. Better yet, write a test and submit it in a PR!
### Configuration
1. Create or update `webpack.config.js` like so:
```javascript
module.exports = {
mode: "development",
devtool: "inline-source-map",
entry: "./app.ts",
output: {
filename: "bundle.js"
},
resolve: {
// Add `.ts` and `.tsx` as a resolvable extension.
extensions: [".ts", ".tsx", ".js"],
// Add support for TypeScripts fully qualified ESM imports.
extensionAlias: {
".js": [".js", ".ts"],
".cjs": [".cjs", ".cts"],
".mjs": [".mjs", ".mts"]
}
},
module: {
rules: [
// all files with a `.ts`, `.cts`, `.mts` or `.tsx` extension will be handled by `ts-loader`
{ test: /\.([cm]?ts|tsx)$/, loader: "ts-loader" }
]
}
};
```
2. Add a [`tsconfig.json`](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) file. (The one below is super simple; but you can tweak this to your hearts desire)
```json
{
"compilerOptions": {
"sourceMap": true
}
}
```
The [tsconfig.json](http://www.typescriptlang.org/docs/handbook/tsconfig-json.html) file controls
TypeScript-related options so that your IDE, the `tsc` command, and this loader all share the
same options.
#### `devtool` / sourcemaps
If you want to be able to debug your original source then you can thanks to the magic of sourcemaps. There are 2 steps to getting this set up with `ts-loader` and webpack.
First, for `ts-loader` to produce **sourcemaps**, you will need to set the [tsconfig.json](http://www.typescriptlang.org/docs/handbook/tsconfig-json.html) option as `"sourceMap": true`.
Second, you need to set the `devtool` option in your `webpack.config.js` to support the type of sourcemaps you want. To make your choice have a read of the [`devtool` webpack docs](https://webpack.js.org/configuration/devtool/). You may be somewhat daunted by the choice available. You may also want to vary the sourcemap strategy depending on your build environment. Here are some example strategies for different environments:
* `devtool: 'inline-source-map'` - Solid sourcemap support; the best "all-rounder". Works well with karma-webpack (not all strategies do)
* `devtool: 'eval-cheap-module-source-map'` - Best support for sourcemaps whilst debugging.
* `devtool: 'source-map'` - Approach that plays well with UglifyJsPlugin; typically you might use this in Production
### Code Splitting and Loading Other Resources
Loading css and other resources is possible but you will need to make sure that
you have defined the `require` function in a [declaration file](https://www.typescriptlang.org/docs/handbook/writing-declaration-files.html).
```typescript
declare var require: {
<T>(path: string): T;
(paths: string[], callback: (...modules: any[]) => void): void;
ensure: (
paths: string[],
callback: (require: <T>(path: string) => T) => void
) => void;
};
```
Then you can simply require assets or chunks per the [webpack documentation](https://webpack.js.org/guides/code-splitting/).
```javascript
require("!style!css!./style.css");
```
The same basic process is required for code splitting. In this case, you `import` modules you need but you
don't directly use them. Instead you require them at [split points](https://webpack.js.org/guides/code-splitting/). See [this example](test/comparison-tests/codeSplitting) and [this example](test/comparison-tests/es6codeSplitting) for more details.
[TypeScript 2.4 provides support for ECMAScript's new `import()` calls. These calls import a module and return a promise to that module.](https://blogs.msdn.microsoft.com/typescript/2017/06/12/announcing-typescript-2-4-rc/) This is also supported in webpack - details on usage can be found [here](https://webpack.js.org/guides/code-splitting-async/#dynamic-import-import-). Happy code splitting!
### Declaration Files (.d.ts)
To output declaration files (.d.ts), you can set "declaration": true in your tsconfig and set "transpileOnly" to false.
If you use ts-loader with "transpileOnly": true along with [fork-ts-checker-webpack-plugin](https://github.com/TypeStrong/fork-ts-checker-webpack-plugin), you will need to configure fork-ts-checker-webpack-plugin to output definition files, you can learn more on the plugin's documentation page: https://github.com/TypeStrong/fork-ts-checker-webpack-plugin#typescript-options
To output a built .d.ts file, you can use the [DeclarationBundlerPlugin](https://www.npmjs.com/package/types-webpack-bundler) in your webpack config.
### Failing the build on TypeScript compilation error
The build **should** fail on TypeScript compilation errors as of webpack 2. If for some reason it does not, you can use the [webpack-fail-plugin](https://www.npmjs.com/package/webpack-fail-plugin).
For more background have a read of [this issue](https://github.com/TypeStrong/ts-loader/issues/108).
### `baseUrl` / `paths` module resolution
If you want to resolve modules according to `baseUrl` and `paths` in your `tsconfig.json` then you can use the [tsconfig-paths-webpack-plugin](https://www.npmjs.com/package/tsconfig-paths-webpack-plugin) package. For details about this functionality, see the [module resolution documentation](https://www.typescriptlang.org/docs/handbook/module-resolution.html#base-url).
This feature requires webpack 2.1+ and TypeScript 2.0+. Use the config below or check the [package](https://github.com/dividab/tsconfig-paths-webpack-plugin/blob/master/README.md) for more information on usage.
```javascript
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
module.exports = {
...
resolve: {
plugins: [new TsconfigPathsPlugin({ configFile: "./path/to/tsconfig.json" })]
}
...
}
```
### Options
There are two types of options: TypeScript options (aka "compiler options") and loader options. TypeScript options should be set using a tsconfig.json file. Loader options can be specified through the `options` property in the webpack configuration:
```javascript
module.exports = {
...
module: {
rules: [
{
test: /\.tsx?$/,
use: [
{
loader: 'ts-loader',
options: {
transpileOnly: true
}
}
]
}
]
}
}
```
### Loader Options
#### transpileOnly
| Type | Default Value |
|------|--------------|
| `boolean` | `false`|
If you want to speed up compilation significantly you can set this flag.
However, many of the benefits you get from static type checking between different dependencies in your application will be lost. `transpileOnly` will *not* speed up compilation of project references.
It's advisable to use `transpileOnly` alongside the [fork-ts-checker-webpack-plugin](https://github.com/TypeStrong/fork-ts-checker-webpack-plugin) to get full type checking again. To see what this looks like in practice then either take a look at [our example](examples/fork-ts-checker-webpack-plugin).
> Tip: When you add the [fork-ts-checker-webpack-plugin](https://github.com/TypeStrong/fork-ts-checker-webpack-plugin) to your webpack config, the `transpileOnly` will default to `true`, so you can skip that option.
If you enable this option, webpack 4 will give you "export not found" warnings any time you re-export a type:
```
WARNING in ./src/bar.ts
1:0-34 "export 'IFoo' was not found in './foo'
@ ./src/bar.ts
@ ./src/index.ts
```
The reason this happens is that when typescript doesn't do a full type check, it does not have enough information to determine whether an imported name is a type or not, so when the name is then exported, typescript has no choice but to emit the export. Fortunately, the extraneous export should not be harmful, so you can just suppress these warnings:
```javascript
module.exports = {
...
stats: {
warningsFilter: /export .* was not found in/
}
}
```
#### happyPackMode
| Type | Default Value |
|------|--------------|
| `boolean` | `false`|
If you're using [HappyPack](https://github.com/amireh/happypack) or [thread-loader](https://github.com/webpack-contrib/thread-loader) to parallelise your builds then you'll need to set this to `true`. This implicitly sets `*transpileOnly*` to `true` and **WARNING!** stops registering **_all_** errors to webpack.
It's advisable to use this with the [fork-ts-checker-webpack-plugin](https://github.com/TypeStrong/fork-ts-checker-webpack-plugin) to get full type checking again. **_IMPORTANT_**: If you are using fork-ts-checker-webpack-plugin alongside HappyPack or thread-loader then ensure you set the `syntactic` diagnostic option like so:
```javascript
new ForkTsCheckerWebpackPlugin({
typescript: {
diagnosticOptions: {
semantic: true,
syntactic: true,
},
},
})
```
This will ensure that the plugin checks for both syntactic errors (eg `const array = [{} {}];`) and semantic errors (eg `const x: number = '1';`). By default the plugin only checks for semantic errors (as when used with `ts-loader` in `transpileOnly` mode, `ts-loader` will still report syntactic errors).
Also, if you are using `thread-loader` in watch mode, remember to set `poolTimeout: Infinity` so workers don't die.
#### resolveModuleName and resolveTypeReferenceDirective
These options should be functions which will be used to resolve the import statements and the `<reference types="...">` directives instead of the default TypeScript implementation. It's not intended that these will typically be used by a user of `ts-loader` - they exist to facilitate functionality such as [Yarn PlugnPlay](https://yarnpkg.com/en/docs/pnp).
#### getCustomTransformers
| Type |
|------|
| ` (program: Program, getProgram: () => Program) => { before?: TransformerFactory<SourceFile>[]; after?: TransformerFactory<SourceFile>[]; afterDeclarations?: TransformerFactory<SourceFile>[]; } ` |
Provide custom transformers - only compatible with TypeScript 2.3+ (and 2.4 if using `transpileOnly` mode). For example usage take a look at [typescript-plugin-styled-components](https://github.com/Igorbek/typescript-plugin-styled-components) or our [test](test/comparison-tests/customTransformer).
You can also pass a path string to locate a js module file which exports the function described above, this useful especially in `happyPackMode`. (Because forked processes cannot serialize functions see more at [related issue](https://github.com/Igorbek/typescript-plugin-styled-components/issues/6#issue-303387183))
#### logInfoToStdOut
| Type | Default Value |
|------|--------------|
| `boolean` | `false`|
This is important if you read from stdout or stderr and for proper error handling.
The default value ensures that you can read from stdout e.g. via pipes or you use webpack -j to generate json output.
#### logLevel
| Type | Default Value |
|------|--------------|
| `string` | `warn` |
Can be `info`, `warn` or `error` which limits the log output to the specified log level.
Beware of the fact that errors are written to stderr and everything else is written to stderr (or stdout if logInfoToStdOut is true).
#### silent
| Type | Default Value |
|------|--------------|
| `boolean` | `false`|
If `true`, no console.log messages will be emitted. Note that most error
messages are emitted via webpack which is not affected by this flag.
#### ignoreDiagnostics
| Type | Default Value |
|------|--------------|
| `number[]` | `[]`|
You can squelch certain TypeScript errors by specifying an array of diagnostic
codes to ignore.
#### reportFiles
| Type | Default Value |
|------|--------------|
| `string[]` | `[]`|
Only report errors on files matching these glob patterns.
```javascript
// in webpack.config.js
{
test: /\.ts$/,
loader: 'ts-loader',
options: { reportFiles: ['src/**/*.{ts,tsx}', '!src/skip.ts'] }
}
```
This can be useful when certain types definitions have errors that are not fatal to your application.
#### compiler
| Type | Default Value |
|------|--------------|
| `string` | `'typescript'`|
Allows use of TypeScript compilers other than the official one. Should be
set to the NPM name of the compiler, eg [`ntypescript`](https://github.com/basarat/ntypescript).
#### configFile
| Type | Default Value |
|------|--------------|
| `string` | `'tsconfig.json'`|
Allows you to specify where to find the TypeScript configuration file.
You may provide
* just a file name. The loader then will search for the config file of each entry point in the respective entry point's containing folder. If a config file cannot be found there, it will travel up the parent directory chain and look for the config file in those folders.
* a relative path to the configuration file. It will be resolved relative to the respective `.ts` entry file.
* an absolute path to the configuration file.
Please note, that if the configuration file is outside of your project directory, you might need to set the `context` option to avoid TypeScript issues (like TS18003).
In this case the `configFile` should point to the `tsconfig.json` and `context` to the project root.
#### colors
| Type | Default Value |
|------|--------------|
| `boolean` | `true`|
If `false`, disables built-in colors in logger messages.
#### errorFormatter
| Type | Default Value |
|------|--------------|
| `(message: ErrorInfo, colors: boolean) => string` | `undefined`|
By default `ts-loader` formats TypeScript compiler output for an error or a warning in the style:
```
[tsl] ERROR in myFile.ts(3,14)
TS4711: you did something very wrong
```
If that format is not to your taste you can supply your own formatter using the `errorFormatter` option. Below is a template for a custom error formatter. Please note that the `colors` parameter is an instance of [`chalk`](https://github.com/chalk/chalk) which you can use to color your output. (This instance will respect the `colors` option.)
```javascript
function customErrorFormatter(error, colors) {
const messageColor =
error.severity === "warning" ? colors.bold.yellow : colors.bold.red;
return (
"Does not compute.... " +
messageColor(Object.keys(error).map(key => `${key}: ${error[key]}`))
);
}
```
If the above formatter received an error like this:
```json
{
"code":2307,
"severity": "error",
"content": "Cannot find module 'components/myComponent2'.",
"file":"/.test/errorFormatter/app.ts",
"line":2,
"character":31
}
```
It would produce an error message that said:
```
Does not compute.... code: 2307,severity: error,content: Cannot find module 'components/myComponent2'.,file: /.test/errorFormatter/app.ts,line: 2,character: 31
```
And the bit after "Does not compute.... " would be red.
#### compilerOptions
| Type | Default Value |
|------|--------------|
| `object` | `{}`|
Allows overriding TypeScript options. Should be specified in the same format
as you would do for the `compilerOptions` property in tsconfig.json.
#### instance
| Type | Default Value |
|------|--------------|
| `string` | `TODO`|
Advanced option to force files to go through different instances of the
TypeScript compiler. Can be used to force segregation between different parts
of your code.
#### appendTsSuffixTo
| Type | Default Value |
|------|--------------|
| `(RegExp \| string)[]` | `[]`|
#### appendTsxSuffixTo
| Type | Default Value |
|------|--------------|
| `(RegExp \| string)[]` | `[]`|
A list of regular expressions to be matched against filename. If filename matches one of the regular expressions, a `.ts` or `.tsx` suffix will be appended to that filename.
If you're using [HappyPack](https://github.com/amireh/happypack) or [thread-loader](https://github.com/webpack-contrib/thread-loader) with `ts-loader`, you need use the `string` type for the regular expressions, not `RegExp` object.
```js
// change this:
{ appendTsSuffixTo: [/\.vue$/] }
// to:
{ appendTsSuffixTo: ['\\.vue$'] }
```
This is useful for `*.vue` [file format](https://vuejs.org/v2/guide/single-file-components.html) for now. (Probably will benefit from the new single file format in the future.)
Example:
webpack.config.js:
```javascript
module.exports = {
entry: "./index.vue",
output: { filename: "bundle.js" },
resolve: {
extensions: [".ts", ".vue"]
},
module: {
rules: [
{ test: /\.vue$/, loader: "vue-loader" },
{
test: /\.ts$/,
loader: "ts-loader",
options: { appendTsSuffixTo: [/\.vue$/] }
}
]
}
};
```
index.vue
```vue
<template><p>hello {{msg}}</p></template>
<script lang="ts">
export default {
data(): Object {
return {
msg: "world"
};
}
};
</script>
```
We can handle `.tsx` by quite similar way:
webpack.config.js:
```javascript
module.exports = {
entry: './index.vue',
output: { filename: 'bundle.js' },
resolve: {
extensions: ['.ts', '.tsx', '.vue', '.vuex']
},
module: {
rules: [
{ test: /\.vue$/, loader: 'vue-loader',
options: {
loaders: {
ts: 'ts-loader',
tsx: 'babel-loader!ts-loader',
}
}
},
{ test: /\.ts$/, loader: 'ts-loader', options: { appendTsSuffixTo: [/TS\.vue$/] } }
{ test: /\.tsx$/, loader: 'babel-loader!ts-loader', options: { appendTsxSuffixTo: [/TSX\.vue$/] } }
]
}
}
```
tsconfig.json (set `jsx` option to `preserve` to let babel handle jsx)
```json
{
"compilerOptions": {
"jsx": "preserve"
}
}
```
index.vue
```vue
<script lang="tsx">
export default {
functional: true,
render(h, c) {
return (<div>Content</div>);
}
}
</script>
```
Or if you want to use only tsx, just use the `appendTsxSuffixTo` option only:
```javascript
{ test: /\.ts$/, loader: 'ts-loader' }
{ test: /\.tsx$/, loader: 'babel-loader!ts-loader', options: { appendTsxSuffixTo: [/\.vue$/] } }
```
#### onlyCompileBundledFiles
| Type | Default Value |
|------|--------------|
| `boolean` | `false`|
The default behavior of `ts-loader` is to act as a drop-in replacement for the `tsc` command,
so it respects the `include`, `files`, and `exclude` options in your `tsconfig.json`, loading
any files specified by those options. The `onlyCompileBundledFiles` option modifies this behavior,
loading only those files that are actually bundled by webpack, as well as any `.d.ts` files included
by the `tsconfig.json` settings. `.d.ts` files are still included because they may be needed for
compilation without being explicitly imported, and therefore not picked up by webpack.
#### useCaseSensitiveFileNames
| Type | Default Value |
|------|--------------|
| `boolean` | determined by typescript based on platform |
The default behavior of `ts-loader` is to act as a drop-in replacement for the `tsc` command,
so it respects the `useCaseSensitiveFileNames` set internally by typescript. The `useCaseSensitiveFileNames` option modifies this behavior,
by changing the way in which ts-loader resolves file paths to compile. Setting this to true can have some performance benefits due to simplifying the file resolution codepath.
#### allowTsInNodeModules
| Type | Default Value |
|------|--------------|
| `boolean` | `false`|
By default, `ts-loader` will not compile `.ts` files in `node_modules`.
You should not need to recompile `.ts` files there, but if you really want to, use this option.
Note that this option acts as a *whitelist* - any modules you desire to import must be included in
the `"files"` or `"include"` block of your project's `tsconfig.json`.
See: [https://github.com/Microsoft/TypeScript/issues/12358](https://github.com/Microsoft/TypeScript/issues/12358)
```javascript
// in webpack.config.js
{
test: /\.ts$/,
loader: 'ts-loader',
options: { allowTsInNodeModules: true }
}
```
And in your `tsconfig.json`:
```json
{
"include": [
"node_modules/whitelisted_module.ts"
],
"files": [
"node_modules/my_module/whitelisted_file.ts"
]
}
```
#### context
| Type | Default Value |
|------|--------------|
| `string` | `undefined`|
If set, will parse the TypeScript configuration file with given **absolute path** as base path.
Per default the directory of the configuration file is used as base path. Relative paths in the configuration
file are resolved with respect to the base path when parsed. Option `context` allows to set option
`configFile` to a path other than the project root (e.g. a NPM package), while the base path for `ts-loader`
can remain the project root.
Keep in mind that **not** having a `tsconfig.json` in your project root can cause different behaviour between `ts-loader` and `tsc`.
When using editors like `VS Code` it is advised to add a `tsconfig.json` file to the root of the project and extend the config file
referenced in option `configFile`. For more information please [read the PR](https://github.com/TypeStrong/ts-loader/pull/681) that
is the base and [read the PR](https://github.com/TypeStrong/ts-loader/pull/688) that contributed this option.
webpack:
```javascript
{
loader: require.resolve('ts-loader'),
options: {
context: __dirname,
configFile: require.resolve('ts-config-react-app')
}
}
```
Extending `tsconfig.json`:
```json
{ "extends": "./node_modules/ts-config-react-app/index" }
```
Note that changes in the extending file while not be respected by `ts-loader`. Its purpose is to satisfy the code editor.
#### experimentalFileCaching
| Type | Default Value |
|------|--------------|
| `boolean` | `true`|
By default whenever the TypeScript compiler needs to check that a file/directory exists or resolve symlinks it makes syscalls. It does not cache the result of these operations and this may result in many syscalls with the same arguments ([see comment](https://github.com/TypeStrong/ts-loader/issues/825#issue-354725524) with example).
In some cases it may produce performance degradation.
This flag enables caching for some FS-functions like `fileExists`, `realpath` and `directoryExists` for TypeScript compiler. Note that caches are cleared between compilations.
#### projectReferences
| Type | Default Value |
|------|--------------|
| `boolean` | `false`|
ts-loader has opt-in support for [project references](https://www.typescriptlang.org/docs/handbook/project-references.html). With this configuration option enabled, `ts-loader` will incrementally rebuild upstream projects the same way `tsc --build` does. Otherwise, source files in referenced projects will be treated as if theyre part of the root project.
In order to make use of this option your project needs to be correctly configured to build the project references and then to use them as part of the build. See the [Project References Guide](REFERENCES.md) and the example code in the examples which can be found [here](examples/project-references-example/).
### Usage with webpack watch
Because TS will generate .js and .d.ts files, you should ignore these files, otherwise watchers may go into an infinite watch loop. For example, when using webpack, you may wish to add this to your webpack.conf.js file:
```javascript
// for webpack 4
plugins: [
new webpack.WatchIgnorePlugin([
/\.js$/,
/\.d\.[cm]?ts$/
])
],
// for webpack 5
plugins: [
new webpack.WatchIgnorePlugin({
paths:[
/\.js$/,
/\.d\.[cm]ts$/
]})
],
```
It's worth noting that use of the `LoaderOptionsPlugin` is [only supposed to be a stopgap measure](https://webpack.js.org/plugins/loader-options-plugin/). You may want to look at removing it entirely.
### Hot Module replacement
We do not support HMR as we did not yet work out a reliable way how to set it up.
If you want to give `webpack-dev-server` HMR a try, follow the official [webpack HMR guide](https://webpack.js.org/guides/hot-module-replacement/), then tweak a few config options for `ts-loader`:
1. Set `transpileOnly` to `true` (see [transpileOnly](#transpileonly) for config details and recommendations above).
2. Inside your HMR acceptance callback function, maybe re-require the module that was replaced.
## Contributing
This is your TypeScript loader! We want you to help make it even better. Please feel free to contribute; see the [contributor's guide](CONTRIBUTING.md) to get started.
## History
`ts-loader` was started by [James Brantly](https://github.com/jbrantly), since 2016 [John Reilly](https://github.com/johnnyreilly) has been taking good care of it. If you're interested, you can [read more about how that came to pass](https://johnnyreilly.com/but-you-cant-die-i-love-you-ts-loader).
## License
MIT License
+331
View File
@@ -0,0 +1,331 @@
# Using TypeScript Project References with ts-loader and webpack
Project References were added to TypeScript in 3.0. The benefits of using project references include:
* Better code organisation
* Logical separation between components
* Faster build times
If you are using TypeScript in your web project you can also use project references to improve your code and build workflow. This article describes some of the ways to set up your project to use references. I am using ts-loader to transpile the TypeScript code to JavaScript and webpack to bundle code.
An example repo using the configuration above is available at the link below:
[https://github.com/appzuka/project-references-example](https://github.com/appzuka/project-references-example)
There are 2 stages to using project references in your project:
1. Configure and build the project references
1. Setup your codebase to consume the compiled projects
To gain an understanding of how project references work, for the first part of this guide we will use <code>tsc</code> to build the project references. Later on, we will configure ts-loader to do this automatically.
### Configure and build the project references
This stage just involves following the directions from the TypeScript documentation:
[https://www.typescriptlang.org/docs/handbook/project-references.html](https://www.typescriptlang.org/docs/handbook/project-references.html)
There are a few points to note:
1. Referenced projects must have the new composite setting enabled.
1. Each referenced project has its own <code>tsconfig.json</code>
1. There will be a root level <code>tsconfig.json</code> which includes the lower level projects as references. Building this will build all subprojects.
1. You should be using configuration file inheritance (<code>{ “extends”: …}</code>) to avoid duplication in your config.
1. You need to use <code>tsc --build</code> to compile the project.
1. When you compile the project <code>tsc --build</code> will create a file called tsconfig.tsbuildinfo that contains the signatures and timestamps of all files required to build the project. On subsequent builds TypeScript will use that information to detect the least costly way to type-check and emit changes to your project.
1. There is no need to use the incremental compiler option. <code>tsc --build</code> will generate and use tsconfig.tsbuildinfo anyway.
1. If you delete your compiled code and re-run <code>tsc --build</code> the code will **not** be rebuilt unless you also delete the <code>tsconfig.tsbuildinfo</code> file. Use the <code>tsc --build --clean</code> command to do this for you.
1. If you set the <code>declaration</code> and <code>declarationMap</code> settings in <code>tsconfig.json</code> the <code>outDir</code> folder will contain <code>.d.ts</code> and <code>.d.ts.map</code> files alongside the transpiled JavaScript. When you consume the compiled project you should consume the <code>outDir</code> folder, not the <code>src</code>. Even though your root project is in TypeScript it can use full syntax checking without the subprojects TypeScript source because the <code>outDir</code> folder contains the definitions in the <code>.d.ts</code> file. Vscode (and many other code editors and IDEs) will be able to find the definitions and perform syntax checking in the editor just as if you were not using project references and importing the TypeScript source directly.
### Project Structure
The TypeScript implementation of project references allows you to structure the project in almost any way you wish. Just configure the input and output folders in tsconfig.json to your needs and TypeScript will build it for you.
For a web project you might like a structure similar to the one below. You could put all your project references in a packages folder with the top-level project code in src:
```
tsconfig.json
tsconfig-base.json
src
- (source code for the main project)
dist
- main.js (final bundle produced by webpack)
packages
- reference1
- tsconfig.json (inherits from tsconfig-base.json)
- src
- lib
- reference2
- tsconfig.json (inherits from tsconfig-base.json)
- src
- lib
```
Each project reference has its own <code>tsconfig.json</code> with the source code for each package in a <code>src</code> subfolder. When the project is built the compiled JavaScript for each project will be in its <code>lib</code> subfolder.
The source code for your main project is in a top-level <code>src</code> folder and the final bundle will be in a top-level <code>dist</code> folder. The top-level <code>src</code> folder is not a referenced project — it is normal TypeScript source that webpack will bundle. It imports from the <code>lib</code> folders of the referenced projects built by <code>tsc</code>.
This structure works well because:
* Having packages grouped together under a packages folder organises your codebase nicely.
* Other tools such as yarn workspaces and lerna use and understand this organisation.
* Each package is fully self-contained in its own folder. It contains the source, compiled code, tsconfig.json and (optionally) its own <code>package.json</code> which describes how the package is used.
* You can drop the package into another project, import it with a simple statement and everything will be linked up.
This is just one way to structure your project. Some other options include:
* Not putting the projects references in a packages folder. They could all be at the top level, or a different folder, or nested folders.
* The output folder of each project does not have to be in a lib folder of that project. You could have a top-level lib folder which contains the output of all projects.
Almost any structure is compatible with project references. You have freedom to specify the paths of the referenced projects and their outputs in the <code>tsconfig.json</code> files. You will import the compiled JavaScript files into the main project and some structures make this easier than others, but you have the freedom to choose what works for you.
### Test Build your Projects
You should now check that the building of the projects is successful and produces the code you expect.
In each project reference folder execute <code>tsc --build</code>, check there are no errors and the output is as you expect. Use <code>tsc --build --clean</code> to remove the output and repeat. You can use <code>tsc --build --verbose</code> to see what <code>tsc</code> is doing.
If you have a top-level <code>tsconfig.json</code> similar to:
```
{
"files": ["src/index.ts"],
"references": [
{ "path": "./reference1" },
{ "path": "./reference2" }
]
}
```
Then executing <code>tsc --build</code> in the top-level will compile all of your subprojects with one command. The build process is smart and can manage dependencies between subprojects.
In the final step of this guide we will get ts-loader to do the build automatically when called from webpack, but for now, just make sure that the build process works when using <code>tsc --build</code> manually.
### Setup your codebase to consume the project
Now your subprojects are built you can use them in your root project. Lets say your reference1 project exports a number:
```
// packages/reference1/src/index.ts
export const Meaning = 42;
```
After building the reference with <code>tsc --build</code> the compiled JavaScript will be found in <code>packages/reference1/lib/index.js</code>. In your root project you need to import this. There are several ways you can do this. Lets start with a naive approach that will work but has severe downsides:
```
// src/index.ts
// Don't do this!
import { Meaning } from '../packages/reference1/lib';
```
This will work because TypeScript and webpack will both find the file. The downsides are:
* The organisation of your root project and components are now intertwined. If you change the internal structure of your subproject you will need to update every import statement in the entire project.
* The import location will depend on the location on the source file. For example, if you want to do the same import from a subfolder in your root project you will need to replace <code>../packages/reference1/lib</code> with <code>../../packages/reference1/lib</code>. If you re-organise your project structure you will need to fix every import.
The solution to this is module resolution — how TypeScript and webpack resolve the targets of import statements. You can read about this at the links below:
* [https://www.typescriptlang.org/docs/handbook/module-resolution.html](https://www.typescriptlang.org/docs/handbook/module-resolution.html)
* [https://webpack.js.org/concepts/module-resolution](https://webpack.js.org/concepts/module-resolution)
Module resolution is nothing new and it is not part of project references, but understanding it will be a huge help getting everything working. Some points to note:
* TypeScript and webpack can use different methods to resolve modules. It will help if you can set them up so they are using the same method. (See the example below using alias in webpack and/or tsconfig-paths-webpack-plugin.)
* Resolution works differently for relative (<code>./reference1</code>) and absolute (<code>reference1</code>) imports.
* TypeScript has 2 strategies for module resolution: <code>classic</code> and <code>node</code>. You probably want to use <code>node</code>.
* You can use a webpack plugin <code>tsconfig-paths-webpack-plugin</code> so that you just need to define paths in your <code>tsconfig.json</code> and then dont need to repeat these in your webpack config.
Using the example above, we would like to just import from <code>packages/reference</code> and have TypeScript and webpack both know that this refers to the actual location.
```
// src/index.ts
// Better!
import { Meaning } from 'packages/reference1/lib';
```
We can achieve this using the paths configuration in <code>tsconfig.json</code> (or better, in <code>tsconfig-base.json</code> so the settings are made once and inherited by all projects):
```
{
"compilerOptions": {
"baseUrl": ".", // This must be specified if "paths" is.
"paths": {
"packages/*": ["packages/*"]
}
}
}
```
Now TypeScript understands that when it sees <code>packages/reference1</code> in an import statement, it should look in <code>./packages/reference1</code>. The path is relative to the root <code>tsconfig.json</code> so it does not matter where the source file which imports this is located.
Unless you are using tsconfig-paths-webpack-plugin you may need to include a corresponding resolve-alias setting in your <code>webpack.config.js</code>:
```
const path = require('path');
module.exports = {
modules: [
"node_modules",
path.resolve(__dirname)
],
resolve: {
alias: {
packages: path.resolve(__dirname, 'packages/'),
}
}
};
```
(In this case the <code>path.resolve(__dirname)</code> in the modules section accomplishes the same thing, but depending on your project structure you may need an alias.)
If you are getting module not found errors when you build, knowing whether these are coming from TypeScript or webpack will help you to resolve the issue.
Errors which come from TypeScript when you build the project look similar to the following:
```
ERROR in ...project-references-demo/src/index.tsx
./src/index.tsx
[tsl] ERROR in ...project-references-demo/src/index.tsx(1,27)
TS2307: Cannot find module 'mypackages/zoo' or its corresponding type declarations.
```
Note the <code>[tsl]</code> in the message and also the TypeScript error code <code>TS2307</code>. This indicates that the error was passed to webpack by ts-loader when it tried to transpile the file. You can also check whether errors are coming from TypeScript by building your project manually with <code>tsc</code> and checking whether it reports errors.
Errors from webpack look similar to the following:
```
ERROR in ./src/index.tsx
Module not found: Error: Can't resolve 'mypackages/zoo' in '...project-references-demo/src'
@ ./src/index.tsx 6:12-37
```
If you just get these errors it indicates that <code>tsconfig.json</code> is correctly configured and TypeScript is able to resolve your modules, but webpack is not. Look into the resolve section of <code>webpack.config.js</code> and check whether you need to add an alias.
You can use module resolution to make your project work with project references even if your structure is very different from that outlined here. As long as webpack and TypeScript can find the built code it will work.
### Can you import the TypeScript Source instead of the JavaScript?
You can import the TypeScript source from your projects, but you probably should not. If you do set up your project to import the TypeScript, webpack will bundle your project just fine, but then you are not using project references. You have succeeded in organising your codebase but you are not getting the advantage of reducing build time by using the compiled files in <code>lib</code>. In fact, you are slowing down your build by requiring tsc or ts-loader to build the reference and then not using it.
If your project is large you could see a significant benefit from pre-building large sections of code. If your project is not so large you may prefer to just structure your codebase and skip project references.
### Using ts-loader to build project references
Up to this point, we ran <code>tsc --build</code> on its own and then used webpack and ts-loader to build the whole project, importing the references. You can configure ts-loader to build the references for you, which simplifies the build process.
The top-level project in <code>src</code> is TypeScript code, so you will already be using ts-loader to load the TypeScript source into webpack. Just add <code>projectReferences: true</code> to the ts-loader configuration and you no longer need to run <code>tsc</code> in a separate process:
```
// webpack.config.js
"module": {
"rules": [
{
"test": /\.tsx?$/,
"exclude": /node_modules/,
"use": {
"loader": "ts-loader",
"options": {
"projectReferences": true
}
}
}
]
}
```
When webpack uses ts-loader to process a TypeScript file ts-loader will now check whether any of your project references need rebuilding and rebuild them before webpack proceeds if necessary. This includes when webpack is in watch mode as used by webpack-dev-server.
Setting <code>projectReferences: true</code> in ts-loader alone will not magically convert your code to use project references. All it does is to run <code>tsc --build</code> as part of the build process. You need to configure project references and structure your project to use them as described here.
If you have come this far congratulations — you are now using TypeScript project references in your web project. You can stop here, but in the next section of this guide there are some tips to clean up the project further and create a library of reusable, version-controlled components.
### Using package.json
We can clean this up further by including a <code>package.json</code> in the project reference subfolder. If this contains the following:
```
//packages/reference1/package.json
{
"name": "reference1",
"version": "1.0.0",
"description": "Project Reference1",
"main": "lib/index.js",
"directories": {
"lib": "lib"
},
"license": "ISC"
}
```
then you can just import as follows:
```
// src/index.ts
import { Meaning } from 'packages/reference1';
```
The module setting in <code>package.json</code> tells the bundler to import from <code>lib/index.js</code> when it sees the import statement above.
### Using node_modules
In the above approach we need to add paths to <code>tsconfig.json</code> so that the module resolution knows where to find our package. But the module resolution system automatically looks in <code>node_modules</code>, so if we link our reference in <code>node_modules</code> we wont need the paths and aliases:
```
ln -s ../packages/reference1 node_modules/reference1
node_modules/reference1 -> packages/reference1
```
It probably makes sense to use npm scopes:
```
ln -f ../../packages/reference1 node_modules/@myscope/reference1
node_modules/@myscope/reference1 -> packages/reference1
````
then you can consume the code with:
```
// src/index.ts
import { Meaning } from '@myscope/reference1';
```
So you benefit from not having to configure paths and aliases, but you need to create the links in node_modules after cloning the project, unless you use Yarn workspaces.
### Using Yarn Workspaces
If you use yarn workspaces the <code>node_modules</code> links will automatically be created for you when you execute <code>yarn install</code>. Simply include the following in your root level <code>package.json</code>:
``
{
"private": true,
"workspaces": ["packages/*"]
}
``
In the subprojects <code>package.json</code> you should use the name of the package you want to be linked in node_modules:
```
//packages/reference1/package.json
{
"name": "@myscope/reference1",
"version": "1.0.0",
"module": "lib/index.js
}
```
When you run yarn install the links in <code>node_modules</code> will be created for you.
You can now use your project references anywhere in your codebase with a simple import statement, exactly like you import npm modules. If you have a more complex application, for example with client and server applications, you can share modules easily.
### Building a Component Library
A common problem in code organisation is how to re-use code in multiple projects. Project references help toward this goal by providing a logical separation between components. This will mean you can drop a component into another project and use it. But there is still the matter of how you do this:
* You could copy the project reference folder into all top-level projects you want to use it in. This has the disadvantage that you end up with multiple copies of code. If you patch or enhance a component you need to copy the patch to all the other projects, rebuild them and test.
* Another approach would be to symlink the component into each top-level project. The downside of this is that once you amend the component you could break all of the projects which depend on it.
A smarter solution is to publish the components as npm packages. You can use semantic versioning each time you publish using a version in the format major.minor.patch. You then add the components to other projects using <code>yarn add @myscope/reference1</code>.
Versioning works exactly the same as any other npm package. You specify in the consuming projects <code>tsconfig.json</code> what version changes are acceptable:
```
"@myscope/reference1": "1.0.1", // Only version 1.0.1 can be used
"@myscope/reference1": "~1.0.1", // Patch updates are acceptable
"@myscope/reference1": "^1.0.1", // Minor version changes are OK
```
You can then update and publish new versions of the component with new version numbers. The other project will not be broken as it will continue to use the version specified in its <code>package.json</code>. When you are ready to update you can use the same yarn tools you would use to update any package (<code>yarn outdated / upgrade / upgrade-interactive</code> or the npm equivalents).
If you want to keep your packages private you can set up your own private npm repository with [Verdaccio](https://verdaccio.org/) or you can use [Github Packages](https://github.com/features/packages)
### Lerna
If your project references are complex and have their own scripts for testing and building you could use [Lerna](https://lerna.js.org/). This works well with yarn workspaces and the project structure outlined above. If you have a test script in reference1 you could use the following command to execute it:
```
lerna run --scope=reference1 test
```
The same command without the <code>--scope</code> argument would execute the test scripts in all subprojects.
Yarn workspaces and Lerna introduce more power but also more complexity in the workflow. They are not required to use project references so it is up to you whether the extra learning curve they introduce is worthwhile.
### Build Times in Development
Using ts-loader and webpack-dev-server, when you change a file in one of the project references ts-loader will automatically rebuild the reference and include the change in the new bundle. Rebuilding the reference may take a few seconds. By comparison, when you change a file in the root source (non-reference) webpack will get ts-loader to rebuild just that file and create a new bundle very quickly, typically less than 1 second.
So if you are developing code in a reference and find the few seconds it takes to rebuild the reference too much, you could benefit from importing from the TypeScript source directly. This will be at the expense of longer warm start times as you will not be using the pre-built code for that referenced project.
+16
View File
@@ -0,0 +1,16 @@
# Security Policy
## Supported Versions
Use this section to tell people about which versions of your project are
currently being supported with security updates.
| Version | Supported |
| ------- | ------------------ |
| 9.x.x | :white_check_mark: |
| < 9.0 | :x: |
## Reporting a Vulnerability
To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security).
Tidelift will coordinate the fix and disclosure.
+10
View File
@@ -0,0 +1,10 @@
import * as webpack from 'webpack';
import type { TSInstance } from './interfaces';
/**
* This returns a function that has options to add assets and also to provide errors to webpack
* In webpack 4 we can do both during the afterCompile hook
* In webpack 5 only errors should be provided during aftercompile. Assets should be
* emitted during the afterProcessAssets hook
*/
export declare function makeAfterCompile(instance: TSInstance, configFilePath: string | undefined): (compilation: webpack.Compilation, callback: () => void) => void;
//# sourceMappingURL=after-compile.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"after-compile.d.ts","sourceRoot":"","sources":["../src/after-compile.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AAInC,OAAO,KAAK,EAIV,UAAU,EAEX,MAAM,cAAc,CAAC;AAUtB;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAC9B,QAAQ,EAAE,UAAU,EACpB,cAAc,EAAE,MAAM,GAAG,SAAS,iBAKb,OAAO,CAAC,WAAW,YAAY,MAAM,IAAI,UAoD/D"}
+284
View File
@@ -0,0 +1,284 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeAfterCompile = makeAfterCompile;
const path = require("path");
const webpack = require("webpack");
const constants = require("./constants");
const instances_1 = require("./instances");
const utils_1 = require("./utils");
/**
* This returns a function that has options to add assets and also to provide errors to webpack
* In webpack 4 we can do both during the afterCompile hook
* In webpack 5 only errors should be provided during aftercompile. Assets should be
* emitted during the afterProcessAssets hook
*/
function makeAfterCompile(instance, configFilePath) {
let getCompilerOptionDiagnostics = true;
let checkAllFilesForErrors = true;
return (compilation, callback) => {
// Don't add errors for child compilations
if (compilation.compiler.isChild()) {
callback();
return;
}
if (instance.loaderOptions.transpileOnly) {
provideAssetsFromSolutionBuilderHost(instance, compilation);
callback();
return;
}
removeCompilationTSLoaderErrors(compilation, instance.loaderOptions);
provideCompilerOptionDiagnosticErrorsToWebpack(getCompilerOptionDiagnostics, compilation, instance, configFilePath);
getCompilerOptionDiagnostics = false;
const modules = determineModules(compilation, instance);
const filesToCheckForErrors = determineFilesToCheckForErrors(checkAllFilesForErrors, instance);
checkAllFilesForErrors = false;
const filesWithErrors = new Map();
provideErrorsToWebpack(filesToCheckForErrors, filesWithErrors, compilation, modules, instance);
provideSolutionErrorsToWebpack(compilation, modules, instance);
provideDeclarationFilesToWebpack(filesToCheckForErrors, instance, compilation);
provideTsBuildInfoFilesToWebpack(instance, compilation);
provideAssetsFromSolutionBuilderHost(instance, compilation);
instance.filesWithErrors = filesWithErrors;
instance.modifiedFiles = undefined;
instance.projectsMissingSourceMaps = new Set();
callback();
};
}
/**
* handle compiler option errors after the first compile
*/
function provideCompilerOptionDiagnosticErrorsToWebpack(getCompilerOptionDiagnostics, compilation, instance, configFilePath) {
if (getCompilerOptionDiagnostics) {
const { languageService, loaderOptions, compiler, program } = instance;
const errors = (0, utils_1.formatErrors)(program === undefined
? languageService.getCompilerOptionsDiagnostics()
: program.getOptionsDiagnostics(), loaderOptions, instance.colors, compiler, { file: configFilePath || 'tsconfig.json' }, compilation.compiler.context);
compilation.errors.push(...errors);
}
}
/**
* build map of all modules based on normalized filename
* this is used for quick-lookup when trying to find modules
* based on filepath
*/
function determineModules(compilation, { filePathKeyMapper }) {
const modules = new Map();
compilation.modules.forEach(module => {
if (module instanceof webpack.NormalModule && module.resource) {
const modulePath = filePathKeyMapper(module.resource);
const existingModules = modules.get(modulePath);
if (existingModules !== undefined) {
if (!existingModules.includes(module)) {
existingModules.push(module);
}
}
else {
modules.set(modulePath, [module]);
}
}
});
return modules;
}
function determineFilesToCheckForErrors(checkAllFilesForErrors, instance) {
const { files, modifiedFiles, filesWithErrors, otherFiles } = instance;
// calculate array of files to check
const filesToCheckForErrors = new Map();
if (checkAllFilesForErrors) {
// check all files on initial run
for (const [filePath, file] of files) {
addFileToCheckForErrors(filePath, file);
}
for (const [filePath, file] of otherFiles) {
addFileToCheckForErrors(filePath, file);
}
}
else if (modifiedFiles !== null &&
modifiedFiles !== undefined &&
modifiedFiles.size) {
const reverseDependencyGraph = (0, utils_1.populateReverseDependencyGraph)(instance);
// check all modified files, and all dependants
for (const modifiedFileName of modifiedFiles.keys()) {
for (const fileName of (0, utils_1.collectAllDependants)(reverseDependencyGraph, modifiedFileName).keys()) {
const fileToCheckForErrors = files.get(fileName) || otherFiles.get(fileName);
if (fileToCheckForErrors) { //file may have been removed
addFileToCheckForErrors(fileName, fileToCheckForErrors);
}
}
}
}
// re-check files with errors from previous build
if (filesWithErrors !== undefined) {
for (const [fileWithErrorName, fileWithErrors] of filesWithErrors) {
addFileToCheckForErrors(fileWithErrorName, fileWithErrors);
}
}
return filesToCheckForErrors;
function addFileToCheckForErrors(filePath, file) {
if (file && !(0, utils_1.isReferencedFile)(instance, filePath)) {
filesToCheckForErrors.set(filePath, file);
}
}
}
function provideErrorsToWebpack(filesToCheckForErrors, filesWithErrors, compilation, modules, instance) {
const { compiler, files, loaderOptions, compilerOptions, otherFiles, } = instance;
const filePathRegex = compilerOptions.allowJs === true
? constants.dtsTsTsxJsJsxRegex
: constants.dtsTsTsxRegex;
// Im pretty sure this will never be undefined here
const program = (0, utils_1.ensureProgram)(instance);
for (const [filePath, { fileName }] of filesToCheckForErrors.entries()) {
if (fileName.match(filePathRegex) === null) {
continue;
}
const sourceFile = program && program.getSourceFile(fileName);
const errors = [];
if (program && sourceFile) {
errors.push(...program.getSyntacticDiagnostics(sourceFile), ...program
.getSemanticDiagnostics(sourceFile)
// Output file has not been built from source file - this message is redundant with
// program.getOptionsDiagnostics() separately added in instances.ts
.filter(({ code }) => code !== 6305));
}
if (errors.length > 0) {
const fileWithError = files.get(filePath) || otherFiles.get(filePath);
filesWithErrors.set(filePath, fileWithError);
}
// if we have access to a webpack module, use that
const associatedModules = modules.get(instance.filePathKeyMapper(fileName));
if (associatedModules !== undefined) {
associatedModules.forEach(module => {
removeModuleTSLoaderError(module, loaderOptions);
// append errors
const formattedErrors = (0, utils_1.formatErrors)(errors, loaderOptions, instance.colors, compiler, { module }, compilation.compiler.context);
formattedErrors.forEach(error => {
if (module.addError) {
module.addError(error);
}
else {
module.errors.push(error);
}
});
compilation.errors.push(...formattedErrors);
});
}
else {
// otherwise it's a more generic error
const formattedErrors = (0, utils_1.formatErrors)(errors, loaderOptions, instance.colors, compiler, { file: fileName }, compilation.compiler.context);
compilation.errors.push(...formattedErrors);
}
}
}
function provideSolutionErrorsToWebpack(compilation, modules, instance) {
if (!instance.solutionBuilderHost ||
!(instance.solutionBuilderHost.diagnostics.global.length ||
instance.solutionBuilderHost.diagnostics.perFile.size)) {
return;
}
const { compiler, loaderOptions, solutionBuilderHost: { diagnostics }, } = instance;
for (const [filePath, perFileDiagnostics] of diagnostics.perFile) {
// if we have access to a webpack module, use that
const associatedModules = modules.get(filePath);
if (associatedModules !== undefined) {
associatedModules.forEach(module => {
removeModuleTSLoaderError(module, loaderOptions);
// append errors
const formattedErrors = (0, utils_1.formatErrors)(perFileDiagnostics, loaderOptions, instance.colors, compiler, { module }, compilation.compiler.context);
formattedErrors.forEach(error => {
if (module.addError) {
module.addError(error);
}
else {
module.errors.push(error);
}
});
compilation.errors.push(...formattedErrors);
});
}
else {
// otherwise it's a more generic error
const formattedErrors = (0, utils_1.formatErrors)(perFileDiagnostics, loaderOptions, instance.colors, compiler, { file: path.resolve(perFileDiagnostics[0].file.fileName) }, compilation.compiler.context);
compilation.errors.push(...formattedErrors);
}
}
// Add global solution errors
compilation.errors.push(...(0, utils_1.formatErrors)(diagnostics.global, instance.loaderOptions, instance.colors, instance.compiler, { file: 'tsconfig.json' }, compilation.compiler.context));
}
/**
* gather all declaration files from TypeScript and output them to webpack.
* JavaScript declaration files are included if `allowJs` is set.
*/
function provideDeclarationFilesToWebpack(filesToCheckForErrors, instance, compilation) {
const filePathRegex = instance.compilerOptions.allowJs === true
? constants.dtsTsTsxJsJsxRegex
: constants.dtsTsTsxRegex;
for (const { fileName } of filesToCheckForErrors.values()) {
if (fileName.match(filePathRegex) === null) {
continue;
}
addDeclarationFilesAsAsset((0, instances_1.getEmitOutput)(instance, fileName), compilation);
}
}
function addDeclarationFilesAsAsset(outputFiles, compilation, skipOutputFile) {
outputFilesToAsset(outputFiles, compilation, outputFile => skipOutputFile && skipOutputFile(outputFile)
? true
: !outputFile.name.match(constants.dtsDtsxOrDtsDtsxMapRegex));
}
function outputFileToAsset(outputFile, compilation) {
const assetPath = path
.relative(compilation.compiler.outputPath, outputFile.name)
// According to @alexander-akait (and @sokra) we should always '/' https://github.com/TypeStrong/ts-loader/pull/1251#issuecomment-799606985
.replace(/\\/g, '/');
// As suggested by @JonWallsten here: https://github.com/TypeStrong/ts-loader/pull/1251#issuecomment-800032753
compilation.emitAsset(assetPath, new webpack.sources.RawSource(outputFile.text));
}
function outputFilesToAsset(outputFiles, compilation, skipOutputFile) {
for (const outputFile of outputFiles) {
if (!skipOutputFile || !skipOutputFile(outputFile)) {
outputFileToAsset(outputFile, compilation);
}
}
}
/**
* gather all .tsbuildinfo for the project
*/
function provideTsBuildInfoFilesToWebpack(instance, compilation) {
if (instance.watchHost) {
// Ensure emit is complete
(0, instances_1.getEmitFromWatchHost)(instance);
if (instance.watchHost.tsbuildinfo) {
outputFileToAsset(instance.watchHost.tsbuildinfo, compilation);
}
instance.watchHost.outputFiles.clear();
instance.watchHost.tsbuildinfo = undefined;
}
}
/**
* gather all solution builder assets
*/
function provideAssetsFromSolutionBuilderHost(instance, compilation) {
if (instance.solutionBuilderHost) {
// written files
outputFilesToAsset(instance.solutionBuilderHost.writtenFiles, compilation);
instance.solutionBuilderHost.writtenFiles.length = 0;
}
}
/**
* handle all other errors. The basic approach here to get accurate error
* reporting is to start with a "blank slate" each compilation and gather
* all errors from all files. Since webpack tracks errors in a module from
* compilation-to-compilation, and since not every module always runs through
* the loader, we need to detect and remove any pre-existing errors.
*/
function removeCompilationTSLoaderErrors(compilation, loaderOptions) {
compilation.errors = compilation.errors.filter(error => error.details !== (0, utils_1.tsLoaderSource)(loaderOptions));
}
function removeModuleTSLoaderError(module, loaderOptions) {
const warnings = module.getWarnings();
const errors = module.getErrors();
module.clearWarningsAndErrors();
Array.from(warnings || []).forEach(warning => module.addWarning(warning));
Array.from(errors || [])
.filter((error) => error.loaderSource !== (0, utils_1.tsLoaderSource)(loaderOptions))
.forEach(error => module.addError(error));
}
//# sourceMappingURL=after-compile.js.map
+13
View File
@@ -0,0 +1,13 @@
import type * as typescript from 'typescript';
import type { LoaderOptions } from './interfaces';
import type * as logger from './logger';
export declare function getCompiler(loaderOptions: LoaderOptions, log: logger.Logger): {
compiler: typeof typescript | undefined;
compilerCompatible: boolean;
compilerDetailsLogMessage: string | undefined;
errorMessage: string | undefined;
};
export declare function getCompilerOptions(configParseResult: typescript.ParsedCommandLine, compiler: typeof typescript): {
skipLibCheck: boolean;
} & typescript.CompilerOptions;
//# sourceMappingURL=compilerSetup.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"compilerSetup.d.ts","sourceRoot":"","sources":["../src/compilerSetup.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,KAAK,UAAU,MAAM,YAAY,CAAC;AAE9C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,KAAK,KAAK,MAAM,MAAM,UAAU,CAAC;AAExC,wBAAgB,WAAW,CAAC,aAAa,EAAE,aAAa,EAAE,GAAG,EAAE,MAAM,CAAC,MAAM;;;;;EA6C3E;AAED,wBAAgB,kBAAkB,CAChC,iBAAiB,EAAE,UAAU,CAAC,iBAAiB,EAC/C,QAAQ,EAAE,OAAO,UAAU;;+BA8B5B"}
+64
View File
@@ -0,0 +1,64 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCompiler = getCompiler;
exports.getCompilerOptions = getCompilerOptions;
const semver = require("semver");
function getCompiler(loaderOptions, log) {
let compiler;
let errorMessage;
let compilerDetailsLogMessage;
let compilerCompatible = false;
try {
compiler = require(loaderOptions.compiler);
}
catch (e) {
errorMessage =
loaderOptions.compiler === 'typescript'
? 'Could not load TypeScript. Try installing with `yarn add typescript` or `npm install typescript`. If TypeScript is installed globally, try using `yarn link typescript` or `npm link typescript`.'
: `Could not load TypeScript compiler with NPM package name \`${loaderOptions.compiler}\`. Are you sure it is correctly installed?`;
}
if (errorMessage === undefined) {
compilerDetailsLogMessage = `ts-loader: Using ${loaderOptions.compiler}@${compiler.version}`;
compilerCompatible = false;
if (loaderOptions.compiler === 'typescript') {
if (compiler.version !== undefined &&
semver.gte(compiler.version, '3.6.3')) {
// don't log yet in this case, if a tsconfig.json exists we want to combine the message
compilerCompatible = true;
}
else {
log.logError(`${compilerDetailsLogMessage}. This version is incompatible with ts-loader. Please upgrade to the latest version of TypeScript.`);
}
}
else {
log.logWarning(`${compilerDetailsLogMessage}. This version may or may not be compatible with ts-loader.`);
}
}
return {
compiler,
compilerCompatible,
compilerDetailsLogMessage,
errorMessage,
};
}
function getCompilerOptions(configParseResult, compiler) {
const defaultOptions = { skipLibCheck: true };
const compilerOptions = Object.assign(defaultOptions, configParseResult.options, {
suppressOutputPathCheck: true, // This is why: https://github.com/Microsoft/TypeScript/issues/7363
});
// if `module` is not specified and not using ES6+ target, default to CJS module output
if (compilerOptions.module === undefined &&
compilerOptions.target !== undefined &&
compilerOptions.target < compiler.ScriptTarget.ES2015) {
compilerOptions.module = compiler.ModuleKind.CommonJS;
}
if (configParseResult.options.configFile) {
Object.defineProperty(compilerOptions, 'configFile', {
enumerable: false,
writable: false,
value: configParseResult.options.configFile,
});
}
return compilerOptions;
}
//# sourceMappingURL=compilerSetup.js.map
+18
View File
@@ -0,0 +1,18 @@
import type { Chalk } from 'chalk';
import type * as typescript from 'typescript';
import type * as webpack from 'webpack';
import type { LoaderOptions } from './interfaces';
import type * as logger from './logger';
interface ConfigFile {
config?: any;
error?: typescript.Diagnostic;
}
export declare function getConfigFile(compiler: typeof typescript, colors: Chalk, loader: webpack.LoaderContext<LoaderOptions>, loaderOptions: LoaderOptions, compilerCompatible: boolean, log: logger.Logger, compilerDetailsLogMessage: string): {
configFilePath: string | undefined;
configFile: ConfigFile;
configFileError: webpack.WebpackError | undefined;
};
export declare function getConfigParseResult(compiler: typeof typescript, configFile: ConfigFile, basePath: string, configFilePath: string | undefined, loaderOptions: LoaderOptions): typescript.ParsedCommandLine;
export declare function getParsedCommandLine(compiler: typeof typescript, loaderOptions: LoaderOptions, configFilePath: string): typescript.ParsedCommandLine | undefined;
export {};
//# sourceMappingURL=config.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAEnC,OAAO,KAAK,KAAK,UAAU,MAAM,YAAY,CAAC;AAC9C,OAAO,KAAK,KAAK,OAAO,MAAM,SAAS,CAAC;AAGxC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,KAAK,KAAK,MAAM,MAAM,UAAU,CAAC;AAGxC,UAAU,UAAU;IAClB,MAAM,CAAC,EAAE,GAAG,CAAC;IACb,KAAK,CAAC,EAAE,UAAU,CAAC,UAAU,CAAC;CAC/B;AAED,wBAAgB,aAAa,CAC3B,QAAQ,EAAE,OAAO,UAAU,EAC3B,MAAM,EAAE,KAAK,EACb,MAAM,EAAE,OAAO,CAAC,aAAa,CAAC,aAAa,CAAC,EAC5C,aAAa,EAAE,aAAa,EAC5B,kBAAkB,EAAE,OAAO,EAC3B,GAAG,EAAE,MAAM,CAAC,MAAM,EAClB,yBAAyB,EAAE,MAAM;;;;EAsDlC;AAgDD,wBAAgB,oBAAoB,CAClC,QAAQ,EAAE,OAAO,UAAU,EAC3B,UAAU,EAAE,UAAU,EACtB,QAAQ,EAAE,MAAM,EAChB,cAAc,EAAE,MAAM,GAAG,SAAS,EAClC,aAAa,EAAE,aAAa,gCA8B7B;AAGD,wBAAgB,oBAAoB,CAClC,QAAQ,EAAE,OAAO,UAAU,EAC3B,aAAa,EAAE,aAAa,EAC5B,cAAc,EAAE,MAAM,GACrB,UAAU,CAAC,iBAAiB,GAAG,SAAS,CAwB1C"}
+114
View File
@@ -0,0 +1,114 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getConfigFile = getConfigFile;
exports.getConfigParseResult = getConfigParseResult;
exports.getParsedCommandLine = getParsedCommandLine;
const path = require("path");
const compilerSetup_1 = require("./compilerSetup");
const utils_1 = require("./utils");
function getConfigFile(compiler, colors, loader, loaderOptions, compilerCompatible, log, compilerDetailsLogMessage) {
const configFilePath = findConfigFile(compiler, path.dirname(loader.resourcePath), loaderOptions.configFile);
let configFileError;
let configFile;
if (configFilePath !== undefined) {
if (compilerCompatible) {
log.logInfo(`${compilerDetailsLogMessage} and ${configFilePath}`);
}
else {
log.logInfo(`ts-loader: Using config file at ${configFilePath}`);
}
configFile = compiler.readConfigFile(configFilePath, compiler.sys.readFile);
if (configFile.error !== undefined) {
configFileError = (0, utils_1.formatErrors)([configFile.error], loaderOptions, colors, compiler, { file: configFilePath }, loader.context)[0];
}
}
else {
if (compilerCompatible) {
log.logInfo(compilerDetailsLogMessage);
}
configFile = {
config: {
compilerOptions: {},
files: [],
},
};
}
if (configFileError === undefined) {
configFile.config.compilerOptions = Object.assign({}, configFile.config.compilerOptions);
}
return {
configFilePath,
configFile,
configFileError,
};
}
/**
* Find a tsconfig file by name or by path.
* By name, the tsconfig.json is found using the same method as `tsc`, starting in the current
* directory and continuing up the parent directory chain.
* By path, the file will be found by resolving the given path relative to the requesting entry file.
*
* @param compiler The TypeScript compiler instance
* @param requestDirPath The directory in which the entry point requesting the tsconfig.json lies
* @param configFile The tsconfig file name to look for or a path to that file
* @return The absolute path to the tsconfig file, undefined if none was found.
*/
function findConfigFile(compiler, requestDirPath, configFile) {
// If `configFile` is an absolute path, return it right away
if (path.isAbsolute(configFile)) {
return compiler.sys.fileExists(configFile) ? configFile : undefined;
}
// If `configFile` is a relative path, resolve it.
// We define a relative path as: starts with
// one or two dots + a common directory delimiter
if (configFile.match(/^\.\.?(\/|\\)/) !== null) {
const resolvedPath = path.resolve(requestDirPath, configFile);
return compiler.sys.fileExists(resolvedPath) ? resolvedPath : undefined;
// If `configFile` is a file name, find it in the directory tree
}
else {
while (true) {
const fileName = path.join(requestDirPath, configFile);
if (compiler.sys.fileExists(fileName)) {
return fileName;
}
const parentPath = path.dirname(requestDirPath);
if (parentPath === requestDirPath) {
break;
}
requestDirPath = parentPath;
}
return undefined;
}
}
function getConfigParseResult(compiler, configFile, basePath, configFilePath, loaderOptions) {
const configParseResult = compiler.parseJsonConfigFileContent(configFile.config, {
...compiler.sys,
useCaseSensitiveFileNames: (0, utils_1.useCaseSensitiveFileNames)(compiler, loaderOptions),
}, basePath, getCompilerOptionsToExtend(compiler, loaderOptions, basePath, configFilePath || 'tsconfig.json'));
if (!loaderOptions.projectReferences) {
configParseResult.projectReferences = undefined;
}
// set internal options.configFilePath flag on options to denote that we read this from a file
configParseResult.options = Object.assign({}, configParseResult.options, {
configFilePath,
});
return configParseResult;
}
const extendedConfigCache = new Map();
function getParsedCommandLine(compiler, loaderOptions, configFilePath) {
const result = compiler.getParsedCommandLineOfConfigFile(configFilePath, getCompilerOptionsToExtend(compiler, loaderOptions, path.dirname(configFilePath), configFilePath), {
...compiler.sys,
useCaseSensitiveFileNames: (0, utils_1.useCaseSensitiveFileNames)(compiler, loaderOptions),
// eslint-disable-next-line @typescript-eslint/no-empty-function
onUnRecoverableConfigFileDiagnostic: () => { },
}, extendedConfigCache);
if (result) {
result.options = (0, compilerSetup_1.getCompilerOptions)(result, compiler);
}
return result;
}
function getCompilerOptionsToExtend(compiler, loaderOptions, basePath, configFileName) {
return compiler.convertCompilerOptionsFromJson(loaderOptions.compilerOptions, basePath, configFileName).options;
}
//# sourceMappingURL=config.js.map
+18
View File
@@ -0,0 +1,18 @@
export declare const EOL: string;
export declare const CarriageReturnLineFeed = "\r\n";
export declare const LineFeed = "\n";
export declare const CarriageReturnLineFeedCode = 0;
export declare const LineFeedCode = 1;
export declare const extensionRegex: RegExp;
export declare const tsxRegex: RegExp;
export declare const tsTsxRegex: RegExp;
export declare const declarationRegex: RegExp;
export declare const dtsDtsxOrDtsDtsxMapRegex: RegExp;
export declare const dtsTsTsxRegex: RegExp;
export declare const dtsTsTsxJsJsxRegex: RegExp;
export declare const tsTsxJsJsxRegex: RegExp;
export declare const jsJsx: RegExp;
export declare const jsJsxMap: RegExp;
export declare const jsonRegex: RegExp;
export declare const nodeModules: RegExp;
//# sourceMappingURL=constants.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,GAAG,QAAS,CAAC;AAC1B,eAAO,MAAM,sBAAsB,SAAS,CAAC;AAC7C,eAAO,MAAM,QAAQ,OAAO,CAAC;AAE7B,eAAO,MAAM,0BAA0B,IAAI,CAAC;AAC5C,eAAO,MAAM,YAAY,IAAI,CAAC;AAE9B,eAAO,MAAM,cAAc,QAAa,CAAC;AACzC,eAAO,MAAM,QAAQ,QAAY,CAAC;AAClC,eAAO,MAAM,UAAU,QAAsB,CAAC;AAC9C,eAAO,MAAM,gBAAgB,QAAyB,CAAC;AACvD,eAAO,MAAM,wBAAwB,QAAiC,CAAC;AACvE,eAAO,MAAM,aAAa,QAA4B,CAAC;AACvD,eAAO,MAAM,kBAAkB,QAAoC,CAAC;AACpE,eAAO,MAAM,eAAe,QAA4B,CAAC;AACzD,eAAO,MAAM,KAAK,QAAsB,CAAC;AACzC,eAAO,MAAM,QAAQ,QAA2B,CAAC;AACjD,eAAO,MAAM,SAAS,QAAa,CAAC;AACpC,eAAO,MAAM,WAAW,QAAkB,CAAC"}
+22
View File
@@ -0,0 +1,22 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.nodeModules = exports.jsonRegex = exports.jsJsxMap = exports.jsJsx = exports.tsTsxJsJsxRegex = exports.dtsTsTsxJsJsxRegex = exports.dtsTsTsxRegex = exports.dtsDtsxOrDtsDtsxMapRegex = exports.declarationRegex = exports.tsTsxRegex = exports.tsxRegex = exports.extensionRegex = exports.LineFeedCode = exports.CarriageReturnLineFeedCode = exports.LineFeed = exports.CarriageReturnLineFeed = exports.EOL = void 0;
const os = require("os");
exports.EOL = os.EOL;
exports.CarriageReturnLineFeed = '\r\n';
exports.LineFeed = '\n';
exports.CarriageReturnLineFeedCode = 0;
exports.LineFeedCode = 1;
exports.extensionRegex = /\.[^.]+$/;
exports.tsxRegex = /\.tsx$/i;
exports.tsTsxRegex = /\.([cm]?ts|tsx)$/i;
exports.declarationRegex = /\.d\.([cm]?ts|tsx)$/i;
exports.dtsDtsxOrDtsDtsxMapRegex = /\.d\.([cm]?ts|tsx)(\.map)?$/i;
exports.dtsTsTsxRegex = /(\.d)?\.([cm]?ts|tsx)$/i;
exports.dtsTsTsxJsJsxRegex = /((\.d)?\.([cm]?[tj]s|[tj]sx))$/i;
exports.tsTsxJsJsxRegex = /\.([cm]?[tj]s|[tj]sx)$/i;
exports.jsJsx = /\.([cm]?js|jsx)$/i;
exports.jsJsxMap = /\.([cm]?js|jsx)\.map$/i;
exports.jsonRegex = /\.json$/i;
exports.nodeModules = /node_modules/i;
//# sourceMappingURL=constants.js.map
+15
View File
@@ -0,0 +1,15 @@
import type * as webpack from 'webpack';
import type { LoaderOptions } from './interfaces';
/**
* The entry point for ts-loader
*/
declare function loader(this: webpack.LoaderContext<LoaderOptions>, contents: string, inputSourceMap?: Record<string, any>): void;
export = loader;
/**
* expose public types via declaration merging
*/
declare namespace loader {
interface Options extends LoaderOptions {
}
}
//# sourceMappingURL=index.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,KAAK,OAAO,MAAM,SAAS,CAAC;AAWxC,OAAO,KAAK,EAEV,aAAa,EAId,MAAM,cAAc,CAAC;AAYtB;;GAEG;AACH,iBAAS,MAAM,CACb,IAAI,EAAE,OAAO,CAAC,aAAa,CAAC,aAAa,CAAC,EAC1C,QAAQ,EAAE,MAAM,EAChB,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,QAcrC;AAkrBD,SAAS,MAAM,CAAC;AAEhB;;GAEG;AAEH,kBAAU,MAAM,CAAC;IAEf,UAAiB,OAAQ,SAAQ,aAAa;KAAG;CAClD"}
+484
View File
@@ -0,0 +1,484 @@
"use strict";
const crypto = require("crypto");
const path = require("path");
const constants = require("./constants");
const instances_1 = require("./instances");
const utils_1 = require("./utils");
const source_map_1 = require("source-map");
const loaderOptionsCache = {};
/**
* The entry point for ts-loader
*/
function loader(contents, inputSourceMap) {
this.cacheable && this.cacheable();
const callback = this.async();
const options = getLoaderOptions(this);
const instanceOrError = (0, instances_1.getTypeScriptInstance)(options, this);
if (instanceOrError.error !== undefined) {
callback(new Error(instanceOrError.error.message));
return;
}
const instance = instanceOrError.instance;
(0, instances_1.buildSolutionReferences)(instance, this);
successLoader(this, contents, callback, instance, inputSourceMap);
}
function successLoader(loaderContext, contents, callback, instance, inputSourceMap) {
(0, instances_1.initializeInstance)(loaderContext, instance);
(0, instances_1.reportTranspileErrors)(instance, loaderContext);
const rawFilePath = path.normalize(loaderContext.resourcePath);
const filePath = instance.loaderOptions.appendTsSuffixTo.length > 0 ||
instance.loaderOptions.appendTsxSuffixTo.length > 0
? (0, utils_1.appendSuffixesIfMatch)({
'.ts': instance.loaderOptions.appendTsSuffixTo,
'.tsx': instance.loaderOptions.appendTsxSuffixTo,
}, rawFilePath)
: rawFilePath;
const fileVersion = updateFileInCache(instance.loaderOptions, filePath, contents, instance);
const { outputText, sourceMapText } = instance.loaderOptions.transpileOnly
? getTranspilationEmit(filePath, contents, instance, loaderContext)
: getEmit(rawFilePath, filePath, instance, loaderContext);
// the following function is async, which means it will immediately return and run in the "background"
// Webpack will be notified when it's finished when the function calls the `callback` method
makeSourceMapAndFinish(sourceMapText, outputText, filePath, contents, loaderContext, fileVersion, callback, instance, inputSourceMap);
}
function makeSourceMapAndFinish(sourceMapText, outputText, filePath, contents, loaderContext, fileVersion, callback, instance, inputSourceMap) {
if (outputText === null || outputText === undefined) {
setModuleMeta(loaderContext, instance, fileVersion);
const additionalGuidance = (0, utils_1.isReferencedFile)(instance, filePath)
? ' The most common cause for this is having errors when building referenced projects.'
: !instance.loaderOptions.allowTsInNodeModules &&
filePath.indexOf('node_modules') !== -1
? ' By default, ts-loader will not compile .ts files in node_modules.\n' +
'You should not need to recompile .ts files there, but if you really want to, use the allowTsInNodeModules option.\n' +
'See: https://github.com/Microsoft/TypeScript/issues/12358'
: '';
callback(new Error(`TypeScript emitted no output for ${filePath}.${additionalGuidance}`), outputText, undefined);
return;
}
const { sourceMap, output } = makeSourceMap(sourceMapText, outputText, filePath, contents, loaderContext);
setModuleMeta(loaderContext, instance, fileVersion);
// there are two cases where we don't need to perform input source map mapping:
// - either the ts-compiler did not generate a source map (tsconfig had `sourceMap` set to false)
// - or we did not get an input source map
//
// in the first case, we simply return undefined.
// in the second case we only need to return the newly generated source map
// this avoids that we have to make a possibly expensive call to the source-map lib
if (sourceMap === undefined || !inputSourceMap) {
callback(null, output, sourceMap);
return;
}
// otherwise we have to make a mapping to the input source map which is asynchronous
mapToInputSourceMap(sourceMap, loaderContext, inputSourceMap)
.then(mappedSourceMap => {
callback(null, output, mappedSourceMap);
})
.catch((e) => {
callback(e);
});
}
function setModuleMeta(loaderContext, instance, fileVersion) {
// _module.meta is not available inside happypack
if (!instance.loaderOptions.happyPackMode &&
loaderContext._module.buildMeta !== undefined) {
// Make sure webpack is aware that even though the emitted JavaScript may be the same as
// a previously cached version the TypeScript may be different and therefore should be
// treated as new
loaderContext._module.buildMeta.tsLoaderFileVersion = fileVersion;
}
}
/**
* Get a unique hash based on the contents of the options
* Hash is created from the values converted to strings
* Values which are functions (such as getCustomTransformers) are
* converted to strings by this code, which JSON.stringify would not do.
*/
function getOptionsHash(loaderOptions) {
const hash = crypto.createHash('sha256');
Object.keys(loaderOptions).forEach(key => {
const value = loaderOptions[key];
if (value !== undefined) {
const valueString = typeof value === 'function' ? value.toString() : JSON.stringify(value);
hash.update(key + valueString);
}
});
return hash.digest('hex').substring(0, 16);
}
/**
* either retrieves loader options from the cache
* or creates them, adds them to the cache and returns
*/
function getLoaderOptions(loaderContext) {
const loaderOptions = loaderContext.getOptions();
// If no instance name is given in the options, use the hash of the loader options
// In this way, if different options are given the instances will be different
const instanceName = loaderOptions.instance || 'default_' + getOptionsHash(loaderOptions);
if (!loaderOptionsCache.hasOwnProperty(instanceName)) {
loaderOptionsCache[instanceName] = new WeakMap();
}
const cache = loaderOptionsCache[instanceName];
if (cache.has(loaderOptions)) {
return cache.get(loaderOptions);
}
validateLoaderOptions(loaderOptions);
const options = makeLoaderOptions(instanceName, loaderOptions, loaderContext);
cache.set(loaderOptions, options);
return options;
}
const validLoaderOptions = [
'silent',
'logLevel',
'logInfoToStdOut',
'instance',
'compiler',
'context',
'configFile',
'transpileOnly',
'ignoreDiagnostics',
'errorFormatter',
'colors',
'compilerOptions',
'appendTsSuffixTo',
'appendTsxSuffixTo',
'onlyCompileBundledFiles',
'happyPackMode',
'getCustomTransformers',
'reportFiles',
'experimentalWatchApi',
'allowTsInNodeModules',
'experimentalFileCaching',
'projectReferences',
'resolveModuleName',
'resolveTypeReferenceDirective',
'useCaseSensitiveFileNames',
];
/**
* Validate the supplied loader options.
* At present this validates the option names only; in future we may look at validating the values too
* @param loaderOptions
*/
function validateLoaderOptions(loaderOptions) {
const loaderOptionKeys = Object.keys(loaderOptions);
for (let i = 0; i < loaderOptionKeys.length; i++) {
const option = loaderOptionKeys[i];
const isUnexpectedOption = validLoaderOptions.indexOf(option) === -1;
if (isUnexpectedOption) {
throw new Error(`ts-loader was supplied with an unexpected loader option: ${option}
Please take a look at the options you are supplying; the following are valid options:
${validLoaderOptions.join(' / ')}
`);
}
}
if (loaderOptions.context !== undefined &&
!path.isAbsolute(loaderOptions.context)) {
throw new Error(`Option 'context' has to be an absolute path. Given '${loaderOptions.context}'.`);
}
}
function makeLoaderOptions(instanceName, loaderOptions, loaderContext) {
var _a;
const hasForkTsCheckerWebpackPlugin = (_a = loaderContext._compiler) === null || _a === void 0 ? void 0 : _a.options.plugins.some(plugin => {
var _a;
return typeof plugin === 'object' &&
((_a = plugin.constructor) === null || _a === void 0 ? void 0 : _a.name) === 'ForkTsCheckerWebpackPlugin';
});
const options = Object.assign({}, {
silent: false,
logLevel: 'WARN',
logInfoToStdOut: false,
compiler: 'typescript',
context: undefined,
// Set default transpileOnly to true if there is an instance of ForkTsCheckerWebpackPlugin
transpileOnly: hasForkTsCheckerWebpackPlugin,
compilerOptions: {},
appendTsSuffixTo: [],
appendTsxSuffixTo: [],
transformers: {},
happyPackMode: false,
colors: true,
onlyCompileBundledFiles: false,
reportFiles: [],
// When the watch API usage stabilises look to remove this option and make watch usage the default behaviour when available
experimentalWatchApi: false,
allowTsInNodeModules: false,
experimentalFileCaching: true,
}, loaderOptions);
options.ignoreDiagnostics = (0, utils_1.arrify)(options.ignoreDiagnostics).map(Number);
options.logLevel = options.logLevel.toUpperCase();
options.instance = instanceName;
options.configFile = options.configFile || 'tsconfig.json';
// happypack can be used only together with transpileOnly mode
options.transpileOnly = options.happyPackMode ? true : options.transpileOnly;
return options;
}
/**
* Either add file to the overall files cache or update it in the cache when the file contents have changed
* Also add the file to the modified files
*/
function updateFileInCache(options, filePath, contents, instance) {
let fileWatcherEventKind;
// Update file contents
const key = instance.filePathKeyMapper(filePath);
let file = instance.files.get(key);
if (file === undefined) {
file = instance.otherFiles.get(key);
if (file !== undefined) {
if (!(0, utils_1.isReferencedFile)(instance, filePath)) {
instance.otherFiles.delete(key);
instance.files.set(key, file);
instance.changedFilesList = true;
}
}
else {
if (instance.watchHost !== undefined) {
fileWatcherEventKind = instance.compiler.FileWatcherEventKind.Created;
}
file = { fileName: filePath, version: 0 };
if (!(0, utils_1.isReferencedFile)(instance, filePath)) {
instance.files.set(key, file);
instance.changedFilesList = true;
}
}
}
if (instance.watchHost !== undefined && contents === undefined) {
fileWatcherEventKind = instance.compiler.FileWatcherEventKind.Deleted;
}
// filePath is a root file as it was passed to the loader. But it
// could have been found earlier as a dependency of another file. If
// that is the case, compiling this file changes the structure of
// the program and we need to increase the instance version.
//
// See https://github.com/TypeStrong/ts-loader/issues/943
if (!(0, utils_1.isReferencedFile)(instance, filePath) &&
!instance.rootFileNames.has(filePath) &&
// however, be careful not to add files from node_modules unless
// it is allowed by the options.
(options.allowTsInNodeModules || filePath.indexOf('node_modules') === -1)) {
instance.version++;
instance.rootFileNames.add(filePath);
}
if (file.text !== contents) {
file.version++;
file.text = contents;
file.modifiedTime = new Date();
instance.version++;
if (instance.watchHost !== undefined &&
fileWatcherEventKind === undefined) {
fileWatcherEventKind = instance.compiler.FileWatcherEventKind.Changed;
}
}
// Added in case the files were already updated by the watch API
if (instance.modifiedFiles && instance.modifiedFiles.get(key)) {
fileWatcherEventKind = instance.compiler.FileWatcherEventKind.Changed;
}
if (instance.watchHost !== undefined && fileWatcherEventKind !== undefined) {
instance.hasUnaccountedModifiedFiles =
instance.watchHost.invokeFileWatcher(filePath, fileWatcherEventKind) ||
instance.hasUnaccountedModifiedFiles;
}
// push this file to modified files hash.
if (!instance.modifiedFiles) {
instance.modifiedFiles = new Map();
}
instance.modifiedFiles.set(key, true);
return file.version;
}
function getEmit(rawFilePath, filePath, instance, loaderContext) {
var _a;
const outputFiles = (0, instances_1.getEmitOutput)(instance, filePath);
loaderContext.clearDependencies();
loaderContext.addDependency(rawFilePath);
const dependencies = [];
const addDependency = (file) => {
file = path.resolve(file);
loaderContext.addDependency(file);
dependencies.push(file);
};
// Make this file dependent on *all* definition files in the program
if (!(0, utils_1.isReferencedFile)(instance, filePath)) {
for (const { fileName: defFilePath } of instance.files.values()) {
if (defFilePath.match(constants.dtsDtsxOrDtsDtsxMapRegex) &&
// Remove the project reference d.ts as we are adding dependency for .ts later
// This removed extra build pass (resulting in new stats object in initial build)
!((_a = instance.solutionBuilderHost) === null || _a === void 0 ? void 0 : _a.getOutputFileKeyFromReferencedProject(defFilePath))) {
addDependency(defFilePath);
}
}
}
// Additionally make this file dependent on all imported files
const fileDependencies = instance.dependencyGraph.get(instance.filePathKeyMapper(filePath));
if (fileDependencies) {
for (const { resolvedFileName, originalFileName } of fileDependencies) {
// In the case of dependencies that are part of a project reference,
// the real dependency that webpack should watch is the JS output file.
addDependency((0, instances_1.getInputFileNameFromOutput)(instance, path.resolve(resolvedFileName)) ||
originalFileName);
}
}
addDependenciesFromSolutionBuilder(instance, filePath, addDependency);
loaderContext._module.buildMeta.tsLoaderDefinitionFileVersions =
dependencies.map(defFilePath => path.relative(loaderContext.rootContext, defFilePath) +
'@' +
((0, utils_1.isReferencedFile)(instance, defFilePath)
? instance
.solutionBuilderHost.getInputFileStamp(defFilePath)
.toString()
: (instance.files.get(instance.filePathKeyMapper(defFilePath)) ||
instance.otherFiles.get(instance.filePathKeyMapper(defFilePath)) || {
version: '?',
}).version));
return getOutputAndSourceMapFromOutputFiles(outputFiles);
}
function getOutputAndSourceMapFromOutputFiles(outputFiles) {
const outputFile = outputFiles
.filter(file => file.name.match(constants.jsJsx))
.pop();
const outputText = outputFile === undefined ? undefined : outputFile.text;
const sourceMapFile = outputFiles
.filter(file => file.name.match(constants.jsJsxMap))
.pop();
const sourceMapText = sourceMapFile === undefined ? undefined : sourceMapFile.text;
return { outputText, sourceMapText };
}
function addDependenciesFromSolutionBuilder(instance, filePath, addDependency) {
if (!instance.solutionBuilderHost) {
return;
}
// Add all the input files from the references as
const resolvedFilePath = instance.filePathKeyMapper(filePath);
if (!(0, utils_1.isReferencedFile)(instance, filePath)) {
if (instance.configParseResult.fileNames.some(f => instance.filePathKeyMapper(f) === resolvedFilePath)) {
addDependenciesFromProjectReferences(instance, instance.configFilePath, instance.configParseResult.projectReferences, addDependency);
}
return;
}
// Referenced file find the config for it
for (const [configFile, configInfo,] of instance.solutionBuilderHost.configFileInfo.entries()) {
if (!configInfo.config ||
!configInfo.config.projectReferences ||
!configInfo.config.projectReferences.length) {
continue;
}
if (configInfo.outputFileNames) {
if (!configInfo.outputFileNames.has(resolvedFilePath)) {
continue;
}
}
else if (!configInfo.config.fileNames.some(f => instance.filePathKeyMapper(f) === resolvedFilePath)) {
continue;
}
// Depend on all the dts files from the program
if (configInfo.dtsFiles) {
configInfo.dtsFiles.forEach(addDependency);
}
addDependenciesFromProjectReferences(instance, configFile, configInfo.config.projectReferences, addDependency);
break;
}
}
function addDependenciesFromProjectReferences(instance, configFile, projectReferences, addDependency) {
if (!projectReferences || !projectReferences.length) {
return;
}
// This is the config for the input file
const seenMap = new Map();
seenMap.set(instance.filePathKeyMapper(configFile), true);
// Add dependencies to all the input files from the project reference files since building them
const queue = projectReferences.slice();
while (true) {
const currentRef = queue.pop();
if (!currentRef) {
break;
}
const refConfigFile = instance.filePathKeyMapper(instance.compiler.resolveProjectReferencePath(currentRef));
if (seenMap.has(refConfigFile)) {
continue;
}
const refConfigInfo = instance.solutionBuilderHost.configFileInfo.get(refConfigFile);
if (!refConfigInfo) {
continue;
}
seenMap.set(refConfigFile, true);
if (refConfigInfo.config) {
refConfigInfo.config.fileNames.forEach(addDependency);
if (refConfigInfo.config.projectReferences) {
queue.push(...refConfigInfo.config.projectReferences);
}
}
}
}
/**
* Transpile file
*/
function getTranspilationEmit(fileName, contents, instance, loaderContext) {
if ((0, utils_1.isReferencedFile)(instance, fileName)) {
const outputFiles = instance.solutionBuilderHost.getOutputFilesFromReferencedProjectInput(fileName);
addDependenciesFromSolutionBuilder(instance, fileName, file => loaderContext.addDependency(path.resolve(file)));
return getOutputAndSourceMapFromOutputFiles(outputFiles);
}
const { outputText, sourceMapText, diagnostics } = instance.compiler.transpileModule(contents, {
compilerOptions: { ...instance.compilerOptions, rootDir: undefined },
transformers: instance.transformers,
reportDiagnostics: true,
fileName,
});
const module = loaderContext._module;
addDependenciesFromSolutionBuilder(instance, fileName, file => loaderContext.addDependency(path.resolve(file)));
// _module.errors is not available inside happypack - see https://github.com/TypeStrong/ts-loader/issues/336
if (!instance.loaderOptions.happyPackMode) {
const errors = (0, utils_1.formatErrors)(diagnostics, instance.loaderOptions, instance.colors, instance.compiler, { module }, loaderContext.context);
errors.forEach(error => module.addError(error));
}
return { outputText, sourceMapText };
}
function makeSourceMap(sourceMapText, outputText, filePath, contents, loaderContext) {
if (sourceMapText === undefined) {
return { output: outputText, sourceMap: undefined };
}
return {
output: outputText.replace(/^\/\/# sourceMappingURL=[^\r\n]*/gm, ''),
sourceMap: Object.assign(JSON.parse(sourceMapText), {
sources: [loaderContext.remainingRequest],
file: filePath,
sourcesContent: [contents],
}),
};
}
/**
* This method maps the newly generated @param{sourceMap} to the input source map.
* This is required when ts-loader is not the first loader in the Webpack loader chain.
*/
function mapToInputSourceMap(sourceMap, loaderContext, inputSourceMap) {
return new Promise((resolve, reject) => {
const inMap = {
file: loaderContext.remainingRequest,
mappings: inputSourceMap.mappings,
names: inputSourceMap.names,
sources: inputSourceMap.sources,
sourceRoot: inputSourceMap.sourceRoot,
sourcesContent: inputSourceMap.sourcesContent,
version: inputSourceMap.version,
};
Promise.all([
new source_map_1.SourceMapConsumer(inMap),
new source_map_1.SourceMapConsumer(sourceMap),
])
.then(sourceMapConsumers => {
try {
const generator = source_map_1.SourceMapGenerator.fromSourceMap(sourceMapConsumers[1]);
generator.applySourceMap(sourceMapConsumers[0]);
const mappedSourceMap = generator.toJSON();
// before resolving, we free memory by calling destroy on the source map consumers
sourceMapConsumers.forEach(sourceMapConsumer => sourceMapConsumer.destroy());
resolve(mappedSourceMap);
}
catch (e) {
//before rejecting, we free memory by calling destroy on the source map consumers
sourceMapConsumers.forEach(sourceMapConsumer => sourceMapConsumer.destroy());
reject(e);
}
})
.catch(reject);
});
}
module.exports = loader;
//# sourceMappingURL=index.js.map
+5
View File
@@ -0,0 +1,5 @@
import type * as webpack from 'webpack';
import type { TSInstance } from './interfaces';
export declare function getTSInstanceFromCache(key: webpack.Compiler, name: string): TSInstance | undefined;
export declare function setTSInstanceInCache(key: webpack.Compiler | undefined, name: string, instance: TSInstance): void;
//# sourceMappingURL=instance-cache.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"instance-cache.d.ts","sourceRoot":"","sources":["../src/instance-cache.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,OAAO,MAAM,SAAS,CAAC;AACxC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAY/C,wBAAgB,sBAAsB,CACpC,GAAG,EAAE,OAAO,CAAC,QAAQ,EACrB,IAAI,EAAE,MAAM,GACX,UAAU,GAAG,SAAS,CAUxB;AAED,wBAAgB,oBAAoB,CAClC,GAAG,EAAE,OAAO,CAAC,QAAQ,GAAG,SAAS,EACjC,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,UAAU,QAOrB"}
+29
View File
@@ -0,0 +1,29 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTSInstanceFromCache = getTSInstanceFromCache;
exports.setTSInstanceInCache = setTSInstanceInCache;
// Some loaders (e.g. thread-loader) will set the _compiler property to undefined.
// We can't use undefined as a WeakMap key as it will throw an error at runtime,
// thus we keep a dummy "marker" object to use as key in those situations.
const marker = {};
// Each TypeScript instance is cached based on the webpack instance (key of the WeakMap)
// and also the name that was generated or passed via the options (string key of the
// internal Map)
const cache = new WeakMap();
function getTSInstanceFromCache(key, name) {
const compiler = key !== null && key !== void 0 ? key : marker;
let instances = cache.get(compiler);
if (!instances) {
instances = new Map();
cache.set(compiler, instances);
}
return instances.get(name);
}
function setTSInstanceInCache(key, name, instance) {
var _a;
const compiler = key !== null && key !== void 0 ? key : marker;
const instances = (_a = cache.get(compiler)) !== null && _a !== void 0 ? _a : new Map();
instances.set(name, instance);
cache.set(compiler, instances);
}
//# sourceMappingURL=instance-cache.js.map
+24
View File
@@ -0,0 +1,24 @@
import type * as typescript from 'typescript';
import * as webpack from 'webpack';
import type { LoaderOptions, TSInstance } from './interfaces';
/**
* The loader is executed once for each file seen by webpack. However, we need to keep
* a persistent instance of TypeScript that contains all of the files in the program
* along with definition files and options. This function either creates an instance
* or returns the existing one. Multiple instances are possible by using the
* `instance` property.
*/
export declare function getTypeScriptInstance(loaderOptions: LoaderOptions, loader: webpack.LoaderContext<LoaderOptions>): {
instance?: TSInstance;
error?: webpack.WebpackError;
};
export declare function initializeInstance(loader: webpack.LoaderContext<LoaderOptions>, instance: TSInstance): void;
export declare function getCustomTransformers(loaderOptions: LoaderOptions, program: typescript.Program | undefined, getProgram: (() => typescript.Program | undefined) | undefined): any;
export declare function reportTranspileErrors(instance: TSInstance, loader: webpack.LoaderContext<LoaderOptions>): void;
export declare function buildSolutionReferences(instance: TSInstance, loader: webpack.LoaderContext<LoaderOptions>): void;
export declare function forEachResolvedProjectReference<T>(resolvedProjectReferences: readonly (typescript.ResolvedProjectReference | undefined)[] | undefined, cb: (resolvedProjectReference: typescript.ResolvedProjectReference) => T | undefined): T | undefined;
export declare function getOutputFileNames(instance: TSInstance, configFile: typescript.ParsedCommandLine, inputFileName: string): string[];
export declare function getInputFileNameFromOutput(instance: TSInstance, filePath: string): string | undefined;
export declare function getEmitFromWatchHost(instance: TSInstance, filePath?: string): typescript.OutputFile[] | undefined;
export declare function getEmitOutput(instance: TSInstance, filePath: string): typescript.OutputFile[];
//# sourceMappingURL=instances.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"instances.d.ts","sourceRoot":"","sources":["../src/instances.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,KAAK,UAAU,MAAM,YAAY,CAAC;AAC9C,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AAYnC,OAAO,KAAK,EAAe,aAAa,EAAW,UAAU,EAAE,MAAM,cAAc,CAAC;AAqBpF;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CACnC,aAAa,EAAE,aAAa,EAC5B,MAAM,EAAE,OAAO,CAAC,aAAa,CAAC,aAAa,CAAC,GAC3C;IAAE,QAAQ,CAAC,EAAE,UAAU,CAAC;IAAC,KAAK,CAAC,EAAE,OAAO,CAAC,YAAY,CAAA;CAAE,CAiCzD;AAwQD,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,OAAO,CAAC,aAAa,CAAC,aAAa,CAAC,EAC5C,QAAQ,EAAE,UAAU,QAiFrB;AAED,wBAAgB,qBAAqB,CACnC,aAAa,EAAE,aAAa,EAC5B,OAAO,EAAE,UAAU,CAAC,OAAO,GAAG,SAAS,EACvC,UAAU,EAAE,CAAC,MAAM,UAAU,CAAC,OAAO,GAAG,SAAS,CAAC,GAAG,SAAS,OA8B/D;AAgBD,wBAAgB,qBAAqB,CACnC,QAAQ,EAAE,UAAU,EACpB,MAAM,EAAE,OAAO,CAAC,aAAa,CAAC,aAAa,CAAC,QAyB7C;AAED,wBAAgB,uBAAuB,CACrC,QAAQ,EAAE,UAAU,EACpB,MAAM,EAAE,OAAO,CAAC,aAAa,CAAC,aAAa,CAAC,QA4B7C;AAED,wBAAgB,+BAA+B,CAAC,CAAC,EAC/C,yBAAyB,EACrB,SAAS,CAAC,UAAU,CAAC,wBAAwB,GAAG,SAAS,CAAC,EAAE,GAC5D,SAAS,EACb,EAAE,EAAE,CACF,wBAAwB,EAAE,UAAU,CAAC,wBAAwB,KAC1D,CAAC,GAAG,SAAS,GACjB,CAAC,GAAG,SAAS,CA8Bf;AA0ED,wBAAgB,kBAAkB,CAChC,QAAQ,EAAE,UAAU,EACpB,UAAU,EAAE,UAAU,CAAC,iBAAiB,EACxC,aAAa,EAAE,MAAM,GACpB,MAAM,EAAE,CA2CV;AAED,wBAAgB,0BAA0B,CACxC,QAAQ,EAAE,UAAU,EACpB,QAAQ,EAAE,MAAM,GACf,MAAM,GAAG,SAAS,CA2BpB;AAED,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,UAAU,EAAE,QAAQ,CAAC,EAAE,MAAM,uCAyD3E;AAED,wBAAgB,aAAa,CAAC,QAAQ,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,2BAqCnE"}
+506
View File
@@ -0,0 +1,506 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTypeScriptInstance = getTypeScriptInstance;
exports.initializeInstance = initializeInstance;
exports.getCustomTransformers = getCustomTransformers;
exports.reportTranspileErrors = reportTranspileErrors;
exports.buildSolutionReferences = buildSolutionReferences;
exports.forEachResolvedProjectReference = forEachResolvedProjectReference;
exports.getOutputFileNames = getOutputFileNames;
exports.getInputFileNameFromOutput = getInputFileNameFromOutput;
exports.getEmitFromWatchHost = getEmitFromWatchHost;
exports.getEmitOutput = getEmitOutput;
const chalk = require("chalk");
const fs = require("fs");
const path = require("path");
const webpack = require("webpack");
const after_compile_1 = require("./after-compile");
const compilerSetup_1 = require("./compilerSetup");
const config_1 = require("./config");
const constants_1 = require("./constants");
const instance_cache_1 = require("./instance-cache");
const logger = require("./logger");
const servicesHost_1 = require("./servicesHost");
const utils_1 = require("./utils");
const watch_run_1 = require("./watch-run");
const instancesBySolutionBuilderConfigs = new Map();
/**
* The loader is executed once for each file seen by webpack. However, we need to keep
* a persistent instance of TypeScript that contains all of the files in the program
* along with definition files and options. This function either creates an instance
* or returns the existing one. Multiple instances are possible by using the
* `instance` property.
*/
function getTypeScriptInstance(loaderOptions, loader) {
const existing = (0, instance_cache_1.getTSInstanceFromCache)(loader._compiler, loaderOptions.instance);
if (existing) {
if (!existing.initialSetupPending) {
(0, utils_1.ensureProgram)(existing);
}
return { instance: existing };
}
const level = loaderOptions.colors && chalk.supportsColor ? chalk.supportsColor.level : 0;
const colors = new chalk.Instance({ level });
const log = logger.makeLogger(loaderOptions, colors);
const compiler = (0, compilerSetup_1.getCompiler)(loaderOptions, log);
if (compiler.errorMessage !== undefined) {
return {
error: (0, utils_1.makeError)(loaderOptions, colors.red(compiler.errorMessage), ''),
};
}
return successfulTypeScriptInstance(loaderOptions, loader, log, colors, compiler.compiler, compiler.compilerCompatible, compiler.compilerDetailsLogMessage);
}
function createFilePathKeyMapper(compiler, loaderOptions) {
// Cache file path key - a map lookup is much faster than filesystem/regex operations & the result will never change
const filePathMapperCache = new Map();
// FileName lowercasing copied from typescript
const fileNameLowerCaseRegExp = /[^\u0130\u0131\u00DFa-z0-9\\/:\-_\. ]+/g;
return (0, utils_1.useCaseSensitiveFileNames)(compiler, loaderOptions)
? pathResolve
: toFileNameLowerCase;
function pathResolve(filePath) {
let cachedPath = filePathMapperCache.get(filePath);
if (!cachedPath) {
cachedPath = path.resolve(filePath);
filePathMapperCache.set(filePath, cachedPath);
}
return cachedPath;
}
function toFileNameLowerCase(filePath) {
let cachedPath = filePathMapperCache.get(filePath);
if (!cachedPath) {
const filePathKey = pathResolve(filePath);
cachedPath = fileNameLowerCaseRegExp.test(filePathKey)
? filePathKey.replace(fileNameLowerCaseRegExp, ch => ch.toLowerCase())
: filePathKey;
filePathMapperCache.set(filePath, cachedPath);
}
return cachedPath;
}
}
function successfulTypeScriptInstance(loaderOptions, loader, log, colors, compiler, compilerCompatible, compilerDetailsLogMessage) {
const configFileAndPath = (0, config_1.getConfigFile)(compiler, colors, loader, loaderOptions, compilerCompatible, log, compilerDetailsLogMessage);
if (configFileAndPath.configFileError !== undefined) {
const { message, file } = configFileAndPath.configFileError;
return {
error: (0, utils_1.makeError)(loaderOptions, colors.red('error while reading tsconfig.json:' + constants_1.EOL + message), file),
};
}
const { configFilePath, configFile } = configFileAndPath;
if (configFilePath) {
loader.addBuildDependency(configFilePath);
}
const filePathKeyMapper = createFilePathKeyMapper(compiler, loaderOptions);
if (configFilePath && loaderOptions.projectReferences) {
const configFileKey = filePathKeyMapper(configFilePath);
const existing = getExistingSolutionBuilderHost(configFileKey);
if (existing) {
// Reuse the instance if config file for project references is shared.
(0, instance_cache_1.setTSInstanceInCache)(loader._compiler, loaderOptions.instance, existing);
return { instance: existing };
}
}
const module = loader._module;
const basePath = loaderOptions.context || path.dirname(configFilePath || '');
const configParseResult = (0, config_1.getConfigParseResult)(compiler, configFile, basePath, configFilePath, loaderOptions);
if (configParseResult.errors.length > 0 && !loaderOptions.happyPackMode) {
const errors = (0, utils_1.formatErrors)(configParseResult.errors, loaderOptions, colors, compiler, { file: configFilePath }, loader.context);
errors.forEach(error => module.addError(error));
return {
error: (0, utils_1.makeError)(loaderOptions, colors.red('error while parsing tsconfig.json'), configFilePath || ''),
};
}
const compilerOptions = (0, compilerSetup_1.getCompilerOptions)(configParseResult, compiler);
const rootFileNames = new Set();
const files = new Map();
const otherFiles = new Map();
const appendTsTsxSuffixesIfRequired = loaderOptions.appendTsSuffixTo.length > 0 ||
loaderOptions.appendTsxSuffixTo.length > 0
? (filePath) => (0, utils_1.appendSuffixesIfMatch)({
'.ts': loaderOptions.appendTsSuffixTo,
'.tsx': loaderOptions.appendTsxSuffixTo,
}, filePath)
: (filePath) => filePath;
if (loaderOptions.transpileOnly) {
// quick return for transpiling
// we do need to check for any issues with TS options though
const transpileInstance = {
compiler,
compilerOptions,
appendTsTsxSuffixesIfRequired,
loaderOptions,
rootFileNames,
files,
otherFiles,
version: 0,
program: undefined, // temporary, to be set later
dependencyGraph: new Map(),
transformers: {}, // this is only set temporarily, custom transformers are created further down
colors,
initialSetupPending: true,
reportTranspileErrors: true,
configFilePath,
configParseResult,
log,
filePathKeyMapper,
};
(0, instance_cache_1.setTSInstanceInCache)(loader._compiler, loaderOptions.instance, transpileInstance);
return { instance: transpileInstance };
}
// Load initial files (core lib files, any files specified in tsconfig.json)
let normalizedFilePath;
try {
const filesToLoad = loaderOptions.onlyCompileBundledFiles
? configParseResult.fileNames.filter(fileName => constants_1.dtsDtsxOrDtsDtsxMapRegex.test(fileName))
: configParseResult.fileNames;
filesToLoad.forEach(filePath => {
normalizedFilePath = path.normalize(filePath);
files.set(filePathKeyMapper(normalizedFilePath), {
fileName: normalizedFilePath,
text: fs.readFileSync(normalizedFilePath, 'utf-8'),
version: 0,
});
rootFileNames.add(normalizedFilePath);
});
}
catch (exc) {
return {
error: (0, utils_1.makeError)(loaderOptions, colors.red(`A file specified in tsconfig.json could not be found: ${normalizedFilePath}`), normalizedFilePath),
};
}
const instance = {
compiler,
compilerOptions,
appendTsTsxSuffixesIfRequired,
loaderOptions,
rootFileNames,
files,
otherFiles,
languageService: null,
version: 0,
transformers: {}, // this is only set temporarily, custom transformers are created further down
dependencyGraph: new Map(),
colors,
initialSetupPending: true,
configFilePath,
configParseResult,
log,
filePathKeyMapper,
};
(0, instance_cache_1.setTSInstanceInCache)(loader._compiler, loaderOptions.instance, instance);
return { instance };
}
function getExistingSolutionBuilderHost(key) {
const existing = instancesBySolutionBuilderConfigs.get(key);
if (existing)
return existing;
for (const instance of instancesBySolutionBuilderConfigs.values()) {
if (instance.solutionBuilderHost.configFileInfo.has(key)) {
return instance;
}
}
return undefined;
}
function addAssetHooks(loader, instance) {
// makeAfterCompile is a closure. It returns a function which closes over the variable checkAllFilesForErrors
// We need to get the function once and then reuse it, otherwise it will be recreated each time
// and all files will always be checked.
const cachedMakeAfterCompile = (0, after_compile_1.makeAfterCompile)(instance, instance.configFilePath);
const makeAssetsCallback = (compilation) => {
compilation.hooks.processAssets.tap({
name: 'ts-loader',
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL,
}, () => {
cachedMakeAfterCompile(compilation, () => {
return null;
});
});
};
// We need to add the hook above for each run.
// For the first run, we just need to add the hook to loader._compilation
makeAssetsCallback(loader._compilation);
// For future calls in watch mode we need to watch for a new compilation and add the hook
loader._compiler.hooks.compilation.tap('ts-loader', makeAssetsCallback);
}
function initializeInstance(loader, instance) {
if (!instance.initialSetupPending) {
return;
}
instance.initialSetupPending = false;
if (instance.loaderOptions.transpileOnly) {
const program = (instance.program =
instance.configParseResult.projectReferences !== undefined
? instance.compiler.createProgram({
rootNames: instance.configParseResult.fileNames,
options: instance.configParseResult.options,
projectReferences: instance.configParseResult.projectReferences,
})
: instance.compiler.createProgram([], instance.compilerOptions));
const getProgram = () => program;
instance.transformers = getCustomTransformers(instance.loaderOptions, program, getProgram);
// Setup watch run for solution building
if (instance.solutionBuilderHost) {
addAssetHooks(loader, instance);
loader._compiler.hooks.watchRun.tapAsync('ts-loader', (0, watch_run_1.makeWatchRun)(instance, loader));
}
}
else {
if (!loader._compiler.hooks) {
throw new Error("You may be using an old version of webpack; please check you're using at least version 4. Or you should set `transpileOnly` or `happyPackMode` to true when using with `thread-loader`.");
}
if (instance.loaderOptions.experimentalWatchApi) {
instance.log.logInfo('Using watch api');
// If there is api available for watch, use it instead of language service
instance.watchHost = (0, servicesHost_1.makeWatchHost)(getScriptRegexp(instance), loader, instance, instance.configParseResult.projectReferences);
instance.watchOfFilesAndCompilerOptions =
instance.compiler.createWatchProgram(instance.watchHost);
instance.builderProgram =
instance.watchOfFilesAndCompilerOptions.getProgram();
const getProgram = () => { var _a; return (_a = instance.builderProgram) === null || _a === void 0 ? void 0 : _a.getProgram(); };
instance.program = getProgram();
instance.transformers = getCustomTransformers(instance.loaderOptions, instance.program, getProgram);
}
else {
instance.servicesHost = (0, servicesHost_1.makeServicesHost)(getScriptRegexp(instance), loader, instance, instance.configParseResult.projectReferences);
instance.languageService = instance.compiler.createLanguageService(instance.servicesHost, instance.compiler.createDocumentRegistry());
const getProgram = () => instance.languageService.getProgram();
instance.transformers = getCustomTransformers(instance.loaderOptions, getProgram(), getProgram);
}
addAssetHooks(loader, instance);
loader._compiler.hooks.watchRun.tapAsync('ts-loader', (0, watch_run_1.makeWatchRun)(instance, loader));
}
}
function getCustomTransformers(loaderOptions, program, getProgram) {
// same strategy as https://github.com/s-panferov/awesome-typescript-loader/pull/531/files
let { getCustomTransformers: customerTransformers } = loaderOptions;
let getCustomTransformers = Function.prototype;
if (typeof customerTransformers === 'function') {
getCustomTransformers = customerTransformers;
}
else if (typeof customerTransformers === 'string') {
try {
customerTransformers = require(customerTransformers);
}
catch (err) {
throw new Error(`Failed to load customTransformers from "${loaderOptions.getCustomTransformers}": ${err instanceof Error ? err.message : 'unknown error'}`);
}
if (typeof customerTransformers !== 'function') {
throw new Error(`Custom transformers in "${loaderOptions.getCustomTransformers}" should export a function, got ${typeof customerTransformers}`);
}
getCustomTransformers = customerTransformers;
}
return getCustomTransformers(program, getProgram);
}
function getScriptRegexp(instance) {
// If resolveJsonModules is set, we should accept json files
if (instance.configParseResult.options.resolveJsonModule) {
// if allowJs is set then we should accept js(x) files
return instance.configParseResult.options.allowJs === true
? /\.([cm]?[tj]s|[tj]sx|json)$/i
: /\.([cm]?ts|tsx|json)$/i;
}
// if allowJs is set then we should accept js(x) files
return instance.configParseResult.options.allowJs === true
? /\.([cm]?[tj]s|[tj]sx)$/i
: /\.([cm]?ts|tsx)$/i;
}
function reportTranspileErrors(instance, loader) {
if (!instance.reportTranspileErrors) {
return;
}
const module = loader._module;
instance.reportTranspileErrors = false;
// happypack does not have _module.errors - see https://github.com/TypeStrong/ts-loader/issues/336
if (!instance.loaderOptions.happyPackMode) {
const solutionErrors = (0, servicesHost_1.getSolutionErrors)(instance, loader.context);
const diagnostics = instance.program.getOptionsDiagnostics();
const errors = (0, utils_1.formatErrors)(diagnostics, instance.loaderOptions, instance.colors, instance.compiler, { file: instance.configFilePath || 'tsconfig.json' }, loader.context);
[...solutionErrors, ...errors].forEach(error => module.addError(error));
}
}
function buildSolutionReferences(instance, loader) {
if (!(0, utils_1.supportsSolutionBuild)(instance)) {
return;
}
if (!instance.solutionBuilderHost) {
// Use solution builder
instance.log.logInfo('Using SolutionBuilder api');
const scriptRegex = getScriptRegexp(instance);
instance.solutionBuilderHost = (0, servicesHost_1.makeSolutionBuilderHost)(scriptRegex, loader, instance);
const solutionBuilder = instance.compiler.createSolutionBuilderWithWatch(instance.solutionBuilderHost, instance.configParseResult.projectReferences.map(ref => ref.path), { verbose: true });
solutionBuilder.build();
instance.solutionBuilderHost.ensureAllReferenceTimestamps();
instancesBySolutionBuilderConfigs.set(instance.filePathKeyMapper(instance.configFilePath), instance);
}
else {
instance.solutionBuilderHost.buildReferences();
}
}
function forEachResolvedProjectReference(resolvedProjectReferences, cb) {
let seenResolvedRefs;
return worker(resolvedProjectReferences);
function worker(resolvedRefs) {
if (resolvedRefs) {
for (const resolvedRef of resolvedRefs) {
if (!resolvedRef) {
continue;
}
if (seenResolvedRefs &&
seenResolvedRefs.some(seenRef => seenRef === resolvedRef)) {
// ignore recursives
continue;
}
(seenResolvedRefs || (seenResolvedRefs = [])).push(resolvedRef);
const result = cb(resolvedRef) || worker(resolvedRef.references);
if (result) {
return result;
}
}
}
return undefined;
}
}
// This code is here as a temporary holder
function fileExtensionIs(fileName, ext) {
return fileName.endsWith(ext);
}
function rootDirOfOptions(instance, configFile) {
return (configFile.options.rootDir ||
instance.compiler.getDirectoryPath(configFile.options.configFilePath));
}
function getOutputPathWithoutChangingExt(instance, inputFileName, configFile, ignoreCase, outputDir) {
return outputDir
? instance.compiler.resolvePath(outputDir, instance.compiler.getRelativePathFromDirectory(rootDirOfOptions(instance, configFile), inputFileName, ignoreCase))
: inputFileName;
}
function getOutputJSFileName(instance, inputFileName, configFile, ignoreCase) {
if (configFile.options.emitDeclarationOnly) {
return undefined;
}
const isJsonFile = fileExtensionIs(inputFileName, '.json');
const outputFileName = instance.compiler.changeExtension(getOutputPathWithoutChangingExt(instance, inputFileName, configFile, ignoreCase, configFile.options.outDir), isJsonFile
? '.json'
: fileExtensionIs(inputFileName, '.tsx') &&
configFile.options.jsx === instance.compiler.JsxEmit.Preserve
? '.jsx'
: '.js');
return !isJsonFile ||
instance.compiler.comparePaths(inputFileName, outputFileName, configFile.options.configFilePath, ignoreCase) !== instance.compiler.Comparison.EqualTo
? outputFileName
: undefined;
}
function getOutputFileNames(instance, configFile, inputFileName) {
const ignoreCase = !(0, utils_1.useCaseSensitiveFileNames)(instance.compiler, instance.loaderOptions);
if (instance.compiler.getOutputFileNames) {
return instance.compiler.getOutputFileNames(configFile, inputFileName, ignoreCase);
}
const outputs = [];
const addOutput = (fileName) => fileName && outputs.push(fileName);
const js = getOutputJSFileName(instance, inputFileName, configFile, ignoreCase);
addOutput(js);
if (!fileExtensionIs(inputFileName, '.json')) {
if (js && configFile.options.sourceMap) {
addOutput(`${js}.map`);
}
if ((configFile.options.declaration || configFile.options.composite) &&
instance.compiler.hasTSFileExtension(inputFileName)) {
const dts = instance.compiler.getOutputDeclarationFileName(inputFileName, configFile, ignoreCase);
addOutput(dts);
if (configFile.options.declarationMap) {
addOutput(`${dts}.map`);
}
}
}
return outputs;
}
function getInputFileNameFromOutput(instance, filePath) {
if (filePath.match(constants_1.tsTsxRegex) && !constants_1.declarationRegex.test(filePath)) {
return undefined;
}
if (instance.solutionBuilderHost) {
return instance.solutionBuilderHost.getInputFileNameFromOutput(filePath);
}
const program = (0, utils_1.ensureProgram)(instance);
return (program &&
program.getResolvedProjectReferences &&
forEachResolvedProjectReference(program.getResolvedProjectReferences(), ({ commandLine }) => {
const { options, fileNames } = commandLine;
if (!options.outFile && !options.out) {
const input = fileNames.find(file => getOutputFileNames(instance, commandLine, file).find(name => path.resolve(name) === filePath));
return input && path.resolve(input);
}
return undefined;
}));
}
function getEmitFromWatchHost(instance, filePath) {
const program = (0, utils_1.ensureProgram)(instance);
const builderProgram = instance.builderProgram;
if (builderProgram && program) {
if (filePath) {
const existing = instance.watchHost.outputFiles.get(instance.filePathKeyMapper(filePath));
if (existing) {
return existing;
}
}
const outputFiles = [];
const writeFile = (fileName, text, writeByteOrderMark) => {
if (fileName.endsWith('.tsbuildinfo')) {
instance.watchHost.tsbuildinfo = {
name: fileName,
writeByteOrderMark,
text,
};
}
else {
outputFiles.push({ name: fileName, writeByteOrderMark, text });
}
};
const sourceFile = filePath ? program.getSourceFile(filePath) : undefined;
// Try emit Next file
while (true) {
const result = builderProgram.emitNextAffectedFile(writeFile,
/*cancellationToken*/ undefined,
/*emitOnlyDtsFiles*/ false, instance.transformers);
if (!result) {
break;
}
// Only put the output file in the cache if the source came from webpack and
// was processed by the loaders
if (result.affected === sourceFile) {
instance.watchHost.outputFiles.set(instance.filePathKeyMapper(result.affected.fileName), outputFiles.slice());
return outputFiles;
}
}
}
return undefined;
}
function getEmitOutput(instance, filePath) {
if (fileExtensionIs(filePath, instance.compiler.Extension.Dts)) {
return [];
}
if ((0, utils_1.isReferencedFile)(instance, filePath)) {
return instance.solutionBuilderHost.getOutputFilesFromReferencedProjectInput(filePath);
}
const program = (0, utils_1.ensureProgram)(instance);
if (program !== undefined) {
const sourceFile = program.getSourceFile(filePath);
const outputFiles = [];
const writeFile = (fileName, text, writeByteOrderMark) => outputFiles.push({ name: fileName, writeByteOrderMark, text });
const outputFilesFromWatch = getEmitFromWatchHost(instance, filePath);
if (outputFilesFromWatch) {
return outputFilesFromWatch;
}
program.emit(sourceFile, writeFile,
/*cancellationToken*/ undefined,
/*emitOnlyDtsFiles*/ false, instance.transformers);
return outputFiles;
}
else {
// Emit Javascript
return instance.languageService.getProgram().getSourceFile(filePath) ===
undefined
? []
: instance.languageService.getEmitOutput(filePath).outputFiles;
}
}
//# sourceMappingURL=instances.js.map
+236
View File
@@ -0,0 +1,236 @@
import type * as typescript from 'typescript';
import type { Chalk } from 'chalk';
import type * as logger from './logger';
export interface ErrorInfo {
code: number;
severity: Severity;
content: string;
file: string;
line: number;
character: number;
context: string;
}
export type FileLocation = {
/** 1-based */
line: number;
/** 1-based */
character: number;
};
export interface HostMayBeCacheable {
clearCache?(): void;
fileExistsCache?: Map<string, boolean>;
directoryExistsCache?: Map<string, boolean>;
realpathCache?: Map<string, string>;
}
export interface CacheableHost extends HostMayBeCacheable {
fileExists: typescript.ModuleResolutionHost['fileExists'];
directoryExists: NonNullable<typescript.ModuleResolutionHost['directoryExists']>;
realpath?: typescript.ModuleResolutionHost['realpath'];
}
export interface ModuleResolutionHostMayBeCacheable extends typescript.ModuleResolutionHost, HostMayBeCacheable {
readFile(filePath: string, encoding?: string): string | undefined;
trace: NonNullable<typescript.ModuleResolutionHost['trace']>;
directoryExists: NonNullable<typescript.ModuleResolutionHost['directoryExists']>;
getCurrentDirectory: NonNullable<typescript.ModuleResolutionHost['getCurrentDirectory']>;
getDirectories: NonNullable<typescript.ModuleResolutionHost['getDirectories']>;
useCaseSensitiveFileNames: NonNullable<typescript.LanguageServiceHost['useCaseSensitiveFileNames']>;
getNewLine: NonNullable<typescript.LanguageServiceHost['getNewLine']>;
getDefaultLibFileName: NonNullable<typescript.LanguageServiceHost['getDefaultLibFileName']>;
readDirectory: NonNullable<typescript.LanguageServiceHost['readDirectory']>;
}
export interface ServiceHostWhichMayBeCacheable extends typescript.LanguageServiceHost, HostMayBeCacheable {
}
export interface WatchHost extends typescript.WatchCompilerHostOfFilesAndCompilerOptions<typescript.EmitAndSemanticDiagnosticsBuilderProgram>, HostMayBeCacheable {
invokeFileWatcher: WatchFactory['invokeFileWatcher'];
updateRootFileNames(): void;
outputFiles: Map<FilePathKey, typescript.OutputFile[]>;
tsbuildinfo?: typescript.OutputFile;
}
export type WatchCallbacks<T> = Map<FilePathKey, {
fileName: string;
callbacks: T[];
}>;
export interface WatchFactory {
watchedFiles: WatchCallbacks<typescript.FileWatcherCallback>;
watchedDirectories: WatchCallbacks<typescript.DirectoryWatcherCallback>;
watchedDirectoriesRecursive: WatchCallbacks<typescript.DirectoryWatcherCallback>;
invokeFileWatcher(fileName: string, eventKind: typescript.FileWatcherEventKind): boolean;
/** Used to watch changes in source files, missing files needed to update the program or config file */
watchFile: typescript.WatchHost['watchFile'];
/** Used to watch resolved module's failed lookup locations, config file specs, type roots where auto type reference directives are added */
watchDirectory: typescript.WatchHost['watchDirectory'];
}
export interface SolutionDiagnostics {
global: typescript.Diagnostic[];
perFile: Map<FilePathKey, typescript.Diagnostic[]>;
transpileErrors: [FilePathKey | undefined, typescript.Diagnostic[]][];
}
export type FilePathKey = string & {
__filePathKeyBrand: any;
};
export interface SolutionBuilderWithWatchHost extends typescript.SolutionBuilderWithWatchHost<typescript.EmitAndSemanticDiagnosticsBuilderProgram>, WatchFactory {
diagnostics: SolutionDiagnostics;
writtenFiles: typescript.OutputFile[];
configFileInfo: Map<FilePathKey, ConfigFileInfo>;
outputAffectingInstanceVersion: Map<FilePathKey, true>;
getInputFileStamp(fileName: string): Date;
updateSolutionBuilderInputFile(fileName: string): void;
getOutputFileKeyFromReferencedProject(outputFileName: string): FilePathKey | undefined;
getOutputFileAndKeyFromReferencedProject(oututFileName: string): {
key: FilePathKey;
outputFile: string | false;
} | undefined;
getOutputFileTextAndKeyFromReferencedProject(oututFileName: string): {
key: FilePathKey;
text: string | undefined;
} | undefined;
getInputFileNameFromOutput(outputFileName: string): string | undefined;
getOutputFilesFromReferencedProjectInput(inputFileName: string): typescript.OutputFile[];
buildReferences(): void;
ensureAllReferenceTimestamps(): void;
clearCache(): void;
close(): void;
}
export interface ConfigFileInfo {
config: typescript.ParsedCommandLine | undefined;
outputFileNames?: Map<FilePathKey, {
inputFileName: string;
outputNames: string[];
}>;
tsbuildInfoFile?: string;
dtsFiles?: string[];
}
interface CacheWithRedirects<T> {
ownMap: Map<string, T>;
redirectsMap: Map<typescript.Path, Map<string, T>>;
getOrCreateMapOfCacheRedirects(redirectedReference: typescript.ResolvedProjectReference | undefined): Map<string, T>;
clear(): void;
setOwnOptions(newOptions: typescript.CompilerOptions): void;
setOwnMap(newOwnMap: Map<string, T>): void;
}
interface PerModuleNameCache {
get(directory: string): typescript.ResolvedModuleWithFailedLookupLocations | undefined;
set(directory: string, result: typescript.ResolvedModuleWithFailedLookupLocations): void;
}
export interface ModuleResolutionCache extends typescript.ModuleResolutionCache {
directoryToModuleNameMap: CacheWithRedirects<Map<string, typescript.ResolvedModuleWithFailedLookupLocations>>;
moduleNameToDirectoryMap: CacheWithRedirects<PerModuleNameCache>;
clear(): void;
update(compilerOptions: typescript.CompilerOptions): void;
}
export interface TSInstance {
compiler: typeof typescript;
compilerOptions: typescript.CompilerOptions;
/** Used for Vue for the most part */
appendTsTsxSuffixesIfRequired: (filePath: string) => string;
loaderOptions: LoaderOptions;
rootFileNames: Set<string>;
moduleResolutionCache?: ModuleResolutionCache;
typeReferenceResolutionCache?: typescript.TypeReferenceDirectiveResolutionCache;
/**
* a cache of all the files
*/
files: TSFiles;
/**
* contains the modified files - cleared each time after-compile is called
*/
modifiedFiles?: Map<FilePathKey, true>;
/**
* Paths to project references that are missing source maps.
* Cleared each time after-compile is called. Used to dedupe
* warnings about source maps during a single compilation.
*/
projectsMissingSourceMaps?: Set<string>;
servicesHost?: ServiceHostWhichMayBeCacheable;
languageService?: typescript.LanguageService | null;
version: number;
dependencyGraph: DependencyGraph;
filesWithErrors?: TSFiles;
transformers: typescript.CustomTransformers;
colors: Chalk;
otherFiles: TSFiles;
watchHost?: WatchHost;
watchOfFilesAndCompilerOptions?: typescript.WatchOfFilesAndCompilerOptions<typescript.EmitAndSemanticDiagnosticsBuilderProgram>;
builderProgram?: typescript.EmitAndSemanticDiagnosticsBuilderProgram;
program?: typescript.Program;
hasUnaccountedModifiedFiles?: boolean;
changedFilesList?: boolean;
reportTranspileErrors?: boolean;
solutionBuilderHost?: SolutionBuilderWithWatchHost;
configFilePath: string | undefined;
filePathKeyMapper: (fileName: string) => FilePathKey;
initialSetupPending: boolean;
configParseResult: typescript.ParsedCommandLine;
log: logger.Logger;
}
export interface LoaderOptionsCache {
[name: string]: WeakMap<LoaderOptions, LoaderOptions>;
}
export type DependencyGraph = Map<FilePathKey, ResolvedModule[]>;
export type ReverseDependencyGraph = Map<FilePathKey, Map<FilePathKey, true>>;
export type LogLevel = 'INFO' | 'WARN' | 'ERROR';
export type ResolveModuleName = (moduleName: string, containingFile: string, compilerOptions: typescript.CompilerOptions, moduleResolutionHost: typescript.ModuleResolutionHost) => typescript.ResolvedModuleWithFailedLookupLocations;
export type CustomResolveModuleName = (moduleName: string, containingFile: string, compilerOptions: typescript.CompilerOptions, moduleResolutionHost: typescript.ModuleResolutionHost, parentResolver: ResolveModuleName) => typescript.ResolvedModuleWithFailedLookupLocations;
export type CustomResolveTypeReferenceDirective = (typeDirectiveName: string, containingFile: string, compilerOptions: typescript.CompilerOptions, moduleResolutionHost: typescript.ModuleResolutionHost, parentResolver: typeof typescript.resolveTypeReferenceDirective) => typescript.ResolvedTypeReferenceDirectiveWithFailedLookupLocations;
export interface LoaderOptions {
silent: boolean;
logLevel: LogLevel;
logInfoToStdOut: boolean;
instance: string;
compiler: string;
configFile: string;
context: string;
transpileOnly: boolean;
ignoreDiagnostics: number[];
reportFiles: string[];
errorFormatter: (message: ErrorInfo, colors: Chalk) => string;
onlyCompileBundledFiles: boolean;
colors: boolean;
compilerOptions: typescript.CompilerOptions;
appendTsSuffixTo: (RegExp | string)[];
appendTsxSuffixTo: (RegExp | string)[];
happyPackMode: boolean;
getCustomTransformers: string | ((program: typescript.Program, getProgram: () => typescript.Program) => typescript.CustomTransformers | undefined);
experimentalWatchApi: boolean;
allowTsInNodeModules: boolean;
experimentalFileCaching: boolean;
projectReferences: boolean;
resolveModuleName: CustomResolveModuleName;
resolveTypeReferenceDirective: CustomResolveTypeReferenceDirective;
useCaseSensitiveFileNames?: boolean;
}
export interface TSFile {
fileName: string;
text?: string;
version: number;
modifiedTime?: Date;
projectReference?: {
/**
* Undefined here means weve already checked and confirmed there is no
* project reference for the file. Dont bother checking again.
*/
project?: typescript.ResolvedProjectReference;
outputFileName?: string;
};
}
/** where key is filepath */
export type TSFiles = Map<FilePathKey, TSFile>;
export interface ResolvedModule {
originalFileName: string;
resolvedFileName: string;
resolvedModule?: ResolvedModule;
isExternalLibraryImport?: boolean;
}
export interface TSCommon {
resolveTypeReferenceDirective(typeReferenceDirectiveName: string, containingFile: string | undefined, options: typescript.CompilerOptions, host: typescript.ModuleResolutionHost, redirectedReference?: typescript.ResolvedProjectReference, cache?: typescript.TypeReferenceDirectiveResolutionCache, resolutionMode?: typescript.SourceFile['impliedNodeFormat']): typescript.ResolvedTypeReferenceDirectiveWithFailedLookupLocations;
}
/**
* Compiler APIs we use that are marked internal and not included in TypeScript's public API declarations
* @internal
*/
export interface TSInternal {
getModeForFileReference?: (ref: typescript.FileReference | string, containingFileMode: typescript.SourceFile['impliedNodeFormat']) => typescript.SourceFile['impliedNodeFormat'];
}
export type Severity = 'error' | 'warning';
export {};
//# sourceMappingURL=interfaces.d.ts.map
File diff suppressed because one or more lines are too long
+3
View File
@@ -0,0 +1,3 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
//# sourceMappingURL=interfaces.js.map
+17
View File
@@ -0,0 +1,17 @@
import type { Chalk } from 'chalk';
import type { LoaderOptions } from './interfaces';
type LoggerFunc = (message: string) => void;
export interface Logger {
log: LoggerFunc;
logInfo: LoggerFunc;
logWarning: LoggerFunc;
logError: LoggerFunc;
}
export declare enum LogLevel {
INFO = 1,
WARN = 2,
ERROR = 3
}
export declare function makeLogger(loaderOptions: LoaderOptions, colors: Chalk): Logger;
export {};
//# sourceMappingURL=logger.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAEnC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAIlD,KAAK,UAAU,GAAG,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;AAE5C,MAAM,WAAW,MAAM;IACrB,GAAG,EAAE,UAAU,CAAC;IAChB,OAAO,EAAE,UAAU,CAAC;IACpB,UAAU,EAAE,UAAU,CAAC;IACvB,QAAQ,EAAE,UAAU,CAAC;CACtB;AAED,oBAAY,QAAQ;IAClB,IAAI,IAAI;IACR,IAAI,IAAI;IACR,KAAK,IAAI;CACV;AAqDD,wBAAgB,UAAU,CACxB,aAAa,EAAE,aAAa,EAC5B,MAAM,EAAE,KAAK,GACZ,MAAM,CAQR"}
+37
View File
@@ -0,0 +1,37 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LogLevel = void 0;
exports.makeLogger = makeLogger;
const console_1 = require("console");
var LogLevel;
(function (LogLevel) {
LogLevel[LogLevel["INFO"] = 1] = "INFO";
LogLevel[LogLevel["WARN"] = 2] = "WARN";
LogLevel[LogLevel["ERROR"] = 3] = "ERROR";
})(LogLevel || (exports.LogLevel = LogLevel = {}));
const stderrConsole = new console_1.Console(process.stderr);
const stdoutConsole = new console_1.Console(process.stdout);
const doNothingLogger = (_message) => { };
const makeLoggerFunc = (loaderOptions) => loaderOptions.silent
? (_whereToLog, _message) => { }
: (whereToLog, message) => console.log.call(whereToLog, message);
const makeExternalLogger = (loaderOptions, logger) => (message) => logger(loaderOptions.logInfoToStdOut ? stdoutConsole : stderrConsole, message);
const makeLogInfo = (loaderOptions, logger, green) => LogLevel[loaderOptions.logLevel] <= LogLevel.INFO
? (message) => logger(loaderOptions.logInfoToStdOut ? stdoutConsole : stderrConsole, green(message))
: doNothingLogger;
const makeLogError = (loaderOptions, logger, red) => LogLevel[loaderOptions.logLevel] <= LogLevel.ERROR
? (message) => logger(stderrConsole, red(message))
: doNothingLogger;
const makeLogWarning = (loaderOptions, logger, yellow) => LogLevel[loaderOptions.logLevel] <= LogLevel.WARN
? (message) => logger(stderrConsole, yellow(message))
: doNothingLogger;
function makeLogger(loaderOptions, colors) {
const logger = makeLoggerFunc(loaderOptions);
return {
log: makeExternalLogger(loaderOptions, logger),
logInfo: makeLogInfo(loaderOptions, logger, colors.green),
logWarning: makeLogWarning(loaderOptions, logger, colors.yellow),
logError: makeLogError(loaderOptions, logger, colors.red),
};
}
//# sourceMappingURL=logger.js.map
+7
View File
@@ -0,0 +1,7 @@
import type * as webpack from 'webpack';
export declare function makeResolver(_options: webpack.WebpackOptionsNormalized): ResolveSync;
export type ResolveSync = {
(context: any, path: string, moduleName: string): string | false;
(path: string, moduleName: string): string | false;
};
//# sourceMappingURL=resolver.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"resolver.d.ts","sourceRoot":"","sources":["../src/resolver.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,OAAO,MAAM,SAAS,CAAC;AAIxC,wBAAgB,YAAY,CAC1B,QAAQ,EAAE,OAAO,CAAC,wBAAwB,GACzC,WAAW,CAMb;AAED,MAAM,MAAM,WAAW,GAAG;IACxB,CAAC,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,GAAG,KAAK,CAAC;IACjE,CAAC,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,GAAG,KAAK,CAAC;CACpD,CAAC"}
+11
View File
@@ -0,0 +1,11 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeResolver = makeResolver;
function makeResolver(_options) {
/* Currently, `enhanced-resolve` does not work properly alongside `ts-loader`.
* This feature is disabled until a proper worflow has been worked out. */
return (_context, _path, _moduleName) => {
throw new Error();
};
}
//# sourceMappingURL=resolver.js.map
+18
View File
@@ -0,0 +1,18 @@
import type * as typescript from 'typescript';
import type * as webpack from 'webpack';
import type { FilePathKey, LoaderOptions, ServiceHostWhichMayBeCacheable, SolutionBuilderWithWatchHost, TSInstance, WatchHost } from './interfaces';
/**
* Create the TypeScript language service
*/
export declare function makeServicesHost(scriptRegex: RegExp, loader: webpack.LoaderContext<LoaderOptions>, instance: TSInstance, projectReferences?: ReadonlyArray<typescript.ProjectReference>): ServiceHostWhichMayBeCacheable;
export declare function updateFileWithText(instance: TSInstance, key: FilePathKey, filePath: string, text: (nFilePath: string) => string): void;
/**
* Create the TypeScript Watch host
*/
export declare function makeWatchHost(scriptRegex: RegExp, loader: webpack.LoaderContext<LoaderOptions>, instance: TSInstance, projectReferences?: ReadonlyArray<typescript.ProjectReference>): WatchHost;
/**
* Create the TypeScript Watch host
*/
export declare function makeSolutionBuilderHost(scriptRegex: RegExp, loader: webpack.LoaderContext<LoaderOptions>, instance: TSInstance): SolutionBuilderWithWatchHost;
export declare function getSolutionErrors(instance: TSInstance, context: string): webpack.WebpackError[];
//# sourceMappingURL=servicesHost.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"servicesHost.d.ts","sourceRoot":"","sources":["../src/servicesHost.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,KAAK,UAAU,MAAM,YAAY,CAAC;AAC9C,OAAO,KAAK,KAAK,OAAO,MAAM,SAAS,CAAC;AAIxC,OAAO,KAAK,EAKV,WAAW,EACX,aAAa,EAIb,8BAA8B,EAC9B,4BAA4B,EAG5B,UAAU,EAIV,SAAS,EACV,MAAM,cAAc,CAAC;AAgHtB;;GAEG;AACH,wBAAgB,gBAAgB,CAC9B,WAAW,EAAE,MAAM,EACnB,MAAM,EAAE,OAAO,CAAC,aAAa,CAAC,aAAa,CAAC,EAC5C,QAAQ,EAAE,UAAU,EACpB,iBAAiB,CAAC,EAAE,aAAa,CAAC,UAAU,CAAC,gBAAgB,CAAC,GAC7D,8BAA8B,CAiGhC;AAkND,wBAAgB,kBAAkB,CAChC,QAAQ,EAAE,UAAU,EACpB,GAAG,EAAE,WAAW,EAChB,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,MAAM,QAuBpC;AAED;;GAEG;AACH,wBAAgB,aAAa,CAC3B,WAAW,EAAE,MAAM,EACnB,MAAM,EAAE,OAAO,CAAC,aAAa,CAAC,aAAa,CAAC,EAC5C,QAAQ,EAAE,UAAU,EACpB,iBAAiB,CAAC,EAAE,aAAa,CAAC,UAAU,CAAC,gBAAgB,CAAC,aA6H/D;AA0DD;;GAEG;AACH,wBAAgB,uBAAuB,CACrC,WAAW,EAAE,MAAM,EACnB,MAAM,EAAE,OAAO,CAAC,aAAa,CAAC,aAAa,CAAC,EAC5C,QAAQ,EAAE,UAAU,GACnB,4BAA4B,CAqe9B;AAED,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,0BAqBtE"}
+810
View File
@@ -0,0 +1,810 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeServicesHost = makeServicesHost;
exports.updateFileWithText = updateFileWithText;
exports.makeWatchHost = makeWatchHost;
exports.makeSolutionBuilderHost = makeSolutionBuilderHost;
exports.getSolutionErrors = getSolutionErrors;
const path = require("path");
const config_1 = require("./config");
const constants = require("./constants");
const instances_1 = require("./instances");
const resolver_1 = require("./resolver");
const utils_1 = require("./utils");
function makeResolversAndModuleResolutionHost(scriptRegex, loader, instance, fileExists, enableFileCaching) {
const { compiler, compilerOptions, appendTsTsxSuffixesIfRequired, loaderOptions: { resolveModuleName: customResolveModuleName, resolveTypeReferenceDirective: customResolveTypeReferenceDirective, }, } = instance;
const newLine = compilerOptions.newLine === constants.CarriageReturnLineFeedCode
? constants.CarriageReturnLineFeed
: compilerOptions.newLine === constants.LineFeedCode
? constants.LineFeed
: constants.EOL;
// loader.context seems to work fine on Linux / Mac regardless causes problems for @types resolution on Windows for TypeScript < 2.3
const getCurrentDirectory = () => loader.context;
// make a (sync) resolver that follows webpack's rules
const resolveSync = (0, resolver_1.makeResolver)(loader._compiler.options);
const moduleResolutionHost = {
trace: logData => instance.log.log(logData),
fileExists,
readFile,
realpath: compiler.sys.realpath && realpath,
directoryExists,
getCurrentDirectory,
getDirectories,
readDirectory,
useCaseSensitiveFileNames: () => (0, utils_1.useCaseSensitiveFileNames)(compiler, instance.loaderOptions),
getNewLine: () => newLine,
getDefaultLibFileName: options => compiler.getDefaultLibFilePath(options),
};
if (enableFileCaching) {
addCache(moduleResolutionHost);
}
return makeResolvers(compiler, compilerOptions, moduleResolutionHost, customResolveTypeReferenceDirective, customResolveModuleName, resolveSync, appendTsTsxSuffixesIfRequired, scriptRegex, instance);
function readFile(filePath, encoding) {
return (instance.compiler.sys.readFile(filePath, encoding) ||
(0, utils_1.fsReadFile)(filePath, encoding));
}
function directoryExists(directoryName) {
return compiler.sys.directoryExists(directoryName);
}
function realpath(path) {
return compiler.sys.realpath(path);
}
function getDirectories(path) {
return compiler.sys.getDirectories(path);
}
function readDirectory(path, extensions, exclude, include, depth) {
return compiler.sys.readDirectory(path, extensions, exclude, include, depth);
}
}
/**
* Create the TypeScript language service
*/
function makeServicesHost(scriptRegex, loader, instance, projectReferences) {
const { compiler, compilerOptions, files, filePathKeyMapper } = instance;
const { moduleResolutionHost, resolveModuleNames, resolveTypeReferenceDirectives, } = makeResolversAndModuleResolutionHost(scriptRegex, loader, instance, filePathToCheck => compiler.sys.fileExists(filePathToCheck) ||
(0, utils_1.fsReadFile)(filePathToCheck) !== undefined, instance.loaderOptions.experimentalFileCaching);
const servicesHost = {
getProjectVersion: () => `${instance.version}`,
getProjectReferences: () => projectReferences,
getScriptFileNames: () => [...files.values()]
.map(({ fileName }) => fileName)
.filter(filePath => filePath.match(scriptRegex)),
getScriptVersion: (fileName) => {
var _a;
fileName = path.normalize(fileName);
const key = filePathKeyMapper(fileName);
const file = files.get(key);
if (file) {
return file.version.toString();
}
const outputFileAndKey = (_a = instance.solutionBuilderHost) === null || _a === void 0 ? void 0 : _a.getOutputFileAndKeyFromReferencedProject(fileName);
if (outputFileAndKey !== undefined) {
instance.solutionBuilderHost.outputAffectingInstanceVersion.set(outputFileAndKey.key, true);
}
return outputFileAndKey && outputFileAndKey.outputFile
? outputFileAndKey.outputFile
: '';
},
getScriptSnapshot: (fileName) => {
// This is called any time TypeScript needs a file's text
// We either load from memory or from disk
fileName = path.normalize(fileName);
const key = filePathKeyMapper(fileName);
let file = files.get(key);
if (file === undefined) {
if (instance.solutionBuilderHost) {
const outputFileAndKey = instance.solutionBuilderHost.getOutputFileTextAndKeyFromReferencedProject(fileName);
if (outputFileAndKey !== undefined) {
instance.solutionBuilderHost.outputAffectingInstanceVersion.set(outputFileAndKey.key, true);
return outputFileAndKey && outputFileAndKey.text !== undefined
? compiler.ScriptSnapshot.fromString(outputFileAndKey.text)
: undefined;
}
}
const text = moduleResolutionHost.readFile(fileName);
if (text === undefined) {
return undefined;
}
file = { fileName, version: 0, text };
files.set(key, file);
}
return compiler.ScriptSnapshot.fromString(file.text);
},
...moduleResolutionHost,
getCompilationSettings: () => compilerOptions,
log: moduleResolutionHost.trace,
// used for (/// <reference types="...">) see https://github.com/Realytics/fork-ts-checker-webpack-plugin/pull/250#issuecomment-485061329
resolveTypeReferenceDirectives,
resolveModuleNames,
getCustomTransformers: () => instance.transformers,
};
return servicesHost;
}
function makeResolvers(compiler, compilerOptions, moduleResolutionHost, customResolveTypeReferenceDirective, customResolveModuleName, resolveSync, appendTsTsxSuffixesIfRequired, scriptRegex, instance) {
const resolveModuleName = makeResolveModuleName(compiler, compilerOptions, moduleResolutionHost, customResolveModuleName, instance);
const resolveModuleNames = (moduleNames, containingFile, _reusedNames, redirectedReference, _, containingSourceFile) => {
const resolvedModules = moduleNames.map(moduleName => resolveModule(resolveSync, resolveModuleName, appendTsTsxSuffixesIfRequired, scriptRegex, moduleName, containingFile, redirectedReference, containingSourceFile));
(0, utils_1.populateDependencyGraph)(resolvedModules, instance, containingFile);
return resolvedModules;
};
const resolveTypeReferenceDirective = makeResolveTypeReferenceDirective(compiler, compilerOptions, moduleResolutionHost, customResolveTypeReferenceDirective, instance);
const resolveTypeReferenceDirectives = (typeDirectiveNames, containingFile, redirectedReference, options, containingFileMode // new impliedNodeFormat is accepted by compilerHost
) => typeDirectiveNames.map(directive => resolveTypeReferenceDirective(directive, containingFile, options, redirectedReference, containingFileMode).resolvedTypeReferenceDirective);
return {
resolveTypeReferenceDirectives,
resolveModuleNames,
moduleResolutionHost,
};
}
function createWatchFactory(filePathKeyMapper, compiler) {
const watchedFiles = new Map();
const watchedDirectories = new Map();
const watchedDirectoriesRecursive = new Map();
return {
watchedFiles,
watchedDirectories,
watchedDirectoriesRecursive,
invokeFileWatcher,
watchFile,
watchDirectory,
};
function invokeWatcherCallbacks(map, key, fileName, eventKind) {
var _a;
const callbacks = (_a = map.get(filePathKeyMapper(key))) === null || _a === void 0 ? void 0 : _a.callbacks;
if (callbacks !== undefined && callbacks.length) {
// The array copy is made to ensure that even if one of the callback removes the callbacks,
// we dont miss any callbacks following it
const cbs = callbacks.slice();
for (const cb of cbs) {
cb(fileName, eventKind);
}
return true;
}
return false;
}
function invokeFileWatcher(fileName, eventKind) {
fileName = path.normalize(fileName);
let result = invokeWatcherCallbacks(watchedFiles, fileName, fileName, eventKind);
if (eventKind !== compiler.FileWatcherEventKind.Changed) {
const directory = path.dirname(fileName);
result =
invokeWatcherCallbacks(watchedDirectories, directory, fileName) ||
result;
result = invokeRecursiveDirectoryWatcher(directory, fileName) || result;
}
return result;
}
function invokeRecursiveDirectoryWatcher(directory, fileAddedOrRemoved) {
directory = path.normalize(directory);
let result = invokeWatcherCallbacks(watchedDirectoriesRecursive, directory, fileAddedOrRemoved);
const basePath = path.dirname(directory);
if (directory !== basePath) {
result =
invokeRecursiveDirectoryWatcher(basePath, fileAddedOrRemoved) || result;
}
return result;
}
function createWatcher(file, callbacks, callback) {
const key = filePathKeyMapper(file);
const existing = callbacks.get(key);
if (existing === undefined) {
callbacks.set(key, {
fileName: path.normalize(file),
callbacks: [callback],
});
}
else {
existing.callbacks.push(callback);
}
return {
close: () => {
const existing = callbacks.get(key);
if (existing !== undefined) {
(0, utils_1.unorderedRemoveItem)(existing.callbacks, callback);
if (!existing.callbacks.length) {
callbacks.delete(key);
}
}
},
};
}
function watchFile(fileName, callback, _pollingInterval) {
return createWatcher(fileName, watchedFiles, callback);
}
function watchDirectory(fileName, callback, recursive) {
return createWatcher(fileName, recursive === true ? watchedDirectoriesRecursive : watchedDirectories, callback);
}
}
function updateFileWithText(instance, key, filePath, text) {
const nFilePath = path.normalize(filePath);
const file = instance.files.get(key) || instance.otherFiles.get(key);
if (file !== undefined) {
const newText = text(nFilePath);
if (newText !== file.text) {
file.text = newText;
file.version++;
file.modifiedTime = new Date();
instance.version++;
if (!instance.modifiedFiles) {
instance.modifiedFiles = new Map();
}
instance.modifiedFiles.set(key, true);
if (instance.watchHost !== undefined) {
instance.watchHost.invokeFileWatcher(nFilePath, instance.compiler.FileWatcherEventKind.Changed);
}
}
}
}
/**
* Create the TypeScript Watch host
*/
function makeWatchHost(scriptRegex, loader, instance, projectReferences) {
const { compiler, compilerOptions, files, otherFiles, filePathKeyMapper } = instance;
const { watchFile, watchDirectory, invokeFileWatcher } = createWatchFactory(filePathKeyMapper, compiler);
const { moduleResolutionHost, resolveModuleNames, resolveTypeReferenceDirectives, } = makeResolversAndModuleResolutionHost(scriptRegex, loader, instance, fileName => files.has(filePathKeyMapper(fileName)) ||
compiler.sys.fileExists(fileName), instance.loaderOptions.experimentalFileCaching);
const watchHost = {
rootFiles: getRootFileNames(),
options: compilerOptions,
...moduleResolutionHost,
readFile: readFileWithCachingText,
watchFile: (fileName, callback, pollingInterval, options) => {
var _a;
const outputFileKey = (_a = instance.solutionBuilderHost) === null || _a === void 0 ? void 0 : _a.getOutputFileKeyFromReferencedProject(fileName);
if (!outputFileKey || outputFileKey === filePathKeyMapper(fileName)) {
return watchFile(fileName, callback, pollingInterval, options);
}
// Handle symlink to outputFile
const outputFileName = instance.solutionBuilderHost.realpath(fileName);
return watchFile(outputFileName, (_fileName, eventKind) => callback(fileName, eventKind), pollingInterval, options);
},
watchDirectory,
// used for (/// <reference types="...">) see https://github.com/Realytics/fork-ts-checker-webpack-plugin/pull/250#issuecomment-485061329
resolveTypeReferenceDirectives,
resolveModuleNames,
invokeFileWatcher,
updateRootFileNames: () => {
instance.changedFilesList = false;
if (instance.watchOfFilesAndCompilerOptions !== undefined) {
instance.watchOfFilesAndCompilerOptions.updateRootFileNames(getRootFileNames());
}
},
createProgram: projectReferences === undefined
? compiler.createEmitAndSemanticDiagnosticsBuilderProgram
: createBuilderProgramWithReferences,
outputFiles: new Map(),
};
return watchHost;
function getRootFileNames() {
return [...files.values()]
.map(({ fileName }) => fileName)
.filter(filePath => filePath.match(scriptRegex));
}
function readFileWithCachingText(fileName, encoding) {
var _a;
fileName = path.normalize(fileName);
const key = filePathKeyMapper(fileName);
const file = files.get(key) || otherFiles.get(key);
if (file !== undefined) {
return file.text;
}
const text = moduleResolutionHost.readFile(fileName, encoding);
if (text === undefined) {
return undefined;
}
if (!((_a = instance.solutionBuilderHost) === null || _a === void 0 ? void 0 : _a.getOutputFileKeyFromReferencedProject(fileName))) {
otherFiles.set(key, { fileName, version: 0, text });
}
return text;
}
function createBuilderProgramWithReferences(rootNames, options, host, oldProgram, configFileParsingDiagnostics) {
const program = compiler.createProgram({
rootNames: rootNames,
options: options,
host,
oldProgram: oldProgram && oldProgram.getProgram(),
configFileParsingDiagnostics,
projectReferences,
});
const builderProgramHost = host;
return compiler.createEmitAndSemanticDiagnosticsBuilderProgram(program, builderProgramHost, oldProgram, configFileParsingDiagnostics);
}
}
const missingFileModifiedTime = new Date(0);
function identity(x) {
return x;
}
function toLowerCase(x) {
return x.toLowerCase();
}
const fileNameLowerCaseRegExp = /[^\u0130\u0131\u00DFa-z0-9\\/:\-_\. ]+/g;
function toFileNameLowerCase(x) {
return fileNameLowerCaseRegExp.test(x)
? x.replace(fileNameLowerCaseRegExp, toLowerCase)
: x;
}
function createGetCanonicalFileName(instance) {
return (0, utils_1.useCaseSensitiveFileNames)(instance.compiler, instance.loaderOptions)
? identity
: toFileNameLowerCase;
}
function createModuleResolutionCache(instance, moduleResolutionHost) {
const cache = instance.compiler.createModuleResolutionCache(moduleResolutionHost.getCurrentDirectory(), createGetCanonicalFileName(instance), instance.compilerOptions);
// Add new API optional methods
if (!cache.clear) {
cache.clear = () => {
cache.directoryToModuleNameMap.clear();
cache.moduleNameToDirectoryMap.clear();
};
}
if (!cache.update) {
cache.update = options => {
if (!options.configFile)
return;
const ref = {
sourceFile: options.configFile,
commandLine: { options },
};
cache.directoryToModuleNameMap.setOwnMap(cache.directoryToModuleNameMap.getOrCreateMapOfCacheRedirects(ref));
cache.moduleNameToDirectoryMap.setOwnMap(cache.moduleNameToDirectoryMap.getOrCreateMapOfCacheRedirects(ref));
cache.directoryToModuleNameMap.setOwnOptions(options);
cache.moduleNameToDirectoryMap.setOwnOptions(options);
};
}
return cache;
}
/**
* Create the TypeScript Watch host
*/
function makeSolutionBuilderHost(scriptRegex, loader, instance) {
const { compiler, loaderOptions: { transpileOnly }, filePathKeyMapper, } = instance;
// loader.context seems to work fine on Linux / Mac regardless causes problems for @types resolution on Windows for TypeScript < 2.3
const formatDiagnosticHost = {
getCurrentDirectory: compiler.sys.getCurrentDirectory,
getCanonicalFileName: createGetCanonicalFileName(instance),
getNewLine: () => compiler.sys.newLine,
};
const diagnostics = {
global: [],
perFile: new Map(),
transpileErrors: [],
};
const reportDiagnostic = (d) => {
if (transpileOnly) {
const filePath = d.file ? filePathKeyMapper(d.file.fileName) : undefined;
const last = diagnostics.transpileErrors[diagnostics.transpileErrors.length - 1];
if (diagnostics.transpileErrors.length && last[0] === filePath) {
last[1].push(d);
}
else {
diagnostics.transpileErrors.push([filePath, [d]]);
}
}
else if (d.file) {
const filePath = filePathKeyMapper(d.file.fileName);
const existing = diagnostics.perFile.get(filePath);
if (existing) {
existing.push(d);
}
else {
diagnostics.perFile.set(filePath, [d]);
}
}
else {
diagnostics.global.push(d);
}
instance.log.logInfo(compiler.formatDiagnostic(d, formatDiagnosticHost));
};
const reportSolutionBuilderStatus = (d) => instance.log.logInfo(compiler.formatDiagnostic(d, formatDiagnosticHost));
const reportWatchStatus = (d, newLine, _options) => instance.log.logInfo(`${compiler.flattenDiagnosticMessageText(d.messageText, compiler.sys.newLine)}${newLine + newLine}`);
const outputFiles = new Map();
const inputFiles = new Map();
const writtenFiles = [];
const outputAffectingInstanceVersion = new Map();
let timeoutId;
const { resolveModuleNames, resolveTypeReferenceDirectives, moduleResolutionHost, } = makeResolversAndModuleResolutionHost(scriptRegex, loader, instance, fileName => {
const filePathKey = filePathKeyMapper(fileName);
return (instance.files.has(filePathKey) ||
instance.otherFiles.has(filePathKey) ||
compiler.sys.fileExists(fileName));
},
/*enableFileCaching*/ true);
const configFileInfo = new Map();
const allWatches = [];
const sysHost = compiler.createSolutionBuilderWithWatchHost(compiler.sys, compiler.createEmitAndSemanticDiagnosticsBuilderProgram, reportDiagnostic, reportSolutionBuilderStatus, reportWatchStatus);
// Keeps track of the various `typescript.CustomTransformers` for each program that is created.
const customTransformers = new Map();
// let lastBuilderProgram: typescript.CreateProgram | undefined = undefined;
const solutionBuilderHost = {
...sysHost,
...moduleResolutionHost,
createProgram: (rootNames, options, host, oldProgram, configFileParsingDiagnostics, projectReferences) => {
var _a, _b, _c, _d;
(_a = instance.moduleResolutionCache) === null || _a === void 0 ? void 0 : _a.update(options || {});
(_b = instance.typeReferenceResolutionCache) === null || _b === void 0 ? void 0 : _b.update(options || {});
const result = sysHost.createProgram(rootNames, options, host, oldProgram, configFileParsingDiagnostics, projectReferences);
(_c = instance.typeReferenceResolutionCache) === null || _c === void 0 ? void 0 : _c.update(instance.compilerOptions);
(_d = instance.moduleResolutionCache) === null || _d === void 0 ? void 0 : _d.update(instance.compilerOptions);
if (options) {
// The `configFilePath` is the same value that is used as the `project` parameter of
// `getCustomtransformers` below.
const project = options.configFilePath;
if (typeof project === "string") {
// Custom transformers need a reference to the `typescript.Program`, that reference is
// unavailable during the the `getCustomTransformers` callback below.
const transformers = (0, instances_1.getCustomTransformers)(instance.loaderOptions, result.getProgram(), result.getProgram);
customTransformers.set(project, transformers);
}
}
return result;
},
resolveModuleNames,
resolveTypeReferenceDirectives,
diagnostics,
...createWatchFactory(filePathKeyMapper, compiler),
getCustomTransformers: function (project) {
return customTransformers.get(project);
},
// Overrides
writeFile: (name, text, writeByteOrderMark) => {
var _a;
const key = filePathKeyMapper(name);
updateFileWithText(instance, key, name, () => text);
const existing = ensureOutputFile(name);
const hash = hashOutputText(text);
outputFiles.set(key, hash);
writtenFiles.push({
name,
text,
writeByteOrderMark: !!writeByteOrderMark,
});
compiler.sys.writeFile(name, text, writeByteOrderMark);
(_a = moduleResolutionHost.fileExistsCache) === null || _a === void 0 ? void 0 : _a.delete(name);
if (outputAffectingInstanceVersion.has(key) &&
(!existing || existing !== hash)) {
instance.version++;
}
if (instance.watchHost &&
!instance.files.has(key) &&
!instance.otherFiles.has(key)) {
// If file wasnt updated in files or other files of instance, let watch host know of the change
if (!existing) {
instance.hasUnaccountedModifiedFiles =
instance.watchHost.invokeFileWatcher(name, compiler.FileWatcherEventKind.Created) || instance.hasUnaccountedModifiedFiles;
}
else if (existing !== hash) {
instance.hasUnaccountedModifiedFiles =
instance.watchHost.invokeFileWatcher(name, compiler.FileWatcherEventKind.Changed) || instance.hasUnaccountedModifiedFiles;
}
}
},
createDirectory: sysHost.createDirectory &&
(directory => {
var _a;
sysHost.createDirectory(directory);
(_a = moduleResolutionHost.directoryExistsCache) === null || _a === void 0 ? void 0 : _a.delete(directory);
}),
afterProgramEmitAndDiagnostics: transpileOnly ? undefined : storeDtsFiles,
setTimeout: (callback, _time, ...args) => {
timeoutId = [callback, args];
return timeoutId;
},
clearTimeout: _timeoutId => {
timeoutId = undefined;
},
getParsedCommandLine: file => {
const config = (0, config_1.getParsedCommandLine)(compiler, instance.loaderOptions, file);
configFileInfo.set(filePathKeyMapper(file), { config });
return config;
},
writtenFiles,
configFileInfo,
outputAffectingInstanceVersion,
getInputFileStamp,
updateSolutionBuilderInputFile,
getOutputFileKeyFromReferencedProject,
getOutputFileAndKeyFromReferencedProject,
getOutputFileTextAndKeyFromReferencedProject,
getInputFileNameFromOutput: fileName => {
const result = getInputFileNameFromOutput(fileName);
return typeof result === 'string' ? result : undefined;
},
getOutputFilesFromReferencedProjectInput,
buildReferences,
ensureAllReferenceTimestamps,
clearCache,
close,
};
return solutionBuilderHost;
function close() {
allWatches.slice().forEach(w => w.close());
}
function clearCache() {
moduleResolutionHost.clearCache();
outputFiles.clear();
inputFiles.clear();
}
function buildReferences() {
if (!timeoutId) {
ensureAllReferenceTimestamps();
return;
}
diagnostics.global.length = 0;
diagnostics.perFile.clear();
diagnostics.transpileErrors.length = 0;
while (timeoutId) {
const [callback, args] = timeoutId;
timeoutId = undefined;
callback(...args);
}
ensureAllReferenceTimestamps();
}
function ensureAllReferenceTimestamps() {
if (inputFiles.size !== solutionBuilderHost.watchedFiles.size) {
for (const { fileName, } of instance.solutionBuilderHost.watchedFiles.values()) {
instance.solutionBuilderHost.getInputFileStamp(fileName);
}
}
}
function storeDtsFiles(builderProgram) {
const program = builderProgram.getProgram();
for (const configInfo of configFileInfo.values()) {
if (!configInfo.config ||
program.getRootFileNames() !== configInfo.config.fileNames ||
program.getCompilerOptions() !== configInfo.config.options ||
program.getProjectReferences() !== configInfo.config.projectReferences) {
continue;
}
configInfo.dtsFiles = program
.getSourceFiles()
.map(file => path.resolve(file.fileName))
.filter(fileName => fileName.match(constants.dtsDtsxOrDtsDtsxMapRegex));
return;
}
}
function getInputFileNameFromOutput(outputFileName) {
const resolvedFileName = filePathKeyMapper(outputFileName);
for (const configInfo of configFileInfo.values()) {
ensureInputOutputInfo(configInfo);
if (configInfo.outputFileNames) {
for (const { inputFileName, outputNames, } of configInfo.outputFileNames.values()) {
if (outputNames.some(outputName => resolvedFileName === filePathKeyMapper(outputName))) {
return inputFileName;
}
}
}
if (configInfo.tsbuildInfoFile &&
filePathKeyMapper(configInfo.tsbuildInfoFile) === resolvedFileName) {
return true;
}
}
const realPath = solutionBuilderHost.realpath(outputFileName);
return filePathKeyMapper(realPath) !== resolvedFileName
? getInputFileNameFromOutput(realPath)
: undefined;
}
function ensureInputOutputInfo(configInfo) {
if (configInfo.outputFileNames || !configInfo.config) {
return;
}
configInfo.outputFileNames = new Map();
configInfo.config.fileNames.forEach(inputFile => configInfo.outputFileNames.set(filePathKeyMapper(inputFile), {
inputFileName: path.resolve(inputFile),
outputNames: (0, instances_1.getOutputFileNames)(instance, configInfo.config, inputFile),
}));
configInfo.tsbuildInfoFile = instance.compiler
.getTsBuildInfoEmitOutputFilePath
? instance.compiler.getTsBuildInfoEmitOutputFilePath(configInfo.config.options)
: // before api
instance.compiler.getOutputPathForBuildInfo(configInfo.config.options);
}
function getOutputFileAndKeyFromReferencedProject(outputFileName) {
const outputFile = ensureOutputFile(outputFileName);
return outputFile !== undefined
? {
key: getOutputFileKeyFromReferencedProject(outputFileName),
outputFile,
}
: undefined;
}
function getOutputFileTextAndKeyFromReferencedProject(outputFileName) {
const key = getOutputFileKeyFromReferencedProject(outputFileName);
if (!key) {
return undefined;
}
const file = writtenFiles.find(w => filePathKeyMapper(w.name) === key);
if (file) {
return { key, text: file.text };
}
const outputFile = outputFiles.get(key);
return {
key,
text: outputFile !== false
? compiler.sys.readFile(outputFileName)
: undefined,
};
}
function getOutputFileKeyFromReferencedProject(outputFileName) {
const key = filePathKeyMapper(outputFileName);
if (outputFiles.has(key))
return key;
const realKey = filePathKeyMapper(solutionBuilderHost.realpath(outputFileName));
if (realKey !== key && outputFiles.has(realKey))
return realKey;
return getInputFileNameFromOutput(outputFileName) ? realKey : undefined;
}
function hashOutputText(text) {
return compiler.sys.createHash ? compiler.sys.createHash(text) : text;
}
function ensureOutputFile(outputFileName) {
const key = getOutputFileKeyFromReferencedProject(outputFileName);
if (!key) {
return undefined;
}
const outputFile = outputFiles.get(key);
if (outputFile !== undefined) {
return outputFile;
}
if (!getInputFileNameFromOutput(outputFileName)) {
return undefined;
}
const text = compiler.sys.readFile(outputFileName);
const hash = text === undefined ? false : hashOutputText(text);
outputFiles.set(key, hash);
return hash;
}
function getTypeScriptOutputFile(outputFileName) {
const key = filePathKeyMapper(outputFileName);
const writtenFile = writtenFiles.find(w => filePathKeyMapper(w.name) === key);
if (writtenFile)
return writtenFile;
// Read from sys
const text = compiler.sys.readFile(outputFileName);
return text !== undefined
? {
name: outputFileName,
text,
writeByteOrderMark: false,
}
: undefined;
}
function getOutputFilesFromReferencedProjectInput(inputFileName) {
const resolvedFileName = filePathKeyMapper(inputFileName);
for (const configInfo of configFileInfo.values()) {
ensureInputOutputInfo(configInfo);
if (configInfo.outputFileNames) {
const result = configInfo.outputFileNames.get(resolvedFileName);
if (result) {
return result.outputNames
.map(getTypeScriptOutputFile)
.filter(output => !!output);
}
}
}
return [];
}
function getInputFileStamp(fileName) {
const key = filePathKeyMapper(fileName);
const existing = inputFiles.get(key);
if (existing !== undefined) {
return existing;
}
const time = compiler.sys.getModifiedTime(fileName) || missingFileModifiedTime;
inputFiles.set(key, time);
return time;
}
function updateSolutionBuilderInputFile(fileName) {
const key = filePathKeyMapper(fileName);
const existing = inputFiles.get(key) || missingFileModifiedTime;
const newTime = compiler.sys.getModifiedTime(fileName) || missingFileModifiedTime;
if (existing.getTime() === newTime.getTime()) {
return;
}
const eventKind = existing == missingFileModifiedTime
? compiler.FileWatcherEventKind.Created
: newTime === missingFileModifiedTime
? compiler.FileWatcherEventKind.Deleted
: compiler.FileWatcherEventKind.Changed;
solutionBuilderHost.invokeFileWatcher(fileName, eventKind);
}
}
function getSolutionErrors(instance, context) {
const solutionErrors = [];
if (instance.solutionBuilderHost &&
instance.solutionBuilderHost.diagnostics.transpileErrors.length) {
instance.solutionBuilderHost.diagnostics.transpileErrors.forEach(([filePath, errors]) => solutionErrors.push(...(0, utils_1.formatErrors)(errors, instance.loaderOptions, instance.colors, instance.compiler, { file: filePath ? undefined : 'tsconfig.json' }, context)));
}
return solutionErrors;
}
function makeResolveTypeReferenceDirective(compiler, compilerOptions, moduleResolutionHost, customResolveTypeReferenceDirective, instance) {
var _a, _b;
if (customResolveTypeReferenceDirective === undefined) {
// Until the api is published
if (compiler.createTypeReferenceDirectiveResolutionCache !== undefined &&
!instance.typeReferenceResolutionCache) {
instance.typeReferenceResolutionCache =
compiler.createTypeReferenceDirectiveResolutionCache(moduleResolutionHost.getCurrentDirectory(), createGetCanonicalFileName(instance), instance.compilerOptions, (_b = (_a = instance.moduleResolutionCache) === null || _a === void 0 ? void 0 : _a.getPackageJsonInfoCache) === null || _b === void 0 ? void 0 : _b.call(_a));
}
return (typeDirectiveName, containingFile, options, redirectedReference, containingFileMode) => {
// Copy-pasted from https://github.com/TypeStrong/ts-node/blob/9f789d0d91c6eba30ac7f7aad45194a23b44f159/src/resolver-functions.ts#L139
const nameIsString = typeof typeDirectiveName === 'string';
const mode = nameIsString
? undefined
: compiler.getModeForFileReference(typeDirectiveName, containingFileMode);
const strName = nameIsString
? typeDirectiveName
: typeDirectiveName.fileName.toLowerCase();
return compiler.resolveTypeReferenceDirective(strName, containingFile, options, moduleResolutionHost, redirectedReference, undefined, mode);
};
}
return (directive, containingFile) => customResolveTypeReferenceDirective(directive, // unsure whether we should evolve this further
containingFile, compilerOptions, moduleResolutionHost, compiler.resolveTypeReferenceDirective);
}
function isJsImplementationOfTypings(resolvedModule, tsResolution) {
return (resolvedModule.resolvedFileName.endsWith('js') &&
/\.d\.ts$/.test(tsResolution.resolvedFileName));
}
function resolveModule(resolveSync, resolveModuleName, appendTsTsxSuffixesIfRequired, scriptRegex, moduleName, containingFile, redirectedReference, containingSourceFile) {
let resolutionResult;
try {
const originalFileName = resolveSync(path.normalize(path.dirname(containingFile)), moduleName);
if (originalFileName) {
const resolvedFileName = appendTsTsxSuffixesIfRequired(originalFileName);
if (resolvedFileName.match(scriptRegex) !== null) {
resolutionResult = { resolvedFileName, originalFileName };
}
}
}
catch (e) { }
const tsResolution = resolveModuleName(moduleName, containingFile, redirectedReference, containingSourceFile);
if (tsResolution.resolvedModule !== undefined) {
const resolvedFileName = path.normalize(tsResolution.resolvedModule.resolvedFileName);
const tsResolutionResult = {
...tsResolution.resolvedModule,
originalFileName: resolvedFileName,
resolvedFileName,
};
return resolutionResult === undefined ||
resolutionResult.resolvedFileName ===
tsResolutionResult.resolvedFileName ||
isJsImplementationOfTypings(resolutionResult, tsResolutionResult)
? tsResolutionResult
: resolutionResult;
}
return resolutionResult;
}
function makeResolveModuleName(compiler, compilerOptions, moduleResolutionHost, customResolveModuleName, instance) {
if (customResolveModuleName === undefined) {
if (!instance.moduleResolutionCache) {
instance.moduleResolutionCache = createModuleResolutionCache(instance, moduleResolutionHost);
}
return (moduleName, containingFileName, redirectedReference, containingFile) => compiler.resolveModuleName(moduleName, containingFileName, compilerOptions, moduleResolutionHost, instance.moduleResolutionCache, redirectedReference, containingFile === null || containingFile === void 0 ? void 0 : containingFile.impliedNodeFormat);
}
return (moduleName, containingFile) => customResolveModuleName(moduleName, containingFile, compilerOptions, moduleResolutionHost, compiler.resolveModuleName);
}
function addCache(host) {
host.fileExists = createCache(host.fileExists, (host.fileExistsCache = new Map()));
host.directoryExists = createCache(host.directoryExists, (host.directoryExistsCache = new Map()));
host.realpath =
host.realpath &&
createCache(host.realpath, (host.realpathCache = new Map()));
host.clearCache = clearCache;
function createCache(originalFunction, cache) {
return function getCached(arg) {
let res = cache.get(arg);
if (res !== undefined) {
return res;
}
res = originalFunction(arg);
cache.set(arg, res);
return res;
};
}
function clearCache() {
var _a, _b, _c;
(_a = host.fileExistsCache) === null || _a === void 0 ? void 0 : _a.clear();
(_b = host.directoryExistsCache) === null || _b === void 0 ? void 0 : _b.clear();
(_c = host.realpathCache) === null || _c === void 0 ? void 0 : _c.clear();
}
}
//# sourceMappingURL=servicesHost.js.map
+1
View File
@@ -0,0 +1 @@
//# sourceMappingURL=stringify-loader.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"stringify-loader.d.ts","sourceRoot":"","sources":["../src/stringify-loader.ts"],"names":[],"mappings":""}
+3
View File
@@ -0,0 +1,3 @@
"use strict";
module.exports = (source) => JSON.stringify(source);
//# sourceMappingURL=stringify-loader.js.map
+32
View File
@@ -0,0 +1,32 @@
import type { Chalk } from 'chalk';
import * as webpack from 'webpack';
import type * as typescript from 'typescript';
import type { FileLocation, FilePathKey, LoaderOptions, ResolvedModule, ReverseDependencyGraph, TSInstance } from './interfaces';
/**
* Take TypeScript errors, parse them and format to webpack errors
* Optionally adds a file name
*/
export declare function formatErrors(diagnostics: ReadonlyArray<typescript.Diagnostic> | undefined, loaderOptions: LoaderOptions, colors: Chalk, compiler: typeof typescript, merge: {
file?: string;
module?: webpack.Module;
}, context: string): webpack.WebpackError[];
export declare function fsReadFile(fileName: string, encoding?: BufferEncoding | undefined): string | undefined;
export declare function makeError(loaderOptions: LoaderOptions, message: string, file: string, location?: FileLocation, endLocation?: FileLocation): webpack.WebpackError;
export declare function tsLoaderSource(loaderOptions: LoaderOptions): string;
export declare function appendSuffixIfMatch(patterns: (RegExp | string)[], filePath: string, suffix: string): string;
export declare function appendSuffixesIfMatch(suffixDict: {
[suffix: string]: (RegExp | string)[];
}, filePath: string): string;
export declare function unorderedRemoveItem<T>(array: T[], item: T): boolean;
export declare function populateDependencyGraph(resolvedModules: ResolvedModule[], instance: TSInstance, containingFile: string): void;
export declare function populateReverseDependencyGraph(instance: TSInstance): ReverseDependencyGraph;
/**
* Recursively collect all possible dependants of passed file
*/
export declare function collectAllDependants(reverseDependencyGraph: ReverseDependencyGraph, fileName: FilePathKey, result?: Map<FilePathKey, true>): Map<FilePathKey, true>;
export declare function arrify<T>(val: T | T[]): T[];
export declare function ensureProgram(instance: TSInstance): typescript.Program | undefined;
export declare function supportsSolutionBuild(instance: TSInstance): boolean;
export declare function isReferencedFile(instance: TSInstance, filePath: string): boolean;
export declare function useCaseSensitiveFileNames(compiler: typeof typescript, loaderOptions: LoaderOptions): boolean;
//# sourceMappingURL=utils.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAInC,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AACnC,OAAO,KAAK,KAAK,UAAU,MAAM,YAAY,CAAC;AAG9C,OAAO,KAAK,EAEV,YAAY,EACZ,WAAW,EACX,aAAa,EACb,cAAc,EACd,sBAAsB,EAEtB,UAAU,EACX,MAAM,cAAc,CAAC;AAqBtB;;;GAGG;AACH,wBAAgB,YAAY,CAC1B,WAAW,EAAE,aAAa,CAAC,UAAU,CAAC,UAAU,CAAC,GAAG,SAAS,EAC7D,aAAa,EAAE,aAAa,EAC5B,MAAM,EAAE,KAAK,EACb,QAAQ,EAAE,OAAO,UAAU,EAC3B,KAAK,EAAE;IAAE,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,OAAO,CAAC,MAAM,CAAA;CAAE,EACjD,OAAO,EAAE,MAAM,GACd,OAAO,CAAC,YAAY,EAAE,CA8DxB;AAuBD,wBAAgB,UAAU,CACxB,QAAQ,EAAE,MAAM,EAChB,QAAQ,GAAE,cAAc,GAAG,SAAkB,sBAQ9C;AAED,wBAAgB,SAAS,CACvB,aAAa,EAAE,aAAa,EAC5B,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,MAAM,EACZ,QAAQ,CAAC,EAAE,YAAY,EACvB,WAAW,CAAC,EAAE,YAAY,GACzB,OAAO,CAAC,YAAY,CAoBtB;AAuBD,wBAAgB,cAAc,CAAC,aAAa,EAAE,aAAa,UAE1D;AAED,wBAAgB,mBAAmB,CACjC,QAAQ,EAAE,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,EAC7B,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,GACb,MAAM,CASR;AAED,wBAAgB,qBAAqB,CACnC,UAAU,EAAE;IAAE,CAAC,MAAM,EAAE,MAAM,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAA;CAAE,EACrD,QAAQ,EAAE,MAAM,GACf,MAAM,CAMR;AAED,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,IAAI,EAAE,CAAC,GAAG,OAAO,CAUnE;AAED,wBAAgB,uBAAuB,CACrC,eAAe,EAAE,cAAc,EAAE,EACjC,QAAQ,EAAE,UAAU,EACpB,cAAc,EAAE,MAAM,QASvB;AAED,wBAAgB,8BAA8B,CAAC,QAAQ,EAAE,UAAU,0BAyBlE;AAED;;GAEG;AACH,wBAAgB,oBAAoB,CAClC,sBAAsB,EAAE,sBAAsB,EAC9C,QAAQ,EAAE,WAAW,EACrB,MAAM,GAAE,GAAG,CAAC,WAAW,EAAE,IAAI,CAAa,GACzC,GAAG,CAAC,WAAW,EAAE,IAAI,CAAC,CAWxB;AAED,wBAAgB,MAAM,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,EAAE,OAMrC;AAED,wBAAgB,aAAa,CAAC,QAAQ,EAAE,UAAU,kCAkBjD;AAED,wBAAgB,qBAAqB,CAAC,QAAQ,EAAE,UAAU,WAOzD;AAED,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,WAOtE;AAED,wBAAgB,yBAAyB,CACvC,QAAQ,EAAE,OAAO,UAAU,EAC3B,aAAa,EAAE,aAAa,WAK7B"}
+249
View File
@@ -0,0 +1,249 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatErrors = formatErrors;
exports.fsReadFile = fsReadFile;
exports.makeError = makeError;
exports.tsLoaderSource = tsLoaderSource;
exports.appendSuffixIfMatch = appendSuffixIfMatch;
exports.appendSuffixesIfMatch = appendSuffixesIfMatch;
exports.unorderedRemoveItem = unorderedRemoveItem;
exports.populateDependencyGraph = populateDependencyGraph;
exports.populateReverseDependencyGraph = populateReverseDependencyGraph;
exports.collectAllDependants = collectAllDependants;
exports.arrify = arrify;
exports.ensureProgram = ensureProgram;
exports.supportsSolutionBuild = supportsSolutionBuild;
exports.isReferencedFile = isReferencedFile;
exports.useCaseSensitiveFileNames = useCaseSensitiveFileNames;
const fs = require("fs");
const micromatch = require("micromatch");
const path = require("path");
const webpack = require("webpack");
const constants = require("./constants");
const instances_1 = require("./instances");
/**
* The default error formatter.
*/
function defaultErrorFormatter(error, colors) {
const messageColor = error.severity === 'warning' ? colors.bold.yellow : colors.bold.red;
return (colors.grey('[tsl] ') +
messageColor(error.severity.toUpperCase()) +
(error.file === ''
? ''
: messageColor(' in ') +
colors.bold.cyan(`${error.file}(${error.line},${error.character})`)) +
constants.EOL +
messageColor(` TS${error.code}: ${error.content}`));
}
/**
* Take TypeScript errors, parse them and format to webpack errors
* Optionally adds a file name
*/
function formatErrors(diagnostics, loaderOptions, colors, compiler, merge, context) {
return diagnostics === undefined
? []
: diagnostics
.filter(diagnostic => {
if (loaderOptions.ignoreDiagnostics.indexOf(diagnostic.code) !== -1) {
return false;
}
if (loaderOptions.reportFiles.length > 0 &&
diagnostic.file !== undefined) {
const relativeFileName = path.relative(context, diagnostic.file.fileName);
const matchResult = micromatch([relativeFileName], loaderOptions.reportFiles);
if (matchResult.length === 0) {
return false;
}
}
return true;
})
.map(diagnostic => {
const file = diagnostic.file;
const { start, end } = file === undefined || diagnostic.start === undefined
? { start: undefined, end: undefined }
: getFileLocations(file, diagnostic.start, diagnostic.length);
const errorInfo = {
code: diagnostic.code,
severity: compiler.DiagnosticCategory[diagnostic.category].toLowerCase(),
content: compiler.flattenDiagnosticMessageText(diagnostic.messageText, constants.EOL),
file: file === undefined ? '' : path.normalize(file.fileName),
line: start === undefined ? 0 : start.line,
character: start === undefined ? 0 : start.character,
context,
};
const message = loaderOptions.errorFormatter === undefined
? defaultErrorFormatter(errorInfo, colors)
: loaderOptions.errorFormatter(errorInfo, colors);
const error = makeError(loaderOptions, message, merge.file === undefined ? errorInfo.file : merge.file, start, end);
return Object.assign(error, merge);
});
}
function getFileLocations(file, position, length = 0) {
const startLC = file.getLineAndCharacterOfPosition(position);
const start = {
line: startLC.line + 1,
character: startLC.character + 1,
};
const endLC = length > 0
? file.getLineAndCharacterOfPosition(position + length)
: undefined;
const end = endLC === undefined
? undefined
: { line: endLC.line + 1, character: endLC.character + 1 };
return { start, end };
}
function fsReadFile(fileName, encoding = 'utf8') {
fileName = path.normalize(fileName);
try {
return fs.readFileSync(fileName, encoding);
}
catch (e) {
return undefined;
}
}
function makeError(loaderOptions, message, file, location, endLocation) {
const error = new webpack.WebpackError(message);
error.file = file;
error.loc =
location === undefined
? { name: file }
: makeWebpackLocation(location, endLocation);
error.details = tsLoaderSource(loaderOptions);
return error;
// return {
// message,
// file,
// loc:
// location === undefined
// ? { name: file }
// : makeWebpackLocation(location, endLocation),
// details: tsLoaderSource(loaderOptions),
// };
}
function makeWebpackLocation(location, endLocation) {
const start = {
line: location.line,
column: location.character - 1,
};
const end = endLocation === undefined
? undefined
: { line: endLocation.line, column: endLocation.character - 1 };
return { start, end };
}
function tsLoaderSource(loaderOptions) {
return `ts-loader-${loaderOptions.instance}`;
}
function appendSuffixIfMatch(patterns, filePath, suffix) {
if (patterns.length > 0) {
for (const regexp of patterns) {
if (filePath.match(regexp) !== null) {
return filePath + suffix;
}
}
}
return filePath;
}
function appendSuffixesIfMatch(suffixDict, filePath) {
let amendedPath = filePath;
for (const suffix in suffixDict) {
amendedPath = appendSuffixIfMatch(suffixDict[suffix], amendedPath, suffix);
}
return amendedPath;
}
function unorderedRemoveItem(array, item) {
for (let i = 0; i < array.length; i++) {
if (array[i] === item) {
// Fill in the "hole" left at `index`.
array[i] = array[array.length - 1];
array.pop();
return true;
}
}
return false;
}
function populateDependencyGraph(resolvedModules, instance, containingFile) {
resolvedModules = resolvedModules.filter(mod => mod !== null && mod !== undefined);
if (resolvedModules.length) {
const containingFileKey = instance.filePathKeyMapper(containingFile);
instance.dependencyGraph.set(containingFileKey, resolvedModules);
}
}
function populateReverseDependencyGraph(instance) {
const reverseDependencyGraph = new Map();
for (const [fileKey, resolvedModules] of instance.dependencyGraph.entries()) {
const inputFileName = instance.solutionBuilderHost &&
(0, instances_1.getInputFileNameFromOutput)(instance, fileKey);
const containingFileKey = inputFileName
? instance.filePathKeyMapper(inputFileName)
: fileKey;
resolvedModules.forEach(({ resolvedFileName }) => {
const key = instance.filePathKeyMapper(instance.solutionBuilderHost
? (0, instances_1.getInputFileNameFromOutput)(instance, resolvedFileName) ||
resolvedFileName
: resolvedFileName);
let map = reverseDependencyGraph.get(key);
if (!map) {
map = new Map();
reverseDependencyGraph.set(key, map);
}
map.set(containingFileKey, true);
});
}
return reverseDependencyGraph;
}
/**
* Recursively collect all possible dependants of passed file
*/
function collectAllDependants(reverseDependencyGraph, fileName, result = new Map()) {
result.set(fileName, true);
const dependants = reverseDependencyGraph.get(fileName);
if (dependants !== undefined) {
for (const dependantFileName of dependants.keys()) {
if (!result.has(dependantFileName)) {
collectAllDependants(reverseDependencyGraph, dependantFileName, result);
}
}
}
return result;
}
function arrify(val) {
if (val === null || val === undefined) {
return [];
}
return Array.isArray(val) ? val : [val];
}
function ensureProgram(instance) {
if (instance && instance.watchHost) {
if (instance.hasUnaccountedModifiedFiles) {
if (instance.changedFilesList) {
instance.watchHost.updateRootFileNames();
}
if (instance.watchOfFilesAndCompilerOptions) {
instance.builderProgram = instance.watchOfFilesAndCompilerOptions.getProgram();
instance.program = instance.builderProgram.getProgram();
}
instance.hasUnaccountedModifiedFiles = false;
}
return instance.program;
}
if (instance.languageService) {
return instance.languageService.getProgram();
}
return instance.program;
}
function supportsSolutionBuild(instance) {
return (!!instance.configFilePath &&
!!instance.loaderOptions.projectReferences &&
!!instance.configParseResult.projectReferences &&
!!instance.configParseResult.projectReferences.length);
}
function isReferencedFile(instance, filePath) {
return (!!instance.solutionBuilderHost &&
!!instance.solutionBuilderHost.watchedFiles.get(instance.filePathKeyMapper(filePath)));
}
function useCaseSensitiveFileNames(compiler, loaderOptions) {
return loaderOptions.useCaseSensitiveFileNames !== undefined
? loaderOptions.useCaseSensitiveFileNames
: compiler.sys.useCaseSensitiveFileNames;
}
//# sourceMappingURL=utils.js.map
+7
View File
@@ -0,0 +1,7 @@
import type * as webpack from 'webpack';
import type { LoaderOptions, TSInstance } from './interfaces';
/**
* Make function which will manually update changed files
*/
export declare function makeWatchRun(instance: TSInstance, loader: webpack.LoaderContext<LoaderOptions>): (compiler: webpack.Compiler, callback: (err?: Error) => void) => void;
//# sourceMappingURL=watch-run.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"watch-run.d.ts","sourceRoot":"","sources":["../src/watch-run.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,KAAK,OAAO,MAAM,SAAS,CAAC;AAGxC,OAAO,KAAK,EAAe,aAAa,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAI3E;;GAEG;AACH,wBAAgB,YAAY,CAC1B,QAAQ,EAAE,UAAU,EACpB,MAAM,EAAE,OAAO,CAAC,aAAa,CAAC,aAAa,CAAC,cAS1B,OAAO,CAAC,QAAQ,YAAY,CAAC,GAAG,CAAC,EAAE,KAAK,KAAK,IAAI,UAwDpE"}
+93
View File
@@ -0,0 +1,93 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeWatchRun = makeWatchRun;
const path = require("path");
const constants = require("./constants");
const servicesHost_1 = require("./servicesHost");
const utils_1 = require("./utils");
/**
* Make function which will manually update changed files
*/
function makeWatchRun(instance, loader) {
// Called Before starting compilation after watch
const lastTimes = new Map();
const startTime = 0;
// Save the loader index.
const loaderIndex = loader.loaderIndex;
return (compiler, callback) => {
var _a, _b, _c, _d, _e, _f;
(_b = (_a = instance.servicesHost) === null || _a === void 0 ? void 0 : _a.clearCache) === null || _b === void 0 ? void 0 : _b.call(_a);
(_d = (_c = instance.watchHost) === null || _c === void 0 ? void 0 : _c.clearCache) === null || _d === void 0 ? void 0 : _d.call(_c);
(_e = instance.moduleResolutionCache) === null || _e === void 0 ? void 0 : _e.clear();
(_f = instance.typeReferenceResolutionCache) === null || _f === void 0 ? void 0 : _f.clear();
const promises = [];
if (instance.loaderOptions.transpileOnly) {
instance.reportTranspileErrors = true;
}
else {
const times = compiler.fileTimestamps;
if (times) {
for (const [filePath, date] of times) {
const key = instance.filePathKeyMapper(filePath);
const lastTime = lastTimes.get(key) || startTime;
if (!date ||
date === 'ignore' ||
(date.timestamp || date.safeTime) <= lastTime) {
continue;
}
lastTimes.set(key, date.timestamp || date.safeTime);
promises.push(updateFile(instance, key, filePath, loader, loaderIndex));
}
// On watch update add all known dts files expect the ones in node_modules
// (skip @types/* and modules with typings)
for (const [key, { fileName }] of instance.files.entries()) {
if (fileName.match(constants.dtsDtsxOrDtsDtsxMapRegex) !== null &&
fileName.match(constants.nodeModules) === null) {
promises.push(updateFile(instance, key, fileName, loader, loaderIndex));
}
}
}
}
// Update all the watched files from solution builder
if (instance.solutionBuilderHost) {
for (const { fileName, } of instance.solutionBuilderHost.watchedFiles.values()) {
instance.solutionBuilderHost.updateSolutionBuilderInputFile(fileName);
}
instance.solutionBuilderHost.clearCache();
}
Promise.all(promises)
.then(() => callback())
.catch(err => callback(err));
};
}
function updateFile(instance, key, filePath, loader, loaderIndex) {
return new Promise((resolve, reject) => {
// When other loaders are specified after ts-loader
// (e.g. `{ test: /\.ts$/, use: ['ts-loader', 'other-loader'] }`),
// manually apply them to TypeScript files.
// Otherwise, files not 'preprocessed' by them may cause complication errors (#1111).
if (loaderIndex + 1 < loader.loaders.length &&
instance.rootFileNames.has(path.normalize(filePath))) {
let request = `!!${path.resolve(__dirname, 'stringify-loader.js')}!`;
for (let i = loaderIndex + 1; i < loader.loaders.length; ++i) {
request += loader.loaders[i].request + '!';
}
request += filePath;
loader.loadModule(request, (err, source) => {
if (err) {
reject(err);
}
else {
const text = JSON.parse(source);
(0, servicesHost_1.updateFileWithText)(instance, key, filePath, () => text);
resolve();
}
});
}
else {
(0, servicesHost_1.updateFileWithText)(instance, key, filePath, nFilePath => (0, utils_1.fsReadFile)(nFilePath) || '');
resolve();
}
});
}
//# sourceMappingURL=watch-run.js.map
+3
View File
@@ -0,0 +1,3 @@
var loader = require('./dist');
module.exports = loader;
+1
View File
@@ -0,0 +1 @@
../semver/bin/semver.js
+15
View File
@@ -0,0 +1,15 @@
The ISC License
Copyright (c) Isaac Z. Schlueter and Contributors
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+664
View File
@@ -0,0 +1,664 @@
semver(1) -- The semantic versioner for npm
===========================================
## Install
```bash
npm install semver
````
## Usage
As a node module:
```js
const semver = require('semver')
semver.valid('1.2.3') // '1.2.3'
semver.valid('a.b.c') // null
semver.clean(' =v1.2.3 ') // '1.2.3'
semver.satisfies('1.2.3', '1.x || >=2.5.0 || 5.0.0 - 7.2.3') // true
semver.gt('1.2.3', '9.8.7') // false
semver.lt('1.2.3', '9.8.7') // true
semver.minVersion('>=1.0.0') // '1.0.0'
semver.valid(semver.coerce('v2')) // '2.0.0'
semver.valid(semver.coerce('42.6.7.9.3-alpha')) // '42.6.7'
```
You can also just load the module for the function that you care about if
you'd like to minimize your footprint.
```js
// load the whole API at once in a single object
const semver = require('semver')
// or just load the bits you need
// all of them listed here, just pick and choose what you want
// classes
const SemVer = require('semver/classes/semver')
const Comparator = require('semver/classes/comparator')
const Range = require('semver/classes/range')
// functions for working with versions
const semverParse = require('semver/functions/parse')
const semverValid = require('semver/functions/valid')
const semverClean = require('semver/functions/clean')
const semverInc = require('semver/functions/inc')
const semverDiff = require('semver/functions/diff')
const semverMajor = require('semver/functions/major')
const semverMinor = require('semver/functions/minor')
const semverPatch = require('semver/functions/patch')
const semverPrerelease = require('semver/functions/prerelease')
const semverCompare = require('semver/functions/compare')
const semverRcompare = require('semver/functions/rcompare')
const semverCompareLoose = require('semver/functions/compare-loose')
const semverCompareBuild = require('semver/functions/compare-build')
const semverSort = require('semver/functions/sort')
const semverRsort = require('semver/functions/rsort')
// low-level comparators between versions
const semverGt = require('semver/functions/gt')
const semverLt = require('semver/functions/lt')
const semverEq = require('semver/functions/eq')
const semverNeq = require('semver/functions/neq')
const semverGte = require('semver/functions/gte')
const semverLte = require('semver/functions/lte')
const semverCmp = require('semver/functions/cmp')
const semverCoerce = require('semver/functions/coerce')
// working with ranges
const semverSatisfies = require('semver/functions/satisfies')
const semverMaxSatisfying = require('semver/ranges/max-satisfying')
const semverMinSatisfying = require('semver/ranges/min-satisfying')
const semverToComparators = require('semver/ranges/to-comparators')
const semverMinVersion = require('semver/ranges/min-version')
const semverValidRange = require('semver/ranges/valid')
const semverOutside = require('semver/ranges/outside')
const semverGtr = require('semver/ranges/gtr')
const semverLtr = require('semver/ranges/ltr')
const semverIntersects = require('semver/ranges/intersects')
const semverSimplifyRange = require('semver/ranges/simplify')
const semverRangeSubset = require('semver/ranges/subset')
```
As a command-line utility:
```
$ semver -h
A JavaScript implementation of the https://semver.org/ specification
Copyright Isaac Z. Schlueter
Usage: semver [options] <version> [<version> [...]]
Prints valid versions sorted by SemVer precedence
Options:
-r --range <range>
Print versions that match the specified range.
-i --increment [<level>]
Increment a version by the specified level. Level can
be one of: major, minor, patch, premajor, preminor,
prepatch, prerelease, or release. Default level is 'patch'.
Only one version may be specified.
--preid <identifier>
Identifier to be used to prefix premajor, preminor,
prepatch or prerelease version increments.
-l --loose
Interpret versions and ranges loosely
-n <0|1>
This is the base to be used for the prerelease identifier.
-p --include-prerelease
Always include prerelease versions in range matching
-c --coerce
Coerce a string into SemVer if possible
(does not imply --loose)
--rtl
Coerce version strings right to left
--ltr
Coerce version strings left to right (default)
Program exits successfully if any valid version satisfies
all supplied ranges, and prints all satisfying versions.
If no satisfying versions are found, then exits failure.
Versions are printed in ascending order, so supplying
multiple versions to the utility will just sort them.
```
## Versions
A "version" is described by the `v2.0.0` specification found at
<https://semver.org/>.
A leading `"="` or `"v"` character is stripped off and ignored.
Support for stripping a leading "v" is kept for compatibility with `v1.0.0` of the SemVer
specification but should not be used anymore.
## Ranges
A `version range` is a set of `comparators` that specify versions
that satisfy the range.
A `comparator` is composed of an `operator` and a `version`. The set
of primitive `operators` is:
* `<` Less than
* `<=` Less than or equal to
* `>` Greater than
* `>=` Greater than or equal to
* `=` Equal. If no operator is specified, then equality is assumed,
so this operator is optional but MAY be included.
For example, the comparator `>=1.2.7` would match the versions
`1.2.7`, `1.2.8`, `2.5.3`, and `1.3.9`, but not the versions `1.2.6`
or `1.1.0`. The comparator `>1` is equivalent to `>=2.0.0` and
would match the versions `2.0.0` and `3.1.0`, but not the versions
`1.0.1` or `1.1.0`.
Comparators can be joined by whitespace to form a `comparator set`,
which is satisfied by the **intersection** of all of the comparators
it includes.
A range is composed of one or more comparator sets, joined by `||`. A
version matches a range if and only if every comparator in at least
one of the `||`-separated comparator sets is satisfied by the version.
For example, the range `>=1.2.7 <1.3.0` would match the versions
`1.2.7`, `1.2.8`, and `1.2.99`, but not the versions `1.2.6`, `1.3.0`,
or `1.1.0`.
The range `1.2.7 || >=1.2.9 <2.0.0` would match the versions `1.2.7`,
`1.2.9`, and `1.4.6`, but not the versions `1.2.8` or `2.0.0`.
### Prerelease Tags
If a version has a prerelease tag (for example, `1.2.3-alpha.3`) then
it will only be allowed to satisfy comparator sets if at least one
comparator with the same `[major, minor, patch]` tuple also has a
prerelease tag.
For example, the range `>1.2.3-alpha.3` would be allowed to match the
version `1.2.3-alpha.7`, but it would *not* be satisfied by
`3.4.5-alpha.9`, even though `3.4.5-alpha.9` is technically "greater
than" `1.2.3-alpha.3` according to the SemVer sort rules. The version
range only accepts prerelease tags on the `1.2.3` version.
Version `3.4.5` *would* satisfy the range because it does not have a
prerelease flag, and `3.4.5` is greater than `1.2.3-alpha.7`.
The purpose of this behavior is twofold. First, prerelease versions
frequently are updated very quickly, and contain many breaking changes
that are (by the author's design) not yet fit for public consumption.
Therefore, by default, they are excluded from range-matching
semantics.
Second, a user who has opted into using a prerelease version has
indicated the intent to use *that specific* set of
alpha/beta/rc versions. By including a prerelease tag in the range,
the user is indicating that they are aware of the risk. However, it
is still not appropriate to assume that they have opted into taking a
similar risk on the *next* set of prerelease versions.
Note that this behavior can be suppressed (treating all prerelease
versions as if they were normal versions, for range-matching)
by setting the `includePrerelease` flag on the options
object to any
[functions](https://github.com/npm/node-semver#functions) that do
range matching.
#### Prerelease Identifiers
The method `.inc` takes an additional `identifier` string argument that
will append the value of the string as a prerelease identifier:
```javascript
semver.inc('1.2.3', 'prerelease', 'beta')
// '1.2.4-beta.0'
```
command-line example:
```bash
$ semver 1.2.3 -i prerelease --preid beta
1.2.4-beta.0
```
Which then can be used to increment further:
```bash
$ semver 1.2.4-beta.0 -i prerelease
1.2.4-beta.1
```
To get out of the prerelease phase, use the `release` option:
```bash
$ semver 1.2.4-beta.1 -i release
1.2.4
```
#### Prerelease Identifier Base
The method `.inc` takes an optional parameter 'identifierBase' string
that will let you let your prerelease number as zero-based or one-based.
Set to `false` to omit the prerelease number altogether.
If you do not specify this parameter, it will default to zero-based.
```javascript
semver.inc('1.2.3', 'prerelease', 'beta', '1')
// '1.2.4-beta.1'
```
```javascript
semver.inc('1.2.3', 'prerelease', 'beta', false)
// '1.2.4-beta'
```
command-line example:
```bash
$ semver 1.2.3 -i prerelease --preid beta -n 1
1.2.4-beta.1
```
```bash
$ semver 1.2.3 -i prerelease --preid beta -n false
1.2.4-beta
```
### Advanced Range Syntax
Advanced range syntax desugars to primitive comparators in
deterministic ways.
Advanced ranges may be combined in the same way as primitive
comparators using white space or `||`.
#### Hyphen Ranges `X.Y.Z - A.B.C`
Specifies an inclusive set.
* `1.2.3 - 2.3.4` := `>=1.2.3 <=2.3.4`
If a partial version is provided as the first version in the inclusive
range, then the missing pieces are replaced with zeroes.
* `1.2 - 2.3.4` := `>=1.2.0 <=2.3.4`
If a partial version is provided as the second version in the
inclusive range, then all versions that start with the supplied parts
of the tuple are accepted, but nothing that would be greater than the
provided tuple parts.
* `1.2.3 - 2.3` := `>=1.2.3 <2.4.0-0`
* `1.2.3 - 2` := `>=1.2.3 <3.0.0-0`
#### X-Ranges `1.2.x` `1.X` `1.2.*` `*`
Any of `X`, `x`, or `*` may be used to "stand in" for one of the
numeric values in the `[major, minor, patch]` tuple.
* `*` := `>=0.0.0` (Any non-prerelease version satisfies, unless
`includePrerelease` is specified, in which case any version at all
satisfies)
* `1.x` := `>=1.0.0 <2.0.0-0` (Matching major version)
* `1.2.x` := `>=1.2.0 <1.3.0-0` (Matching major and minor versions)
A partial version range is treated as an X-Range, so the special
character is in fact optional.
* `""` (empty string) := `*` := `>=0.0.0`
* `1` := `1.x.x` := `>=1.0.0 <2.0.0-0`
* `1.2` := `1.2.x` := `>=1.2.0 <1.3.0-0`
#### Tilde Ranges `~1.2.3` `~1.2` `~1`
Allows patch-level changes if a minor version is specified on the
comparator. Allows minor-level changes if not.
* `~1.2.3` := `>=1.2.3 <1.(2+1).0` := `>=1.2.3 <1.3.0-0`
* `~1.2` := `>=1.2.0 <1.(2+1).0` := `>=1.2.0 <1.3.0-0` (Same as `1.2.x`)
* `~1` := `>=1.0.0 <(1+1).0.0` := `>=1.0.0 <2.0.0-0` (Same as `1.x`)
* `~0.2.3` := `>=0.2.3 <0.(2+1).0` := `>=0.2.3 <0.3.0-0`
* `~0.2` := `>=0.2.0 <0.(2+1).0` := `>=0.2.0 <0.3.0-0` (Same as `0.2.x`)
* `~0` := `>=0.0.0 <(0+1).0.0` := `>=0.0.0 <1.0.0-0` (Same as `0.x`)
* `~1.2.3-beta.2` := `>=1.2.3-beta.2 <1.3.0-0` Note that prereleases in
the `1.2.3` version will be allowed, if they are greater than or
equal to `beta.2`. So, `1.2.3-beta.4` would be allowed, but
`1.2.4-beta.2` would not, because it is a prerelease of a
different `[major, minor, patch]` tuple.
#### Caret Ranges `^1.2.3` `^0.2.5` `^0.0.4`
Allows changes that do not modify the left-most non-zero element in the
`[major, minor, patch]` tuple. In other words, this allows patch and
minor updates for versions `1.0.0` and above, patch updates for
versions `0.X >=0.1.0`, and *no* updates for versions `0.0.X`.
Many authors treat a `0.x` version as if the `x` were the major
"breaking-change" indicator.
Caret ranges are ideal when an author may make breaking changes
between `0.2.4` and `0.3.0` releases, which is a common practice.
However, it presumes that there will *not* be breaking changes between
`0.2.4` and `0.2.5`. It allows for changes that are presumed to be
additive (but non-breaking), according to commonly observed practices.
* `^1.2.3` := `>=1.2.3 <2.0.0-0`
* `^0.2.3` := `>=0.2.3 <0.3.0-0`
* `^0.0.3` := `>=0.0.3 <0.0.4-0`
* `^1.2.3-beta.2` := `>=1.2.3-beta.2 <2.0.0-0` Note that prereleases in
the `1.2.3` version will be allowed, if they are greater than or
equal to `beta.2`. So, `1.2.3-beta.4` would be allowed, but
`1.2.4-beta.2` would not, because it is a prerelease of a
different `[major, minor, patch]` tuple.
* `^0.0.3-beta` := `>=0.0.3-beta <0.0.4-0` Note that prereleases in the
`0.0.3` version *only* will be allowed, if they are greater than or
equal to `beta`. So, `0.0.3-pr.2` would be allowed.
When parsing caret ranges, a missing `patch` value desugars to the
number `0`, but will allow flexibility within that value, even if the
major and minor versions are both `0`.
* `^1.2.x` := `>=1.2.0 <2.0.0-0`
* `^0.0.x` := `>=0.0.0 <0.1.0-0`
* `^0.0` := `>=0.0.0 <0.1.0-0`
A missing `minor` and `patch` values will desugar to zero, but also
allow flexibility within those values, even if the major version is
zero.
* `^1.x` := `>=1.0.0 <2.0.0-0`
* `^0.x` := `>=0.0.0 <1.0.0-0`
### Range Grammar
Putting all this together, here is a Backus-Naur grammar for ranges,
for the benefit of parser authors:
```bnf
range-set ::= range ( logical-or range ) *
logical-or ::= ( ' ' ) * '||' ( ' ' ) *
range ::= hyphen | simple ( ' ' simple ) * | ''
hyphen ::= partial ' - ' partial
simple ::= primitive | partial | tilde | caret
primitive ::= ( '<' | '>' | '>=' | '<=' | '=' ) partial
partial ::= xr ( '.' xr ( '.' xr qualifier ? )? )?
xr ::= 'x' | 'X' | '*' | nr
nr ::= '0' | ['1'-'9'] ( ['0'-'9'] ) *
tilde ::= '~' partial
caret ::= '^' partial
qualifier ::= ( '-' pre )? ( '+' build )?
pre ::= parts
build ::= parts
parts ::= part ( '.' part ) *
part ::= nr | [-0-9A-Za-z]+
```
## Functions
All methods and classes take a final `options` object argument. All
options in this object are `false` by default. The options supported
are:
- `loose`: Be more forgiving about not-quite-valid semver strings.
(Any resulting output will always be 100% strict compliant, of
course.) For backwards compatibility reasons, if the `options`
argument is a boolean value instead of an object, it is interpreted
to be the `loose` param.
- `includePrerelease`: Set to suppress the [default
behavior](https://github.com/npm/node-semver#prerelease-tags) of
excluding prerelease tagged versions from ranges unless they are
explicitly opted into.
Strict-mode Comparators and Ranges will be strict about the SemVer
strings that they parse.
* `valid(v)`: Return the parsed version, or null if it's not valid.
* `inc(v, releaseType, options, identifier, identifierBase)`:
Return the version incremented by the release
type (`major`, `premajor`, `minor`, `preminor`, `patch`,
`prepatch`, `prerelease`, or `release`), or null if it's not valid
* `premajor` in one call will bump the version up to the next major
version and down to a prerelease of that major version.
`preminor`, and `prepatch` work the same way.
* If called from a non-prerelease version, `prerelease` will work the
same as `prepatch`. It increments the patch version and then makes a
prerelease. If the input version is already a prerelease it simply
increments it.
* `release` will remove any prerelease part of the version.
* `identifier` can be used to prefix `premajor`, `preminor`,
`prepatch`, or `prerelease` version increments. `identifierBase`
is the base to be used for the `prerelease` identifier.
* `prerelease(v)`: Returns an array of prerelease components, or null
if none exist. Example: `prerelease('1.2.3-alpha.1') -> ['alpha', 1]`
* `major(v)`: Return the major version number.
* `minor(v)`: Return the minor version number.
* `patch(v)`: Return the patch version number.
* `intersects(r1, r2, loose)`: Return true if the two supplied ranges
or comparators intersect.
* `parse(v)`: Attempt to parse a string as a semantic version, returning either
a `SemVer` object or `null`.
### Comparison
* `gt(v1, v2)`: `v1 > v2`
* `gte(v1, v2)`: `v1 >= v2`
* `lt(v1, v2)`: `v1 < v2`
* `lte(v1, v2)`: `v1 <= v2`
* `eq(v1, v2)`: `v1 == v2` This is true if they're logically equivalent,
even if they're not the same string. You already know how to
compare strings.
* `neq(v1, v2)`: `v1 != v2` The opposite of `eq`.
* `cmp(v1, comparator, v2)`: Pass in a comparison string, and it'll call
the corresponding function above. `"==="` and `"!=="` do simple
string comparison, but are included for completeness. Throws if an
invalid comparison string is provided.
* `compare(v1, v2)`: Return `0` if `v1 == v2`, or `1` if `v1` is greater, or `-1` if
`v2` is greater. Sorts in ascending order if passed to `Array.sort()`.
* `rcompare(v1, v2)`: The reverse of `compare`. Sorts an array of versions
in descending order when passed to `Array.sort()`.
* `compareBuild(v1, v2)`: The same as `compare` but considers `build` when two versions
are equal. Sorts in ascending order if passed to `Array.sort()`.
* `compareLoose(v1, v2)`: Short for `compare(v1, v2, { loose: true })`.
* `diff(v1, v2)`: Returns the difference between two versions by the release type
(`major`, `premajor`, `minor`, `preminor`, `patch`, `prepatch`, or `prerelease`),
or null if the versions are the same.
### Sorting
* `sort(versions)`: Returns a sorted array of versions based on the `compareBuild`
function.
* `rsort(versions)`: The reverse of `sort`. Returns an array of versions based on
the `compareBuild` function in descending order.
### Comparators
* `intersects(comparator)`: Return true if the comparators intersect
### Ranges
* `validRange(range)`: Return the valid range or null if it's not valid.
* `satisfies(version, range)`: Return true if the version satisfies the
range.
* `maxSatisfying(versions, range)`: Return the highest version in the list
that satisfies the range, or `null` if none of them do.
* `minSatisfying(versions, range)`: Return the lowest version in the list
that satisfies the range, or `null` if none of them do.
* `minVersion(range)`: Return the lowest version that can match
the given range.
* `gtr(version, range)`: Return `true` if the version is greater than all the
versions possible in the range.
* `ltr(version, range)`: Return `true` if the version is less than all the
versions possible in the range.
* `outside(version, range, hilo)`: Return true if the version is outside
the bounds of the range in either the high or low direction. The
`hilo` argument must be either the string `'>'` or `'<'`. (This is
the function called by `gtr` and `ltr`.)
* `intersects(range)`: Return true if any of the range comparators intersect.
* `simplifyRange(versions, range)`: Return a "simplified" range that
matches the same items in the `versions` list as the range specified. Note
that it does *not* guarantee that it would match the same versions in all
cases, only for the set of versions provided. This is useful when
generating ranges by joining together multiple versions with `||`
programmatically, to provide the user with something a bit more
ergonomic. If the provided range is shorter in string-length than the
generated range, then that is returned.
* `subset(subRange, superRange)`: Return `true` if the `subRange` range is
entirely contained by the `superRange` range.
Note that, since ranges may be non-contiguous, a version might not be
greater than a range, less than a range, *or* satisfy a range! For
example, the range `1.2 <1.2.9 || >2.0.0` would have a hole from `1.2.9`
until `2.0.0`, so version `1.2.10` would not be greater than the
range (because `2.0.1` satisfies, which is higher), nor less than the
range (since `1.2.8` satisfies, which is lower), and it also does not
satisfy the range.
If you want to know if a version satisfies or does not satisfy a
range, use the `satisfies(version, range)` function.
### Coercion
* `coerce(version, options)`: Coerces a string to semver if possible
This aims to provide a very forgiving translation of a non-semver string to
semver. It looks for the first digit in a string and consumes all
remaining characters which satisfy at least a partial semver (e.g., `1`,
`1.2`, `1.2.3`) up to the max permitted length (256 characters). Longer
versions are simply truncated (`4.6.3.9.2-alpha2` becomes `4.6.3`). All
surrounding text is simply ignored (`v3.4 replaces v3.3.1` becomes
`3.4.0`). Only text which lacks digits will fail coercion (`version one`
is not valid). The maximum length for any semver component considered for
coercion is 16 characters; longer components will be ignored
(`10000000000000000.4.7.4` becomes `4.7.4`). The maximum value for any
semver component is `Number.MAX_SAFE_INTEGER || (2**53 - 1)`; higher value
components are invalid (`9999999999999999.4.7.4` is likely invalid).
If the `options.rtl` flag is set, then `coerce` will return the right-most
coercible tuple that does not share an ending index with a longer coercible
tuple. For example, `1.2.3.4` will return `2.3.4` in rtl mode, not
`4.0.0`. `1.2.3/4` will return `4.0.0`, because the `4` is not a part of
any other overlapping SemVer tuple.
If the `options.includePrerelease` flag is set, then the `coerce` result will contain
prerelease and build parts of a version. For example, `1.2.3.4-rc.1+rev.2`
will preserve prerelease `rc.1` and build `rev.2` in the result.
### Clean
* `clean(version)`: Clean a string to be a valid semver if possible
This will return a cleaned and trimmed semver version. If the provided
version is not valid a null will be returned. This does not work for
ranges.
ex.
* `s.clean(' = v 2.1.5foo')`: `null`
* `s.clean(' = v 2.1.5foo', { loose: true })`: `'2.1.5-foo'`
* `s.clean(' = v 2.1.5-foo')`: `null`
* `s.clean(' = v 2.1.5-foo', { loose: true })`: `'2.1.5-foo'`
* `s.clean('=v2.1.5')`: `'2.1.5'`
* `s.clean(' =v2.1.5')`: `'2.1.5'`
* `s.clean(' 2.1.5 ')`: `'2.1.5'`
* `s.clean('~1.0.0')`: `null`
## Constants
As a convenience, helper constants are exported to provide information about what `node-semver` supports:
### `RELEASE_TYPES`
- major
- premajor
- minor
- preminor
- patch
- prepatch
- prerelease
```
const semver = require('semver');
if (semver.RELEASE_TYPES.includes(arbitraryUserInput)) {
console.log('This is a valid release type!');
} else {
console.warn('This is NOT a valid release type!');
}
```
### `SEMVER_SPEC_VERSION`
2.0.0
```
const semver = require('semver');
console.log('We are currently using the semver specification version:', semver.SEMVER_SPEC_VERSION);
```
## Exported Modules
<!--
TODO: Make sure that all of these items are documented (classes aren't,
eg), and then pull the module name into the documentation for that specific
thing.
-->
You may pull in just the part of this semver utility that you need if you
are sensitive to packing and tree-shaking concerns. The main
`require('semver')` export uses getter functions to lazily load the parts
of the API that are used.
The following modules are available:
* `require('semver')`
* `require('semver/classes')`
* `require('semver/classes/comparator')`
* `require('semver/classes/range')`
* `require('semver/classes/semver')`
* `require('semver/functions/clean')`
* `require('semver/functions/cmp')`
* `require('semver/functions/coerce')`
* `require('semver/functions/compare')`
* `require('semver/functions/compare-build')`
* `require('semver/functions/compare-loose')`
* `require('semver/functions/diff')`
* `require('semver/functions/eq')`
* `require('semver/functions/gt')`
* `require('semver/functions/gte')`
* `require('semver/functions/inc')`
* `require('semver/functions/lt')`
* `require('semver/functions/lte')`
* `require('semver/functions/major')`
* `require('semver/functions/minor')`
* `require('semver/functions/neq')`
* `require('semver/functions/parse')`
* `require('semver/functions/patch')`
* `require('semver/functions/prerelease')`
* `require('semver/functions/rcompare')`
* `require('semver/functions/rsort')`
* `require('semver/functions/satisfies')`
* `require('semver/functions/sort')`
* `require('semver/functions/valid')`
* `require('semver/ranges/gtr')`
* `require('semver/ranges/intersects')`
* `require('semver/ranges/ltr')`
* `require('semver/ranges/max-satisfying')`
* `require('semver/ranges/min-satisfying')`
* `require('semver/ranges/min-version')`
* `require('semver/ranges/outside')`
* `require('semver/ranges/simplify')`
* `require('semver/ranges/subset')`
* `require('semver/ranges/to-comparators')`
* `require('semver/ranges/valid')`
+189
View File
@@ -0,0 +1,189 @@
#!/usr/bin/env node
// Standalone semver comparison program.
// Exits successfully and prints matching version(s) if
// any supplied version is valid and passes all tests.
const argv = process.argv.slice(2)
let versions = []
const range = []
let inc = null
const version = require('../package.json').version
let loose = false
let includePrerelease = false
let coerce = false
let rtl = false
let identifier
let identifierBase
const semver = require('../')
const parseOptions = require('../internal/parse-options')
let reverse = false
let options = {}
const main = () => {
if (!argv.length) {
return help()
}
while (argv.length) {
let a = argv.shift()
const indexOfEqualSign = a.indexOf('=')
if (indexOfEqualSign !== -1) {
const value = a.slice(indexOfEqualSign + 1)
a = a.slice(0, indexOfEqualSign)
argv.unshift(value)
}
switch (a) {
case '-rv': case '-rev': case '--rev': case '--reverse':
reverse = true
break
case '-l': case '--loose':
loose = true
break
case '-p': case '--include-prerelease':
includePrerelease = true
break
case '-v': case '--version':
versions.push(argv.shift())
break
case '-i': case '--inc': case '--increment':
switch (argv[0]) {
case 'major': case 'minor': case 'patch': case 'prerelease':
case 'premajor': case 'preminor': case 'prepatch':
case 'release':
inc = argv.shift()
break
default:
inc = 'patch'
break
}
break
case '--preid':
identifier = argv.shift()
break
case '-r': case '--range':
range.push(argv.shift())
break
case '-n':
identifierBase = argv.shift()
if (identifierBase === 'false') {
identifierBase = false
}
break
case '-c': case '--coerce':
coerce = true
break
case '--rtl':
rtl = true
break
case '--ltr':
rtl = false
break
case '-h': case '--help': case '-?':
return help()
default:
versions.push(a)
break
}
}
options = parseOptions({ loose, includePrerelease, rtl })
versions = versions.map((v) => {
return coerce ? (semver.coerce(v, options) || { version: v }).version : v
}).filter((v) => {
return semver.valid(v)
})
if (!versions.length) {
return fail()
}
if (inc && (versions.length !== 1 || range.length)) {
return failInc()
}
for (let i = 0, l = range.length; i < l; i++) {
versions = versions.filter((v) => {
return semver.satisfies(v, range[i], options)
})
if (!versions.length) {
return fail()
}
}
versions
.sort((a, b) => semver[reverse ? 'rcompare' : 'compare'](a, b, options))
.map(v => semver.clean(v, options))
.map(v => inc ? semver.inc(v, inc, options, identifier, identifierBase) : v)
.forEach(v => console.log(v))
}
const failInc = () => {
console.error('--inc can only be used on a single version with no range')
fail()
}
const fail = () => process.exit(1)
const help = () => console.log(
`SemVer ${version}
A JavaScript implementation of the https://semver.org/ specification
Copyright Isaac Z. Schlueter
Usage: semver [options] <version> [<version> [...]]
Prints valid versions sorted by SemVer precedence
Options:
-r --range <range>
Print versions that match the specified range.
-i --increment [<level>]
Increment a version by the specified level. Level can
be one of: major, minor, patch, premajor, preminor,
prepatch, prerelease, or release. Default level is 'patch'.
Only one version may be specified.
--preid <identifier>
Identifier to be used to prefix premajor, preminor,
prepatch or prerelease version increments.
-l --loose
Interpret versions and ranges loosely
-p --include-prerelease
Always include prerelease versions in range matching
-c --coerce
Coerce a string into SemVer if possible
(does not imply --loose)
--rtl
Coerce version strings right to left
--ltr
Coerce version strings left to right (default)
-n <base>
Base number to be used for the prerelease identifier.
Can be either 0 or 1, or false to omit the number altogether.
Defaults to 0.
Program exits successfully if any valid version satisfies
all supplied ranges, and prints all satisfying versions.
If no satisfying versions are found, then exits failure.
Versions are printed in ascending order, so supplying
multiple versions to the utility will just sort them.`)
main()
+141
View File
@@ -0,0 +1,141 @@
const ANY = Symbol('SemVer ANY')
// hoisted class for cyclic dependency
class Comparator {
static get ANY () {
return ANY
}
constructor (comp, options) {
options = parseOptions(options)
if (comp instanceof Comparator) {
if (comp.loose === !!options.loose) {
return comp
} else {
comp = comp.value
}
}
comp = comp.trim().split(/\s+/).join(' ')
debug('comparator', comp, options)
this.options = options
this.loose = !!options.loose
this.parse(comp)
if (this.semver === ANY) {
this.value = ''
} else {
this.value = this.operator + this.semver.version
}
debug('comp', this)
}
parse (comp) {
const r = this.options.loose ? re[t.COMPARATORLOOSE] : re[t.COMPARATOR]
const m = comp.match(r)
if (!m) {
throw new TypeError(`Invalid comparator: ${comp}`)
}
this.operator = m[1] !== undefined ? m[1] : ''
if (this.operator === '=') {
this.operator = ''
}
// if it literally is just '>' or '' then allow anything.
if (!m[2]) {
this.semver = ANY
} else {
this.semver = new SemVer(m[2], this.options.loose)
}
}
toString () {
return this.value
}
test (version) {
debug('Comparator.test', version, this.options.loose)
if (this.semver === ANY || version === ANY) {
return true
}
if (typeof version === 'string') {
try {
version = new SemVer(version, this.options)
} catch (er) {
return false
}
}
return cmp(version, this.operator, this.semver, this.options)
}
intersects (comp, options) {
if (!(comp instanceof Comparator)) {
throw new TypeError('a Comparator is required')
}
if (this.operator === '') {
if (this.value === '') {
return true
}
return new Range(comp.value, options).test(this.value)
} else if (comp.operator === '') {
if (comp.value === '') {
return true
}
return new Range(this.value, options).test(comp.semver)
}
options = parseOptions(options)
// Special cases where nothing can possibly be lower
if (options.includePrerelease &&
(this.value === '<0.0.0-0' || comp.value === '<0.0.0-0')) {
return false
}
if (!options.includePrerelease &&
(this.value.startsWith('<0.0.0') || comp.value.startsWith('<0.0.0'))) {
return false
}
// Same direction increasing (> or >=)
if (this.operator.startsWith('>') && comp.operator.startsWith('>')) {
return true
}
// Same direction decreasing (< or <=)
if (this.operator.startsWith('<') && comp.operator.startsWith('<')) {
return true
}
// same SemVer and both sides are inclusive (<= or >=)
if (
(this.semver.version === comp.semver.version) &&
this.operator.includes('=') && comp.operator.includes('=')) {
return true
}
// opposite directions less than
if (cmp(this.semver, '<', comp.semver, options) &&
this.operator.startsWith('>') && comp.operator.startsWith('<')) {
return true
}
// opposite directions greater than
if (cmp(this.semver, '>', comp.semver, options) &&
this.operator.startsWith('<') && comp.operator.startsWith('>')) {
return true
}
return false
}
}
module.exports = Comparator
const parseOptions = require('../internal/parse-options')
const { safeRe: re, t } = require('../internal/re')
const cmp = require('../functions/cmp')
const debug = require('../internal/debug')
const SemVer = require('./semver')
const Range = require('./range')
+5
View File
@@ -0,0 +1,5 @@
module.exports = {
SemVer: require('./semver.js'),
Range: require('./range.js'),
Comparator: require('./comparator.js'),
}
+554
View File
@@ -0,0 +1,554 @@
const SPACE_CHARACTERS = /\s+/g
// hoisted class for cyclic dependency
class Range {
constructor (range, options) {
options = parseOptions(options)
if (range instanceof Range) {
if (
range.loose === !!options.loose &&
range.includePrerelease === !!options.includePrerelease
) {
return range
} else {
return new Range(range.raw, options)
}
}
if (range instanceof Comparator) {
// just put it in the set and return
this.raw = range.value
this.set = [[range]]
this.formatted = undefined
return this
}
this.options = options
this.loose = !!options.loose
this.includePrerelease = !!options.includePrerelease
// First reduce all whitespace as much as possible so we do not have to rely
// on potentially slow regexes like \s*. This is then stored and used for
// future error messages as well.
this.raw = range.trim().replace(SPACE_CHARACTERS, ' ')
// First, split on ||
this.set = this.raw
.split('||')
// map the range to a 2d array of comparators
.map(r => this.parseRange(r.trim()))
// throw out any comparator lists that are empty
// this generally means that it was not a valid range, which is allowed
// in loose mode, but will still throw if the WHOLE range is invalid.
.filter(c => c.length)
if (!this.set.length) {
throw new TypeError(`Invalid SemVer Range: ${this.raw}`)
}
// if we have any that are not the null set, throw out null sets.
if (this.set.length > 1) {
// keep the first one, in case they're all null sets
const first = this.set[0]
this.set = this.set.filter(c => !isNullSet(c[0]))
if (this.set.length === 0) {
this.set = [first]
} else if (this.set.length > 1) {
// if we have any that are *, then the range is just *
for (const c of this.set) {
if (c.length === 1 && isAny(c[0])) {
this.set = [c]
break
}
}
}
}
this.formatted = undefined
}
get range () {
if (this.formatted === undefined) {
this.formatted = ''
for (let i = 0; i < this.set.length; i++) {
if (i > 0) {
this.formatted += '||'
}
const comps = this.set[i]
for (let k = 0; k < comps.length; k++) {
if (k > 0) {
this.formatted += ' '
}
this.formatted += comps[k].toString().trim()
}
}
}
return this.formatted
}
format () {
return this.range
}
toString () {
return this.range
}
parseRange (range) {
// memoize range parsing for performance.
// this is a very hot path, and fully deterministic.
const memoOpts =
(this.options.includePrerelease && FLAG_INCLUDE_PRERELEASE) |
(this.options.loose && FLAG_LOOSE)
const memoKey = memoOpts + ':' + range
const cached = cache.get(memoKey)
if (cached) {
return cached
}
const loose = this.options.loose
// `1.2.3 - 1.2.4` => `>=1.2.3 <=1.2.4`
const hr = loose ? re[t.HYPHENRANGELOOSE] : re[t.HYPHENRANGE]
range = range.replace(hr, hyphenReplace(this.options.includePrerelease))
debug('hyphen replace', range)
// `> 1.2.3 < 1.2.5` => `>1.2.3 <1.2.5`
range = range.replace(re[t.COMPARATORTRIM], comparatorTrimReplace)
debug('comparator trim', range)
// `~ 1.2.3` => `~1.2.3`
range = range.replace(re[t.TILDETRIM], tildeTrimReplace)
debug('tilde trim', range)
// `^ 1.2.3` => `^1.2.3`
range = range.replace(re[t.CARETTRIM], caretTrimReplace)
debug('caret trim', range)
// At this point, the range is completely trimmed and
// ready to be split into comparators.
let rangeList = range
.split(' ')
.map(comp => parseComparator(comp, this.options))
.join(' ')
.split(/\s+/)
// >=0.0.0 is equivalent to *
.map(comp => replaceGTE0(comp, this.options))
if (loose) {
// in loose mode, throw out any that are not valid comparators
rangeList = rangeList.filter(comp => {
debug('loose invalid filter', comp, this.options)
return !!comp.match(re[t.COMPARATORLOOSE])
})
}
debug('range list', rangeList)
// if any comparators are the null set, then replace with JUST null set
// if more than one comparator, remove any * comparators
// also, don't include the same comparator more than once
const rangeMap = new Map()
const comparators = rangeList.map(comp => new Comparator(comp, this.options))
for (const comp of comparators) {
if (isNullSet(comp)) {
return [comp]
}
rangeMap.set(comp.value, comp)
}
if (rangeMap.size > 1 && rangeMap.has('')) {
rangeMap.delete('')
}
const result = [...rangeMap.values()]
cache.set(memoKey, result)
return result
}
intersects (range, options) {
if (!(range instanceof Range)) {
throw new TypeError('a Range is required')
}
return this.set.some((thisComparators) => {
return (
isSatisfiable(thisComparators, options) &&
range.set.some((rangeComparators) => {
return (
isSatisfiable(rangeComparators, options) &&
thisComparators.every((thisComparator) => {
return rangeComparators.every((rangeComparator) => {
return thisComparator.intersects(rangeComparator, options)
})
})
)
})
)
})
}
// if ANY of the sets match ALL of its comparators, then pass
test (version) {
if (!version) {
return false
}
if (typeof version === 'string') {
try {
version = new SemVer(version, this.options)
} catch (er) {
return false
}
}
for (let i = 0; i < this.set.length; i++) {
if (testSet(this.set[i], version, this.options)) {
return true
}
}
return false
}
}
module.exports = Range
const LRU = require('../internal/lrucache')
const cache = new LRU()
const parseOptions = require('../internal/parse-options')
const Comparator = require('./comparator')
const debug = require('../internal/debug')
const SemVer = require('./semver')
const {
safeRe: re,
t,
comparatorTrimReplace,
tildeTrimReplace,
caretTrimReplace,
} = require('../internal/re')
const { FLAG_INCLUDE_PRERELEASE, FLAG_LOOSE } = require('../internal/constants')
const isNullSet = c => c.value === '<0.0.0-0'
const isAny = c => c.value === ''
// take a set of comparators and determine whether there
// exists a version which can satisfy it
const isSatisfiable = (comparators, options) => {
let result = true
const remainingComparators = comparators.slice()
let testComparator = remainingComparators.pop()
while (result && remainingComparators.length) {
result = remainingComparators.every((otherComparator) => {
return testComparator.intersects(otherComparator, options)
})
testComparator = remainingComparators.pop()
}
return result
}
// comprised of xranges, tildes, stars, and gtlt's at this point.
// already replaced the hyphen ranges
// turn into a set of JUST comparators.
const parseComparator = (comp, options) => {
debug('comp', comp, options)
comp = replaceCarets(comp, options)
debug('caret', comp)
comp = replaceTildes(comp, options)
debug('tildes', comp)
comp = replaceXRanges(comp, options)
debug('xrange', comp)
comp = replaceStars(comp, options)
debug('stars', comp)
return comp
}
const isX = id => !id || id.toLowerCase() === 'x' || id === '*'
// ~, ~> --> * (any, kinda silly)
// ~2, ~2.x, ~2.x.x, ~>2, ~>2.x ~>2.x.x --> >=2.0.0 <3.0.0-0
// ~2.0, ~2.0.x, ~>2.0, ~>2.0.x --> >=2.0.0 <2.1.0-0
// ~1.2, ~1.2.x, ~>1.2, ~>1.2.x --> >=1.2.0 <1.3.0-0
// ~1.2.3, ~>1.2.3 --> >=1.2.3 <1.3.0-0
// ~1.2.0, ~>1.2.0 --> >=1.2.0 <1.3.0-0
// ~0.0.1 --> >=0.0.1 <0.1.0-0
const replaceTildes = (comp, options) => {
return comp
.trim()
.split(/\s+/)
.map((c) => replaceTilde(c, options))
.join(' ')
}
const replaceTilde = (comp, options) => {
const r = options.loose ? re[t.TILDELOOSE] : re[t.TILDE]
return comp.replace(r, (_, M, m, p, pr) => {
debug('tilde', comp, _, M, m, p, pr)
let ret
if (isX(M)) {
ret = ''
} else if (isX(m)) {
ret = `>=${M}.0.0 <${+M + 1}.0.0-0`
} else if (isX(p)) {
// ~1.2 == >=1.2.0 <1.3.0-0
ret = `>=${M}.${m}.0 <${M}.${+m + 1}.0-0`
} else if (pr) {
debug('replaceTilde pr', pr)
ret = `>=${M}.${m}.${p}-${pr
} <${M}.${+m + 1}.0-0`
} else {
// ~1.2.3 == >=1.2.3 <1.3.0-0
ret = `>=${M}.${m}.${p
} <${M}.${+m + 1}.0-0`
}
debug('tilde return', ret)
return ret
})
}
// ^ --> * (any, kinda silly)
// ^2, ^2.x, ^2.x.x --> >=2.0.0 <3.0.0-0
// ^2.0, ^2.0.x --> >=2.0.0 <3.0.0-0
// ^1.2, ^1.2.x --> >=1.2.0 <2.0.0-0
// ^1.2.3 --> >=1.2.3 <2.0.0-0
// ^1.2.0 --> >=1.2.0 <2.0.0-0
// ^0.0.1 --> >=0.0.1 <0.0.2-0
// ^0.1.0 --> >=0.1.0 <0.2.0-0
const replaceCarets = (comp, options) => {
return comp
.trim()
.split(/\s+/)
.map((c) => replaceCaret(c, options))
.join(' ')
}
const replaceCaret = (comp, options) => {
debug('caret', comp, options)
const r = options.loose ? re[t.CARETLOOSE] : re[t.CARET]
const z = options.includePrerelease ? '-0' : ''
return comp.replace(r, (_, M, m, p, pr) => {
debug('caret', comp, _, M, m, p, pr)
let ret
if (isX(M)) {
ret = ''
} else if (isX(m)) {
ret = `>=${M}.0.0${z} <${+M + 1}.0.0-0`
} else if (isX(p)) {
if (M === '0') {
ret = `>=${M}.${m}.0${z} <${M}.${+m + 1}.0-0`
} else {
ret = `>=${M}.${m}.0${z} <${+M + 1}.0.0-0`
}
} else if (pr) {
debug('replaceCaret pr', pr)
if (M === '0') {
if (m === '0') {
ret = `>=${M}.${m}.${p}-${pr
} <${M}.${m}.${+p + 1}-0`
} else {
ret = `>=${M}.${m}.${p}-${pr
} <${M}.${+m + 1}.0-0`
}
} else {
ret = `>=${M}.${m}.${p}-${pr
} <${+M + 1}.0.0-0`
}
} else {
debug('no pr')
if (M === '0') {
if (m === '0') {
ret = `>=${M}.${m}.${p
}${z} <${M}.${m}.${+p + 1}-0`
} else {
ret = `>=${M}.${m}.${p
}${z} <${M}.${+m + 1}.0-0`
}
} else {
ret = `>=${M}.${m}.${p
} <${+M + 1}.0.0-0`
}
}
debug('caret return', ret)
return ret
})
}
const replaceXRanges = (comp, options) => {
debug('replaceXRanges', comp, options)
return comp
.split(/\s+/)
.map((c) => replaceXRange(c, options))
.join(' ')
}
const replaceXRange = (comp, options) => {
comp = comp.trim()
const r = options.loose ? re[t.XRANGELOOSE] : re[t.XRANGE]
return comp.replace(r, (ret, gtlt, M, m, p, pr) => {
debug('xRange', comp, ret, gtlt, M, m, p, pr)
const xM = isX(M)
const xm = xM || isX(m)
const xp = xm || isX(p)
const anyX = xp
if (gtlt === '=' && anyX) {
gtlt = ''
}
// if we're including prereleases in the match, then we need
// to fix this to -0, the lowest possible prerelease value
pr = options.includePrerelease ? '-0' : ''
if (xM) {
if (gtlt === '>' || gtlt === '<') {
// nothing is allowed
ret = '<0.0.0-0'
} else {
// nothing is forbidden
ret = '*'
}
} else if (gtlt && anyX) {
// we know patch is an x, because we have any x at all.
// replace X with 0
if (xm) {
m = 0
}
p = 0
if (gtlt === '>') {
// >1 => >=2.0.0
// >1.2 => >=1.3.0
gtlt = '>='
if (xm) {
M = +M + 1
m = 0
p = 0
} else {
m = +m + 1
p = 0
}
} else if (gtlt === '<=') {
// <=0.7.x is actually <0.8.0, since any 0.7.x should
// pass. Similarly, <=7.x is actually <8.0.0, etc.
gtlt = '<'
if (xm) {
M = +M + 1
} else {
m = +m + 1
}
}
if (gtlt === '<') {
pr = '-0'
}
ret = `${gtlt + M}.${m}.${p}${pr}`
} else if (xm) {
ret = `>=${M}.0.0${pr} <${+M + 1}.0.0-0`
} else if (xp) {
ret = `>=${M}.${m}.0${pr
} <${M}.${+m + 1}.0-0`
}
debug('xRange return', ret)
return ret
})
}
// Because * is AND-ed with everything else in the comparator,
// and '' means "any version", just remove the *s entirely.
const replaceStars = (comp, options) => {
debug('replaceStars', comp, options)
// Looseness is ignored here. star is always as loose as it gets!
return comp
.trim()
.replace(re[t.STAR], '')
}
const replaceGTE0 = (comp, options) => {
debug('replaceGTE0', comp, options)
return comp
.trim()
.replace(re[options.includePrerelease ? t.GTE0PRE : t.GTE0], '')
}
// This function is passed to string.replace(re[t.HYPHENRANGE])
// M, m, patch, prerelease, build
// 1.2 - 3.4.5 => >=1.2.0 <=3.4.5
// 1.2.3 - 3.4 => >=1.2.0 <3.5.0-0 Any 3.4.x will do
// 1.2 - 3.4 => >=1.2.0 <3.5.0-0
// TODO build?
const hyphenReplace = incPr => ($0,
from, fM, fm, fp, fpr, fb,
to, tM, tm, tp, tpr) => {
if (isX(fM)) {
from = ''
} else if (isX(fm)) {
from = `>=${fM}.0.0${incPr ? '-0' : ''}`
} else if (isX(fp)) {
from = `>=${fM}.${fm}.0${incPr ? '-0' : ''}`
} else if (fpr) {
from = `>=${from}`
} else {
from = `>=${from}${incPr ? '-0' : ''}`
}
if (isX(tM)) {
to = ''
} else if (isX(tm)) {
to = `<${+tM + 1}.0.0-0`
} else if (isX(tp)) {
to = `<${tM}.${+tm + 1}.0-0`
} else if (tpr) {
to = `<=${tM}.${tm}.${tp}-${tpr}`
} else if (incPr) {
to = `<${tM}.${tm}.${+tp + 1}-0`
} else {
to = `<=${to}`
}
return `${from} ${to}`.trim()
}
const testSet = (set, version, options) => {
for (let i = 0; i < set.length; i++) {
if (!set[i].test(version)) {
return false
}
}
if (version.prerelease.length && !options.includePrerelease) {
// Find the set of versions that are allowed to have prereleases
// For example, ^1.2.3-pr.1 desugars to >=1.2.3-pr.1 <2.0.0
// That should allow `1.2.3-pr.2` to pass.
// However, `1.2.4-alpha.notready` should NOT be allowed,
// even though it's within the range set by the comparators.
for (let i = 0; i < set.length; i++) {
debug(set[i].semver)
if (set[i].semver === Comparator.ANY) {
continue
}
if (set[i].semver.prerelease.length > 0) {
const allowed = set[i].semver
if (allowed.major === version.major &&
allowed.minor === version.minor &&
allowed.patch === version.patch) {
return true
}
}
}
// Version has a -pre, but it's not one of the ones we like.
return false
}
return true
}
+318
View File
@@ -0,0 +1,318 @@
const debug = require('../internal/debug')
const { MAX_LENGTH, MAX_SAFE_INTEGER } = require('../internal/constants')
const { safeRe: re, safeSrc: src, t } = require('../internal/re')
const parseOptions = require('../internal/parse-options')
const { compareIdentifiers } = require('../internal/identifiers')
class SemVer {
constructor (version, options) {
options = parseOptions(options)
if (version instanceof SemVer) {
if (version.loose === !!options.loose &&
version.includePrerelease === !!options.includePrerelease) {
return version
} else {
version = version.version
}
} else if (typeof version !== 'string') {
throw new TypeError(`Invalid version. Must be a string. Got type "${typeof version}".`)
}
if (version.length > MAX_LENGTH) {
throw new TypeError(
`version is longer than ${MAX_LENGTH} characters`
)
}
debug('SemVer', version, options)
this.options = options
this.loose = !!options.loose
// this isn't actually relevant for versions, but keep it so that we
// don't run into trouble passing this.options around.
this.includePrerelease = !!options.includePrerelease
const m = version.trim().match(options.loose ? re[t.LOOSE] : re[t.FULL])
if (!m) {
throw new TypeError(`Invalid Version: ${version}`)
}
this.raw = version
// these are actually numbers
this.major = +m[1]
this.minor = +m[2]
this.patch = +m[3]
if (this.major > MAX_SAFE_INTEGER || this.major < 0) {
throw new TypeError('Invalid major version')
}
if (this.minor > MAX_SAFE_INTEGER || this.minor < 0) {
throw new TypeError('Invalid minor version')
}
if (this.patch > MAX_SAFE_INTEGER || this.patch < 0) {
throw new TypeError('Invalid patch version')
}
// numberify any prerelease numeric ids
if (!m[4]) {
this.prerelease = []
} else {
this.prerelease = m[4].split('.').map((id) => {
if (/^[0-9]+$/.test(id)) {
const num = +id
if (num >= 0 && num < MAX_SAFE_INTEGER) {
return num
}
}
return id
})
}
this.build = m[5] ? m[5].split('.') : []
this.format()
}
format () {
this.version = `${this.major}.${this.minor}.${this.patch}`
if (this.prerelease.length) {
this.version += `-${this.prerelease.join('.')}`
}
return this.version
}
toString () {
return this.version
}
compare (other) {
debug('SemVer.compare', this.version, this.options, other)
if (!(other instanceof SemVer)) {
if (typeof other === 'string' && other === this.version) {
return 0
}
other = new SemVer(other, this.options)
}
if (other.version === this.version) {
return 0
}
return this.compareMain(other) || this.comparePre(other)
}
compareMain (other) {
if (!(other instanceof SemVer)) {
other = new SemVer(other, this.options)
}
return (
compareIdentifiers(this.major, other.major) ||
compareIdentifiers(this.minor, other.minor) ||
compareIdentifiers(this.patch, other.patch)
)
}
comparePre (other) {
if (!(other instanceof SemVer)) {
other = new SemVer(other, this.options)
}
// NOT having a prerelease is > having one
if (this.prerelease.length && !other.prerelease.length) {
return -1
} else if (!this.prerelease.length && other.prerelease.length) {
return 1
} else if (!this.prerelease.length && !other.prerelease.length) {
return 0
}
let i = 0
do {
const a = this.prerelease[i]
const b = other.prerelease[i]
debug('prerelease compare', i, a, b)
if (a === undefined && b === undefined) {
return 0
} else if (b === undefined) {
return 1
} else if (a === undefined) {
return -1
} else if (a === b) {
continue
} else {
return compareIdentifiers(a, b)
}
} while (++i)
}
compareBuild (other) {
if (!(other instanceof SemVer)) {
other = new SemVer(other, this.options)
}
let i = 0
do {
const a = this.build[i]
const b = other.build[i]
debug('build compare', i, a, b)
if (a === undefined && b === undefined) {
return 0
} else if (b === undefined) {
return 1
} else if (a === undefined) {
return -1
} else if (a === b) {
continue
} else {
return compareIdentifiers(a, b)
}
} while (++i)
}
// preminor will bump the version up to the next minor release, and immediately
// down to pre-release. premajor and prepatch work the same way.
inc (release, identifier, identifierBase) {
if (release.startsWith('pre')) {
if (!identifier && identifierBase === false) {
throw new Error('invalid increment argument: identifier is empty')
}
// Avoid an invalid semver results
if (identifier) {
const r = new RegExp(`^${this.options.loose ? src[t.PRERELEASELOOSE] : src[t.PRERELEASE]}$`)
const match = `-${identifier}`.match(r)
if (!match || match[1] !== identifier) {
throw new Error(`invalid identifier: ${identifier}`)
}
}
}
switch (release) {
case 'premajor':
this.prerelease.length = 0
this.patch = 0
this.minor = 0
this.major++
this.inc('pre', identifier, identifierBase)
break
case 'preminor':
this.prerelease.length = 0
this.patch = 0
this.minor++
this.inc('pre', identifier, identifierBase)
break
case 'prepatch':
// If this is already a prerelease, it will bump to the next version
// drop any prereleases that might already exist, since they are not
// relevant at this point.
this.prerelease.length = 0
this.inc('patch', identifier, identifierBase)
this.inc('pre', identifier, identifierBase)
break
// If the input is a non-prerelease version, this acts the same as
// prepatch.
case 'prerelease':
if (this.prerelease.length === 0) {
this.inc('patch', identifier, identifierBase)
}
this.inc('pre', identifier, identifierBase)
break
case 'release':
if (this.prerelease.length === 0) {
throw new Error(`version ${this.raw} is not a prerelease`)
}
this.prerelease.length = 0
break
case 'major':
// If this is a pre-major version, bump up to the same major version.
// Otherwise increment major.
// 1.0.0-5 bumps to 1.0.0
// 1.1.0 bumps to 2.0.0
if (
this.minor !== 0 ||
this.patch !== 0 ||
this.prerelease.length === 0
) {
this.major++
}
this.minor = 0
this.patch = 0
this.prerelease = []
break
case 'minor':
// If this is a pre-minor version, bump up to the same minor version.
// Otherwise increment minor.
// 1.2.0-5 bumps to 1.2.0
// 1.2.1 bumps to 1.3.0
if (this.patch !== 0 || this.prerelease.length === 0) {
this.minor++
}
this.patch = 0
this.prerelease = []
break
case 'patch':
// If this is not a pre-release version, it will increment the patch.
// If it is a pre-release it will bump up to the same patch version.
// 1.2.0-5 patches to 1.2.0
// 1.2.0 patches to 1.2.1
if (this.prerelease.length === 0) {
this.patch++
}
this.prerelease = []
break
// This probably shouldn't be used publicly.
// 1.0.0 'pre' would become 1.0.0-0 which is the wrong direction.
case 'pre': {
const base = Number(identifierBase) ? 1 : 0
if (this.prerelease.length === 0) {
this.prerelease = [base]
} else {
let i = this.prerelease.length
while (--i >= 0) {
if (typeof this.prerelease[i] === 'number') {
this.prerelease[i]++
i = -2
}
}
if (i === -1) {
// didn't increment anything
if (identifier === this.prerelease.join('.') && identifierBase === false) {
throw new Error('invalid increment argument: identifier already exists')
}
this.prerelease.push(base)
}
}
if (identifier) {
// 1.2.0-beta.1 bumps to 1.2.0-beta.2,
// 1.2.0-beta.fooblz or 1.2.0-beta bumps to 1.2.0-beta.0
let prerelease = [identifier, base]
if (identifierBase === false) {
prerelease = [identifier]
}
if (compareIdentifiers(this.prerelease[0], identifier) === 0) {
if (isNaN(this.prerelease[1])) {
this.prerelease = prerelease
}
} else {
this.prerelease = prerelease
}
}
break
}
default:
throw new Error(`invalid increment argument: ${release}`)
}
this.raw = this.format()
if (this.build.length) {
this.raw += `+${this.build.join('.')}`
}
return this
}
}
module.exports = SemVer
+6
View File
@@ -0,0 +1,6 @@
const parse = require('./parse')
const clean = (version, options) => {
const s = parse(version.trim().replace(/^[=v]+/, ''), options)
return s ? s.version : null
}
module.exports = clean
+52
View File
@@ -0,0 +1,52 @@
const eq = require('./eq')
const neq = require('./neq')
const gt = require('./gt')
const gte = require('./gte')
const lt = require('./lt')
const lte = require('./lte')
const cmp = (a, op, b, loose) => {
switch (op) {
case '===':
if (typeof a === 'object') {
a = a.version
}
if (typeof b === 'object') {
b = b.version
}
return a === b
case '!==':
if (typeof a === 'object') {
a = a.version
}
if (typeof b === 'object') {
b = b.version
}
return a !== b
case '':
case '=':
case '==':
return eq(a, b, loose)
case '!=':
return neq(a, b, loose)
case '>':
return gt(a, b, loose)
case '>=':
return gte(a, b, loose)
case '<':
return lt(a, b, loose)
case '<=':
return lte(a, b, loose)
default:
throw new TypeError(`Invalid operator: ${op}`)
}
}
module.exports = cmp
+60
View File
@@ -0,0 +1,60 @@
const SemVer = require('../classes/semver')
const parse = require('./parse')
const { safeRe: re, t } = require('../internal/re')
const coerce = (version, options) => {
if (version instanceof SemVer) {
return version
}
if (typeof version === 'number') {
version = String(version)
}
if (typeof version !== 'string') {
return null
}
options = options || {}
let match = null
if (!options.rtl) {
match = version.match(options.includePrerelease ? re[t.COERCEFULL] : re[t.COERCE])
} else {
// Find the right-most coercible string that does not share
// a terminus with a more left-ward coercible string.
// Eg, '1.2.3.4' wants to coerce '2.3.4', not '3.4' or '4'
// With includePrerelease option set, '1.2.3.4-rc' wants to coerce '2.3.4-rc', not '2.3.4'
//
// Walk through the string checking with a /g regexp
// Manually set the index so as to pick up overlapping matches.
// Stop when we get a match that ends at the string end, since no
// coercible string can be more right-ward without the same terminus.
const coerceRtlRegex = options.includePrerelease ? re[t.COERCERTLFULL] : re[t.COERCERTL]
let next
while ((next = coerceRtlRegex.exec(version)) &&
(!match || match.index + match[0].length !== version.length)
) {
if (!match ||
next.index + next[0].length !== match.index + match[0].length) {
match = next
}
coerceRtlRegex.lastIndex = next.index + next[1].length + next[2].length
}
// leave it in a clean state
coerceRtlRegex.lastIndex = -1
}
if (match === null) {
return null
}
const major = match[2]
const minor = match[3] || '0'
const patch = match[4] || '0'
const prerelease = options.includePrerelease && match[5] ? `-${match[5]}` : ''
const build = options.includePrerelease && match[6] ? `+${match[6]}` : ''
return parse(`${major}.${minor}.${patch}${prerelease}${build}`, options)
}
module.exports = coerce
@@ -0,0 +1,7 @@
const SemVer = require('../classes/semver')
const compareBuild = (a, b, loose) => {
const versionA = new SemVer(a, loose)
const versionB = new SemVer(b, loose)
return versionA.compare(versionB) || versionA.compareBuild(versionB)
}
module.exports = compareBuild
@@ -0,0 +1,3 @@
const compare = require('./compare')
const compareLoose = (a, b) => compare(a, b, true)
module.exports = compareLoose
+5
View File
@@ -0,0 +1,5 @@
const SemVer = require('../classes/semver')
const compare = (a, b, loose) =>
new SemVer(a, loose).compare(new SemVer(b, loose))
module.exports = compare
+58
View File
@@ -0,0 +1,58 @@
const parse = require('./parse.js')
const diff = (version1, version2) => {
const v1 = parse(version1, null, true)
const v2 = parse(version2, null, true)
const comparison = v1.compare(v2)
if (comparison === 0) {
return null
}
const v1Higher = comparison > 0
const highVersion = v1Higher ? v1 : v2
const lowVersion = v1Higher ? v2 : v1
const highHasPre = !!highVersion.prerelease.length
const lowHasPre = !!lowVersion.prerelease.length
if (lowHasPre && !highHasPre) {
// Going from prerelease -> no prerelease requires some special casing
// If the low version has only a major, then it will always be a major
// Some examples:
// 1.0.0-1 -> 1.0.0
// 1.0.0-1 -> 1.1.1
// 1.0.0-1 -> 2.0.0
if (!lowVersion.patch && !lowVersion.minor) {
return 'major'
}
// If the main part has no difference
if (lowVersion.compareMain(highVersion) === 0) {
if (lowVersion.minor && !lowVersion.patch) {
return 'minor'
}
return 'patch'
}
}
// add the `pre` prefix if we are going to a prerelease version
const prefix = highHasPre ? 'pre' : ''
if (v1.major !== v2.major) {
return prefix + 'major'
}
if (v1.minor !== v2.minor) {
return prefix + 'minor'
}
if (v1.patch !== v2.patch) {
return prefix + 'patch'
}
// high and low are preleases
return 'prerelease'
}
module.exports = diff
+3
View File
@@ -0,0 +1,3 @@
const compare = require('./compare')
const eq = (a, b, loose) => compare(a, b, loose) === 0
module.exports = eq
+3
View File
@@ -0,0 +1,3 @@
const compare = require('./compare')
const gt = (a, b, loose) => compare(a, b, loose) > 0
module.exports = gt
+3
View File
@@ -0,0 +1,3 @@
const compare = require('./compare')
const gte = (a, b, loose) => compare(a, b, loose) >= 0
module.exports = gte
+19
View File
@@ -0,0 +1,19 @@
const SemVer = require('../classes/semver')
const inc = (version, release, options, identifier, identifierBase) => {
if (typeof (options) === 'string') {
identifierBase = identifier
identifier = options
options = undefined
}
try {
return new SemVer(
version instanceof SemVer ? version.version : version,
options
).inc(release, identifier, identifierBase).version
} catch (er) {
return null
}
}
module.exports = inc
+3
View File
@@ -0,0 +1,3 @@
const compare = require('./compare')
const lt = (a, b, loose) => compare(a, b, loose) < 0
module.exports = lt
+3
View File
@@ -0,0 +1,3 @@
const compare = require('./compare')
const lte = (a, b, loose) => compare(a, b, loose) <= 0
module.exports = lte
+3
View File
@@ -0,0 +1,3 @@
const SemVer = require('../classes/semver')
const major = (a, loose) => new SemVer(a, loose).major
module.exports = major
+3
View File
@@ -0,0 +1,3 @@
const SemVer = require('../classes/semver')
const minor = (a, loose) => new SemVer(a, loose).minor
module.exports = minor
+3
View File
@@ -0,0 +1,3 @@
const compare = require('./compare')
const neq = (a, b, loose) => compare(a, b, loose) !== 0
module.exports = neq
+16
View File
@@ -0,0 +1,16 @@
const SemVer = require('../classes/semver')
const parse = (version, options, throwErrors = false) => {
if (version instanceof SemVer) {
return version
}
try {
return new SemVer(version, options)
} catch (er) {
if (!throwErrors) {
return null
}
throw er
}
}
module.exports = parse
+3
View File
@@ -0,0 +1,3 @@
const SemVer = require('../classes/semver')
const patch = (a, loose) => new SemVer(a, loose).patch
module.exports = patch
+6
View File
@@ -0,0 +1,6 @@
const parse = require('./parse')
const prerelease = (version, options) => {
const parsed = parse(version, options)
return (parsed && parsed.prerelease.length) ? parsed.prerelease : null
}
module.exports = prerelease
+3
View File
@@ -0,0 +1,3 @@
const compare = require('./compare')
const rcompare = (a, b, loose) => compare(b, a, loose)
module.exports = rcompare
+3
View File
@@ -0,0 +1,3 @@
const compareBuild = require('./compare-build')
const rsort = (list, loose) => list.sort((a, b) => compareBuild(b, a, loose))
module.exports = rsort
+10
View File
@@ -0,0 +1,10 @@
const Range = require('../classes/range')
const satisfies = (version, range, options) => {
try {
range = new Range(range, options)
} catch (er) {
return false
}
return range.test(version)
}
module.exports = satisfies
+3
View File
@@ -0,0 +1,3 @@
const compareBuild = require('./compare-build')
const sort = (list, loose) => list.sort((a, b) => compareBuild(a, b, loose))
module.exports = sort
+6
View File
@@ -0,0 +1,6 @@
const parse = require('./parse')
const valid = (version, options) => {
const v = parse(version, options)
return v ? v.version : null
}
module.exports = valid
+89
View File
@@ -0,0 +1,89 @@
// just pre-load all the stuff that index.js lazily exports
const internalRe = require('./internal/re')
const constants = require('./internal/constants')
const SemVer = require('./classes/semver')
const identifiers = require('./internal/identifiers')
const parse = require('./functions/parse')
const valid = require('./functions/valid')
const clean = require('./functions/clean')
const inc = require('./functions/inc')
const diff = require('./functions/diff')
const major = require('./functions/major')
const minor = require('./functions/minor')
const patch = require('./functions/patch')
const prerelease = require('./functions/prerelease')
const compare = require('./functions/compare')
const rcompare = require('./functions/rcompare')
const compareLoose = require('./functions/compare-loose')
const compareBuild = require('./functions/compare-build')
const sort = require('./functions/sort')
const rsort = require('./functions/rsort')
const gt = require('./functions/gt')
const lt = require('./functions/lt')
const eq = require('./functions/eq')
const neq = require('./functions/neq')
const gte = require('./functions/gte')
const lte = require('./functions/lte')
const cmp = require('./functions/cmp')
const coerce = require('./functions/coerce')
const Comparator = require('./classes/comparator')
const Range = require('./classes/range')
const satisfies = require('./functions/satisfies')
const toComparators = require('./ranges/to-comparators')
const maxSatisfying = require('./ranges/max-satisfying')
const minSatisfying = require('./ranges/min-satisfying')
const minVersion = require('./ranges/min-version')
const validRange = require('./ranges/valid')
const outside = require('./ranges/outside')
const gtr = require('./ranges/gtr')
const ltr = require('./ranges/ltr')
const intersects = require('./ranges/intersects')
const simplifyRange = require('./ranges/simplify')
const subset = require('./ranges/subset')
module.exports = {
parse,
valid,
clean,
inc,
diff,
major,
minor,
patch,
prerelease,
compare,
rcompare,
compareLoose,
compareBuild,
sort,
rsort,
gt,
lt,
eq,
neq,
gte,
lte,
cmp,
coerce,
Comparator,
Range,
satisfies,
toComparators,
maxSatisfying,
minSatisfying,
minVersion,
validRange,
outside,
gtr,
ltr,
intersects,
simplifyRange,
subset,
SemVer,
re: internalRe.re,
src: internalRe.src,
tokens: internalRe.t,
SEMVER_SPEC_VERSION: constants.SEMVER_SPEC_VERSION,
RELEASE_TYPES: constants.RELEASE_TYPES,
compareIdentifiers: identifiers.compareIdentifiers,
rcompareIdentifiers: identifiers.rcompareIdentifiers,
}
+35
View File
@@ -0,0 +1,35 @@
// Note: this is the semver.org version of the spec that it implements
// Not necessarily the package version of this code.
const SEMVER_SPEC_VERSION = '2.0.0'
const MAX_LENGTH = 256
const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER ||
/* istanbul ignore next */ 9007199254740991
// Max safe segment length for coercion.
const MAX_SAFE_COMPONENT_LENGTH = 16
// Max safe length for a build identifier. The max length minus 6 characters for
// the shortest version with a build 0.0.0+BUILD.
const MAX_SAFE_BUILD_LENGTH = MAX_LENGTH - 6
const RELEASE_TYPES = [
'major',
'premajor',
'minor',
'preminor',
'patch',
'prepatch',
'prerelease',
]
module.exports = {
MAX_LENGTH,
MAX_SAFE_COMPONENT_LENGTH,
MAX_SAFE_BUILD_LENGTH,
MAX_SAFE_INTEGER,
RELEASE_TYPES,
SEMVER_SPEC_VERSION,
FLAG_INCLUDE_PRERELEASE: 0b001,
FLAG_LOOSE: 0b010,
}
+9
View File
@@ -0,0 +1,9 @@
const debug = (
typeof process === 'object' &&
process.env &&
process.env.NODE_DEBUG &&
/\bsemver\b/i.test(process.env.NODE_DEBUG)
) ? (...args) => console.error('SEMVER', ...args)
: () => {}
module.exports = debug
+23
View File
@@ -0,0 +1,23 @@
const numeric = /^[0-9]+$/
const compareIdentifiers = (a, b) => {
const anum = numeric.test(a)
const bnum = numeric.test(b)
if (anum && bnum) {
a = +a
b = +b
}
return a === b ? 0
: (anum && !bnum) ? -1
: (bnum && !anum) ? 1
: a < b ? -1
: 1
}
const rcompareIdentifiers = (a, b) => compareIdentifiers(b, a)
module.exports = {
compareIdentifiers,
rcompareIdentifiers,
}
+40
View File
@@ -0,0 +1,40 @@
class LRUCache {
constructor () {
this.max = 1000
this.map = new Map()
}
get (key) {
const value = this.map.get(key)
if (value === undefined) {
return undefined
} else {
// Remove the key from the map and add it to the end
this.map.delete(key)
this.map.set(key, value)
return value
}
}
delete (key) {
return this.map.delete(key)
}
set (key, value) {
const deleted = this.delete(key)
if (!deleted && value !== undefined) {
// If cache is full, delete the least recently used item
if (this.map.size >= this.max) {
const firstKey = this.map.keys().next().value
this.delete(firstKey)
}
this.map.set(key, value)
}
return this
}
}
module.exports = LRUCache
+15
View File
@@ -0,0 +1,15 @@
// parse out just the options we care about
const looseOption = Object.freeze({ loose: true })
const emptyOpts = Object.freeze({ })
const parseOptions = options => {
if (!options) {
return emptyOpts
}
if (typeof options !== 'object') {
return looseOption
}
return options
}
module.exports = parseOptions
+219
View File
@@ -0,0 +1,219 @@
const {
MAX_SAFE_COMPONENT_LENGTH,
MAX_SAFE_BUILD_LENGTH,
MAX_LENGTH,
} = require('./constants')
const debug = require('./debug')
exports = module.exports = {}
// The actual regexps go on exports.re
const re = exports.re = []
const safeRe = exports.safeRe = []
const src = exports.src = []
const safeSrc = exports.safeSrc = []
const t = exports.t = {}
let R = 0
const LETTERDASHNUMBER = '[a-zA-Z0-9-]'
// Replace some greedy regex tokens to prevent regex dos issues. These regex are
// used internally via the safeRe object since all inputs in this library get
// normalized first to trim and collapse all extra whitespace. The original
// regexes are exported for userland consumption and lower level usage. A
// future breaking change could export the safer regex only with a note that
// all input should have extra whitespace removed.
const safeRegexReplacements = [
['\\s', 1],
['\\d', MAX_LENGTH],
[LETTERDASHNUMBER, MAX_SAFE_BUILD_LENGTH],
]
const makeSafeRegex = (value) => {
for (const [token, max] of safeRegexReplacements) {
value = value
.split(`${token}*`).join(`${token}{0,${max}}`)
.split(`${token}+`).join(`${token}{1,${max}}`)
}
return value
}
const createToken = (name, value, isGlobal) => {
const safe = makeSafeRegex(value)
const index = R++
debug(name, index, value)
t[name] = index
src[index] = value
safeSrc[index] = safe
re[index] = new RegExp(value, isGlobal ? 'g' : undefined)
safeRe[index] = new RegExp(safe, isGlobal ? 'g' : undefined)
}
// The following Regular Expressions can be used for tokenizing,
// validating, and parsing SemVer version strings.
// ## Numeric Identifier
// A single `0`, or a non-zero digit followed by zero or more digits.
createToken('NUMERICIDENTIFIER', '0|[1-9]\\d*')
createToken('NUMERICIDENTIFIERLOOSE', '\\d+')
// ## Non-numeric Identifier
// Zero or more digits, followed by a letter or hyphen, and then zero or
// more letters, digits, or hyphens.
createToken('NONNUMERICIDENTIFIER', `\\d*[a-zA-Z-]${LETTERDASHNUMBER}*`)
// ## Main Version
// Three dot-separated numeric identifiers.
createToken('MAINVERSION', `(${src[t.NUMERICIDENTIFIER]})\\.` +
`(${src[t.NUMERICIDENTIFIER]})\\.` +
`(${src[t.NUMERICIDENTIFIER]})`)
createToken('MAINVERSIONLOOSE', `(${src[t.NUMERICIDENTIFIERLOOSE]})\\.` +
`(${src[t.NUMERICIDENTIFIERLOOSE]})\\.` +
`(${src[t.NUMERICIDENTIFIERLOOSE]})`)
// ## Pre-release Version Identifier
// A numeric identifier, or a non-numeric identifier.
createToken('PRERELEASEIDENTIFIER', `(?:${src[t.NUMERICIDENTIFIER]
}|${src[t.NONNUMERICIDENTIFIER]})`)
createToken('PRERELEASEIDENTIFIERLOOSE', `(?:${src[t.NUMERICIDENTIFIERLOOSE]
}|${src[t.NONNUMERICIDENTIFIER]})`)
// ## Pre-release Version
// Hyphen, followed by one or more dot-separated pre-release version
// identifiers.
createToken('PRERELEASE', `(?:-(${src[t.PRERELEASEIDENTIFIER]
}(?:\\.${src[t.PRERELEASEIDENTIFIER]})*))`)
createToken('PRERELEASELOOSE', `(?:-?(${src[t.PRERELEASEIDENTIFIERLOOSE]
}(?:\\.${src[t.PRERELEASEIDENTIFIERLOOSE]})*))`)
// ## Build Metadata Identifier
// Any combination of digits, letters, or hyphens.
createToken('BUILDIDENTIFIER', `${LETTERDASHNUMBER}+`)
// ## Build Metadata
// Plus sign, followed by one or more period-separated build metadata
// identifiers.
createToken('BUILD', `(?:\\+(${src[t.BUILDIDENTIFIER]
}(?:\\.${src[t.BUILDIDENTIFIER]})*))`)
// ## Full Version String
// A main version, followed optionally by a pre-release version and
// build metadata.
// Note that the only major, minor, patch, and pre-release sections of
// the version string are capturing groups. The build metadata is not a
// capturing group, because it should not ever be used in version
// comparison.
createToken('FULLPLAIN', `v?${src[t.MAINVERSION]
}${src[t.PRERELEASE]}?${
src[t.BUILD]}?`)
createToken('FULL', `^${src[t.FULLPLAIN]}$`)
// like full, but allows v1.2.3 and =1.2.3, which people do sometimes.
// also, 1.0.0alpha1 (prerelease without the hyphen) which is pretty
// common in the npm registry.
createToken('LOOSEPLAIN', `[v=\\s]*${src[t.MAINVERSIONLOOSE]
}${src[t.PRERELEASELOOSE]}?${
src[t.BUILD]}?`)
createToken('LOOSE', `^${src[t.LOOSEPLAIN]}$`)
createToken('GTLT', '((?:<|>)?=?)')
// Something like "2.*" or "1.2.x".
// Note that "x.x" is a valid xRange identifer, meaning "any version"
// Only the first item is strictly required.
createToken('XRANGEIDENTIFIERLOOSE', `${src[t.NUMERICIDENTIFIERLOOSE]}|x|X|\\*`)
createToken('XRANGEIDENTIFIER', `${src[t.NUMERICIDENTIFIER]}|x|X|\\*`)
createToken('XRANGEPLAIN', `[v=\\s]*(${src[t.XRANGEIDENTIFIER]})` +
`(?:\\.(${src[t.XRANGEIDENTIFIER]})` +
`(?:\\.(${src[t.XRANGEIDENTIFIER]})` +
`(?:${src[t.PRERELEASE]})?${
src[t.BUILD]}?` +
`)?)?`)
createToken('XRANGEPLAINLOOSE', `[v=\\s]*(${src[t.XRANGEIDENTIFIERLOOSE]})` +
`(?:\\.(${src[t.XRANGEIDENTIFIERLOOSE]})` +
`(?:\\.(${src[t.XRANGEIDENTIFIERLOOSE]})` +
`(?:${src[t.PRERELEASELOOSE]})?${
src[t.BUILD]}?` +
`)?)?`)
createToken('XRANGE', `^${src[t.GTLT]}\\s*${src[t.XRANGEPLAIN]}$`)
createToken('XRANGELOOSE', `^${src[t.GTLT]}\\s*${src[t.XRANGEPLAINLOOSE]}$`)
// Coercion.
// Extract anything that could conceivably be a part of a valid semver
createToken('COERCEPLAIN', `${'(^|[^\\d])' +
'(\\d{1,'}${MAX_SAFE_COMPONENT_LENGTH}})` +
`(?:\\.(\\d{1,${MAX_SAFE_COMPONENT_LENGTH}}))?` +
`(?:\\.(\\d{1,${MAX_SAFE_COMPONENT_LENGTH}}))?`)
createToken('COERCE', `${src[t.COERCEPLAIN]}(?:$|[^\\d])`)
createToken('COERCEFULL', src[t.COERCEPLAIN] +
`(?:${src[t.PRERELEASE]})?` +
`(?:${src[t.BUILD]})?` +
`(?:$|[^\\d])`)
createToken('COERCERTL', src[t.COERCE], true)
createToken('COERCERTLFULL', src[t.COERCEFULL], true)
// Tilde ranges.
// Meaning is "reasonably at or greater than"
createToken('LONETILDE', '(?:~>?)')
createToken('TILDETRIM', `(\\s*)${src[t.LONETILDE]}\\s+`, true)
exports.tildeTrimReplace = '$1~'
createToken('TILDE', `^${src[t.LONETILDE]}${src[t.XRANGEPLAIN]}$`)
createToken('TILDELOOSE', `^${src[t.LONETILDE]}${src[t.XRANGEPLAINLOOSE]}$`)
// Caret ranges.
// Meaning is "at least and backwards compatible with"
createToken('LONECARET', '(?:\\^)')
createToken('CARETTRIM', `(\\s*)${src[t.LONECARET]}\\s+`, true)
exports.caretTrimReplace = '$1^'
createToken('CARET', `^${src[t.LONECARET]}${src[t.XRANGEPLAIN]}$`)
createToken('CARETLOOSE', `^${src[t.LONECARET]}${src[t.XRANGEPLAINLOOSE]}$`)
// A simple gt/lt/eq thing, or just "" to indicate "any version"
createToken('COMPARATORLOOSE', `^${src[t.GTLT]}\\s*(${src[t.LOOSEPLAIN]})$|^$`)
createToken('COMPARATOR', `^${src[t.GTLT]}\\s*(${src[t.FULLPLAIN]})$|^$`)
// An expression to strip any whitespace between the gtlt and the thing
// it modifies, so that `> 1.2.3` ==> `>1.2.3`
createToken('COMPARATORTRIM', `(\\s*)${src[t.GTLT]
}\\s*(${src[t.LOOSEPLAIN]}|${src[t.XRANGEPLAIN]})`, true)
exports.comparatorTrimReplace = '$1$2$3'
// Something like `1.2.3 - 1.2.4`
// Note that these all use the loose form, because they'll be
// checked against either the strict or loose comparator form
// later.
createToken('HYPHENRANGE', `^\\s*(${src[t.XRANGEPLAIN]})` +
`\\s+-\\s+` +
`(${src[t.XRANGEPLAIN]})` +
`\\s*$`)
createToken('HYPHENRANGELOOSE', `^\\s*(${src[t.XRANGEPLAINLOOSE]})` +
`\\s+-\\s+` +
`(${src[t.XRANGEPLAINLOOSE]})` +
`\\s*$`)
// Star ranges basically just allow anything at all.
createToken('STAR', '(<|>)?=?\\s*\\*')
// >=0.0.0 is like a star
createToken('GTE0', '^\\s*>=\\s*0\\.0\\.0\\s*$')
createToken('GTE0PRE', '^\\s*>=\\s*0\\.0\\.0-0\\s*$')
+78
View File
@@ -0,0 +1,78 @@
{
"name": "semver",
"version": "7.7.1",
"description": "The semantic version parser used by npm.",
"main": "index.js",
"scripts": {
"test": "tap",
"snap": "tap",
"lint": "npm run eslint",
"postlint": "template-oss-check",
"lintfix": "npm run eslint -- --fix",
"posttest": "npm run lint",
"template-oss-apply": "template-oss-apply --force",
"eslint": "eslint \"**/*.{js,cjs,ts,mjs,jsx,tsx}\""
},
"devDependencies": {
"@npmcli/eslint-config": "^5.0.0",
"@npmcli/template-oss": "4.23.4",
"benchmark": "^2.1.4",
"tap": "^16.0.0"
},
"license": "ISC",
"repository": {
"type": "git",
"url": "git+https://github.com/npm/node-semver.git"
},
"bin": {
"semver": "bin/semver.js"
},
"files": [
"bin/",
"lib/",
"classes/",
"functions/",
"internal/",
"ranges/",
"index.js",
"preload.js",
"range.bnf"
],
"tap": {
"timeout": 30,
"coverage-map": "map.js",
"nyc-arg": [
"--exclude",
"tap-snapshots/**"
]
},
"engines": {
"node": ">=10"
},
"author": "GitHub Inc.",
"templateOSS": {
"//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.",
"version": "4.23.4",
"engines": ">=10",
"distPaths": [
"classes/",
"functions/",
"internal/",
"ranges/",
"index.js",
"preload.js",
"range.bnf"
],
"allowPaths": [
"/classes/",
"/functions/",
"/internal/",
"/ranges/",
"/index.js",
"/preload.js",
"/range.bnf",
"/benchmarks"
],
"publish": "true"
}
}
+2
View File
@@ -0,0 +1,2 @@
// XXX remove in v8 or beyond
module.exports = require('./index.js')
+16
View File
@@ -0,0 +1,16 @@
range-set ::= range ( logical-or range ) *
logical-or ::= ( ' ' ) * '||' ( ' ' ) *
range ::= hyphen | simple ( ' ' simple ) * | ''
hyphen ::= partial ' - ' partial
simple ::= primitive | partial | tilde | caret
primitive ::= ( '<' | '>' | '>=' | '<=' | '=' ) partial
partial ::= xr ( '.' xr ( '.' xr qualifier ? )? )?
xr ::= 'x' | 'X' | '*' | nr
nr ::= '0' | [1-9] ( [0-9] ) *
tilde ::= '~' partial
caret ::= '^' partial
qualifier ::= ( '-' pre )? ( '+' build )?
pre ::= parts
build ::= parts
parts ::= part ( '.' part ) *
part ::= nr | [-0-9A-Za-z]+
+4
View File
@@ -0,0 +1,4 @@
// Determine if version is greater than all the versions possible in the range.
const outside = require('./outside')
const gtr = (version, range, options) => outside(version, range, '>', options)
module.exports = gtr
+7
View File
@@ -0,0 +1,7 @@
const Range = require('../classes/range')
const intersects = (r1, r2, options) => {
r1 = new Range(r1, options)
r2 = new Range(r2, options)
return r1.intersects(r2, options)
}
module.exports = intersects
+4
View File
@@ -0,0 +1,4 @@
const outside = require('./outside')
// Determine if version is less than all the versions possible in the range
const ltr = (version, range, options) => outside(version, range, '<', options)
module.exports = ltr
+25
View File
@@ -0,0 +1,25 @@
const SemVer = require('../classes/semver')
const Range = require('../classes/range')
const maxSatisfying = (versions, range, options) => {
let max = null
let maxSV = null
let rangeObj = null
try {
rangeObj = new Range(range, options)
} catch (er) {
return null
}
versions.forEach((v) => {
if (rangeObj.test(v)) {
// satisfies(v, range, options)
if (!max || maxSV.compare(v) === -1) {
// compare(max, v, true)
max = v
maxSV = new SemVer(max, options)
}
}
})
return max
}
module.exports = maxSatisfying
+24
View File
@@ -0,0 +1,24 @@
const SemVer = require('../classes/semver')
const Range = require('../classes/range')
const minSatisfying = (versions, range, options) => {
let min = null
let minSV = null
let rangeObj = null
try {
rangeObj = new Range(range, options)
} catch (er) {
return null
}
versions.forEach((v) => {
if (rangeObj.test(v)) {
// satisfies(v, range, options)
if (!min || minSV.compare(v) === 1) {
// compare(min, v, true)
min = v
minSV = new SemVer(min, options)
}
}
})
return min
}
module.exports = minSatisfying
+61
View File
@@ -0,0 +1,61 @@
const SemVer = require('../classes/semver')
const Range = require('../classes/range')
const gt = require('../functions/gt')
const minVersion = (range, loose) => {
range = new Range(range, loose)
let minver = new SemVer('0.0.0')
if (range.test(minver)) {
return minver
}
minver = new SemVer('0.0.0-0')
if (range.test(minver)) {
return minver
}
minver = null
for (let i = 0; i < range.set.length; ++i) {
const comparators = range.set[i]
let setMin = null
comparators.forEach((comparator) => {
// Clone to avoid manipulating the comparator's semver object.
const compver = new SemVer(comparator.semver.version)
switch (comparator.operator) {
case '>':
if (compver.prerelease.length === 0) {
compver.patch++
} else {
compver.prerelease.push(0)
}
compver.raw = compver.format()
/* fallthrough */
case '':
case '>=':
if (!setMin || gt(compver, setMin)) {
setMin = compver
}
break
case '<':
case '<=':
/* Ignore maximum versions */
break
/* istanbul ignore next */
default:
throw new Error(`Unexpected operation: ${comparator.operator}`)
}
})
if (setMin && (!minver || gt(minver, setMin))) {
minver = setMin
}
}
if (minver && range.test(minver)) {
return minver
}
return null
}
module.exports = minVersion

Some files were not shown because too many files have changed in this diff Show More