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

feat(node-resolve): Export defaults #301

Merged
merged 8 commits into from
Apr 22, 2020

Conversation

MichaelDeBoey
Copy link
Contributor

@MichaelDeBoey MichaelDeBoey commented Apr 10, 2020

Rollup Plugin Name: node-resolve

This PR contains:

  • feature

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • no

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

If yes, then include "BREAKING CHANGES:" in the first commit message body, followed by a description of what is breaking.

Note from @shellscape: We're going to change this to a named export and will break. Needs a new major.

List any relevant issue numbers:

Description

Closes #299

@MichaelDeBoey MichaelDeBoey force-pushed the export-node-resolve-defaults branch from 2e86cf2 to 8aa6196 Compare April 10, 2020 20:11
@shellscape
Copy link
Collaborator

Hey thanks for the PR! Looks like that broke a few things. Give that a test run locally.

@MichaelDeBoey
Copy link
Contributor Author

MichaelDeBoey commented Apr 10, 2020

Hmmm tests are failing because I'm doing an export default and a named export now.
But that shouldn't fail, right? 🤔

@shellscape Any idea on how to make this a non-breaking change? 🤔

@NotWoods
Copy link
Member

We might need some wrapper for the common JS output.

Before:

module.exports = nodeResolve(...)

After:

exports.default = nodeResolve(...)
exports.defaults = defaults

Ideal:

mod = nodeResolve(...)
mod.defaults = defaults
module.exports = mod

Also, since export default and export defaults are quite similar, I think defaultOptions or DEFAULT_OPTIONS would be better names.

@MichaelDeBoey
Copy link
Contributor Author

MichaelDeBoey commented Apr 10, 2020

@NotWoods Wrapping the module still fails tests

@shellscape
Copy link
Collaborator

@MichaelDeBoey please merge from master again. we fixed up some CI related things today.

@MichaelDeBoey MichaelDeBoey force-pushed the export-node-resolve-defaults branch from 5ccffec to 284d037 Compare April 12, 2020 17:00
@MichaelDeBoey
Copy link
Contributor Author

@shellscape Rebased, but CI is still failing tho 😕

@shellscape
Copy link
Collaborator

Bummer. Next step is to run tests locally to triage from there: cd packages/node-resolve && pnpm run test

@MichaelDeBoey
Copy link
Contributor Author

@shellscape It's still the same error

 TypeError {
    message: 'nodeResolve is not a function',
  }

This is because the plugin now exports both a default and a named export.
Wrapping it like @NotWoods suggested isn't working either.

So I don't think we can do this without a breaking change/major release. 🤔

@NotWoods
Copy link
Member

The wrapper should be extracted to a separate file, and used as the entry point for only the CJS build. The presence of an export const and export default statement is throwing off Rollup's CJS build.

@MichaelDeBoey
Copy link
Contributor Author

@NotWoods Could you point me in the right direction on how I could best fix this? 🤔

@NotWoods
Copy link
Member

NotWoods commented Apr 13, 2020

This behavior might be better inside an output plugin, but here's something I whipped together:

// cjs-wrapper.js
import nodeResolve, { DEFAULT_OPTIONS } from './index';

nodeResolve.DEFAULT_OPTIONS = DEFAULT_OPTIONS;

export default nodeResolve;
// rollup.config.js
import babel from 'rollup-plugin-babel';
import json from '@rollup/plugin-json';

import pkg from './package.json';

const plugins = [
    json(),
    babel({
      presets: [
        [
          '@babel/preset-env',
          {
            targets: {
              node: 6
            }
          }
        ]
      ]
    })
];
const external = Object.keys(pkg.dependencies).concat(['fs', 'path', 'os', 'util']);

export default [
	{
		input: 'src/index.js',
		plugins,
		external,
		output: [
    		{ file: pkg.module, format: 'es' }
  		]
	},
	{
		input: 'src/cjs-wrapper.js',
		plugins,
		external,
		output: [
    		{ file: pkg.main, format: 'cjs' }
  		]
	},
];

@MichaelDeBoey
Copy link
Contributor Author

@shellscape @NotWoods All is green now 🙂

Copy link
Collaborator

@shellscape shellscape left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working through the issues getting CI good and a solid wrapper going. Now that we've got a good approach, I've got a few points I wanted to touch on. Please don't get discouraged!

packages/node-resolve/src/index.js Outdated Show resolved Hide resolved
packages/node-resolve/src/cjs-wrapper.js Outdated Show resolved Hide resolved
packages/node-resolve/src/cjs-wrapper.js Outdated Show resolved Hide resolved
@MichaelDeBoey MichaelDeBoey force-pushed the export-node-resolve-defaults branch 2 times, most recently from 5e32561 to 3cd8ce3 Compare April 14, 2020 18:14
@MichaelDeBoey
Copy link
Contributor Author

@shellscape @NotWoods All is green again now 🙂

Copy link
Collaborator

@shellscape shellscape left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for continuing to work through this. I'm sorry for the delayed reviews, doing the best I can with the time I've got on my end.

packages/node-resolve/src/index.js Outdated Show resolved Hide resolved
packages/node-resolve/src/index.js Show resolved Hide resolved
@MichaelDeBoey MichaelDeBoey force-pushed the export-node-resolve-defaults branch from 853111a to 53d46c0 Compare April 16, 2020 15:02
@MichaelDeBoey MichaelDeBoey force-pushed the export-node-resolve-defaults branch from bb1bf0a to f983f7c Compare April 16, 2020 20:38
@shellscape
Copy link
Collaborator

@MichaelDeBoey I'll fix this conflict when I get the chance to circle back.

@MichaelDeBoey MichaelDeBoey force-pushed the export-node-resolve-defaults branch from f983f7c to b27916e Compare April 22, 2020 15:50
@MichaelDeBoey
Copy link
Contributor Author

@shellscape Rebase onto upstream/master

@shellscape shellscape merged commit ee5be6a into rollup:master Apr 22, 2020
@shellscape
Copy link
Collaborator

Thanks for all the hard work that went into this one.

@MichaelDeBoey MichaelDeBoey deleted the export-node-resolve-defaults branch April 22, 2020 18:04
@MichaelDeBoey
Copy link
Contributor Author

With pleasure! 🙂

LarsDenBakker pushed a commit to LarsDenBakker/plugins that referenced this pull request Sep 12, 2020
BREAKING CHANGE:

The plugin function is no longer the default export. Moving forward, there are two named exports:

```js
{ DEFAULTS, nodeResolve }
```

* feat(node-resolve): Export defaults

* Wrap module

* Don't export DEFAULT_OPTIONS directly

* Create cjs-wrapper

* Named export

* Freeze defaults

* Deep freeze defaults

* Deep merge defaults
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

Successfully merging this pull request may close these issues.

Export defaults from node-resolve plugin
4 participants