Skip to content

Import JS modules in web editor? #1933

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

Open
Zearin opened this issue Oct 2, 2021 · 4 comments
Open

Import JS modules in web editor? #1933

Zearin opened this issue Oct 2, 2021 · 4 comments
Labels
Feature Request Proposal for adding a new functionality Needs Discussion Requires further conversation or consensus Priority:Low Low-impact issue that can be addressed after higher-priority tasks

Comments

@Zearin
Copy link

Zearin commented Oct 2, 2021

Nature of issue?

  • Existing feature enhancement

NOTE: I am not sure if this is technically an enhancement request or not. If it is not possible to import other files into a sketch in the Web Editor, then this is an enhancement request. (Otherwise, I suppose it’s a…support request (?).)

Feature enhancement details:

I’m learning p5.js, and loving it so far. The web editor definitely makes it easier to jump in and play!

After modifying an existing example, the code got long enough for me to want to split it up into multiple files. However, I don’t want to pollute the global namespace (I actually started out by modifying the example to use P5 in instance mode), so I want to split the code up using JS modules.

But I keep getting the following error in the Web Editor:

Module name, './config.js' does not resolve to a valid URL.

But that file is in the same folder, so I don’t know why this is happening.

(I even tried renaming the file to use the .mjs extension, but no luck.)

So…

How can I import other files into a sketch on the Web Editor?

@welcome
Copy link

welcome bot commented Oct 2, 2021

Welcome! 👋 Thanks for opening your first issue here! And to ensure the community is able to respond to your issue, be sure to follow the issue template if you haven't already.

@I-Omnibus
Copy link

Here's a workaround, although it seems that the solution is as simple as editor.p5.org getting the mime type right for JavaScript files:

/*
Would prefer:

  import * as lib from "./Dependencies.js";
  
or even:

  import * as lib from "https://editor.p5js.org/Omnibus/sketches/8tseBlNuH/Dependencies.js";

but editor.p5.org serves absolute JavaScript urls with incompatible 'module' mime types 
and relative urls are rejected, as the service is not hierarchical 
*/
import * as lib from "https://i-omnibus.github.io/Dependencies.js";

function preload() {
  lib.Dependencies.resolve(["load_me_dynamically"], /*bypassCache*/ true);
}

function setup() {
  noLoop();
  createCanvas(400, 400);
}

function draw() {
  background(220);
}

function mousePressed() {
  print({ mouseX, mouseY });
}

/*
Note that in index.html the script type is 'module' to allow the 'import' keyword:

<script src='sketch.js' type='module'></script>

Which means that the critical functions must be manually imported into the 'dom', as follows:
*/
((
  functions = {
    /* select required functions */
    preload,
    setup,
    draw,
    //mouseMoved,
    //mouseDragged,
    mousePressed,
    //mouseReleased,
    //mouseClicked,
    //doubleClicked,
    //mouseWheel,
  }
) => Object.assign(window, functions)).call();

@raclim raclim added Feature Request Proposal for adding a new functionality Needs Discussion Requires further conversation or consensus Priority:Low Low-impact issue that can be addressed after higher-priority tasks labels Mar 15, 2023
@GregStanton
Copy link

Hi all! I was looking into using modules with the web editor because I'm investigating different options for designing a p5.js add-on library. I made this minimal reproducible example to test out the behavior, and the resulting error message is similar to the one being discussed. I thought I would post the example here in case it's helpful.

@backspaces
Copy link

backspaces commented Jul 13, 2024

So the question I have is: does the p5 web editor support import statements like:

import TwoDraw from 'https://code.agentscript.org/src/TwoDraw.js'

or do i have to use a dynamic import like this:

async function preload() {
  const module = await import('https://code.agentscript.org/src/TwoDraw.js');
  window.TwoDraw = module.default; // Expose the module to the global scope
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Request Proposal for adding a new functionality Needs Discussion Requires further conversation or consensus Priority:Low Low-impact issue that can be addressed after higher-priority tasks
Projects
None yet
Development

No branches or pull requests

5 participants