-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Declaration generated in wrong folder when using object as input
(multi-entry)
#136
Comments
The |
Hi @ezolenkom & @jakearchibald, no luck on either actually. When adding the |
@benkeen when using |
have similar trouble, my project layout is like this: export default [
{
input: 'src/subFolder1/one.ts',
output: [
{
file: 'dist/one.js'
// .....
}
],
plugins: [
typescript({
typescript: require('typescript'),
}),
]
}, {
input: 'src/subFolder2/two.ts',
output: [
{
file: 'dist/two.js'
// .....
}
],
// ...
},
] As an output I have:
|
Works for me on 0.20.1 with @AntonPilyak that is by design -- if d.ts were going into one folder without sub paths, then if you had |
@ezolenko : this feature, I think, creates more inconveniences rather then solves a real problem (due to it is hard to maintain all those directory structure). I have to create a script that copies all these type declarations to the root of the bundle, and add the directories to .npmignore in order to create a bundle that is easy to include. I would suggest you to create these additional directories ONLY for the type files with the same names. And use a directory defined in 'output' section of rollup.config.json for the rest. |
@AntonPilyak you might be able to use |
Deciding if subdirectories should be used based on uniqueness of the paths would be a nightmare in an evolving project, suddenly your typing would move to a subfolder only because you added a new file with the same name in a different folder. Normally if you want pretty typings you will want to merge them into one file anyway (there was an npm module I keep forgetting the name of for that) |
I am using version {
// ...
input: {
foo: 'src/lorem/foo.ts',
bar: 'src/ipsum/bar.ts'
},
output: {
dir: 'dist',
format: 'cjs',
sourcemap: true,
}
} And I get this out:
Obviously, I'd like the I've tried fiddling with The definition files always end up inside |
Moreover it seems like it creates declaration files for every .ts file it finds, not just the entry points.
I've tried scoping This is all a bit confusing. ... I mean, fair enough if it needs to create a declaration file for |
Yeah, it generates declarations for all files found by tsconfig, if you don't want particular file touched, make sure it is excluded or not included in tsconfig. To see list of files found by typescript, run plugin with verbosity 3 and look for "parsed tsconfig" entry. |
For definitions path, where in your example would you want definition for |
Since it is a definition for Will TypeScript know to pair up |
The full scenario where this breaks down is: dir1/foo.ts us set as rollup input and it imports dir2/foo.ts. Rollup will create dist/foo.js bundle that will contain relevant parts of both source files, but typescript needs to create 2 type definition files with the same name somewhere. Easy solution is to use subfolders, mirroring source layout. I think that's what And yes, typescript will know where to find type definitions. So aside from being a bit messy, this shouldn't be a problem. If you want to deploy package with types on npm, set |
Ah, so this plugin doesn't really manage any part of the declaration-build process. It just fires up Still, if I'm only exposing/publishing a couple of modules, why would I want TypeScript to generate |
Thing is, I'm exporting a set of standalone modules ( Worse still, if I have {
// ...
input: {
fooscript: 'src/foo/index.ts',
barscript: 'src/bar/index.ts'
},
output: {
dir: 'dist',
format: 'cjs',
sourcemap: true,
}
} I get:
And now there's no way for TypeScript to pair up ... Is there no way your plugin could feed TypeScript the destination/output path for each JavaScript bundle file? It seems that tsc is receiving the original entry filename and holding on to it tight when generating the declarations. This is something I'm bound to try manually after Rollup has finished - but it would be so much nicer if the plugin would handle it automatically. |
No, plugin uses To write one type definition per rollup input we'll need to merge definitions for all imports for that input. Rollup might be seeing one input ts file, but it could be importing and bundling any number of source files when building that, those need types generated as well. Currently I'm erring on the side of generating definitions for everything typescript finds when looking at tsconfig file. If I generate types only for transpiled modules, type only files will be ignored. I might have to revisit that part, API changed considerable since that part was done... I'll have to look at how to make your example work. Might need better support for multiple bundles in one rollup config. How do you consume that package after building it though? It is not something you can publish on npm and import by package name... |
I'm making a collection of standalone utilities. The root-folder of the published/installed package contains all the built module files in a flat list. (I'm writing them in TS/ES6 in a nested folder-structure, with test-files mixed in, and relying on Rollup to convert them to a flat list of CJS files with an efficient mix of code-splitting and inlining for any shared code.) I then consume these tools by importing them by file-name – like so: import tool1 from 'myTools/tool1';
import tool2 from 'myTools/tool2'; The idea is that by not having a single entry point for the whole toolkit (no |
...I've been playing with I noticed, however, that if you run tsc and set the module/output format to either I wonder if this behavior could be exploited somehow - via file rename/move and unwrapping/rewriting the last (main) module declaration? Side note: I also notice that |
Hi again. Essentially, I generate export * from './__types/foo/index'; It's an ugly standalone hack, but it provides predictably correct results. I wonder if |
That would work, yeah. I have a couple of ideas (when I get around to them), for example generating First I might have to fix related bug of generating too many type declarations and limit that to only root file and anything it actually imports. |
A few thoughts: IMO, the only truly ugly part of my hack is the fact that it stands apart from the Rollup process. The intermediary Since the And if FWIW, here's the meat of my script: const { makeInputMap, getEntrypoints, distFolder, srcFolder } = require('./buildHelpers');
const { writeFileSync } = require('fs');
const { relative } = require('path');
const srcPrefixRe = new RegExp('^' + srcFolder + '/');
const tsExtRe = /\.tsx?$/;
const declDirRelative = './' + relative(
distFolder,
require('../tsconfig.json').compilerOptions.declarationDir
);
const tsEntrypoints = getEntrypoints()
.filter((fileName) => tsExtRe.test(fileName));
Object.entries(makeInputMap(tsEntrypoints))
.forEach(([moduleName, sourcePath]) => {
const tscDeclFile = sourcePath
.replace(srcPrefixRe, declDirRelative + '/')
.replace(tsExtRe, '');
const outFile = distFolder + '/' + moduleName + '.d.ts';
writeFileSync(
outFile,
[
'export * from "' + tscDeclFile + '";',
'import x from "' + tscDeclFile + '";',
'export default x;',
'',
].join('\n')
);
});
console.info('Created local declaration files for TypeScripted entrypoints.'); |
I updated the example above, as it seems that default exports have to be explicitly re-exported. Preliminary testing indicates that blindly re-exporting |
Hi, should this issue be resolved in v0.23.0 ? (...wondering if I should try and update my project to drop all of the custom faffing) |
0.23 will not generate declaration for everything in sight, but it will still generate declarations you actually import in their relative subfolders. You can probably update and have lighter types, but there is nothing to generate root type declarations yet. |
My custom workaround build script (see above) is gradually creeping into more and more of my projects. o_O |
FYI: Development of the official @rollup/plugin-typescript package has since resumed, and it now features type-checking and outputting declaration files, so I'm personally switching over to use that. |
@maranomynet Do you still rely on your script to move the declaration files after rollup does its thing? I didn't have much luck switching myself. |
Yup. that seems to be the only way unless you let
The results of 1 and 2 don't, and can't match when you're using an "input map" and code-splitting, etc. – AFAICT. ...unless you make the rollup plugin a whole lot more involved in the type-declaration generation process. |
Ah yeah that's sort of what I figured, thanks! Gave you a 👍 for your helpful answer, and the 👎 is for how I feel about it. |
This comment was marked as duplicate.
This comment was marked as duplicate.
For any struggling soul out there that has the same src folder structure as @maranomynet and me. I came across a quick fix for it, may be another ugly way but works for me. I'm using rollup-plugin/typescript2 so I'll go with its config in this answer:
And your declarationDir is:
So your output path needs to be like:
This way, rollup will include your types in the same directory as your component main.js and your TS consumer project will pick up the typings. So the bundle will look something like:
|
So I responded in #207 (comment) which appears to duplicate this issue (although OP here might have actually been experiencing a different issue, perhaps with Per my comment there, there are a few workarounds I can think of:
|
Another possible workaround:
|
input
rollupinput
input
input
input
input
(multi-entry)
What happens and why it is wrong
Possibly this is a misconfiguration on my end, but it looks like a bug. When you use an
object
as theinput
property for the rollup configuration, the typings file for the input files are generated in the root folder of the repo (same folder as the rollup config file) and not the target output directory (here,./dist
) along with the bundled JS file.Versions
rollup.config.js
tsconfig.json
Result:
Curiously, if you enter
entryFileNames: 'dist/[name].js'
it creates a superfluous subfolder calleddist
within thedist
folder and creates them there.The text was updated successfully, but these errors were encountered: