question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Processing inline JavaScript

See original GitHub issue

🐛 bug report

In his blog post, Joe Archibald wrote the following example to demonstrate the use of ECMAScript modules in browsers. Unfortunately, this is broken when using parcel-bundler@1.5.1 but works as expected using serve@6.4.9.

<script type="module">
  import {addTextToBody} from './utils.js';

  addTextToBody('Modules are pretty cool.');
</script>
// utils.js
export function addTextToBody(text) {
  const div = document.createElement('div');
  div.textContent = text;
  document.body.appendChild(div);
}

🎛 Configuration (.babelrc, package.json, cli command)

.babelrc

{
  "presets": ["env", "stage-3"]
}

package.json

{
  "name": "playground",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "start": "parcel index.html"
  },
  "dependencies": {
    "babel-preset-env": "^1.6.1",
    "babel-preset-stage-3": "^6.24.1"
  }
}

🤔 Expected Behavior

The example should render the text, “Modules are pretty cool.”

😯 Current Behavior

Firefox reports the following error:

Loading failed for the <script> with source “http://localhost:1234/utils.js”.

Chrome reports:

Failed to load module script: The server responded with a non-JavaScript MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.

Safari reports:

TypeError: 'text/html' is not a valid JavaScript MIME type.

💁 Possible Solution

Unknown

🔦 Context

I am unaffected by this bug, as I have already found a work-around.

💻 Code Sample

Example code provided in initial bug report.

🌍 Your Environment

Software Version(s)
Parcel 1.5.1
Node v9.3.0
npm/Yarn yarn@1.3.2
Operating System macOS 10.13.3

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
devongovettcommented, Feb 20, 2018

The problem is what to do with that JS after we process it. Currently, all asset types produce an output file. But with inline JS, you’d probably want to inject that JS back into the HTML so it remained inline rather than producing a separate file. That would be hard to do in the current architecture.

1reaction
davidnaglicommented, Feb 20, 2018

@DeMoorJasper @devongovett Is there any real reason why we’re not able to handle inline scripts? I don’t really see why we can’t just split out any JavaScript between script tags and process it with JSAsset.

Is this a real feature that we should be focusing on implementing?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Text String Processing from Inline JS - Stack Overflow
I have the following code that is pulling from inline JS on a webpage to pull the webID. This is being used for...
Read more >
Inline JavaScript in HTML: Don't do it, unless you like really ...
Writing inline JavaScript is one of the many things you learn when you want to tinker with how HTML behaves. However, writing JavaScript...
Read more >
JavaScript Quick Start | Processing.js - GitHub Pages
Processing.js converts Processing code to JavaScript and runs it in the browser, using <canvas> for a drawing surface. To use it, download Processing.js...
Read more >
How To Defer Inline JavaScript - Finally! - Startup Hero
Inline JavaScript executes in the order in which it appears in the page. There's no “deferring” it. As a result, if something in...
Read more >
Processing.js for Processing Devs - MozillaWiki
Under the hood, Processing.js uses the new HTML5 canvas element to create your sketch's graphics. The canvas element is a new feature of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found