Nanoscale Parts Manufacturing

    @moxy/next-compile-node-modules

    2.1.1 • Public • Published

    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 Prepend exclusions to the Webpack rule exclude condition besides the built-in¹ ones 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 exclusions are Next.js and Webpack related, such as node-libs-browser and process.

    ² Built-in externals are Next.js related as well as modules associated with React. More specifically, it prevents 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 identify if we are compiling a node module or not via api.caller like so:

    // 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 {
            // ...
        };
    };

    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.

    Install

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

    DownloadsWeekly Downloads

    2,209

    Version

    2.1.1

    License

    MIT

    Unpacked Size

    20.4 kB

    Total Files

    5

    Last publish

    Collaborators

    • avatar
    • avatar
    • avatar
    • avatar
    • avatar
    • avatar
    • avatar
    • avatar
    • avatar
    • avatar
    • avatar
    • avatar
    • avatar
    • avatar
    • avatar