Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Improve mode and NODE_ENV defaults and interaction (#972)
Browse files Browse the repository at this point in the history
Previously the only way to override `mode` was by passing `--mode` on
the command line. However this is not possible with all tools, since
some (such as karma) reject unrecognised arguments.

Now:
* `mode` is derived from `NODE_ENV` if `--mode` wasn't passed, and
  !production `NODE_ENV` falls back to mode `development`.
* if `--mode` is passed, it takes priority over `NODE_ENV`.
* if neither `mode` nor `NODE_ENV is defined, then `NODE_ENV` is set
  to `production`, however `mode` is left undefined, which causes
  webpack to output a helpful warning about relying on defaults.
* the template test runner configs set a default `NODE_ENV` of `test`.
* `@neutrinojs/stylelint` now also correctly sets `failOnError`.

Fixes #900.
Fixes #971.
Closes #955.
  • Loading branch information
edmorley authored Jul 13, 2018
1 parent 3440500 commit 2dce8d2
Show file tree
Hide file tree
Showing 33 changed files with 256 additions and 124 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const neutrino = require('neutrino');

process.env.NODE_ENV = process.env.NODE_ENV || 'test';

module.exports = neutrino().jest();
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const neutrino = require('neutrino');

process.env.NODE_ENV = process.env.NODE_ENV || 'test';

module.exports = neutrino().karma();
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
process.env.NODE_ENV = process.env.NODE_ENV || 'test';

require('neutrino')().mocha();
2 changes: 1 addition & 1 deletion packages/eslint/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ neutrino.use(eslint, {
exclude: [],
eslint: {
cache: true,
failOnError: neutrino.config.get('mode') === 'production',
failOnError: process.env.NODE_ENV !== 'development',
cwd: neutrino.options.root,
useEslintrc: false,
root: true,
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ module.exports = (neutrino, opts = {}) => {
include: !opts.include ? [neutrino.options.source, neutrino.options.tests] : undefined,
eslint: {
cache: true,
failOnError: neutrino.config.get('mode') === 'production',
// Make errors fatal not just for 'production' but also 'test'.
failOnError: process.env.NODE_ENV !== 'development',
cwd: neutrino.options.root,
useEslintrc: false,
root: true,
Expand Down
4 changes: 2 additions & 2 deletions packages/font-loader/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ neutrino.use(fonts);

// Usage showing default options
neutrino.use(fonts, {
name: mode === 'production' ? '[name].[hash:8].[ext]' : '[name].[ext]'
name: process.env.NODE_ENV === 'production' ? '[name].[hash:8].[ext]' : '[name].[ext]'
});
```

Expand All @@ -57,7 +57,7 @@ module.exports = {
module.exports = {
use: [
['@neutrinojs/font-loader', {
name: mode === 'production' ? '[name].[hash:8].[ext]' : '[name].[ext]'
name: process.env.NODE_ENV === 'production' ? '[name].[hash:8].[ext]' : '[name].[ext]'
}]
]
};
Expand Down
2 changes: 1 addition & 1 deletion packages/font-loader/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = (neutrino, options = {}) => {
const isProduction = neutrino.config.get('mode') === 'production';
const isProduction = process.env.NODE_ENV === 'production';
const defaultOptions = {
name: isProduction ? '[name].[hash:8].[ext]' : '[name].[ext]'
};
Expand Down
4 changes: 2 additions & 2 deletions packages/image-loader/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ neutrino.use(images);
// Usage showing default options
neutrino.use(images, {
limit: 8192,
name: mode === 'production' ? '[name].[hash:8].[ext]' : '[name].[ext]'
name: process.env.NODE_ENV === 'production' ? '[name].[hash:8].[ext]' : '[name].[ext]'
});
```

Expand All @@ -59,7 +59,7 @@ module.exports = {
use: [
['@neutrinojs/image-loader', {
limit: 8192,
name: mode === 'production' ? '[name].[hash:8].[ext]' : '[name].[ext]'
name: process.env.NODE_ENV === 'production' ? '[name].[hash:8].[ext]' : '[name].[ext]'
}]
]
};
Expand Down
2 changes: 1 addition & 1 deletion packages/image-loader/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = (neutrino, options = {}) => {
const isProduction = neutrino.config.get('mode') === 'production';
const isProduction = process.env.NODE_ENV === 'production';
const defaultOptions = {
limit: 8192,
name: isProduction ? '[name].[hash:8].[ext]' : '[name].[ext]'
Expand Down
2 changes: 1 addition & 1 deletion packages/image-minify/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const imageminLoader = require.resolve('imagemin-webpack/imagemin-loader');

module.exports = (neutrino, opts = {}) => {
const options = merge({
enabled: neutrino.config.get('mode') === 'production',
enabled: process.env.NODE_ENV === 'production',
imagemin: {
plugins: [
gifsicle(),
Expand Down
14 changes: 9 additions & 5 deletions packages/image-minify/test/middleware_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import Neutrino from '../../neutrino/Neutrino';

const mw = () => require('..');
const options = { rules: ['image'] };
const originalNodeEnv = process.env.NODE_ENV;

test.afterEach(() => {
// Restore the original NODE_ENV after each test (which Ava defaults to 'test').
process.env.NODE_ENV = originalNodeEnv;
});

test('loads middleware', t => {
t.notThrows(mw);
Expand All @@ -12,7 +18,6 @@ test('uses middleware', t => {
t.notThrows(() => {
const api = new Neutrino();

api.config.mode('production');
api.use(mw());
});
});
Expand All @@ -21,35 +26,34 @@ test('uses with options', t => {
t.notThrows(() => {
const api = new Neutrino();

api.config.mode('production');
api.use(mw(), options);
});
});

test('instantiates', t => {
process.env.NODE_ENV = 'production';
const api = new Neutrino();

api.config.mode('production');
api.use(mw());

t.true(api.config.plugins.has('imagemin'));
t.notThrows(() => api.config.toConfig());
});

test('instantiates with options', t => {
process.env.NODE_ENV = 'production';
const api = new Neutrino();

api.config.mode('production');
api.use(mw(), options);

t.true(api.config.plugins.has('imagemin'));
t.notThrows(() => api.config.toConfig());
});

test('disabled in development', t => {
process.env.NODE_ENV = 'development';
const api = new Neutrino();

api.config.mode('development');
api.use(mw(), options);

t.false(api.config.plugins.has('imagemin'));
Expand Down
20 changes: 14 additions & 6 deletions packages/jest/test/jest_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import Neutrino from '../../neutrino/Neutrino';
import neutrino from '../../neutrino';

const mw = () => require('..');
const originalNodeEnv = process.env.NODE_ENV;

test.afterEach(() => {
// Restore the original NODE_ENV after each test (which Ava defaults to 'test').
process.env.NODE_ENV = originalNodeEnv;
});

test('loads middleware', t => {
t.notThrows(mw);
Expand All @@ -11,33 +17,35 @@ test('loads middleware', t => {
test('uses middleware', t => {
t.notThrows(() => {
const api = new Neutrino();

api.config.mode('production');
api.use(mw());
});
});

test('instantiates', t => {
const api = new Neutrino();

api.config.mode('production');
api.use(mw());

t.notThrows(() => api.config.toConfig());
});

test('instantiates in development', t => {
process.env.NODE_ENV = 'development';
const api = new Neutrino();
api.use(mw());

api.config.mode('development');
t.notThrows(() => api.config.toConfig());
});

test('instantiates in production', t => {
process.env.NODE_ENV = 'production';
const api = new Neutrino();
api.use(mw());

t.notThrows(() => api.config.toConfig());
});

test('exposes jest output handler', t => {
const api = new Neutrino();

api.use(mw());

const handler = api.outputHandlers.get('jest');
Expand Down
20 changes: 14 additions & 6 deletions packages/karma/test/karma_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import Neutrino from '../../neutrino/Neutrino';
import neutrino from '../../neutrino';

const mw = () => require('..');
const originalNodeEnv = process.env.NODE_ENV;

test.afterEach(() => {
// Restore the original NODE_ENV after each test (which Ava defaults to 'test').
process.env.NODE_ENV = originalNodeEnv;
});

test('loads middleware', t => {
t.notThrows(mw);
Expand All @@ -11,33 +17,35 @@ test('loads middleware', t => {
test('uses middleware', t => {
t.notThrows(() => {
const api = new Neutrino();

api.config.mode('production');
api.use(mw());
});
});

test('instantiates', t => {
const api = new Neutrino();

api.config.mode('production');
api.use(mw());

t.notThrows(() => api.config.toConfig());
});

test('instantiates in development', t => {
process.env.NODE_ENV = 'development';
const api = new Neutrino();
api.use(mw());

api.config.mode('development');
t.notThrows(() => api.config.toConfig());
});

test('instantiates in production', t => {
process.env.NODE_ENV = 'production';
const api = new Neutrino();
api.use(mw());

t.notThrows(() => api.config.toConfig());
});

test('exposes karma output handler', t => {
const api = new Neutrino();

api.use(mw());

const handler = api.outputHandlers.get('karma');
Expand Down
4 changes: 1 addition & 3 deletions packages/library/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ module.exports = (neutrino, opts = {}) => {
.keys(neutrino.options.mains)
.forEach(key => neutrino.config.entry(key).add(neutrino.options.mains[key]));

const mode = neutrino.config.get('mode');

neutrino.config
.when(hasSourceMap, () => neutrino.use(banner))
.devtool('source-map')
Expand Down Expand Up @@ -109,7 +107,7 @@ module.exports = (neutrino, opts = {}) => {
neutrino.use(loaderMerge('lint', 'eslint'), { envs: ['browser', 'commonjs'] });
}
})
.when(mode === 'production', (config) => {
.when(process.env.NODE_ENV === 'production', (config) => {
config.when(options.clean, () => neutrino.use(clean, options.clean));
});
};
31 changes: 16 additions & 15 deletions packages/library/test/library_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,37 @@ import test from 'ava';
import { validate } from 'webpack';
import Neutrino from '../../neutrino/Neutrino';

const mw = () => require('..');
const expectedExtensions = ['.js', '.jsx', '.vue', '.ts', '.tsx', '.mjs', '.json'];
const originalNodeEnv = process.env.NODE_ENV;

test.afterEach(() => {
// Restore the original NODE_ENV after each test (which Ava defaults to 'test').
process.env.NODE_ENV = originalNodeEnv;
});

test('loads preset', t => {
t.notThrows(() => require('..'));
t.notThrows(mw);
});

test('uses preset', t => {
const api = new Neutrino();

t.notThrows(() => api.use(require('..'), { name: 'alpha' }));
t.notThrows(() => api.use(mw(), { name: 'alpha' }));
});

test('throws when missing library name', t => {
const api = new Neutrino();

const err = t.throws(() => api.use(require('..')));
const err = t.throws(() => api.use(mw()));
t.true(err.message.includes('You must specify a library name'));
});

test('valid preset production', t => {
process.env.NODE_ENV = 'production';
const api = new Neutrino();

api.config.mode('production');
api.use(require('..'), { name: 'alpha' });
api.use(mw(), { name: 'alpha' });
const config = api.config.toConfig();

// Common
Expand All @@ -35,7 +42,6 @@ test('valid preset production', t => {
t.is(config.devServer, undefined);

// NODE_ENV/command specific
t.is(config.mode, 'production');
t.is(config.devtool, 'source-map');
t.not(config.externals, undefined);

Expand All @@ -44,10 +50,10 @@ test('valid preset production', t => {
});

test('valid preset development', t => {
process.env.NODE_ENV = 'development';
const api = new Neutrino();

api.config.mode('development');
api.use(require('..'), { name: 'alpha' });
api.use(mw(), { name: 'alpha' });
const config = api.config.toConfig();

// Common
Expand All @@ -57,7 +63,6 @@ test('valid preset development', t => {
t.is(config.devServer, undefined);

// NODE_ENV/command specific
t.is(config.mode, 'development');
t.is(config.devtool, 'source-map');
t.not(config.externals, undefined);

Expand All @@ -67,9 +72,7 @@ test('valid preset development', t => {

test('valid preset Node.js target', t => {
const api = new Neutrino();

api.config.mode('development');
api.use(require('..'), { name: 'alpha', target: 'node' });
api.use(mw(), { name: 'alpha', target: 'node' });

const errors = validate(api.config.toConfig());

Expand All @@ -78,9 +81,7 @@ test('valid preset Node.js target', t => {

test('valid preset commonjs2 libraryTarget', t => {
const api = new Neutrino();

api.config.mode('development');
api.use(require('..'), { name: 'alpha', libraryTarget: 'commonjs2' });
api.use(mw(), { name: 'alpha', libraryTarget: 'commonjs2' });

const errors = validate(api.config.toConfig());

Expand Down
Loading

0 comments on commit 2dce8d2

Please sign in to comment.