EmberJs: how to dynamicaly inject data into ENV? - ember.js

I need to dynamically inject some data before build stage.
In my config/environment.js i have:
module.exports = function(environment) {
environment = 'production';
var ENV = {
APP: {
API_HOST: 'https://apihost1.com,
secret: 'key1'
}
};
return ENV;
};
How do i can to dynamically change "APP" object to something else:
APP: {
API_HOST: 'https://apihost2.com,
secret: 'key2'
}
before build according to an executing command in my package.json?
"scripts": {
"build": "$CONFIG_NAME ember build",
},
I was thinking about npm scripts but could not implement that.
Is anyone has a solution for it?

You could do something like this in config/environment:
const customConfig = require(`./config.${process.env.CONFIG_NAME}.json`);
module.exports = function(environment) {
environment = 'production';
var ENV = {
APP: {
...customConfig,
otherStuff: 'bla'
}
};
return ENV;
};
then you can run env CONFIG_NAME=foo ember build if you want to merge the content of the config/config.foo.json into the ENV.APP during the build.
env tho is a unix thing, this will work on Linux and MacOS as well as other unix systems.
For windows you can either set the env variable with PowerShell or install cross-env and do npm run cross-env CONFIG_NAME=foo ember build.

Related

jest-haste-map: Haste module naming collision (AWS, RN)

