-
Notifications
You must be signed in to change notification settings - Fork 109
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
Passing @define down to closure compiler #434
Comments
Why don't you pass Or maybe to restate: what are you trying to do? |
$ tsickle test.ts --define DEBUG=true
unknown flag '--define' If you mean on the closure compiler, What I was trying to do is have some compile-time flags to change how the code behaves depending on the target environment (dev/staging vs production). |
Ah, so you want to introduce new `@define`s in TypeScript code.
According to internal docs, this should work as a goog.module:
```js
goog.module('foo');
goog.module.declareLegacyNamespace();
/** @define {string} */
exports.SOME_DEFINE = 'hello';
```
You'd then set `--define foo.SOME_DEFINE` to control this.
I'm not sure we can generate this code from TypeScript via tsickle
successfully - we might bale on the type on the `@define`. But something
like this could work:
```ts
/** @define {string} */
export SOME_DEFINE = 'hello';
```
And then the define would be called whatever your module prefix is, e.g.
`--define path.to.my.file.SOME_DEFINE`. We might need to special case
`@define` somewhere in tsickle for this to work.
|
Using defines already present in javascript (e.g. |
Shouldn't you be using Clutz to generate a `.d.ts` file that contains a
definition for our `@define`?
|
Good point, I haven't looked yet at how clutz handles Thanks for the detailed proposal to have |
I think this now works: // file: my_flag.ts
/** @define */
const MY_FLAG = goog.define('my.project.MY_FLAG', false);
export function logFlag() {
console.log(`flag is ${MY_FLAG}`);
} |
Oh wait, I confused goog.define with @define. |
I think it's the same, Ruben is just using the syntax without the
goog.define call which I think then uses the identifier name.
@sdh fixed this by adding support for defines in modules.
|
@mprobst what's the change from @sdh? I see that google/closure-compiler#1601 is still open. In order for the example from @evmar to work correctly, you need proper typings of declare namespace goog {
function define<T>(namespace: string, defVal: T): T;
} If you want to run your code also through tsc (or ts-node or jasmine), using It would be awesome if tsickle was able to perform the following transformation: // my_module.ts
/** @define */
export const MY_FLAG = false; to // my_module.js
/** @define {boolean} */
exports.MY_FLAG = goog.define('path.to.my_module.MY_FLAG', false); That would enable using other processors than tsickle during development without depending on the closure library. |
Ack, but I think that's far out of scope for tsickle. It's not intended to polyfill |
I suspect this might have worked at some point after reading Martin's comment here: angular/ts-minify#59
However, I tried the following two approaches today and none worked:
This doesn't work because tsickle generates a closure module even though this typescript file is not a module to begin with (i.e. it doesn't export anything). And closure compiler can't override @defines inside modules, as per google/closure-compiler#1601.
.d.ts:
.js
This doesn't work because the externs generated by tsickle also declare both
debug
anddebug.DEBUG
, which clash with the js file when running the closure compiler.What did work, was putting that constant in a regular .js file using
goog.provide()
instead ofgoog.module()
, and then using clutz to make that available to typescript in the form of a module declaration. Besides being really cumbersome, that approach doesn't work for closure's owngoog.DEBUG
becauseclosure/goog/base.js
doesn't have agoog.provide
.Both seem worth fixing:
In 1 I would expect tsickle to not emit a module, since the typescript file is also not a module.
In 2, it would be useful to be able to annotate a
declare
with some@doNotExport
annotation, if we know that something will declare that later during closure compilation (likeclosure/goog/base.js
). It seems like tsickle already has some way of ignoringd.ts
files living insidenode_modules
, because it doesn't generate externs for those, so perhaps that could be made controllable by the programmer?The text was updated successfully, but these errors were encountered: