JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 344
  • Score
    100M100P100Q107998F
  • License MIT

Next.js plugin to compile all node_modules using Babel

Package Exports

  • @moxy/next-compile-node-modules

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@moxy/next-compile-node-modules) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

next-compile-node-modules

NPM version Downloads Build Status Coverage Status Dependency status Dev Dependency status

Next.js plugin to compile all node_modules using Babel.

Motivation

Package authors should publish any JavaScript code to npm as long as it's only pure valid JavaScript (any version). It doesn't make sense to compile at the package level because authors don't know in which context their packages will be used. For instance, a package that has been published with code compiled to ES5 may be used in app that only targets evergreen browsers, making the bundle unnecessarily larger (or vice-versa).

Instead, app developers should compile node_modules using babel-preset-env and instruct it to produce compatible for the browsers they want to support (e.g. "IE 11"). Popular boilerplate projects such as create-react-app now compile all node_modules and you should too!

This plugin changes Next.js's webpack config to compile all node_modules by default. While this has an impact on performance, it only slows down the first compilation and subsequent ones will be much faster thanks to the built-in cache.

Installation

$ npm i --save @moxy/next-compile-node-modules

Usage

// next.config.js
const withCompileNodeModules = require('@moxy/next-compile-node-modules');

module.exports = withCompileNodeModules({ ...options })({ ...nextConfig });

Multiple configurations can be combined together with function composition. For example:

// next.config.js
const withCSS = require('@zeit/next-css');
const withCompileNodeModules = require('@moxy/next-compile-node-modules');

module.exports = withCSS(
    withCompileNodeModules({
        exclude: [require.resolve('some-module')],
    })({
        cssModules: true, // this options will be passed to withCSS plugin through nextConfig
    }),
);

Available options

Option Description Type Default
test The Webpack rule test condition Rule.test /\.js$/
include The Webpack rule include condition Rule.include /[\\/]node_modules[\\/]/
exclude The Webpack rule exclude condition Rule.exclude
serverExternals Prepend additional externals dependencies besides the built-in¹ ones. This option is ignored in the client build or when target is serverless Any supported types

¹ Built-in ones are modules related to react, preventing React from being bundled individually in each page which would cause issues such as "React Hooks would throw: Invalid Hook Call Warning".

Custom babel config

If you are using a custom babel.config.js, you may need to identify if we are compiling a node module or not:

// babel.config.js

module.exports = (api) => {
    const isNodeModule = api.caller((caller) => caller.isNodeModule);

    // You may now use `isNodeModule` to conditionally make changes to the returned config

    return {
        // ...
    };
};

External dependencies

We're removing the property externals from the webpack configuration in order to include external dependencies in the bundle of our applications. This ensures all the dependencies are compiled according to our targets. More information in Webpack's documentation.

Tests

Any parameter passed to the test command, is passed down to Jest.

$ npm t
$ npm t -- --watch # during development

License

Released under the MIT License.