diff --git a/docs/packages/env.md b/docs/packages/env.md deleted file mode 120000 index 5cb6113ca..000000000 --- a/docs/packages/env.md +++ /dev/null @@ -1 +0,0 @@ -../../packages/env/README.md \ No newline at end of file diff --git a/mkdocs.yml b/mkdocs.yml index 137de0c4d..bede6140a 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -54,7 +54,6 @@ pages: - compile-loader: './packages/compile-loader.md' - copy: './packages/copy.md' - dev-server: './packages/dev-server.md' - - env: './packages/env.md' - eslint: './packages/eslint.md' - font-loader: './packages/font-loader.md' - html-loader: './packages/html-loader.md' diff --git a/packages/env/.npmignore b/packages/env/.npmignore deleted file mode 100644 index 193378602..000000000 --- a/packages/env/.npmignore +++ /dev/null @@ -1 +0,0 @@ -/test/ diff --git a/packages/env/README.md b/packages/env/README.md deleted file mode 100644 index 477124f6f..000000000 --- a/packages/env/README.md +++ /dev/null @@ -1,87 +0,0 @@ -# Neutrino Environment Middleware - -`@neutrinojs/env` is Neutrino middleware for injecting environment variable definitions into -source code at `process.env`. You can use this to make a custom environment variable (e.g. an API server backend to -use) available inside your project. Always injects `process.env.NODE_ENV`, unless overridden. - -[![NPM version][npm-image]][npm-url] -[![NPM downloads][npm-downloads]][npm-url] - -## Requirements - -- Node.js v8.3+ -- Yarn v1.2.1+, or npm v5.4+ -- Neutrino v8 - -## Installation - -`@neutrinojs/env` can be installed via the Yarn or npm clients. - -#### Yarn - -```bash -❯ yarn add @neutrinojs/env -``` - -#### npm - -```bash -❯ npm install --save @neutrinojs/env -``` - -## Usage - -`@neutrinojs/env` can be consumed from the Neutrino API, middleware, or presets. Require this package -and plug it into Neutrino: - -```js -// Using function middleware format -const env = require('@neutrinojs/env'); - -// Use with default options -neutrino.use(env); - -// Usage with additional environment variables -neutrino.use(env, ['SECRET_KEY']); -``` - -```js -// Using object or array middleware format - -// Use with default options -module.exports = { - use: ['@neutrinojs/env'] -}; - -// Usage with additional environment variables -module.exports = { - use: [ - ['@neutrinojs/env', ['SECRET_KEY']] - ] -}; -``` - -This middleware optionally accepts an array of environment variables to additionally inject into source code. - -## Customization - -`@neutrinojs/env` creates some conventions to make overriding the configuration easier once you are ready to -make changes. - -### Plugins - -The following is a list of plugins and their identifiers which can be overridden: - -| Name | Description | Environments and Commands | -| --- | --- | --- | -| `env` | Inject environment variables into source code at `process.env`. | all | - -## Contributing - -This middleware is part of the [neutrino-dev](https://github.com/mozilla-neutrino/neutrino-dev) repository, a monorepo -containing all resources for developing Neutrino and its core presets and middleware. Follow the -[contributing guide](https://neutrinojs.org/contributing/) for details. - -[npm-image]: https://img.shields.io/npm/v/@neutrinojs/env.svg -[npm-downloads]: https://img.shields.io/npm/dt/@neutrinojs/env.svg -[npm-url]: https://www.npmjs.com/package/@neutrinojs/env diff --git a/packages/env/index.js b/packages/env/index.js deleted file mode 100644 index 559769ff7..000000000 --- a/packages/env/index.js +++ /dev/null @@ -1,5 +0,0 @@ -const { EnvironmentPlugin } = require('webpack'); - -module.exports = ({ config }, envs = []) => config - .plugin('env') - .use(EnvironmentPlugin, envs); diff --git a/packages/env/package.json b/packages/env/package.json deleted file mode 100644 index 5b0a64f9d..000000000 --- a/packages/env/package.json +++ /dev/null @@ -1,30 +0,0 @@ -{ - "name": "@neutrinojs/env", - "version": "9.0.0-0", - "description": "Neutrino middleware for injecting environment variables into source code", - "main": "index.js", - "keywords": [ - "neutrino", - "neutrino-middleware", - "env", - "environment", - "variables" - ], - "author": "Eli Perelman ", - "license": "MPL-2.0", - "repository": "https://github.com/mozilla-neutrino/neutrino-dev/tree/master/packages/env", - "homepage": "https://neutrinojs.org", - "bugs": "https://github.com/mozilla-neutrino/neutrino-dev/issues", - "publishConfig": { - "access": "public" - }, - "engines": { - "node": ">=8.3", - "npm": ">=5.4.0", - "yarn": ">=1.2.1" - }, - "peerDependencies": { - "neutrino": "^9.0.0-0", - "webpack": "^4.0.0" - } -} diff --git a/packages/env/test/middleware_test.js b/packages/env/test/middleware_test.js deleted file mode 100644 index 8f8add1ce..000000000 --- a/packages/env/test/middleware_test.js +++ /dev/null @@ -1,37 +0,0 @@ -import test from 'ava'; -import Neutrino from '../../neutrino/Neutrino'; - -const mw = () => require('..'); -const options = ['ALPHA_BETA']; - -test('loads middleware', t => { - t.notThrows(mw); -}); - -test('uses middleware', t => { - const api = new Neutrino(); - - t.notThrows(() => api.use(mw())); -}); - -test('uses with options', t => { - const api = new Neutrino(); - - t.notThrows(() => api.use(mw(), options)); -}); - -test('instantiates', t => { - const api = new Neutrino(); - - api.use(mw()); - - t.notThrows(() => api.config.toConfig()); -}); - -test('instantiates with options', t => { - const api = new Neutrino(); - - api.use(mw(), options); - - t.notThrows(() => api.config.toConfig()); -}); diff --git a/packages/web/README.md b/packages/web/README.md index 344fa5461..328b93c27 100644 --- a/packages/web/README.md +++ b/packages/web/README.md @@ -182,8 +182,8 @@ The following shows how you can pass an options object to the Web preset and ove module.exports = { use: [ ['@neutrinojs/web', { - // Change options for @neutrinojs/env - env: [], + // Enables and configures `EnvironmentPlugin`. See below for example usage. + env: false, // Enables Hot Module Replacement. Set to false to disable hot: true, @@ -300,6 +300,36 @@ module.exports = { }; ``` +### Environment variables + +To use environment variables at compile time, use the `env` setting to enable and configure +[EnvironmentPlugin](https://webpack.js.org/plugins/environment-plugin/) (`env` accepts the +same options as the plugin). There is no need to specify `NODE_ENV`, since webpack defines +it automatically. The environment variables can then be used via `process.env.`. + +For example: + +```js +['@neutrinojs/web', { + env: [ + // webpack will output a warning if these are not defined in the environment. + 'VAR_ONE', + 'VAR_TWO', + ] +} +``` + +Or to set default values, use the object form: + +```js +['@neutrinojs/web', { + env: { + VAR_ONE: 'foo', + VAR_TWO: 'bar', + } +} +``` + ### Dev Server Proxy If you are handling requests with a server, you may want to set up a proxy for development. See webpack's [`devServer.proxy`](https://webpack.js.org/configuration/dev-server/#devserver-proxy) for all available options. @@ -421,7 +451,7 @@ _Note: Some plugins are only available in certain environments. To override them | Name | Description | Environments and Commands | | --- | --- | --- | -| `env` | Inject environment variables into source code at `process.env`, defaults to only inject `NODE_ENV`. From `@neutrinojs/env`. | all | +| `env` | Inject environment variables into source code at `process.env`, using `EnvironmentPlugin`. | all | | `extract` | Extracts CSS from JS bundle into a separate stylesheet file. From `@neutrinojs/style-loader`. | all | | `html-sibling-chunks` | Works around `html-webpack-plugin` not supporting `splitChunks` when using multiple entrypoints, via `html-webpack-include-sibling-chunks-plugin`. | all | | `html-{MAIN_NAME}` | Automatically generates HTML files for configured entry points. `{MAIN_NAME}` corresponds to the entry point of each page. By default, there is only a single `index` main, so this would generate a plugin named `html-index`. From `@neutrinojs/html-template` | all | diff --git a/packages/web/index.js b/packages/web/index.js index 6c9a45f4a..f2c92988a 100644 --- a/packages/web/index.js +++ b/packages/web/index.js @@ -3,12 +3,11 @@ const styleLoader = require('@neutrinojs/style-loader'); const fontLoader = require('@neutrinojs/font-loader'); const imageLoader = require('@neutrinojs/image-loader'); const compileLoader = require('@neutrinojs/compile-loader'); -const env = require('@neutrinojs/env'); const htmlTemplate = require('@neutrinojs/html-template'); const clean = require('@neutrinojs/clean'); const loaderMerge = require('@neutrinojs/loader-merge'); const devServer = require('@neutrinojs/dev-server'); -const { HotModuleReplacementPlugin } = require('webpack'); +const { EnvironmentPlugin, HotModuleReplacementPlugin } = require('webpack'); const { resolve } = require('url'); const merge = require('deepmerge'); const HtmlWebpackIncludeSiblingChunksPlugin = require('html-webpack-include-sibling-chunks-plugin'); @@ -19,7 +18,7 @@ module.exports = (neutrino, opts = {}) => { const publicPath = opts.publicPath || './'; const options = merge({ publicPath, - env: [], + env: false, hot: true, html: {}, devServer: { @@ -97,8 +96,9 @@ module.exports = (neutrino, opts = {}) => { }, options.babel) }); - if (options.env.length) { - neutrino.use(env, options.env); + if (options.env) { + neutrino.config.plugin('env') + .use(EnvironmentPlugin, [options.env]); } neutrino.use(htmlLoader); diff --git a/packages/web/package.json b/packages/web/package.json index 376c2ecb9..9a8ff7746 100644 --- a/packages/web/package.json +++ b/packages/web/package.json @@ -29,7 +29,6 @@ "@neutrinojs/clean": "9.0.0-0", "@neutrinojs/compile-loader": "9.0.0-0", "@neutrinojs/dev-server": "9.0.0-0", - "@neutrinojs/env": "9.0.0-0", "@neutrinojs/font-loader": "9.0.0-0", "@neutrinojs/html-loader": "9.0.0-0", "@neutrinojs/html-template": "9.0.0-0", diff --git a/packages/web/test/web_test.js b/packages/web/test/web_test.js index ed856acb5..fa09de20f 100644 --- a/packages/web/test/web_test.js +++ b/packages/web/test/web_test.js @@ -2,21 +2,22 @@ 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']; test('loads preset', t => { - t.notThrows(() => require('..')); + t.notThrows(mw); }); test('uses preset', t => { - t.notThrows(() => new Neutrino().use(require('..'))); + t.notThrows(() => new Neutrino().use(mw())); }); test('valid preset production', t => { const api = new Neutrino(); api.config.mode('production'); - api.use(require('..')); + api.use(mw()); const config = api.config.toConfig(); // Common @@ -40,7 +41,7 @@ test('valid preset development', t => { const api = new Neutrino(); api.config.mode('development'); - api.use(require('..')); + api.use(mw()); const config = api.config.toConfig(); // Common @@ -64,23 +65,39 @@ test('valid preset development', t => { t.is(errors.length, 0); }); +test('supports env option using array form', t => { + const api = new Neutrino(); + + const env = ['VAR1', 'VAR2']; + api.use(mw(), { env }); + t.deepEqual(api.config.plugin('env').get('args'), [ env ]); +}); + +test('supports env option using object form', t => { + const api = new Neutrino(); + + const env = { VAR: 'default-value' }; + api.use(mw(), { env }); + t.deepEqual(api.config.plugin('env').get('args'), [ env ]); +}); + test('throws when minify.babel defined', async t => { const api = new Neutrino(); - const err = t.throws(() => api.use(require('..'), { minify: { babel: false } })); + const err = t.throws(() => api.use(mw(), { minify: { babel: false } })); t.true(err.message.includes('The minify.babel option has been removed')); }); test('throws when minify.image defined', async t => { const api = new Neutrino(); - const err = t.throws(() => api.use(require('..'), { minify: { image: true } })); + const err = t.throws(() => api.use(mw(), { minify: { image: true } })); t.true(err.message.includes('The minify.image option has been removed')); }); test('throws when minify.style defined', async t => { const api = new Neutrino(); - const err = t.throws(() => api.use(require('..'), { minify: { style: false } })); + const err = t.throws(() => api.use(mw(), { minify: { style: false } })); t.true(err.message.includes('The minify.style option has been removed')); });