Giter VIP home page Giter VIP logo

next-plugin-transpile-modules's Introduction

Next.js + Transpile node_modules

Transpile untranspiled modules from node_modules. Makes it easy to have local libraries and keep a slick, manageable dev experience.

Compatibility table

Next.js version Plugin version
Next.js 8 2.x
Next.js 6 / 7 1.x

Installation

npm install --save @weco/next-plugin-transpile-modules

or

yarn add @weco/next-plugin-transpile-modules

Usage

Classic:

// next.config.js
const withTM = require('@weco/next-plugin-transpile-modules');

module.exports = withTM({
  transpileModules: ['somemodule', 'and-another']
});

note: please declare withTM as your last plugin (the "most nested" one).

Example with next-typescript:

const withTypescript = require('@zeit/next-typescript');
const withTM = require('@weco/next-plugin-transpile-modules');

module.exports = withTypescript(
  withTM({
    transpileModules: ['somemodule', 'and-another']
  })
);

With next-compose-plugins:

const withPlugins = require('next-compose-plugins');

const withTypescript = require('@zeit/next-typescript');
const withTM = require('@weco/next-plugin-transpile-modules');

module.exports = withPlugins([
  [withTM, {
    transpileModules: ['some-module', 'and-another'],
  }],
  withTypescript,
], {
  // ...
});

FAQ

I have trouble making it work with Next.js 7

Next.js 7 introduced Webpack 4 and Babel 7, which changed a couple of things, especially for TypeScript and Flow plugins.

If you have a transpilation error when loading a page, check that your babel.config.js is up to date and valid, you may have forgotten a preset there.

I have trouble with transpilation and Yarn workspaces

If you get a transpilation error when using Yarn workspaces, make sure you are using a babel.config.js and not a .babelrc. The former is a project-wide Babel configuration, when the latter works for relative paths only (and won't work as Yarn install dependencies in a parent directory).

I have trouble with Yarn and hot reloading

If you add a local library (let's say with yarn add ../some-shared-module), Yarn will copy those files by default, instead of symlinking them. So your changes to the initial folder won't be copied to your Next.js node_modules directory.

You can go back to npm, or use Yarn workspaces. See an example in the official Next.js repo.

I have trouble making it work with Lerna

Lerna's purpose is to publish different packages from a monorepo, it does not help for and does not intend to help local development with local modules.

This is not coming from me, but from Lerna's maintainer.

So you are probably using it wrong, and I advice you to use npm or Yarn workspaces instead.

But... I really need to make it work with Lerna!

You may need to tell your Webpack configuration how to properly resolve your scoped packages, as they won't be installed in your Next.js directory, but the root of your Lerna setup.

const withTM = require('@weco/next--plugin-transpile-modules');

module.exports = withTM({
  transpileModules: ['@your-project/shared', '@your-project/styleguide'],
  webpack: (config, options) => {
    config.resolve.alias = {
      ...config.resolve.alias,
      // Will make webpack look for these modules in parent directories
      '@your-project/shared': require.resolve('@your-project/shared'),
      '@your-project/styleguide': require.resolve('@your-project/styleguide'),
      // ...
    };
    return config;
  },
});

next-plugin-transpile-modules's People

Contributors

curran avatar jamesgorrie avatar jesstelford avatar kutlugsahin avatar martpie avatar sebastiandedeyne avatar simenb avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.