From 19d5e641c86872cb4ebe5397bf2d7032b4b8c916 Mon Sep 17 00:00:00 2001 From: Eli Perelman Date: Mon, 13 Mar 2017 11:31:22 -0500 Subject: [PATCH] Adding option-configurable compile targets --- docs/presets/neutrino-preset-node/README.md | 98 +++++++++++++++++++++ docs/presets/neutrino-preset-web/README.md | 98 +++++++++++++++++++++ packages/neutrino-preset-node/index.js | 5 +- packages/neutrino-preset-node/package.json | 1 + packages/neutrino-preset-node/yarn.lock | 4 + packages/neutrino-preset-web/index.js | 6 +- 6 files changed, 206 insertions(+), 6 deletions(-) diff --git a/docs/presets/neutrino-preset-node/README.md b/docs/presets/neutrino-preset-node/README.md index 37e23d5..21f5696 100644 --- a/docs/presets/neutrino-preset-node/README.md +++ b/docs/presets/neutrino-preset-node/README.md @@ -231,6 +231,46 @@ The following is a list of plugins and their identifiers which can be overridden By following the [customization guide](../../customization/simple.md) and knowing the rule, loader, and plugin IDs above, you can override and augment the build directly from package.json. +#### Compile targets + +This preset uses babel-preset-env to compile code targeting the Node.js v6.9+. To change +the Node.js target from package.json, specify an object at `neutrino.options.compile.targets` which contains a +[babel-preset-env-compatible](https://github.com/babel/babel-preset-env#targetsnode) Node.js target. + +_Example: Replace the preset Node.js target with support for Node.js 4.2:_ + +```json +{ + "neutrino": { + "options": { + "compile": { + "targets": { + "node": 4.2 + } + } + } + } +} +``` + +_Example: Change support to current Node.js version:_ + +```json +{ + "neutrino": { + "options": { + "compile": { + "targets": { + "node": "current" + } + } + } + } +} +``` + +#### Other customization examples + _Example: Allow importing modules with an `.mjs` extension._ ```json @@ -252,6 +292,64 @@ _Example: Allow importing modules with an `.mjs` extension._ By following the [customization guide](../../customization/advanced.md) and knowing the rule, loader, and plugin IDs above, you can override and augment the build by creating a JS module which overrides the config. +#### Compile targets + +This preset uses babel-preset-env to compile code targeting the Node.js v6.9+. To change +the Node.js target from package.json, specify an object at `neutrino.options.compile.targets` which contains a +[babel-preset-env-compatible](https://github.com/babel/babel-preset-env#targetsnode) Node.js target. + +**Note: Setting these options via `neutrino.options.compile` must be done prior to loading the Node.js preset or they +will not be picked up by the compile middleware. These examples show changing compile targets with options before +loading the preset and overriding them if loaded afterwards.** + +_Example: Replace the preset Node.js target with support for Node.js 4.2:_ + +```js +module.exports = neutrino => { + // Using neutrino.options prior to loading Node.js preset + neutrino.options.compile = { + targets: { + node: 4.2 + } + }; + + // Using compile options override following loading Node.js preset + neutrino.config.module + .rule('compile') + .use('babel') + .tap(options => { + options.presets[0][1].targets.node = 4.2; + + return options; + }); +}; +``` + +_Example: Change support to current Node.js version:_ + +```js +module.exports = neutrino => { + // Using neutrino.options prior to loading Node.js preset + neutrino.options.compile = { + targets: { + node: 4.2 + } + }; + + // Using compile options override following loading Node.js preset + neutrino.config.module + .rule('compile') + .use('babel') + .tap(options => { + options.presets[0][1].targets.node = 'current'; + + return options; + }); +}; +``` + +#### Other customization examples + _Example: Allow importing modules with an `.mjs` extension._ ```js diff --git a/docs/presets/neutrino-preset-web/README.md b/docs/presets/neutrino-preset-web/README.md index a4d3a6d..fd6540e 100644 --- a/docs/presets/neutrino-preset-web/README.md +++ b/docs/presets/neutrino-preset-web/README.md @@ -192,6 +192,48 @@ The following is a list of plugins and their identifiers which can be overridden By following the [customization guide](../../customization/simple.md) and knowing the rule, loader, and plugin IDs above, you can override and augment the build directly from package.json. +#### Compile targets + +This preset uses babel-preset-env to compile code targeting the last 2 browser versions of major browsers. To change +the browser targets from package.json, specify an object at `neutrino.options.compile.targets` which contains a +[browserlist-compatible](https://github.com/ai/browserslist) array of browser targets. + +_Example: Replace the Web preset browser targets with support for browsers with greater than 5% global usage:_ + +```json +{ + "neutrino": { + "options": { + "compile": { + "targets": { + "browsers": [ + "> 5%" + ] + } + } + } + } +} +``` + +_Example: Change support to latest version instead of last 2 versions:_ + +```json +{ + "neutrino": { + "options": { + "compile": { + "targets": { + "browsers": [ + "last 1 version" + ] + } + } + } + } +} +``` + #### Vendoring By defining an entry point in package.json named `vendor` you can split out external dependencies into a chunk separate @@ -241,6 +283,62 @@ _Example: Change the application mount ID from "root" to "app":_ By following the [customization guide](../../customization/advanced.md) and knowing the rule, loader, and plugin IDs above, you can override and augment the build by creating a JS module which overrides the config. +#### Compile targets + +This preset uses babel-preset-env to compile code targeting the last 2 browser versions of major browsers. To change +the browser targets from an override file, specify an object at `neutrino.options.compile.targets` which contains a +[browserlist-compatible](https://github.com/ai/browserslist) array of browser targets. + +**Note: Setting these options via `neutrino.options.compile` must be done prior to loading the Web preset or they +will not be picked up by the compile middleware. These examples show changing compile targets with options before +loading the preset and overriding them if loaded afterwards.** + +_Example: Replace the Web preset browser targets with support for browsers with greater than 5% global usage:_ + +```js +module.exports = neutrino => { + // Using neutrino.options prior to loading Web preset + neutrino.options.compile = { + targets: { + browsers: ['> 5%'] + } + }; + + // Using compile options override following loading Web preset + neutrino.config.module + .rule('compile') + .use('babel') + .tap(options => { + options.presets[0][1].targets.browsers = ['> 5%']; + + return options; + }); +}; +``` + +_Example: Change support to latest version instead of last 2 versions:_ + +```js +module.exports = neutrino => { + // Using neutrino.options prior to loading Web preset + neutrino.options.compile = { + targets: { + browsers: ['last 1 version'] + } + }; + + // Using compile options override following loading Web preset + neutrino.config.module + .rule('compile') + .use('babel') + .tap(options => { + options.presets[0][1].targets.browsers = ['last 1 version']; + + return options; + }); +}; +``` + #### Vendoring By defining an entry point named `vendor` you can split out external dependencies into a chunk separate diff --git a/packages/neutrino-preset-node/index.js b/packages/neutrino-preset-node/index.js index c34711c..be25797 100644 --- a/packages/neutrino-preset-node/index.js +++ b/packages/neutrino-preset-node/index.js @@ -9,6 +9,7 @@ const hot = require('neutrino-middleware-hot'); const namedModules = require('neutrino-middleware-named-modules'); const nodeExternals = require('webpack-node-externals'); const { join } = require('path'); +const { pathOr } = require('ramda'); const MODULES = join(__dirname, 'node_modules'); @@ -30,9 +31,7 @@ module.exports = (neutrino) => { presets: [ [require.resolve('babel-preset-env'), { modules: false, - targets: { - node: 6.9 - } + targets: pathOr({ node: 6.9 }, ['options', 'compile', 'targets'], neutrino) }] ] } diff --git a/packages/neutrino-preset-node/package.json b/packages/neutrino-preset-node/package.json index 93d8bc6..2fdb446 100644 --- a/packages/neutrino-preset-node/package.json +++ b/packages/neutrino-preset-node/package.json @@ -19,6 +19,7 @@ "babel-preset-env": "^1.1.10", "exports-loader": "^0.6.4", "imports-loader": "^0.7.1", + "ramda": "^0.23.0", "webpack-node-externals": "1.5.4", "neutrino-middleware-banner": "^5.0.0", "neutrino-middleware-clean": "^5.0.0", diff --git a/packages/neutrino-preset-node/yarn.lock b/packages/neutrino-preset-node/yarn.lock index 1fe94fa..efa7659 100644 --- a/packages/neutrino-preset-node/yarn.lock +++ b/packages/neutrino-preset-node/yarn.lock @@ -547,6 +547,10 @@ private@^0.1.6: version "0.1.7" resolved "https://registry.yarnpkg.com/private/-/private-0.1.7.tgz#68ce5e8a1ef0a23bb570cc28537b5332aba63ef1" +ramda@^0.23.0: + version "0.23.0" + resolved "https://registry.yarnpkg.com/ramda/-/ramda-0.23.0.tgz#ccd13fff73497a93974e3e86327bfd87bd6e8e2b" + regenerate@^1.2.1: version "1.3.2" resolved "https://registry.yarnpkg.com/regenerate/-/regenerate-1.3.2.tgz#d1941c67bad437e1be76433add5b385f95b19260" diff --git a/packages/neutrino-preset-web/index.js b/packages/neutrino-preset-web/index.js index a2977ca..76151e7 100644 --- a/packages/neutrino-preset-web/index.js +++ b/packages/neutrino-preset-web/index.js @@ -50,7 +50,7 @@ module.exports = (neutrino) => { neutrino.use(styleLoader); neutrino.use(fontLoader); neutrino.use(imageLoader); - neutrino.use(htmlTemplate); + neutrino.use(htmlTemplate, neutrino.options.html); neutrino.use(namedModules); neutrino.use(compileLoader, { include: [neutrino.options.source, neutrino.options.tests], @@ -61,7 +61,7 @@ module.exports = (neutrino) => { modules: false, useBuiltIns: true, include: ['transform-regenerator'], - targets: { + targets: pathOr({ browsers: [ 'last 2 Chrome versions', 'last 2 Firefox versions', @@ -70,7 +70,7 @@ module.exports = (neutrino) => { 'last 2 Safari versions', 'last 2 iOS versions' ] - } + }, ['options', 'compile', 'targets'], neutrino) }] ] }