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

Using parcel but not pointing it at entry point #1225

Closed
mqudsi opened this issue Apr 20, 2018 · 3 comments
Closed

Using parcel but not pointing it at entry point #1225

mqudsi opened this issue Apr 20, 2018 · 3 comments

Comments

@mqudsi
Copy link

mqudsi commented Apr 20, 2018

Hello team,

What is the recommended approach for using parcel, but not pointing it at the outermost resource? For example, let's say I have a JavaScript module that exports a class Foo at foo.js. index.html should be able to include the output generated by parcel foo.js and then create an instance of Foo in inline JavaScript in index.html rather than any external bootstrap.js, etc.

The reason for this is that I would like to use parcel with an ASP.NET Core project, which has its own templating (cshtml). I need to output view-specific parameters to be passed in for the correct instantiation of Foo object.

I tried this:

export default class Foo {
....
}

and directed parcel at foo.ts as the entrypoint, which generated a foo.js that pulled in all the imports used by Foo, but unfortunately it does not seem to actually generate an export for Foo itself, as this in index.html does not work:

<script type="text/javascript" src="dist/foo.js"></script>
<script type="text/javascript">
var foo = new Foo; //Foo is not defined
//nor this
parcelRequire('Foo', foo => console.log(foo)); //module Foo is not found
</script>

I guess what I'm asking is how I can get parcel to package the entrypoint itself in a format that would be usable from plain JavaScript, without doing something like this in a file bootstrap.js:

import Foo from './foo';

(<any>window).Foo = Foo;

Just to expose module Foo for use in index.html:

<script type="text/javascript" src="dist/bootstrap.js"></script>
<script type="text/javascript">
var foo = new window.Foo();
console.log(foo);
</script>

I see in the parcel-generated JavaScript this line:

exports["default"] = Foo;

Is there any way to get to this export?

@fullergalway
Copy link

Same.

@fullergalway
Copy link

fullergalway commented May 3, 2018 via email

@mischnic
Copy link
Member

The last post is correct, that's exactly what parcel build index.js --global YourGlobalVariable is for.

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

4 participants