-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Typescript compiler --copy-files option to copy non-typescript files to dist folder #30835
Comments
Duplicate of #15222? |
If no one else has been able to write a good file copy script, I don't think we'll be the first. Anyway explicit non-goal 4 is:
|
This is a great idea. Especially for those who want have minimum build steps (e.g. just |
This would be really handy! |
For those who need this feature, you can use copyfiles to implement it. https://quangphamsoft.wordpress.com/2017/05/23/typescript-compiler-copy-html/ |
and add it to For example my scripts looks like this
|
honestly, I have just spent hours and hours on a very weird problem just to realize that the reason was that copyfiles does not work the same way under linux and windows. |
We need to copy samples folder to make our tests work, but [typescript compiler doesn't support copying of non-ts files (microsoft/TypeScript#30835) We need to make sure `copyfiles` utility works file on Linux and OSX (tested only on Windows so far)
My version: {
"scripts": {
"prebuild": "del lib/",
"build": "tsc",
"postbuild": "cpy '**/*' '!**/*.ts' ../lib/ --cwd=src/ --no-overwrite --parents"
},
"devDependencies": {
"cpy-cli": "^2.0.0",
"del-cli": "^3.0.0"
}
} |
@RyanCavanaugh babel-cli provides this ability: https://babeljs.io/docs/en/babel-cli#copy-files So why not TypeScript... 😄 |
|
@RyanCavanaugh: Please please reopen this issue: The discussion here is absolutely misleading, putting this feature in the context of "build-piplines"... However what it is really about is "be polite, keep something in the state you encountered on arrival and dont steal stuff". We do not want build pipelines we want to TS to keep our code directory as it was and do not mess this code/state up. The contents of any directory has been created under much thought. As already mentioned, especially on the serverside you might have other code-snippets, config and stuff in non .ts endings that you need colocated with your code. And deleting these files (by not copying them) is the worst DX you can imagine of. TS (and the transpilation process) should behave as I would have written directly a JS files instead of an TS file and keep the directory tree completely intact as it was. Please include an option (as marcospgp has greatly described) as to simply "keep the state as TS encountered it", when using |
Another thing to consider. Although I could write a build step to do this for me, the fact that TypeScript already has the list of projects where I need this to happen (via project references), means we'll now have to maintain this list in two places. |
I ended up creating a
|
Another use case, is in a mixed source environment. I want my javascript source code to be "transpiled" (the reason I use quotes, is that this may only be a nop / straight copy, or it could convert from ES?? to requested target) This SHOULD BE (imho) in the purview of the tsc compiler. |
@redevill, is what you want different to what |
Yes, because I would still like to output the *.d.ts files. However if you use the allowJs:true, you may not also use the declaration: true. |
@redevill, this was fixed in TypeScript 3.7. |
OH!! sweet - Missed that part, I'm on the previous version. Thank you! |
has this been resolved? |
@naufalkhalid - if you are looking to have things like assets *.jpg files copied to the dist folder, my understanding is that the compiler will not do it - and the OP feature request remains unresolved. The argument above, is to keep the responsibility of the compiler strictly to compilation. Package assembly, to be delegated as a separate responsibility, to some other software lib. In my special case I wanted Javascript files copied to the dist folder. In this case you can tell the compiler to include javascript files in your transpile process, and they will be copied to the dist folder as part of this process. (compiling javascript as well as typescript is part of the transpilers job) |
If TS is unwilling to implement an option like this is there any sort of hook that could be used at the end of TS running to activate a copy script? Perhaps that could be an acceptable solution where TS doesn't do the copying itself but provides a way to hook into it post run to allow the dev to do whatever else they please. |
I have seen a several implementations (not hooks) but build scripts where such copy procedures are bundled together with the Compilation request... Gulp, Grunt... or even script section of the package.json can be used to launch your own JS to perform these actions in a "postMyScriptNameHere" script. |
so as it stand right now, we basically need to copy manually? tsc should provide this functionality.. its really important, since we mostly work on web stuff, we normally work with file other than *.ts and *.js |
Honestly I don't get it, it feels like hypocrisy to me. As mentioned here #30835 (comment) If tsc is already walking through all files in the source, copying one to another wouldn't be much of a hassle to implement |
I wanted to share my use case too. I am using a light weight database library that uses I would love to NOT have to manually copy those files, and if I were using babel I wouldn't have to. For now it seems I'll just have to add a manual copy step for these items. |
Spoiler: I am the author of TSCP I was struggling with the same problem, and then created typescript-cp. It detects the source and output directories based on your project's tsconfig.json (using typescript's own config parser, to work as smooth as possible) and copies the non-TS files to the outDir. It has a very basic CLI, with similar arguments to typescript's tsc, but this is # Copy
$ tscp
# Copy for TS project references
$ tscp -b
# Watcher
$ tscp -w
# Watcher for TS project references
$ tscp -b -w
# Custom compiler settings
$ tscp -p tsconfig.production.json
# Help
$ tscp -h Examplepackage.json {
//...
"scripts": {
"start": "tsc -w & tscp -w",
"build": "tsc && tscp"
},
//...
} Feel free to open an issue if something isn't working as expected. |
Knowing this is likely never happening anyways, I felt the need to propose a response to this comment:
I think the point that is missed here is that this isn't a random build issue people want the TypeScript compiler to solve for them. This is a problem created by TypeScript. A lot of npm packages don't need to have a build process at all, since the source code is directly executed (there's nothing to build). But the nature of TypeScript always requires a build process, and in an ecosystem that is often used to not needing it. Since the build output is dumped in a different folder, all of the existing resource files are lost in that process. If someone wants to obfuscate their code, then I agree, that's not the responsibility of tsc. They would need a build process to do that anyways. But when tsc is the sole reason for the problem existing, it feels like it should provide a solution. All that said, I think the right option would be for this to be handled via an entry in tsconfig.json rather than a command line flag. Manually specifying what input files your code requires at runtime with glob patterns seems like a pretty straightforward solution. |
@stabai - You could even surmise that: You may NOT use Typescript unless you also also choose to use some kind of CI. (as TSC - does not provide copy services.) |
I'd like to add my use case here. I'm experimenting with TypeScript as part of an AWS SAM application. In this application, I've got a few different folders with related files in them, and each has a When I run Similarly, I can't use sam-local to test and debug running code, because the transpired version doesn't have the node modules. Using sam build works around this, because it does an NPM ci step as part of building, but then SAM is running off that build artefact. Simply copying the JS files, as is, instead of transpiling, would go a long way to solve this. In particular, making this work with |
What do you mean by "node_modules folders aren't copied"? They aren't supposed to be copied. Also, what do you mean by "simply copying the js files?" If you are currently migrating, you can enable js files in tsconfig |
I can't run the generated JS files if they're in a directory by themselves. They need to have the rest of the files of the project with them - e.g. the node_modules folder, any resource files, etc. Otherwise I can't even run tests. So having As for "simply copying the JS files" - I would like a mode where the JS files were copied - not transpiled to the target ES version. Using allowJS does not put the exact JS files in - it puts a transpired version in, possibly with significant changes. And, in my test case, the transpiled version broke tests, whilst putting the original version in did not. Now, this can be mitigated by not using an outDir - having the TS files transpile to JS in the same folder, and putting up with any warnings for the existing JS files that Simply put, it's important to be able to have a directory, that has the JS files that you're going to publish, in a spot where you can use them locally, and to have that be updated during development. This is not a CI concern - it's an issue for local development. |
This seems like a different issue; Do you have a repo with minimal repro? @twasink |
I'll be making one for a blog post/rant this weekend. The repo I tried this out with is a private work one. But this is the issue that I'm talking about: having a directory that I can use for things such as running unit tests, that has a consistent view of all of my code base (including dependencies, non-JS files, etc). If the only tool I'm using that produces transformed code is |
For anyone struggling to work around this issue, this is what worked for my use case (docker multistage build):
It's a moderately jank solution, but no extra packages/installations required & it's relatively fast in execution time. |
A couple of other approaches which may be helpful to someone.. (Copying a Using
Using
|
I had to copy css files (I need to be able to import the css files from another package), this worked for me: npm install copyfiles -g Inside "scripts" of tsc --build && copyfiles -u 1 src/**/*.css dist |
Had to copy
|
Thanks for the tip. It worked for me! Cheers! |
The below way works for me # Install the dependence
npm install --save-dev copyfiles
# Update the scripts section in the package.json file
{
"copy-files": "copyfiles -u 1 src/**/*.js dist/",
}
|
I have created a simple npm install typescript @rnm/tscx -D {
"scripts": {
"build": "tscx -p tsconfig.json", // same as "tsc -p tsconfig.json"
"dev": "tscx -p tsconfig.json --watch --copyfiles --exec bootstrap.js"
}
} npm run dev It works and makes my life easier.🤣 |
5 years passed, no official solution??? it's 100% necessary feature for 99.99999% frontend project. |
Search Terms
typescript tsc copy files
Suggestion
The typescript compiler should have an option to copy non-compiled files, similarly to Babel: https://babeljs.io/docs/en/babel-cli#copy-files
Use Cases
Almost all use cases where typescript files are compiled into a different folder will require bringing along other non-typescript files.
For the frontend, usually there already is a build system in place so this is trivial.
In the backend however, there is rarely a need for a build system, and thus developers often go out of their way to set up a system where non-typescript files are watched for changes and copied.
Existing npm packages for this kind of functionality are all poorly maintained and unsafe. Currently my only option is either copying all files on any change or writing a custom script.
Examples
tsc -w --copy-files
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: