From b7bc851ff1d897b7c61a1245c5c5c37b4a46f254 Mon Sep 17 00:00:00 2001 From: ApprenticeofEnder Date: Sat, 12 Jun 2021 01:08:34 -0400 Subject: [PATCH 1/7] Add environment variable for API data (PROOF OF CONCEPT) --- package.json | 1 + rollup.config.js | 14 +++++++++++++- .../getCurrentQuestions.request.ts | 5 ++++- .../getExpiredQuestions.request.ts | 4 +++- src/data/getQuestion/getQuestion.request.ts | 4 +++- src/data/getQuestions/getQuestions.request.ts | 4 +++- src/global.d.ts | 5 +++++ yarn.lock | 10 +++++++++- 8 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 src/global.d.ts diff --git a/package.json b/package.json index 52b21b7..b1db191 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "@babel/preset-env": "^7.11.5", "@rollup/plugin-commonjs": "^14.0.0", "@rollup/plugin-node-resolve": "^8.0.0", + "@rollup/plugin-replace": "^2.4.2", "@rollup/plugin-typescript": "^6.0.0", "@storybook/addon-actions": "^6.2.8", "@storybook/addon-essentials": "^6.2.8", diff --git a/rollup.config.js b/rollup.config.js index e5508aa..2e349ba 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -5,6 +5,7 @@ import livereload from 'rollup-plugin-livereload'; import { terser } from 'rollup-plugin-terser'; import sveltePreprocess from 'svelte-preprocess'; import typescript from '@rollup/plugin-typescript'; +import replace from '@rollup/plugin-replace'; const production = !process.env.ROLLUP_WATCH; @@ -74,7 +75,18 @@ export default { // If we're building for production (npm run build // instead of npm run dev), minify - production && terser() + production && terser(), + + replace({ + process: JSON.stringify({ //Use this in Svelte + env: { + isProd: production + } + }), + globalThis: JSON.stringify({ //Use this in TS + __IS_PROD__: production + }) + }) ], watch: { clearScreen: false diff --git a/src/data/getCurrentQuestions/getCurrentQuestions.request.ts b/src/data/getCurrentQuestions/getCurrentQuestions.request.ts index 550ee7d..5318820 100644 --- a/src/data/getCurrentQuestions/getCurrentQuestions.request.ts +++ b/src/data/getCurrentQuestions/getCurrentQuestions.request.ts @@ -1,9 +1,12 @@ import { getCurrentQuestionsExample } from '../getCurrentQuestions' +const IS_PROD = globalThis.__IS_PROD__; + // CHANGE PROMISE TO QUESTION TYPE WHEN LEVEL - DIFFICULTY IS FIXED const getCurrentQuestions = async (): Promise => { // eslint-disable-next-line no-constant-condition - if (false) { // ADD ENV VAR TO TELL US TO USE THE REAL BACKEND OR NO + + if (IS_PROD) { // ADD ENV VAR TO TELL US TO USE THE REAL BACKEND OR NO return fetch('http://127.0.0.1:8001/api/codechallenges/current/questions/', { headers: { 'Content-Type': 'application/json', diff --git a/src/data/getExpiredQuestions/getExpiredQuestions.request.ts b/src/data/getExpiredQuestions/getExpiredQuestions.request.ts index fb0616d..69273f2 100644 --- a/src/data/getExpiredQuestions/getExpiredQuestions.request.ts +++ b/src/data/getExpiredQuestions/getExpiredQuestions.request.ts @@ -1,9 +1,11 @@ import { getExpiredQuestionsExample } from '../getExpiredQuestions' +const IS_PROD = globalThis.__IS_PROD__; + // CHANGE PROMISE TO QUESTION TYPE WHEN LEVEL - DIFFICULTY IS FIXED const getExpiredQuestions = async (): Promise => { // eslint-disable-next-line no-constant-condition - if (false) { // ADD ENV VAR TO TELL US TO USE THE REAL BACKEND OR NO + if (IS_PROD) { // ADD ENV VAR TO TELL US TO USE THE REAL BACKEND OR NO return fetch('http://127.0.0.1:8001/api/codechallenges/expired/questions/', { headers: { 'Content-Type': 'application/json', diff --git a/src/data/getQuestion/getQuestion.request.ts b/src/data/getQuestion/getQuestion.request.ts index caf2042..2a3b2db 100644 --- a/src/data/getQuestion/getQuestion.request.ts +++ b/src/data/getQuestion/getQuestion.request.ts @@ -1,9 +1,11 @@ import { Question } from '../../types' import { getQuestionsExample } from '../getQuestions' +const IS_PROD = globalThis.__IS_PROD__; + const getQuestion = async (questionId: number): Promise => { // eslint-disable-next-line no-constant-condition - if (false) { // ADD ENV VAR TO TELL US TO USE THE REAL BACKEND OR NO + if (IS_PROD) { // ADD ENV VAR TO TELL US TO USE THE REAL BACKEND OR NO return fetch('http://127.0.0.1:8001/api/codechallenges/questions/' + questionId, { headers: { 'Content-Type': 'application/json', diff --git a/src/data/getQuestions/getQuestions.request.ts b/src/data/getQuestions/getQuestions.request.ts index e2620da..2d2b211 100644 --- a/src/data/getQuestions/getQuestions.request.ts +++ b/src/data/getQuestions/getQuestions.request.ts @@ -1,9 +1,11 @@ import { Question } from '../../types' import { getQuestionsExample } from '../getQuestions' +const IS_PROD = globalThis.__IS_PROD__; + const getQuestions = async (): Promise => { // eslint-disable-next-line no-constant-condition - if (false) { // ADD ENV VAR TO TELL US TO USE THE REAL BACKEND OR NO + if (IS_PROD) { // ADD ENV VAR TO TELL US TO USE THE REAL BACKEND OR NO return fetch('http://127.0.0.1:8001/api/codechallenges/questions/', { headers: { 'Content-Type': 'application/json', diff --git a/src/global.d.ts b/src/global.d.ts new file mode 100644 index 0000000..abf401d --- /dev/null +++ b/src/global.d.ts @@ -0,0 +1,5 @@ +export {} + +declare global { + var __IS_PROD__:string +} \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 8381a41..e875f82 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1504,6 +1504,14 @@ is-module "^1.0.0" resolve "^1.17.0" +"@rollup/plugin-replace@^2.4.2": + version "2.4.2" + resolved "https://registry.yarnpkg.com/@rollup/plugin-replace/-/plugin-replace-2.4.2.tgz#a2d539314fbc77c244858faa523012825068510a" + integrity sha512-IGcu+cydlUMZ5En85jxHH4qj2hta/11BHq95iHEyb2sbgiN0eCdzvUcHw5gt9pBL5lTi4JDYJ1acCoMGpTvEZg== + dependencies: + "@rollup/pluginutils" "^3.1.0" + magic-string "^0.25.7" + "@rollup/plugin-typescript@^6.0.0": version "6.1.0" resolved "https://registry.yarnpkg.com/@rollup/plugin-typescript/-/plugin-typescript-6.1.0.tgz#289e7f0ea12fd659bd13ad59dda73b9055538b83" @@ -7714,7 +7722,7 @@ lz-string@^1.4.4: resolved "https://registry.yarnpkg.com/lz-string/-/lz-string-1.4.4.tgz#c0d8eaf36059f705796e1e344811cf4c498d3a26" integrity sha1-wNjq82BZ9wV5bh40SBHPTEmNOiY= -magic-string@^0.25.2: +magic-string@^0.25.2, magic-string@^0.25.7: version "0.25.7" resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.7.tgz#3f497d6fd34c669c6798dcb821f2ef31f5445051" integrity sha512-4CrMT5DOHTDk4HYDlzmwu4FVCcIYI8gauveasrdCu2IKIFOJ3f0v/8MDGJCDL9oD2ppz/Av1b0Nj345H9M+XIA== From 1773bf9d5bcef4e4e0429ac1fe9db9a18187f566 Mon Sep 17 00:00:00 2001 From: MathyouMB Date: Wed, 16 Jun 2021 13:05:38 -0400 Subject: [PATCH 2/7] lint fixes --- src/data/getCurrentQuestions/getCurrentQuestions.request.ts | 4 ++-- src/data/getExpiredQuestions/getExpiredQuestions.request.ts | 2 +- src/data/getQuestion/getQuestion.request.ts | 2 +- src/data/getQuestions/getQuestions.request.ts | 2 +- src/global.d.ts | 4 +++- 5 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/data/getCurrentQuestions/getCurrentQuestions.request.ts b/src/data/getCurrentQuestions/getCurrentQuestions.request.ts index 5318820..d2388b9 100644 --- a/src/data/getCurrentQuestions/getCurrentQuestions.request.ts +++ b/src/data/getCurrentQuestions/getCurrentQuestions.request.ts @@ -1,11 +1,11 @@ import { getCurrentQuestionsExample } from '../getCurrentQuestions' -const IS_PROD = globalThis.__IS_PROD__; +const IS_PROD = globalThis.__IS_PROD__ // CHANGE PROMISE TO QUESTION TYPE WHEN LEVEL - DIFFICULTY IS FIXED const getCurrentQuestions = async (): Promise => { // eslint-disable-next-line no-constant-condition - + if (IS_PROD) { // ADD ENV VAR TO TELL US TO USE THE REAL BACKEND OR NO return fetch('http://127.0.0.1:8001/api/codechallenges/current/questions/', { headers: { diff --git a/src/data/getExpiredQuestions/getExpiredQuestions.request.ts b/src/data/getExpiredQuestions/getExpiredQuestions.request.ts index 69273f2..ca0d0ad 100644 --- a/src/data/getExpiredQuestions/getExpiredQuestions.request.ts +++ b/src/data/getExpiredQuestions/getExpiredQuestions.request.ts @@ -1,6 +1,6 @@ import { getExpiredQuestionsExample } from '../getExpiredQuestions' -const IS_PROD = globalThis.__IS_PROD__; +const IS_PROD = globalThis.__IS_PROD__ // CHANGE PROMISE TO QUESTION TYPE WHEN LEVEL - DIFFICULTY IS FIXED const getExpiredQuestions = async (): Promise => { diff --git a/src/data/getQuestion/getQuestion.request.ts b/src/data/getQuestion/getQuestion.request.ts index 2a3b2db..26df0b8 100644 --- a/src/data/getQuestion/getQuestion.request.ts +++ b/src/data/getQuestion/getQuestion.request.ts @@ -1,7 +1,7 @@ import { Question } from '../../types' import { getQuestionsExample } from '../getQuestions' -const IS_PROD = globalThis.__IS_PROD__; +const IS_PROD = globalThis.__IS_PROD__ const getQuestion = async (questionId: number): Promise => { // eslint-disable-next-line no-constant-condition diff --git a/src/data/getQuestions/getQuestions.request.ts b/src/data/getQuestions/getQuestions.request.ts index 2d2b211..f1a9e22 100644 --- a/src/data/getQuestions/getQuestions.request.ts +++ b/src/data/getQuestions/getQuestions.request.ts @@ -1,7 +1,7 @@ import { Question } from '../../types' import { getQuestionsExample } from '../getQuestions' -const IS_PROD = globalThis.__IS_PROD__; +const IS_PROD = globalThis.__IS_PROD__ const getQuestions = async (): Promise => { // eslint-disable-next-line no-constant-condition diff --git a/src/global.d.ts b/src/global.d.ts index abf401d..e028162 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -1,5 +1,7 @@ export {} declare global { + // eslint-disable-next-line no-var + // eslint-disable-next-line no-unused-vars var __IS_PROD__:string -} \ No newline at end of file +} From 4afe719ef5d19f55cecd1975405bcbc8daf1d507 Mon Sep 17 00:00:00 2001 From: ApprenticeofEnder Date: Sun, 20 Jun 2021 19:13:51 -0400 Subject: [PATCH 3/7] Add sample .env --- .env_sample | 1 + src/global.d.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 .env_sample diff --git a/.env_sample b/.env_sample new file mode 100644 index 0000000..c56063e --- /dev/null +++ b/.env_sample @@ -0,0 +1 @@ +ROLLUP_WATCH=true \ No newline at end of file diff --git a/src/global.d.ts b/src/global.d.ts index abf401d..299cc7a 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -1,5 +1,5 @@ export {} declare global { - var __IS_PROD__:string + let __IS_PROD__:string } \ No newline at end of file From d4b8a7f728252a28a196340d18e717bf579eb2ea Mon Sep 17 00:00:00 2001 From: ApprenticeofEnder Date: Sun, 27 Jun 2021 20:49:40 -0400 Subject: [PATCH 4/7] Fix build fail --- src/global.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/global.d.ts b/src/global.d.ts index f851dba..5bf9fca 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -4,4 +4,4 @@ declare global { // eslint-disable-next-line no-var // eslint-disable-next-line no-unused-vars let __IS_PROD__:string -} \ No newline at end of file +} From b531e821e1c6be9e29d5ab9c7a48713f41f2b624 Mon Sep 17 00:00:00 2001 From: ApprenticeofEnder Date: Fri, 2 Jul 2021 20:42:19 -0400 Subject: [PATCH 5/7] Modified gitignore and config files to allow easier env var setup and workflow --- .env_sample | 3 ++- .gitignore | 2 ++ package.json | 1 + rollup.config.js | 7 +++++-- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/.env_sample b/.env_sample index c56063e..bff2623 100644 --- a/.env_sample +++ b/.env_sample @@ -1 +1,2 @@ -ROLLUP_WATCH=true \ No newline at end of file +IS_PROD=true +API_URL=http://127.0.0.1:8001/api/codechallenges \ No newline at end of file diff --git a/.gitignore b/.gitignore index e0ccaf9..194ceee 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,7 @@ .DS_Store .vscode +.env +yarn.lock stories diff --git a/package.json b/package.json index b1db191..00e9a7a 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "@typescript-eslint/parser": "^4.22.0", "babel-jest": "^26.5.2", "babel-loader": "^8.2.2", + "dotenv": "^10.0.0", "eslint": "^7.24.0", "eslint-config-standard": "^16.0.2", "eslint-plugin-import": "^2.22.1", diff --git a/rollup.config.js b/rollup.config.js index 2e349ba..22c9262 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -6,8 +6,11 @@ import { terser } from 'rollup-plugin-terser'; import sveltePreprocess from 'svelte-preprocess'; import typescript from '@rollup/plugin-typescript'; import replace from '@rollup/plugin-replace'; +import dotenv from 'dotenv'; -const production = !process.env.ROLLUP_WATCH; +dotenv.config(); + +const production = process.env.IS_PROD; function serve() { let server; @@ -84,7 +87,7 @@ export default { } }), globalThis: JSON.stringify({ //Use this in TS - __IS_PROD__: production + IS_PROD: production }) }) ], From 39a07b511b9146404280d4e32cbaf4bf9be4b505 Mon Sep 17 00:00:00 2001 From: ApprenticeofEnder Date: Fri, 2 Jul 2021 20:42:34 -0400 Subject: [PATCH 6/7] Fixing code to use env var update --- src/data/getCurrentQuestions/getCurrentQuestions.request.ts | 2 +- src/data/getExpiredQuestions/getExpiredQuestions.request.ts | 2 +- src/data/getQuestion/getQuestion.request.ts | 4 ++-- src/data/getQuestions/getQuestions.request.ts | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/data/getCurrentQuestions/getCurrentQuestions.request.ts b/src/data/getCurrentQuestions/getCurrentQuestions.request.ts index d2388b9..75e8f53 100644 --- a/src/data/getCurrentQuestions/getCurrentQuestions.request.ts +++ b/src/data/getCurrentQuestions/getCurrentQuestions.request.ts @@ -1,6 +1,6 @@ import { getCurrentQuestionsExample } from '../getCurrentQuestions' -const IS_PROD = globalThis.__IS_PROD__ +const IS_PROD = globalThis.IS_PROD // CHANGE PROMISE TO QUESTION TYPE WHEN LEVEL - DIFFICULTY IS FIXED const getCurrentQuestions = async (): Promise => { diff --git a/src/data/getExpiredQuestions/getExpiredQuestions.request.ts b/src/data/getExpiredQuestions/getExpiredQuestions.request.ts index ca0d0ad..32db96c 100644 --- a/src/data/getExpiredQuestions/getExpiredQuestions.request.ts +++ b/src/data/getExpiredQuestions/getExpiredQuestions.request.ts @@ -1,6 +1,6 @@ import { getExpiredQuestionsExample } from '../getExpiredQuestions' -const IS_PROD = globalThis.__IS_PROD__ +const IS_PROD = globalThis.IS_PROD // CHANGE PROMISE TO QUESTION TYPE WHEN LEVEL - DIFFICULTY IS FIXED const getExpiredQuestions = async (): Promise => { diff --git a/src/data/getQuestion/getQuestion.request.ts b/src/data/getQuestion/getQuestion.request.ts index 26df0b8..3f7be78 100644 --- a/src/data/getQuestion/getQuestion.request.ts +++ b/src/data/getQuestion/getQuestion.request.ts @@ -1,7 +1,7 @@ -import { Question } from '../../types' +import type { Question } from '../../types' import { getQuestionsExample } from '../getQuestions' -const IS_PROD = globalThis.__IS_PROD__ +const IS_PROD = globalThis.IS_PROD const getQuestion = async (questionId: number): Promise => { // eslint-disable-next-line no-constant-condition diff --git a/src/data/getQuestions/getQuestions.request.ts b/src/data/getQuestions/getQuestions.request.ts index f1a9e22..99e6eca 100644 --- a/src/data/getQuestions/getQuestions.request.ts +++ b/src/data/getQuestions/getQuestions.request.ts @@ -1,7 +1,7 @@ -import { Question } from '../../types' +import type { Question } from '../../types' import { getQuestionsExample } from '../getQuestions' -const IS_PROD = globalThis.__IS_PROD__ +const IS_PROD = globalThis.IS_PROD const getQuestions = async (): Promise => { // eslint-disable-next-line no-constant-condition From 870c990e9e67d418e5c8d5f5871e105d2e36b478 Mon Sep 17 00:00:00 2001 From: ApprenticeofEnder Date: Fri, 2 Jul 2021 20:46:27 -0400 Subject: [PATCH 7/7] Update yarn.lock because fml --- yarn.lock | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/yarn.lock b/yarn.lock index e875f82..5bf9068 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4734,6 +4734,11 @@ dotenv-webpack@^1.8.0: dependencies: dotenv-defaults "^1.0.2" +dotenv@^10.0.0: + version "10.0.0" + resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-10.0.0.tgz#3d4227b8fb95f81096cdd2b66653fb2c7085ba81" + integrity sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q== + dotenv@^6.2.0: version "6.2.0" resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-6.2.0.tgz#941c0410535d942c8becf28d3f357dbd9d476064"