Git Product home page Git Product logo

rollup-plugin-pnp-resolve's Introduction

Plug'n'Play resolver for Rollup

Installation

⚠️ This plugin isn't needed anymore; the rollup-plugin-node-resolver supports Plug'n'Play by default.

yarn add -D rollup-plugin-pnp-resolve

Usage

Simply inject the plugin into your pipeline:

const resolve = require(`rollup-plugin-pnp-resolve`);

module.exports = {
  plugins: [
    resolve(),
  ],
};

Don't forget that if you're using commonjs, then you also need to inject the rollup-plugin-commonjs plugin:

const commonjs = require(`rollup-plugin-commonjs`);
const resolve = require(`rollup-plugin-pnp-resolve`);

module.exports = {
  plugins: [
    commonjs(),
    resolve(),
  ],
};

You can also pass options to be forwarded to PNP's resolveRequest

const resolve = require(`rollup-plugin-pnp-resolve`);

module.exports = {
  plugins: [
    resolve({extensions: ['.jsx', '.js']}),
  ],
};

License (MIT)

Copyright © 2016 Maël Nison

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

rollup-plugin-pnp-resolve's People

Contributors

arcanis avatar jlhwung avatar marco-silva0000 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

rollup-plugin-pnp-resolve's Issues

Seems to not work in monorepo? (works without this plugin)

I have a monorepo with 3 worktrees (workspaces: modules/*, commands/*, presets/*) and all of them are in ESM.

I have build script on all of them which is similar to what's seen in the Berry monorepo: build: "run build:compile \"$(pwd)\"", so running the build:compile from the root package.json. Which script is PKG_PATH=\"$0\" rollup -c.

I have this plugin installed and this rollup config

import resolve from 'rollup-plugin-pnp-resolve';

export default {
  input: path.join(process.env.PKG_PATH, 'src/index.js'),
  output: [
    {
      file: path.join(process.env.PKG_PATH, 'dist/cjs/index.js'),
      format: 'cjs',
    },
    {
      file: path.join(process.env.PKG_PATH, 'dist/esm/index.js'),
      format: 'esm',
    }
  ],
  plugins: [
    resolve(),
  ],
};

When I run build (for all workspaces with yarn workspaces foreach -ptv run build) I got error that it cannot resolve the dependencies.

➤ YN0000: [@hela/sade]: /home/charlike/github/tunnckoCore/hela/modules/sade/src/index.js → modules/sade/dist/cjs/index.js, modules/sade/dist/esm/index.js...
➤ YN0000: [@hela/sade]: [!] Error: 'default' is not exported by .yarn/cache/mri-npm-1.1.4-d22a399f26-1.zip/node_modules/mri/lib/index.js
➤ YN0000: [@hela/sade]: https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module
➤ YN0000: [@hela/sade]: modules/sade/src/index.js (3:7)
➤ YN0000: [@hela/sade]: 2: 
➤ YN0000: [@hela/sade]: 3: import mri from 'mri';

It obviously tries to get the dependency in wrong way .yarn/cache/mri-npm-1.1.4-d22a399f26-1.zip/node_modules/mri/lib/index.js

When I run rollup without config and from the terminal, from the monorepo root

rollup -i ./modules/sade/src/index.js -o bundle.js

it works...

How to resolve main fields

As of right now this plugin cannot be used as a drop-in for rollup-plugin-node-resolve because it ignores mainfields. It was mentioned in webpack/enhanced-resolve#162 (comment) that this should be done in a separate plugin but mainfields used throughout many packages to identify commonJS and ES module builds while extensions are rarely used to identify these.

Would you consider adding support for a mainFields option to this plugin? I would like to put together a pull request for this.

Right now I'm using this custom jest resolver

/**
 * Uses the specified mainField with PnP resolution
 * @param {string} request
 * @param {string} mainField
 * @param {JestResolverOptions} resolverOptions
 */
function resolveMainField(request, mainField, resolverOptions) {
	const issuer = `${resolverOptions.basedir}/`;
	const location = pnp.resolveToUnqualified(request, issuer, {
		extensions: resolverOptions.extensions,
	});
	/* 
	 * intuitevly packageInformation points to @material-ui/core's package.json
	 * but it returns the workspace root 
	const locator = pnp.findPackageLocator(location);
	const packageInformation = pnp.getPackageInformation(locator); */
	const packageJson = fse.readJsonSync(
		path.resolve(location, './package.json'),
	);

	const resolution = path.resolve(location, packageJson[mainField]);
	return resolution;
}

Overall I feel like I'm missing something because this goes back to resolve-by-filesystem. Shouldn't this information be available via pnpapi?

Unable to resolve dependencies

I'm trying a simple setup where I just want to import { LitElement, html } from "lit-element"; and have the sources pulled into my bundle.

With my PnP setup, this always results in these issues:

src/js/index.js → dist/index.bundle.js...
(!) Unresolved dependencies
https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency
lit-element (imported by src/js/index.js)

For this, I'm using the following configuration:

import commonjs from "rollup-plugin-commonjs";
import resolve from "rollup-plugin-pnp-resolve";

export default [
  {
    input: "src/js/index.js",

    output: {
      file: "dist/index.bundle.js",
      format: "iife",
      sourcemap: "inline",
    },
    plugins: [
      resolve(),
      commonjs(),
    ]
  }
];

If I use rollup-plugin-node-resolve instead, everything works as expected, even though I'm definitely using PnP :D

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.