I have a React-native project with AWS Amplify.
In the root directory, there is an amplify folder.
Inside this amplify folder, there is a backend folder, and a #current-cloud-backend folder.
These two are basically identical.
When I try to start my project with npm run start I receive this error:
The following files share their name; please adjust your hasteImpl:
* <rootDir>/amplify-backup/backend/function/cxLoyaltyMainAppVerifyAuthChallengeResponse/src/package.json
* <rootDir>/amplify/#current-cloud-backend/function/cxLoyaltyMainAppVerifyAuthChallengeResponse/src/package.json
And it is complaining that inside these two folders, each lambda function has it's own package.json, in which they are named identical to their counterpart folder.
What I have done so far
I have found many people mentioning to put modulePathIgnorePatterns: ['<rootDir>/build'] inside of my root package.json under jest. Some also say to put it inside of jest.config.js which I cannot find anywhere.
I have also tried creating a root rn-cli.config.js and added
module.exports = {
resolver: {
blacklistRE: blacklist( [
/node_modules\/.*\/node_modules\/react-native\/.*/,
] )
},
};
which also does not work.
I am really running out of ideas here, anyone have any ideas? Thank you
I am using the Expo CLI and was having the same problem.
The solution that worked for me:
metro.config.js at the root directory. (instead of rn-cli.config.js)
const blacklist = require('metro-config/src/defaults/blacklist');
module.exports = {
resolver: {
blacklistRE: blacklist([/#current-cloud-backend\/.*/]),
},
transformer: {
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: false,
},
}),
},
};
UPDATE 2022! Just change the folder of the previous answer. It´s no longer defaults/blacklist, but defaults/exclusionList. So the solution:
I am using the Expo CLI and was having the same problem.
The solution that worked for me:
metro.config.js at the root directory. (instead of rn-cli.config.js)
const blacklist = require('metro-config/src/defaults/exclusionList');
module.exports = {
resolver: {
blacklistRE: blacklist([/#current-cloud-backend\/.*/]),
},
transformer: {
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: false,
},
}),
},
};
Adding the below snippet in the metro.config.js file worked for me
I am using:
react-native-cli: 2.0.1
react-native: 0.63.4
amplify: 5.3.0
const exclusionList = require('metro- config/src/defaults/exclusionList');
module.exports = {
resolver: {
blacklistRE: exclusionList([/#current-cloud-backend\/.*/]),
},
transformer: {
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: false,
},
}),
},
};
Also, you will need to install metro-config as a dependency by running npm i -D metro-config
In my case I have a project managed with Expo and a rule to resolve files of type cjs. I only had to include the line:
defaultConfig.resolve.blacklistRE = blacklist([/#current-cloud-backend/.*/]);
Final result:
const { getDefaultConfig } = require("#expo/metro-config");
const blacklist = require('metro-config/src/defaults/exclusionList');
const defaultConfig = getDefaultConfig(__dirname);
defaultConfig.resolve.assetExts.push("cjs");
defaultConfig.resolve.blacklistRE = blacklist([/#current-cloud-backend\/.*/]);
module.exports = defaultConfig;

How to point an ember app to Staging environment VS Production environment upon deploying a test link?

I am having a problem when it comes to directing a test link to staging environment VS to production environment.
Current behaviour: When I tried to run this command on my terminal ember deploy staging and it created a test link successfully. However, my component has a line of code that checks which environment the app currently pointed to. Unfortunately, it pointed to production environment
Expected behaviour: What I am trying to achieve is when I run the command ember deploy staging it should point to staging environment not production environment
Question: How to tell ember that create a test linkfor a staging environment?
Please see my code below for ../config/deploy.js
/* jshint node: true */
module.exports = function(deployTarget) {
var ENV = {
build: {
environment: 'production'
},
};
ENV['revision-data'] = {
type: 'git-commit'
};
ENV['s3'] = {
accessKeyId: process.env.S3_ACCESS_KEY_ID,
secretAccessKey: process.env.S3_ACCESS_KEY_SECRET,
bucket: 'sample',
prefix: deployTarget + '/dist/www',
region: 'us-east-1',
filePattern: '**/*.{js,css,png,gif,ico,jpg,xml,txt,svg,swf,eot,ttf,woff,woff2}'
};
ENV['redis'] = {
host: process.env.REDIS_HOST,
port: process.env.REDIS_PORT,
password: process.env.REDIS_PASSWORD,
maxRecentUploads: 30
};
ENV['gzip'] = {
'filePattern': '**/*.{js,css,json,ico,xml,txt,svg,eot,ttf,woff,woff2}'
};
ENV['rollbar'] = {
accessToken: process.env.ROLLBAR_ACCESS_TOKEN,
accessServerToken: process.env.ROLLBAR_SERVER_ACCESS_TOKEN,
minifiedPrependUrl: 'https://cdn1-' + deployTarget + '.sample.com/dist/www/',
rollbarConfig: {
enabled: true,
environment: deployTarget,
captureUncaught: true
}
};
if (deployTarget === 'production') {
ENV.rollbar.minifiedPrependUrl = 'https://cdn1.sample.com/dist/www/';
}
return ENV;
};
this is the code of my component that checks which environment you are currently in once you created a test link /components/my-component/component.js
import Ember from 'ember';
import config from '../../../config/environment';
export default Ember.Component.extend({
isProduction: config.environment === 'production',
myEnvironment: config.environment;
});
And the myEnvironment returns production environment upon logging {{log myEnvironment}} on my /component/my-component/template.hbs
This is the code of my .env.deploy.staging
CLIENT_HOST=https://staging.sample.com
ASSETS_PREFIX=https://cdn1-staging.sample.com/dist/www/
S3_ACCESS_KEY_ID=ABCDEF123456
S3_ACCESS_KEY_SECRET=ABCDEF123456
ROLLBAR_ACCESS_TOKEN=ABCDEF123456
ROLLBAR_SERVER_ACCESS_TOKEN=ABCDEF123456
Any response is much appreciated. Hoping someone could help me, thank you!

Exporting webpack config as a function fails

I'am trying to handle development and production eviroment variables in my webpack configuration (see https://webpack.js.org/guides/production/), but it fails with
WebpackOptionsValidationError: Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
- configuration should be an object.
package.json
{
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "./node_modules/.bin/webpack",
"start": "npm run build && node server.js"
},
"devDependencies": {
//...
"webpack": "^4.20.2",
"webpack-cli": "^3.1.2",
"webpack-dev-middleware": "^3.4.0",
"webpack-hot-middleware": "^2.24.2"
}
}
webpack.config.js
const path = require('path'),
webpack = require('webpack'),
HtmlWebpackPlugin = require('html-webpack-plugin');
let config = {
entry: {
app: [
'./src/app/App.tsx', 'webpack-hot-middleware/client'
],
vendor: ['react', 'react-dom']
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'js/[name].bundle.js'
},
// ...
}
This export is working as expected without errors or warnings
module.exports = config; // everything is fine
But this fails
module.exports = function(env, argv) { // this errors
return config;
};
There is a similiar, but unanswered question here: webpack base config as a function doesn't work
It's a very mysterious behaviour, appreciate if anyone could help!
Well,it is working. I didn't notice that the error takes place on a total different spot of my code.
I was doing a tutorial about HMR with webpack and express. An it's this lines of code in the express setup which causes the trouble:
server.js
const webpackConfig = require('./webpack.config');
const compiler = webpack(webpackConfig);
//...
app.use(
require('webpack-dev-middleware')(compiler, {
noInfo: true,
publicPath: webpackConfig.output.publicPath
})
);
The webpackConfig is only getting a function without being called and so it's not returning an object. So adding parenthesis is all it took to make it work.
const webpackConfig = require('./webpack.config')();
//..
The documentation is a bit quirky. You properly forgot the set the env variable from package.json
For instance "start": "webpack --env.prod --", in package.json will pass {prod: true} as the env variable.
Hope this helps.
More info here: https://webpack.js.org/api/cli/#environment-options

ember cli sass ENOTDIR: not a directory, scandir

I'm trying to use ember cli with sass, so i installed the ember-cli-sass, and i'm trying to configure like this:
var EmberApp = require('ember-cli/lib/broccoli/ember-app');
var emberCLIBuild = function(defaults) {
var app = new EmberApp(defaults, {
sassOptions: {
includePaths: [
'app/styles/admin/main.scss',
'app/styles/site/main.scss'
]
}
});
return app.toTree();
};
module.exports = emberCLIBuild;
but, when i try to run ember serve, the terminal will throw an error:
ENOTDIR: not a directory, scandir '/Users/xxxx/DEV/PubCrawl/Site/tmp/sass_compiler-input_base_path-55sPHD0L.tmp/1/'
How could i fix this? I don't see the problem.
Thanks.
According the demo on the github page, it looks like it takes paths, not file names (which makes sense since it's called includePaths). I think you want:
includePaths: [
'app/styles/admin',
'app/styles/site'
]

Is it possible to pass environment variables to Ember cli sass during build?

I am using ember-cli-sass
I want to define some sass variables depending on env variable.
my theme1.scss file
#if 'theme1' == process.env.THEME {
$color-secondary: #eee;
$color-primary: #ff0;
}
How can i send my env to the broccoli build process
and how can I access the env variables in sass script?
after tracing the code back thru the ember-cli-sass module
to the base broccoli-sass code i found that we can pass functions to sass in sassOptions.
var sass = require('node-sass');
var app = new EmberApp({
sassOptions: {
functions: {
variable: function () {
return new sass.types.String(process.env.VARIABLE);
}
}
}
});
and then we can use it as a method in sassScript
#if variable() == 'foo' {
...
}