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

Parcel 2 JS API: Node.js ES Modules support #5224

Closed
ai opened this issue Oct 8, 2020 · 4 comments
Closed

Parcel 2 JS API: Node.js ES Modules support #5224

ai opened this issue Oct 8, 2020 · 4 comments

Comments

@ai
Copy link

ai commented Oct 8, 2020

πŸ› bug report

@parcel/core exports an object with { default, … } instead of native ESM.

πŸŽ› Configuration (.babelrc, package.json, cli command)

import Parcel from '@parcel/core'

let bundler = new Parcel({
  entries: join(SRC, 'index.pug'),
  defaultConfig: {
    ...defaultConfigContents,
    filePath: join(ROOT, 'node_modules', '@parcel', 'config-default')
  }
})

πŸ€” Expected Behavior

Parcel is a class.

😯 Current Behavior

Parcel is:

{
  registerSerializableClass: [Getter],
  unregisterSerializableClass: [Getter],
  prepareForSerialization: [Getter],
  restoreDeserializedObject: [Getter],
  serialize: [Getter],
  deserialize: [Getter],
  default: [Getter],
  BuildError: [Getter],
  createWorkerFarm: [Getter],
  INTERNAL_RESOLVE: [Getter],
  INTERNAL_TRANSFORM: [Getter]
}

Error:

TypeError: Parcel is not a constructor

πŸ’ Possible Solution

Use conditional exports

🌍 Your Environment

Software Version(s)
Parcel 2.0.0-nightly.420
Node 14.13.1
npm/Yarn 1.22.10
Operating System Fedora 32
@mischnic
Copy link
Member

mischnic commented Oct 8, 2020

Good idea.

We forgot to update the docs after a recent PR: parcel-bundler/website#731
This is the current API (and what works at the moment):

import ParcelCore from "@parcel/core";
const { default: Parcel } = ParcelCore; // TODO

import path from "path";
import { createRequire } from "module";
import { dirname } from "path";
import { fileURLToPath } from "url";

let bundler = new Parcel({
	entries: path.join(dirname(fileURLToPath(import.meta.url)), "index.pug"),
	defaultConfig: createRequire(import.meta.url).resolve(
		"@parcel/config-default"
	),
});

(async () => {
	await bundler.run();
})();

@devongovett
Copy link
Member

Hmm we should do this for the "@parcel/plugin" package as well, and probably support loading plugins that are ESM. Not sure how to do that though as we currently use node's require to load all plugins. We'd need to add ESM support to the package manager.

let plugin = await packageManager.require(resolved, resolveFrom, {
autoinstall,
});

@mischnic
Copy link
Member

We'd need to add ESM support to the package manager.

AFAICT, the only ways to somehow intercept/modify import calls are:

@devongovett
Copy link
Member

The api woks via esm now

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

No branches or pull requests

3 participants