Skip to content
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

Allow the minifiers to rename the function parameters and still be able to inject #14

Closed
remojansen opened this issue May 6, 2015 · 3 comments · Fixed by #34
Closed

Comments

@remojansen
Copy link
Member

To allow the minifiers to rename the function parameters and still be able to inject the right services, the function needs to be annotated.

Will be implemented using Reflection Metadata API. Useful links:
microsoft/TypeScript#2249
https://www.npmjs.com/package/reflect-metadata
https://github.com/matjaz/property-DI/

@sanex3339
Copy link
Contributor

Right now in my project i find some workaround with help of gulp.

After typescript compiling but before minification, i parse with regexp current stream content (gulp-data plugin), find any functions arguments matched to pattern like: ISettings, IParams, IInterfaceA. And then push them into array and put that array as except parameter for uglify2 plugin.

let reservedVariables = [];
.pipe(ts({
            out:  'output.js',
            typescript: require('typescript')
        }))
.pipe(data(function(file) {
            let results,
                result,
                res;

            results = String(file.contents).match(/function[^(]*\(([^)]*)\)/g);

            for (result in results) {
                res = results[result].match(/function[^(]*\(([^)]*)\)/)[1].split(',');

                res.forEach((value) => {
                    if (value === '') {
                        return false;
                    }

                    if (/I[A-Z]\w*/.test(value)) {
                        reservedVariables.push(value);
                    }
                });
            }

            reservedVariables = reservedVariables.filter(function (value, index, self) {
                return self.indexOf(value) === index;
            });

            return  reservedVariables;
        }))
.pipe(uglify({
            mangle: {
                except: reservedVariables
            }
        }))

@remojansen
Copy link
Member Author

@sanex3339 thanks for sharing that!

This was referenced Jan 9, 2016
@remojansen
Copy link
Member Author

You can now use:

@inject("IFoo","IBar")
class FooBar implements IFooBar {

  public foo : IFoo;
  public bar : IBar;

  public log() {
    console.log("foobar");
  }

  constructor(foo : IFoo, bar : IBar) {
    this.foo = foo;
    this.bar = bar;
  }
}

For more details check out #33

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants