Skip to content

Import package version from package.json #1764

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

Merged
merged 4 commits into from
May 10, 2019

Conversation

mmermerkaya
Copy link
Contributor

@mmermerkaya mmermerkaya commented May 7, 2019

Imports version strings directly from package.json, which allows TS to do type checking and warn if there's a typo or a type mismatch. Removes the need for rollup-plugin-replace.

@@ -15,6 +15,8 @@
* limitations under the License.
*/

import './setup';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:/


/** The global object in both Node and browser environments. */
export const root: any =
(typeof self === 'object' && self.self === self && self) ||

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bonkers

Copy link
Member

@Feiyang1 Feiyang1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like being type safe and eliminating typos, but It's unfortunate that it requires setting variables on global for testing. I feel this extra (hackish) step makes our code harder to understand.

How about we import the version number from package.json directly. We just need an extra rollup plugin and setting a typescript compiler flag. It has all the benefits, is easy to understand and the change is minimal.
I made a PR using this approach for @firebase/app - #1775 WDYT?

values: {
JSCORE_VERSION: firebasePkg.version
JSCORE_VERSION: JSON.stringify(firebasePkg.version)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

firebasePkg.version is already string. Isn't JSON.stringify redundant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without stringify, the replacement would be the value of the string, as x.y.z. So the code would look like this: const version = 1.2.3;

With stringify, it's instead replaced with "x.y.z", so the example would be const version = "1.2.3";, which is what we want.

Rollup plugin example here: https://github.com/rollup/rollup-plugin-replace#usage

@@ -28,7 +28,7 @@ import { createFirebaseNamespaceCore } from '../firebaseNamespaceCore';
export function createFirebaseNamespaceLite(): FirebaseNamespace {
const namespace = createFirebaseNamespaceCore(FirebaseAppLiteImpl);

namespace.SDK_VERSION = '${JSCORE_VERSION}_LITE';
namespace.SDK_VERSION = `${__JSCORE_VERSION__}_LITE`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__JSCORE_VERSION__-LITE ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want to change the original.

@mmermerkaya
Copy link
Contributor Author

Yeah I agree about the testing stuff. I didn't realize that it would take so much effort to make it work until it was too late 😅

I like your version, thanks for that! I tried to keep the replace plugin in case we use it for something else, but all three packages that I touched only use it for the version string. YAGNI, I guess 😄

I'll merge your PR to my branch, and replace the others as well.

mmermerkaya and others added 2 commits May 10, 2019 14:01
* import package.json for version

* [AUTOMATED]: Prettier Code Styling

* use var in es5 build
@mmermerkaya mmermerkaya force-pushed the mmermerkaya-standardize-replace branch from 3c7d84a to f2bba9c Compare May 10, 2019 13:42
@mmermerkaya mmermerkaya changed the title Standardize replace plugin usage Import package version from package.json May 10, 2019
@mmermerkaya
Copy link
Contributor Author

Done. Edited OP as well.

@mmermerkaya mmermerkaya force-pushed the mmermerkaya-standardize-replace branch from f2bba9c to 7410820 Compare May 10, 2019 15:06
@mmermerkaya mmermerkaya force-pushed the mmermerkaya-standardize-replace branch from 7410820 to d0dca90 Compare May 10, 2019 16:13
@mmermerkaya
Copy link
Contributor Author

Looks like this changed the TypeScript output directory structure in App and Installations, because we added an import from a parent directory of the current top level directory in both of these packages. Should be fine with the .d.ts file location fixes in package.jsons, but it would be great if we could use something like dts-bundle-generator or rollup-plugin-dts to generate a d.ts bundle instead of relying on declarations generated by the TS compiler, just like we do with the actual code.

@mmermerkaya mmermerkaya merged commit 9a49973 into master May 10, 2019
@mmermerkaya mmermerkaya deleted the mmermerkaya-standardize-replace branch May 10, 2019 18:51
@firebase firebase locked and limited conversation to collaborators Oct 11, 2019
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.

3 participants