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 ability to define custom variables #194

Closed
wants to merge 1 commit into from
Closed

Conversation

jaredpalmer
Copy link
Owner

@jaredpalmer jaredpalmer commented Sep 3, 2019

Add --define flag for defining custom variables that will be replaced at build-time. It uses babel-plugin-transform-replace-expressions under the hood. to replace expressions with values in noth your source code (in src) and in your node_modules as well. Other babel transforms are NOT executed on node_modules at this time, just this special replace-expression plugin.

Usage

Pass a comma-separated list of key-values to the --define flag in your package.json. You do not need to add quotes for strings values. Numbers WILL be cast to numbers automagically.

tsdx --build --define FOO=hello # => FOO = "hello"
tsdx --build --define FOO=hello,VERSION=2 # => FOO = "hello", VERSION = 2

It works the same way for tsdx watch --define

Example

We want to define ZOOP as "foo" and VERSION as 2 to in our package.

First, to get TypeScript to play nicely, you should add a ./src/env.d.ts file with variable declarations.

// ./src/env.d.ts
declare var ZOOP: string;
declare var VERSION: number;

Now we can use these like they were magically equivalent to whatever we set them to. Since these are treated as values by TypeScript, you do not need to import them.

// src/index.ts
export function sup() {
  console.log(VERSION)
  console.log(ZOOP)
}

In our package.json, we use the --define flag

// package.json
"scripts": {
   "start": "tsdx watch --define ZOOP=foo,VERSION=2",
   "build": "tsdx build --define ZOOP=foo,VERSION=2"
}

When we run yarn build or yarn start, TSDX will do the replacements and our output will look like this... (note: only including CJS outputs for brevity, replacements are made in all module formats).

// dist/mylib.cjs.development.js
'use strict';

function sup() {
  console.log(2);
  console.log("foo");
}

exports.sup = sup;
//# sourceMappingURL=mylib.cjs.development.js.map
// dist/mylib.cjs.production.js
"use strict";exports.sup=function(){console.log(2);console.log("foo")};
//# sourceMappingURL=mylib.cjs.production.min.js.map	

Future Work / Discussion

  • Ability to define different values for dev/prod?

@jaredpalmer jaredpalmer requested a review from swyxio September 3, 2019 15:19
@jaredpalmer
Copy link
Owner Author

Will cut a release tomorrrow when I land in Poland

@swyxio
Copy link
Collaborator

swyxio commented Sep 5, 2019

the example is well written, but i'm not sure when i would want this. whats the benefit? i assume this is something youre doing in formik. if you want my personal reaction, i feel this is a bit niche and something i'd lean on the .babelrc to set up separately.

and there's also the question of the api. if we added this to tsdx.config.js, we wouldn't need to automagically cast numbers, and we'd be able to resolve these values from dotenv, ternary it based on process.env, or pull from other data sources. feels a lot more powerful than just adding the flag, imo.

but this is me kinda bikeshedding. do what u want if u feel strong enough about it :)

@jaredpalmer
Copy link
Owner Author

Yeah i had started down this path before implementing tsdx.config.js.

@jaredpalmer jaredpalmer closed this Sep 9, 2019
@swyxio swyxio deleted the feat/defines branch September 9, 2019 17:11
@dfsa-bot
Copy link

dfsa-bot commented May 6, 2020

Is this still applicable? Can't find any instructions in main readme file

@agilgur5
Copy link
Collaborator

agilgur5 commented May 6, 2020

@dfsa-bot as you can see above, this PR was closed, not merged. There aren't instructions as it never existed.
Per #636 and above comments, you can use babel-plugin-transform-replace-expressions for the same effect. It's not really worthwhile to increase the surface area of the TSDX API for a redundant flag.

@dfsa-bot
Copy link

dfsa-bot commented May 7, 2020

@agilgur5 do you mind sharing an example of the above?

@agilgur5
Copy link
Collaborator

agilgur5 commented May 7, 2020

@dfsa-bot Sorry, this isn't a support forum and I do not have the resources to help with everyone's code, especially for a different library.

I would encourage you to look at the docs of babel-plugin-transform-replace-expressions, which has its own examples.

Repository owner locked as resolved and limited conversation to collaborators May 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants