Code Monkey home page Code Monkey logo

typescript-brunch's Introduction

typescript-brunch Build Status

Adds TypeScript support to Brunch.

Usage

Install the plugin and TypeScript via NPM with npm install --save-dev typescript-brunch typescript.

Or, do manual install:

  • Add "typescript-brunch": "x.y" to package.json of your brunch app. Pick a plugin version that corresponds to your major.minor (x.y) Brunch version. You still need to install TypeScript manually npm install --save-dev typescript.
  • If you want to use git version of plugin, add "typescript-brunch": "github:brunch/typescript-brunch".

brunch-config

If there is a tsconfig.json file present in the root of your project, the compilerOptions from that will be used as defaults.

Any options in your brunch-config.js or brunch-config.coffee will override those.

module.exports = {
  // ...
  plugins: {
    brunchTypescript: {
      removeComments: true
    }
  }
};

If no options are provided, this plugin will default to the following:

{
  target: "es5",
  module: "commonjs",
  emitDecoratorMetadata: true,
  experimentalDecorators: true
}

Errors

From version 1.8.2 up to current version, this plugin may report TypeScript errors that you are not expecting. This is due to the fact that this plugin compiles each file separately in isolation, and doesn't take advantage of the full TypeScript project. As such there are some errors which may appear which are false positives.

Starting in 1.8.3 you could add an ignoreErrors property the plugin config object in the brunch-config file. This was an array of error numbers to ignore. Starting in 2.0.1, you can ignore all TypeScript errors by setting ignoreErrors to true (this was broken in 2.0.0). Setting it to an array still works as before.

We are hoping to support the full language service, at least for brunch build at some point, but until then, we recommend that you add tsc --noEmit to your test script or build script to catch proper errors within your project.

Just to note that this shouldn't affect any TypeScript support your editor/IDE provides, which should also allow you to identify real errors.

Contributors

License

The MIT License (MIT)

Copyright (c) 2017 Baptiste Donaux (http://www.baptiste-donaux.fr)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

typescript-brunch's People

Contributors

baptistedonaux avatar colinbate avatar denysdovhan avatar jpoehls avatar kripod avatar oeb25 avatar paulmedynski avatar paulmillr avatar rudi-c avatar shvaikalesh avatar thorn1089 avatar xtity avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

typescript-brunch's Issues

BaseUrl support

To avoid having import "web/static/app/foo/bar" in my phoenix app, I tried to add "baseUrl":"web/static/app" to my tsconfig.conf so I could do import "foo/bar"

But, while this works if I call tsc directly, brunch-typescript fails to resolve the dependency.

Is there any way to have baseUrl support in brunch-typescript?

Error while loading the tsconfig.json should not be hidden

Steps to reproduce:

  1. Create any simple project with .tsx file containing JSX tags and tsconfig.json setting "jsx":"react"
  2. Add a comment inside tsconfig.json
  3. Try to build the project

Result: compilation will fail with "Unrecognized token" error.
Expected: compilation should fail with an error pointing to the real cause.

error: Compiling of app/vendor.ts failed. Cannot read property 'resolvedModule' of undefined

Hi everyone, I'm trying to import libs in npm_modules via import keyword but had the next error: "error: Compiling of app/vendor.ts failed. Cannot read property 'resolvedModule' of undefined".

In my fork of the plugin I added some fixes than solve 'resolvedModule' issue.
https://github.com/davidjsalazarmoreno/brunch-typescript/blob/master/index.js#L14
https://github.com/davidjsalazarmoreno/brunch-typescript/blob/master/index.js#L58

But seems not work with latest versions of brunch plugin, the error aparently gone but not produce any javascript file from .ts with node_modules imports.

You can see the issue in this repository: https://github.com/davidjsalazarmoreno/brunch-typescript-issue

tsconfig: https://github.com/davidjsalazarmoreno/brunch-typescript-issue/blob/master/tsconfig.json
brunch-config: https://github.com/davidjsalazarmoreno/brunch-typescript-issue/blob/master/brunch-config.js

Thanks everyone.

Remove compiler error suppression

Once #49 is merged, we can remove some or all of the compiler error/warning suppression. Most of them seem to be unnecessary now that the compiler can load other files properly.

My goal is to get this plugin to report exactly the same errors as tsc does on the command line. Right now we're suppressing many actual problems.

how are global namespaces working with this plugin?

This relates to #18 and what @colinbate explains

From my understanding, this plugin looks at each file without any outside context.

I'm working on this Inferno project using this plugin and I'm trying to wrap my head around why commenting out

exports.npm = {
    globals: {
        Inferno: 'inferno',
        Component: 'inferno-component'
    }
}

breaks then. Without the ts plugin, those globals would be accessible without having to explicitly import them, so it makes sense to declare them. However with the ts plugin config, I see that it's still necessary to declare those globals, or else those modules are unrecognizable. Can someone provide context as to why that happens?

Compile .tsx files by default

As TypeScript evolves, the ability of compiling .tsx files has been added to tsc in version 1.6. Those files should now be compiled by default when using brunch-typescript.

Plugin doesn't respect Brunch conventions configuration

The plugin doesn't properly delegate to Brunch's conventions property on config. There is a hard-coded regex against (^bower_components|vendor|node_modules) at

this.isIgnored = anymatch(options.ignore || /(^bower_components|vendor|node_modules)/);

This means that if you have code in your app/ directory under a directory that starts with "vendor", even if you've properly adjusted the Brunch conventions, the Typescript files won't be compiled. On the latest versions of both Brunch and typescript-brunch, this causes the original Typescript source to be passed to deppack, resulting in a very confusing error when a statement of the form

import Foo = require("bar");

is found.

This plugin should create the matcher by using the value of conventions.vendor rather than hard-coding a constant value. Note that the conventions properties may themselves be functions or regexes rather than string literals, e.g.

conventions: {
  vendor: function() { return false; }
}

Use the tsconfig.json if present.

This is an enhancement that would be useful to have, particularly since most editors/IDEs want you to have the tsconfig.json file anyway. It should be a matter of reading in the config and passing the compilerOptions object to the transpileModule API.

Option noEmit doesn't work

I was getting this error while trying to build typescript files: error: Compiling of app/drawable.ts failed. Debug Failure. False expression: Output generation failed

Looks like if I put "noEmit": true, in my tsconfig.json, to prevent my error checker from generating any files, brunch-typescript fails to override that.
Even if I put this in my brunch-config.coffee:

plugins:
    brunchTypescript:
        noEmit: false
        removeComments: true
        watch: false

I found a dirty hack to fix this: put this line in transpile.js (after options.noResolve = true; line):
options.noEmit = false;

Definition files usage

I tried to use a definition file with this plugin. I naively copied it next to my app.ts, but I only get this error:

error: Compiling of app/excalibur.d.ts failed. Debug Failure. False expression: Output generation failed

The excalibur.d.ts comes from https://github.com/excaliburjs/Excalibur/r eleases/tag/v0.7.0 but I get the same error with an empty test.d.ts

updating TypesScript from 2.1 to 2.5

I decided to write this up, because it wasn't clear for me how to do it and maybe this will be helpful for other people.

If you are using npm 5.0 with package-lock.json or shrinkwrap you can update to later versions of typescript without having to use forks.

Principle described here, and here's example in case package-lock.json file:

    "brunch-typescript": {
      "version": "2.1.0",
      "resolved": "https://registry.npmjs.org/brunch-typescript/-/brunch-typescript-2.1.0.tgz",
      "integrity": "sha1-CbJJHp2mxm4o+6udxIxmFhsL6xg=",
      "dev": true,
      "requires": {
        "anymatch": "1.3.0",
        "typescript": "https://registry.npmjs.org/typescript/-/typescript-2.5.2.tgz"
      },
      "dependencies": {
        "typescript": {
          "version": "https://registry.npmjs.org/typescript/-/typescript-2.5.2.tgz",
          "integrity": "sha1-8PBF4Zb2mnLwayX9O9OdAcPOmYQ=",
          "dev": true
        }
      }
    },

just find "brunch-typescript" section in your file and change "2.1.6" to "https://registry.npmjs.org/typescript/-/typescript-2.5.2.tgz", then run npm install.

Ambient global namespace not found

I am trying to use the youtube definition file https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/7de6c3dd94feaeb21f20054b9f30d5dabc5efabd/youtube/youtube.d.ts

This file is referenced from my app.ts file with /// <reference path="youtube.d.ts" />.

I can compile my app.ts file with tsc -m commonjs, but with brunch, I got:
Error 2503: Cannot find namespace 'YT'. (Line: 104, Col: 28)

If I copy the content of the youtube.d.ts file in my app.ts brunch can then compile it.

Any idea?

Can't get typescript-brunch to use my tsconfig.json

Hi there,

I've been using brunch with typescript-brunch just fine for a while now. I've just started using async/await in my TS and I've hit a wall when changing the TS target to ES2015.

My directory structure is how brunch new set it up:

/app
- application.ts
- ...
/assets
- ...
/public
- ...
tsconfig.json
brunch-config.js
...

My tsconfig.json is

{
    "compilerOptions": { 
      "target": "es2015",
      "module": "commonjs",
      "emitDecoratorMetadata": true,
      "experimentalDecorators": true
    }
}

My brunch-config.js is

module.exports = {
   npm: {
     globals: {
      $: 'jquery',
      jQuery: 'jquery',
      NProgress: 'nprogress'
  }},
  files: {
    javascripts: {
      joinTo: {
        'js/app.js': /^app/,
        'js/vendor.js': /(^node_modules|^vendor)/ 
      },
      order: {
        before: /.*jquery.js*/
      }
    },
    stylesheets: { joinTo: 'app.css' },
    templates: { joinTo: 'app.js' }
  },
  plugins: {
    sass: {
      options: {
        includePaths: [
          'node_modules/bootstrap-sass/assets/stylesheets/',
          'vendor/'
        ]
      }
    }
  }
}

When I do brunch build with the async TS code all I get is

error: Compiling of app/application.ts failed.

However when I run tsc in the root directory, compilation succeeds.

What am I doing wrong?

Allow targetExtension specified in configuration

Allowing the developer to specify the target extension in the plugin configuration would allow for plugin output to be passed to babel plugin for custom jsx transformation before writing to file. My use case is the customized JSX transform required by infernojs. I added the following to the TypescriptCompiler constructor after mapping of options keys (line 67) , and it worked as expected:

if(this.options.targetExtension){this.targetExtension = this.options.targetExtension};

I suppose you could do something like specifying an option in the config like:

brunchTypescript:{targetJS:true}

and then add the following to the constructor:

if(this.options.targetJS){this.targetExtension = ".js"};

brunch can't compile the files if lib option is set in tsconfig.json

as soon as I set lib in tsconfig.json all of my typescript files are failing to compile with this "helpful" message:

17:57:27 - error: Compiling of File.ts failed. TypeError: Cannot read property 'lineMap' of undefined

as I understand it, that's what we get from brunch trying to set --lib and --noLib at the same time, so to fix it in my project I used the following the the brunch-config.js

module.exports = {
    plugins: {
        brunchTypescript: {
            "lib": undefined,
        }
    }
}

and proper fix would be to do that in the transpile.js

allowSyntheticDefaultImports imports undefined modules, when using with e.g. *act (react, preact)

Hi,

I've been using brunch now for about a month and absolutely love it! This is amazing!

Anyway, my issue is that the following compilerOption does not work as expected-

{
	"compilerOptions": {
		"allowSyntheticDefaultImports": true
	}
}

I'm supposed to be able to import packages with no default import as if I were,

import * as Package from 'packageName';

but when I try and

console.log({ Package })

if I try and use allowSyntheticDefaultImports, the Package variable is undefined.

What's going on here?
Thanks!

Error: processing of Foo.tsx failed. SyntaxError: Unexpected token, despite `ignoreErrors: true`

I'm using typescript-brunch as follows:

brunch-config.js

exports.files = {
  javascripts: {
    joinTo: {
      'vendor.js': /^(?!app)/,
      'app.js': /^app/,
    }
  },
  stylesheets: { joinTo: 'app.css' }
};

exports.plugins = {
  brunchTypescript: {
    ignoreErrors: true,
  },
  babel: { presets: ['latest', 'react'] }
};

tsconfig.json

{
    "compilerOptions": {
      "target": "es5",
      "module": "commonjs",
      "jsx": "react",
      "declaration": true,
      "sourceMap": true,
      "experimentalDecorators": true,
      "lib": ["es2017", "dom"],
      "suppressImplicitAnyIndexErrors": true,
      "removeComments": true,
      "esModuleInterop": true
    },
    "exclude": ["node_modules", "dist"]
}

As far as I can tell, my typescript-brunch setup is consistent with the instructions in the README files. But whenever I run brunch, I get:

10:42:15 - info: compiling
10:42:15 - error: Processing of app/components/Edit.tsx failed. SyntaxError: Unexpected token (18:16)
Stack trace was suppressed. Run with `LOGGY_STACKS=1` to see the trace. 
... and so on for every tsx file

This causes brunch build --production to fail.

There are no errors in my code; I can run tsc -noEmit just fine. And the 18:16 position indicator is not accurate and doesn't point to anything actionable.

After a lot of trial and error, I found that this error only occurs for TypeScript files that contain JSX code.

I tried other things, like

exports.files = {
  javascripts: {
    joinTo: {
      'vendor.js': /^(?!app)/,
      // 'app.js': /^app/,
    },
    entryPoints: {
      'app/initialize.tsx': 'app.js',
    },
...

but the errors persist. What am I doing wrong?

Compilation of wrong types should not proceed

Try this:

let myAdd: (baseValue:number, increment:number) => number =
    function(x, y) { return x + y; };

exports.myAdd = myAdd;
exports.zzz = myAdd(5, '2');

It seems to go through. Not sure why.

outFile and watching

Hi there,

Do I need to do extra things in order to make the plugin work?

"outFile": "web/static/ts/backoffice/dist/backoffice.js"

I edit my files but I have to run

tsc

manually to see the dist file generated. So I conclude that the watching of the plugin is not really working with my config.

Usage a bit unclear / incomplete.

Hi there,

I was trying to hunt down a brunch plugin to support typescript and this repo looks like the most active. But, one thing I noticed is that although the Usage section goes in great detail on how to install and configure the plugin, there's no mention or examples of how to set the files pattern (joinTo) to watch the TypeScript files.

Can both *.ts and *.js files be watched with file patterns? Something like:

files: {
    javascripts: { joinTo: {
       'app.js': /^app\/[a-zA-Z0-9_\-]*\.(js|ts)/
    }
 },

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.