9

I have a bunch of typescript files following ecmascript 2015 module pattern in source folder. I have tsconfig setup to output the transpiled files to lib folder. That works good. But is there any way to bundle them together into a sigle file that can be used in the browser? I want my module to be available via npm and also as a script tag.

Refer to my dummy github project for details. tried the gulp-typescript, concat, uglify way, but in vain.

1 Answer 1

22

Use webpack with ts-loader to compile TypeScript and create a single bundle:

  1. Install webpack

    > npm install webpack ts-loader --save-dev
    
  2. Create webpack.config.js

    const path = require('path');
    module.exports = {
       entry: "./src/index.ts",
       output: {
           filename: "bundle.js",
           path: path.resolve(__dirname, 'dist')
       },
       resolve: {
           extensions: [".webpack.js", ".web.js", ".ts", ".js"]
       },
       module: {
           rules: [{ test: /\.ts$/, loader: "ts-loader" }]
       }
    }
    
  3. Run webpack

    > webpack
    
  4. You may also need to install webpack-cli (messages will tell you as you go to run the webpack command)

    > npm install webpack-cli
    

See the documentation on webpack and ts-loader for more as configuration options change over time.

Sign up to request clarification or add additional context in comments.

1 Comment

I am actually creating a library. So this is a definitely a good start. But i will need to separate the location of transpiled js and bundled js. Also need support for both node and browser environments. I am considering to incorporate gulp which will create the js, and then apply webpack wiith advanced options to create library bundles from the js, without needing ts-loader. What's your opinion?

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.