Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Declaring multiple namespaced packages with glob/regex #149

Closed
equinusocio opened this issue Dec 16, 2020 · 4 comments
Closed

Declaring multiple namespaced packages with glob/regex #149

equinusocio opened this issue Dec 16, 2020 · 4 comments

Comments

@equinusocio
Copy link

equinusocio commented Dec 16, 2020

Hello! How we can declare multiple scoped packages without specifying all of them? For example i want to declare all packages that are under the @MyScope. Before v6 we used:

  const withTM = require('next-transpile-modules')(['@MyScope/.+']);
@equinusocio equinusocio changed the title Declaring multiple scoped packages with glob/regex Declaring multiple namespaced packages with glob/regex Dec 16, 2020
@martpie
Copy link
Owner

martpie commented Dec 16, 2020

Hello 👋

It was a consequence of the previous way the plugin worked, it was not documented and was never officially supported (as said in the changelog). Since v5 this is not possible anymore, and is probably not something we will re-add.

You can read more there: #143 (comment)

@martpie martpie closed this as completed Dec 16, 2020
@martpie martpie pinned this issue Dec 16, 2020
@nandorojo
Copy link

nandorojo commented Apr 12, 2021

For anyone interested, this is how I added all scoped packages:

First, create a scopes array:

const scopes = [
  '@fullcalendar', 
  '@motify',
  '@nandorojo',
  '@dripsy', 
]

Next, get the names of any packages in those scopes. If you want to black list any, add ignoreTranspileModules: true to their package.json (or implement that as an array in this file).

// next.config.js

const path = require('path')
const fs = require('fs')

// you might need to change this to the relative path of your node_modules!
const node_modules = path.resolve(__dirname, '../..', 'node_modules')

const scopes = [
  '@fullcalendar', 
  '@motify',
  '@nandorojo',
  '@dripsy', 
]

const scopedPackages = []

fs.readdirSync(node_modules)
  .filter((name) => scopes.includes(name))
  .forEach((scope) => {
    fs.readdirSync(path.resolve(node_modules, scope))
      .filter((name) => !name.startsWith('.'))
      .forEach((folderName) => {
        const { name, ignoreTranspileModules } = require(path.resolve(
          node_modules,
          scope,
          folderName,
          'package.json'
        ))
        if (!ignoreTranspileModules) {
          scopedPackages.push(name)
        }
      })
  })

// add it to transpile modules
module.exports = require('next-transpile-modules')(
  [
    'dripsy',
    'expo-next-react-navigation',
    'moti',
    ...scopedPackages,
  ],
  { resolveSymlinks: true }
)

@flaviouk
Copy link

I'm using yarn workspaces and got it working using this:

const path = require('path')
const withPlugins = require('next-compose-plugins')
const withTM = require('next-transpile-modules')
const { getAllPackages } = require('standard-monorepo')

// getAllPackages takes a context path, defaults to process.cwd()
// It gets all the package.jsons found in "workspaces"
const packages = getAllPackages(path.join(__dirname, '../../')).map(
  pkg => pkg.name,
)

module.exports = withPlugins([withTM(packages)], config)

@slbls
Copy link

slbls commented Dec 7, 2021

If anyone is interested, here's a reusable function based on @nandorojo's code:

const path = require("path");
const fs = require("fs");

/**
 * Get an array of all modules within the scope of an array of module scopes.
 *
 * This is useful in combination with the `next-transpile-modules` Next.js
 * plugin, as it requires specifying all modules individually, regardless of
 * if they share a scope.
 *
 * @example
 * // Returns `["@example/hello", "@example/world"]`.
 * getModulesFromScopes(["@example"])
 *
 * @see https://github.com/martpie/next-transpile-modules/issues/143
 * @see https://github.com/martpie/next-transpile-modules/issues/149
 */
const getModulesFromScopes = (
	scopes,
	nodeModulesPath = path.resolve(__dirname, "node_modules")
) => {
	const nodeModulesContents = fs.readdirSync(nodeModulesPath);

	const modules = nodeModulesContents.reduce((accumulator, scopeFolder) => {
		if (!scopes.includes(scopeFolder)) {
			return accumulator;
		}

		const scopeFolderPath = path.resolve(nodeModulesPath, scopeFolder);
		const scopeFolderContents = fs.readdirSync(scopeFolderPath);

		const scopeModules = scopeFolderContents.reduce(
			(accumulator, moduleFolder) => {
				if (moduleFolder.startsWith(".")) {
					return accumulator;
				}

				const modulePath = path.resolve(
					nodeModulesPath,
					scopeFolder,
					moduleFolder,
					"package.json"
				);

				const {
					name: moduleName,
					ignoreTranspileModules
				} = require(modulePath);

				if (!ignoreTranspileModules) {
					accumulator.push(moduleName)
				}

				return accumulator;
			},
			[]
		);

		return accumulator.concat(scopeModules);
	}, []);

	return modules;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants