Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,26 @@ function logTrace(stats, showCompiling) {
lastTrace = '';
}

function start({ input, port }) {
function start({ input, port, env }) {
const environment = 'development';
const compiler = getCompiler({ environment, input });
if (port) {
process.env['NULLSTACK_SERVER_PORT'] = port;
}
if (env) {
process.env['NULLSTACK_ENVIRONMENT_NAME'] = env;
}
console.log(` 🚀️ Starting your application in ${environment} mode...`);
console.log();
compiler.watch({}, (error, stats) => logTrace(stats, true));
}

function build({ input, output, cache, mode = 'ssr' }) {
function build({ input, output, cache, env, mode = 'ssr' }) {
const environment = 'production';
const compiler = getCompiler({ environment, input, cache });
if (env) {
process.env['NULLSTACK_ENVIRONMENT_NAME'] = env;
}
console.log(` 🚀️ Building your application in ${mode} mode...`);
compiler.run((error, stats) => {
logTrace(stats, false);
Expand All @@ -78,6 +84,7 @@ program
.description('Start application in development environment')
.option('-p, --port <port>', 'Port number to run the server')
.option('-i, --input <input>', 'Path to project that will be started')
.option('-e, --env <name>', 'Name of the environment file that should be loaded')
.helpOption('-h, --help', 'Learn more about this command')
.action(start)

Expand All @@ -89,6 +96,7 @@ program
.option('-i, --input <input>', 'Path to project that will be built')
.option('-o, --output <output>', 'Path to build output folder')
.option('-c, --cache', 'Cache build results in .production folder')
.option('-e, --env <name>', 'Name of the environment file that should be loaded')
.helpOption('-h, --help', 'Learn more about this command')
.action(build)

Expand Down
9 changes: 9 additions & 0 deletions server/dotenv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import dotenv from 'dotenv'

let path = '.env'

if (process.env.NULLSTACK_ENVIRONMENT_NAME) {
path += `.${process.env.NULLSTACK_ENVIRONMENT_NAME}`
}

dotenv.config({ path })
2 changes: 2 additions & 0 deletions server/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ environment.mode = process.env.NULLSTACK_ENVIRONMENT_MODE || 'ssr';

environment.key = "{{NULLSTACK_ENVIRONMENT_KEY}}"

environment.name = process.env.NULLSTACK_ENVIRONMENT_NAME || '';

Object.freeze(environment);

export default environment;
2 changes: 1 addition & 1 deletion server/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'dotenv/config';
import './dotenv';
import { normalize } from 'path';
import element from '../shared/element';
import fragment from '../shared/fragment';
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
"purgecss-webpack-plugin": "^4.1.3"
},
"scripts": {
"start": "npx nullstack start --input=./tests --port=6969",
"build": "npx nullstack build --input=./tests --mode=ssr",
"start": "npx nullstack start --input=./tests --port=6969 --env=test",
"build": "npx nullstack build --input=./tests --mode=ssr --env=test",
"test": "npm run build && jest",
"script": "node src/scripts/run.js"
}
}
}
5 changes: 3 additions & 2 deletions tests/src/ContextEnvironment.njs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Nullstack from 'nullstack';

class ContextEnvironment extends Nullstack {
render({environment}) {

render({ environment }) {
return (
<div>
<div data-environment={!!environment} />
Expand All @@ -11,6 +11,7 @@ class ContextEnvironment extends Nullstack {
<div data-development={environment.development.toString()} />
<div data-production={environment.production.toString()} />
<div data-static={(environment.mode === 'ssg').toString()} />
<div data-name={environment.name} />
<div data-key={environment.key} />
</div>
)
Expand Down
5 changes: 5 additions & 0 deletions tests/src/ContextEnvironment.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ describe('ContextEnvironment', () => {
expect(element).toBeTruthy();
});

test('has a key with the environment name', async () => {
const element = await page.$('[data-name="test"]');
expect(element).toBeTruthy();
});

});
2 changes: 2 additions & 0 deletions tests/src/scripts/run.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
process.env.NULLSTACK_ENVIRONMENT_NAME = 'test'

const { default: application } = require('../../.development/server.js');

async function getProjectName() {
Expand Down