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

Add precompile support? #10490

Closed
acrazing opened this issue Aug 23, 2016 · 2 comments
Closed

Add precompile support? #10490

acrazing opened this issue Aug 23, 2016 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@acrazing
Copy link

acrazing commented Aug 23, 2016

  • We often use process.env.XXX to judge current compile environment is production or development (see follow code), this is same to c/c++ precompile, too many things could be simplified!

    // currently: use envify to handle this:
    if(process.env.NODE_ENV === 'production') {
      // do something
    }
    
    // use precompile: auto remove block if not true or replace with block if true
    #define NODE_ENV production
    #if NODE_ENV == production
      // do something
    #endif
  • Some functions just transform const to const, this could be done in compile, rather than in runtime, for example:

    function array2map<T>(data: T[], keyField: string = 'id'): { [key: string]: T } {
      const out: { [key: string]: T } = {}
      data.forEach((item) => out[(item as any)[keyField]] = item)
      return out
    }
    
    const options = [
      {
        name: 'Option A',
        value: 'A',
      },
      {
        name: 'Option B',
        value: 'B',
      }
    ]
    const optionsMap = arrray2map(options)
    // currently, we can only run this in runtime,
    // but this just transform one const to another const,
    // this completely could be done in compilation phase.
@yortus
Copy link
Contributor

yortus commented Aug 23, 2016

For your first point, there are already open issues like #4691.

For your second point, it is certainly useful to be able to do arbitrary computations at compile-time using the same source code (I believe D supports this). Not sure it will get much support here though. The compiler would have to actually execute the code to get the results, which would make the compiler pretty unstable given the many footguns of JS. For example if your array2Map had a bug in it, the compiler may never halt and not even give you an error message.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 23, 2016
@RyanCavanaugh
Copy link
Member

Duplicate on the first point as noted.

The second part is not the sort of thing we'd ever want to get into. It's much, much too complex relative to good solutions available today and the actual gain you'd get in runtime performance in the majority of scenarios.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants