130 Commits

Author SHA1 Message Date
Anton Gilgur
b9dce9dfd1
refactor: split out a common typecheckFile func (#344)
* refactor: split out a common typecheckFile func

- this is used in 3 places and going to be more for the code I'm adding
  to fix type-only imports
  - so DRY it up and standardize the functionality too
    - some places had `noErrors = false` in one place while others had
      it in another
    - same for `printDiagnostics`
    - but the ordering actually doesn't matter, so just keep it
      consistent and the same
      - and then can split a common function that does both out

- technically, now getDiagnostics is _only_ used in typecheckFile, so
  I could link to the two together, but I'm refactoring that one up
  a little
  - but this a good, small example of how refactoring one part of a
    codebase can make it easier to identify more similar pieces and then
    refactor even more

* fix lint error on shadowed name
2022-06-20 16:39:13 -06:00
Anton Gilgur
fee9547ebc
refactor(cache): simplify clean method (#358)
* refactor(cache): simplify `clean` method

- `cache().clean()` is only called during instantiation in `options`, so we can instead just call it inside of the constructor
- there is no need to call `this.init()` in `clean` as it's only used in the constructor anyway, which already calls `this.init()`
  - and if `noCache` is `true`, `init` is basically a no-op too
    - so technically we don't need to call `init` _at all_ if `noCache`, but that's a larger refactor that I'm splitting into a separate commit/PR

- now that `clean` is just one conditional, we can invert it and return early instead
- it's also not necessary and less efficient to call `emptyDir` before `remove`; `remove` will unlink all the contents anyway
  - docs here: 0220eac966/docs/remove-sync.md
    - though this also just normal FS behavior
- IMO, it's also not necessary to check if it's a directory, we can `remove` it either way
  - and not necessary to log out if we _don't_ clean it
- then also just simplify the logic to use a `filter` instead of a nested `if`
  - which we already do in several places, so this follows existing code style

* undo dir and not clean log removal

- as requested in code review, will go with a safety-first operating assumption

- as such, with safety as modus operandus, make the logging more detailed in these scenarios, since they're not supposed to happen
  - and don't check code coverage on these as they're not supposed to happen in normal usage
2022-06-20 16:37:30 -06:00
Anton Gilgur
4a0c2977f8
fix: filter "missed" declarations as well (#347)
- these should run through the same `filter` as runs in `transform` etc
  - prior to this, the plugin `exclude` would filter files in
    `transform`, meaning no JS would be output for them, but would still
    output declarations for these very same files that no JS was
    produced for
    - (this would only happen if one were using an `include` glob or
      something that made the file appear twice, i.e. once by Rollup
      in `transform` and once in `parsedConfig.fileNames`)
  - this change makes it so the plugin `exclude` affects both JS
    and DTS output equivalently
    - it was very confusing when it didn't, and users relied on setting
      different `tsconfig` `exclude`s to workaround this (as a
      `tsconfig` `exclude` would make the file not appear in
      `parsedConfig.fileNames`)
2022-06-06 18:18:23 -06:00
Anton Gilgur
c0fb53d3bc
clean: remove redundant allImportedFiles check in _onwrite (#346)
- this checks if any of the files in `parsedConfig.fileNames` are _not_
  in `allImportedFiles`, but all the files in `parsedConfig.fileNames`
  are explicitly added in the `options` hook on line 98
  - so this is redundant / dead code; the check will never be true

- this can be considered a remnant of an old bug as an old commit fixed
  a bug with `allImportedFiles` after it was released:
  f24359e668
2022-06-06 18:17:31 -06:00
Anton Gilgur
b1e3b44df1
fix: set noErrors to false when there are compilerOptions errors (#342)
- previously, while errors would be printed, the flag for `noErrors` was
  not set to `false`, and so there would be no yellow warning about
  errors
  - this is mostly just fixing asymmetric UX, but personally, I actually
    have missed this error before myself, so maybe this will help
    alleviate that
2022-06-06 18:07:43 -06:00
Anton Gilgur
d8a8e42dc2
optim: don't getScriptSnapshot when we already have the snapshot (#341)
- the snapshot is set above, so no need to get it
- also the snapshot from above is used below already, so this doesn't
  seem to be intentional
  - it originates from 2d330640316894752bae5ae64d94bc90652e4564 which
    similarly doesn't seem intentional
2022-06-06 18:04:02 -06:00
Anton Gilgur
b08f272efb
refactor: sort all top-level imports (#337)
- basically, general format is:
  ```ts
  import x from "external-dep"

  import y from "./internal-dep"
  ```
  - so external deps, new line, then internal/local deps
  - with some further sorting within there, like trying to keep Node
    built-ins (e.g. `path`) at the top half of externals, then core deps
    like `typescript`, then any other external deps
    - and similar for internal deps -- core internals at the top half of
      internals, then any other internal deps
    - just to keep things consistent between files -- makes the top
      easier to read through when it's similar between files
    - also makes it easier for contributors to understand where to put
      imports, as there's a sorting already there

- this is how I generally sort my imports and how I wrote most of the
  unit test suite's imports as well

- there is automation for this that we should probably add once TSLint
  is replaced here; some previous art:
  - https://github.com/trivago/prettier-plugin-sort-imports
  - https://github.com/lydell/eslint-plugin-simple-import-sort/
  - Older:
    - https://github.com/renke/import-sort/tree/master/packages/import-sort-style-module
    - https://github.com/mcdougal/js-isort
    - inspired by Python's `isort` ofc
2022-06-01 14:40:54 -06:00
Anton Gilgur
ccd6815486
fix: don't attempt to change declarationMap sources when no output (#334)
- when using rpt2 as a configPlugin, there is no Rollup `output`, so it
  will be `undefined`
  - when `declarationMap: true` in `tsconfig`, this block of code is
    called as a workaround due to the placeholder dir we must use for
    output -- but, in this case, it errors out, since the
    `declarationDir` is `undefined`
    - `path.relative` needs a `string` as a arg, not `undefined`
    - so skip this transformation entirely when there's no `output`, as
      it doesn't need to be done in that case anyway
    - this transformation code happens to have been written by me 2
      years ago too, so I had fixed one bug with that but created
      a different bug 😅 (fortunately one that only I have stumbled upon)
2022-06-01 14:40:00 -06:00
Anton Gilgur
01272d3ebe
refactor: invert some conditionals for better readability (#335)
- a few `if (cond) { big block } return` could be inverted to
  `if (!cond) return` then the block unindented instead
  - generally speaking, this makes it a lot more readable (less
    indentation, etc) and follows the existing code style in much of the
    codebase, where it's a few quick one-line ifs and then just the rest
    of the code

- shorten the resolvedFileName conditional by using optional chaining
  - prefer the simpler `x?.y` over the older `x && x.y` syntax
- add a `resolved` variable for less repetition of the whole statement
- add a comment to the `pathNormalize` line about why it's used in that
  one place and link to the longer description in the PR as well

- shorten comment about `useTsconfigDeclarationDir` so it doesn't take
  up so much space or look so important as a result
  - and it's easier to read this way and I made the explanation less
    verbose and quicker to read too
- remove the `else` there and just add an early return instead, similar
  to the inverted conditionals above
  - similarly makes it less unindented, more readable etc
2022-05-31 20:42:36 -06:00
Anton Gilgur
e145d0baeb
fix: use .d.ts instead of .vue.d.ts for Vue declarations (#336)
* - fix for vue declaration file names

* fix: use .d.ts instead of .vue.d.ts for Vue declarations

- `.vue.d.ts` was recommended by the `rollup-plugin-vue` maintainers
  back when this code was originally written, but apparently it causes
  problems with some imports
  - plain `.d.ts` seems to work fine according to users
    - and plain is the standard in TS too I believe

Co-authored-by: ezolenko <zolenkoe@gmail.com>
2022-05-31 20:23:02 -06:00
Anton Gilgur
197061bf92
refactor: prefer native methods to lodash where possible (#328)
- _.endsWith -> String.endsWith

- _.concat -> Array.concat
- _.each -> Array.forEach
- _.filter -> Array.filter
- _.map -> Array.map
- _.some -> Array.some

- _.has -> `key in Object`
- _.defaults -> Object.assign
- _.get -> `?.` and `??` (optional chaining and nullish coalescing)

- refactor: replace fairly complicated `expandIncludeWithDirs` func to
  just use a few simple `forEach`s
  - not as FP anymore, more imperative, but much simpler to read IMO
- refactor: add a `getDiagnostics` helper to DRY up some code
  - also aids readability IMO

- a few places are still using lodash, but this paves the way toward
  removing it or replacing it with much smaller individual deps
  - _.compact still used because Array.filter heavily complicates the
    type-checking currently
  - _.isFunction still used because while it's a one-liner natively,
    need to import a function in several places
    - also the package `lodash.isFunction` is lodash v3 and quite
      different from the v4 implementation, so couldn't replace with it
      unfortunately
  - _.merge is a deep merge, so there's no native version of this
    - but we may remove deep merges entirely in the future (as tsconfig
      doesn't quite perform a deep merge), or could replace this with a
      smaller `lodash.merge` package or similar

- see also https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore
2022-05-16 16:30:48 -06:00
Anton Gilgur
7af216b463
clean: remove partial.ts as this is built into TS (#322)
- at least as of TS 2.1: https://www.typescriptlang.org/docs/handbook/utility-types.html#partialtype
  - we have a peerDep on TS >=2.4, so should definitely be compatible
  - and TS is on ~4.6 at this point, so that's _really_ old

- remove the file and the declarations and declaration maps
  - don't rebuild as that's usually done as a separate commit
2022-05-10 17:15:55 -06:00
Anton Gilgur
895431d3ae
deps: use normalizePath from @rollup/pluginutils (#320)
- this was introduced in v4.1.0 of @rollup/pluginutils:
  https://github.com/rollup/plugins/blob/master/packages/pluginutils/CHANGELOG.md#v410

- this is the same as the code in `normalize.ts` but it uses constants
  from Node and is used by multiple Rollup plugins, so just helps with
  standardization
  - also less code and types to ship in the bundle!

- removed the dist files for `normalize` as well, but didn't do a build
  in this commit as those are usually done in separate commits
2022-05-04 10:17:56 -06:00
ezolenko
08d2f5bcad - removing semver dependency (refuses to be rolled up for some reason)
- updating dependencies
2022-04-21 11:58:01 -06:00
Tony Ross
6fb0e75f53
Fix duplicate output with multiple entry points (#251)
On Windows the normalized paths in resolveId end up in POSIX format.
This cause rollup to treat the returned path as a new piece of content.
This in turn results in duplicate output for references across entry points.

Fixed by normalizing the path to use host OS separators before returning.
2020-11-20 13:11:55 -07:00
ezolenko
fc2274d2ea - package version
- syntax
2020-10-02 10:49:00 -06:00
ezolenko
5f02a91820 - fix for windows path and build 2020-10-02 09:26:52 -06:00
ezolenko
ee33209beb Merge branch 'fix-declaration-map-sources' of https://github.com/agilgur5/rollup-plugin-typescript2 into agilgur5-fix-declaration-map-sources_2 2020-10-02 09:26:24 -06:00
Anton Gilgur
ec0568ba2c (fix): declaration maps should have correct sources
- previously, declarationDir was set to cwd if useTsconfigDeclarationDir
  wasn't true, however, declarations aren't output to cwd, but to
  Rollup's output destination, so this was incorrect
  - instead, don't set declarationDir, which defaults it to outDir,
    which is currently set to a placeholder
    - previously, it rewrote declarations to output to Rollup's dest
      from cwd, now rewrite from outDir placeholder instead
  - and add a rewrite of sources to match relative path from Rollup's
    output dest instead of outDir placeholder

- also change the one line in the docs that says it'll be
  `process.cwd()`; every other reference says it'll be the output dest
2020-09-30 19:46:37 -04:00
ezolenko
2cace3fd80 - updating dependencies 2020-09-25 13:43:38 -06:00
Anton Gilgur
9e308c19b0
(refactor): use Rollup's Plugin interface instead of manual types (#222)
- more importantly, using Plugin ensures that the types are accurate
2020-03-31 11:39:41 -06:00
ezolenko
034abe0a83 - updating dependencies 2020-03-26 23:45:45 -06:00
Eugene Zolenko
67748cc7cd - fix for plugin option type 2020-03-10 10:43:17 -06:00
Eugene Zolenko
b7c7389dcd - exporting IOptions type 2020-03-03 17:08:53 -07:00
Eugene Zolenko
e957a893a3 - fix for indent 2020-02-12 11:48:43 -07:00
Eugene Zolenko
df241da73f - undoing debug code 2020-02-11 16:30:44 -07:00
Eugene Zolenko
cd76b42f55 - warning message for objectHashIgnoreUnknownHack option 2020-02-11 16:28:57 -07:00
Eugene Zolenko
23f63b4da2 - build 2019-12-03 10:24:39 -07:00
chencheng (云谦)
4217346054 feat: support options.cwd (#197) 2019-12-03 10:20:41 -07:00
Eugene Zolenko
bc5b419c79 - merging #190
- readme and tslint updates
2019-11-05 10:34:39 -07:00
Marijn Haverbeke
e12631c160 Restore "Use PluginContext.emitFile to emit declarations (when possible) (#181)""
This reverts commit c35e35d627ec2b00739dede33ef1d6746ece7ad4.
2019-11-04 14:07:38 +01:00
Eugene Zolenko
c35e35d627 Revert "Use PluginContext.emitFile to emit declarations (when possible) (#181)"
This reverts commit 551a14e773141cd993bb9d1ffd349c92481edf11.
2019-10-15 11:15:21 -06:00
Marijn Haverbeke
551a14e773 Use PluginContext.emitFile to emit declarations (when possible) (#181)
Issue #180
2019-10-15 11:00:06 -06:00
Eugene Zolenko
63a7ac281e - fix for linter 2019-09-26 15:27:21 -06:00
Charles Simard-Lecours
f24359e668 fix(plugin) fix missing files 2019-09-26 14:52:56 -04:00
Mateusz Burzyński
dca40becaf Add .js extension to virtual tslib (#170) 2019-09-09 14:56:07 -06:00
Eugene Zolenko
0420a5f06a - build with updated packages 2019-08-28 17:49:56 -06:00
Eugene Zolenko
24284605f3 - build and package version 2019-08-28 17:35:44 -06:00
Nick McCurdy
ffda2e539b Move cache directory to node_modules (#167)
* Move cache directory to node_modules

* Use find-cache-dir to find .cache in node_modules
2019-08-28 17:26:05 -06:00
Eugene Zolenko
b0a0ecb5ee - not trying to resolve imports unrelated to this plugin
- version
#162
2019-08-01 15:57:35 -06:00
Eugene Zolenko
23420c4b89 - not generating typings for files that are not actually imported
#162, #136
2019-08-01 15:45:37 -06:00
Eugene Zolenko
e60eb417d4 - fix for declarations for type only imports on watch
#163
2019-07-26 11:36:19 -06:00
Eugene Zolenko
9be93f36d0 - updating dependencies 2019-07-10 14:21:21 -06:00
Eugene Zolenko
2d33064031 - adding all imports to rollup watch #147, #7 2019-03-25 16:25:03 -06:00
ezolenko
6572018189 - fix for #144 2019-03-13 08:48:56 -06:00
ezolenko
33b99bf9af - fix for lint errors 2019-03-11 14:02:46 -06:00
ezolenko
578536a596 - merge 2019-03-11 13:58:45 -06:00
ezolenko
29b13a089b - updating dependencies 2019-03-11 13:52:52 -06:00
Yu Chao Liang
b00ae06943
- if output.file not exist use output.dir 2019-03-09 02:18:35 +08:00
Eugene Zolenko
9188132749 - refactoring 2019-02-25 11:18:48 -07:00