0

I am working on HTML+TS+CSS project with VS2015. Is it possible to add JS file to combined TS output file? If so how to do that?

2 Answers 2

1

You can use the setting allowJs.

Allow JavaScript files to be compiled.

When combined with the outFile setting, it'll output the JS files to the combined file as well as the compiled TS.

To test I made a quick web app, that included bootstrap and jquery and what not.

I then also added a custom JS file, a custom TS file, and a tsconfig.json.

This is the contents of the tsconfig.json file:

{
  "compilerOptions": {
    "allowJs": true,
    "outFile": "../output.js",

    "alwaysStrict": true,
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": false,
    "sourceMap": true,
    "target": "es5"
  },
  "exclude": [
    "node_modules",
    "wwwroot"
  ]
}

Looking at the output.js, I can see bootstrap, jquery, and both my custom scripts.

Note that I put the output file outside of the project directory. This is because by default the TS compiler will pull in all TS and JS files in the project. So if the output file is in the project, and all files in the project are included ... you get an error message telling you that's not a good idea.

You can control what files and paths are included and excluded by using the excluded and included properties in the tsconfig.json.

e.g.

{
  "compilerOptions": {
    "allowJs": true,
    "outFile": "output.js",

    "alwaysStrict": true,
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": false,
    "sourceMap": true,
    "target": "es5"
  },
  "exclude": [
    "node_modules",
    "Scripts/bootstrap.js",
    "Scripts/jquery-1.10.2.js",
    "output.js"
  ]
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can import .js file into your .ts file by

import 'jsFilePath';

and then declare variable to be used. for example -

declare wheel: any;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.