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

Commit

Permalink
Remove targets from library preset to force es2015 target (#1322)
Browse files Browse the repository at this point in the history
* Remove targets from library preset to force es2015 target

* Remove forceAllTransforms

* Support settings compile targets as top-level option in library preset
  • Loading branch information
eliperelman authored Mar 24, 2019
1 parent 186461a commit 434947e
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 19 deletions.
74 changes: 59 additions & 15 deletions packages/library/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
- Automatically marks dependencies as external
- Easily extensible to customize your library as needed

**Important! This preset does not include
@babel/polyfill for size reasons. If you need
polyfills in your library code, consider
importing @babel/polyfill, core-js, or other
alternative.**

## Requirements

- Node.js ^8.10 or 10+
Expand Down Expand Up @@ -243,15 +249,17 @@ module.exports = {

// Disable cleaning the output build directory
clean: false,

// Target specific browsers or Node.js versions with @babel/preset-env
targets: {},

// Add additional Babel plugins, presets, or env options
babel: {
// Override options for @babel/preset-env
presets: [
['@babel/preset-env', {
// Passing in browser targets to @babel/preset-env will replace them
// instead of merging them when using the 'web' target
targets: { browsers: 'ie 9' }
debug: neutrino.options.debug,
useBuiltIns: 'entry'
}]
]
}
Expand All @@ -260,7 +268,7 @@ module.exports = {
};
```

_Example: Override the library Babel compilation target to Node.js v6 and commonjs2 module:_
_Example: Override the library to output a commonjs2 module:_

```js
const library = require('@neutrinojs/library');
Expand All @@ -270,23 +278,59 @@ module.exports = {
library({
name: 'Logger',
target: 'node',
libraryTarget: 'commonjs2',
// Add additional Babel plugins, presets, or env options
babel: {
// Override options for @babel/preset-env
presets: [
['@babel/preset-env', {
targets: {
node: '6.0'
}
}]
]
libraryTarget: 'commonjs2'
})
]
};
```

### Targets

Using the `targets` option, you can target specific browsers or Node.js versions
to compile to with `@babel/preset-env`. By default, this preset does not set any
targets and will compile to an ES5 baseline.

_Example: Override the library Babel compilation target to Node.js v6:_

```js
const library = require('@neutrinojs/library');

module.exports = {
use: [
library({
name: 'Logger',
target: 'node',
targets: {
node: '6.0'
}
})
]
};
```

Setting to `false` will override Neutrino's default targets and allow
`@babel/preset-env` to read targets from a [`.browserslistrc` file](https://babeljs.io/docs/en/babel-preset-env#browserslist-integration).

```js
const library = require('@neutrinojs/library');

module.exports = {
use: [
library({
name: 'Logger',
target: 'node',
targets: false
})
]
};
```

When using a `.browserslistrc` file, be aware that file changes may not
invalidate cache as expected: https://github.com/babel/babel-loader/issues/690

See [`@babel/preset-env`](https://babeljs.io/docs/en/babel-preset-env#targets)
for all other available settings.

## Customizing

To override the build configuration, start with the documentation on [customization](https://neutrinojs.org/customization/).
Expand Down
11 changes: 7 additions & 4 deletions packages/library/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@ module.exports = (opts = {}) => {
target: 'web',
libraryTarget: 'umd',
babel: {},
externals: {}
externals: {},
targets: {}
}, opts);

if (options.targets === false) {
options.targets = {};
}

Object.assign(options, {
babel: babelMerge({
plugins: [
Expand All @@ -36,9 +41,7 @@ module.exports = (opts = {}) => {
[require.resolve('@babel/preset-env'), {
debug: neutrino.options.debug,
useBuiltIns: 'entry',
targets: options.target === 'node' ?
{ node: '8.10' } :
{ browsers: 'ie 9' }
targets: options.targets
}]
]
}, options.babel)
Expand Down
26 changes: 26 additions & 0 deletions packages/library/test/library_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,32 @@ test('valid preset commonjs2 libraryTarget', t => {
t.is(errors.length, 0);
});

test('targets option test', t => {
const api = new Neutrino();
const targets = {
browsers: ['last 2 iOS versions']
};

api.use(mw({ name: 'alpha', targets }));

t.deepEqual(api.config.module
.rule('compile')
.use('babel')
.get('options')
.presets[0][1].targets, targets);
});

test('targets false option test', t => {
const api = new Neutrino();
api.use(mw({ name: 'alpha', targets: false }));

t.deepEqual(api.config.module
.rule('compile')
.use('babel')
.get('options')
.presets[0][1].targets, {});
});

test('updates lint config by default when target is web', t => {
const api = new Neutrino();
api.use(lint());
Expand Down

0 comments on commit 434947e

Please sign in to comment.