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

Comments

Projects
None yet
2 participants
@remojansen
Member

remojansen commented May 6, 2015

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

This comment has been minimized.

Show comment
Hide comment
@sanex3339

sanex3339 Nov 5, 2015

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
            }
        }))
Contributor

sanex3339 commented Nov 5, 2015

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

This comment has been minimized.

Show comment
Hide comment
@remojansen

remojansen Nov 6, 2015

Member

@sanex3339 thanks for sharing that!

Member

remojansen commented Nov 6, 2015

@sanex3339 thanks for sharing that!

This was referenced Jan 9, 2016

@remojansen remojansen closed this in #34 Jan 12, 2016

@remojansen

This comment has been minimized.

Show comment
Hide comment
@remojansen

remojansen Jan 12, 2016

Member

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

Member

remojansen commented Jan 12, 2016

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