parcel icon indicating copy to clipboard operation
parcel copied to clipboard

parcel build generates web app that errors with Uncaught Error: Origin not found

Open micrology opened this issue 1 year ago • 4 comments

🐛 bug report

Generating a web app with npx parcel build produces a runtime error 'Origin not found'. Doing the same, but with the --no-optimize flag set avoids the error.

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

{
	"name": "participatory-system-mapper",
	"version": "2.1.1",
	"description": "Participatory System Mapper, browser app for collaboratively creating system maps",
	"keywords": [
		"social network",
		"participatory",
		"stakeholder",
		"policy analysis"
	],
	"bugs": {
		"email": "[email protected]"
	},
	"author": "Nigel Gilbert",
	"repository": {
		"type": "git",
		"url": "https://github.com/micrology/prsm"
	},
	"targets": {
		"default": {
			"engines": {
				"browserslist": "defaults"
			},
			"distDir": "dist",
			"publicUrl": "."
		}
	},
	"parcelIgnore": [
		"/doc/help/"
	],
	"scripts": {
		"lint": "./node_modules/.bin/eslint ./js/*.js --fix --quiet",
		"pretty": "prettier --write ./html/*.html ./*.html ./js/*.js ./css/*.css doc/**/*.{html,css} package.json",
		"build-help": "cd doc/help && npm run build",
		"check": "npm run --silent lint && npm run --silent build-help && npm run --silent pretty",
		"dev": "rm -rf dist/* && npx parcel build './html/*.html' --no-optimize --no-scope-hoist",
		"build": "rm -rf dist/* && npx parcel build './html/*.html'",
		"jsdoc": "npx jsdoc -r -c .jsdoc.json",
		"clean": "rm -rf dist/*",
		"rebuild-docker": "docker buildx build -f docker/docker-y-websocket/Dockerfile --platform linux/amd64,linux/arm64 -t micrology/prsm-y-websocket --push . && docker buildx build --platform linux/amd64,linux/arm64 -t micrology/prsm-httpd --push -f docker/docker-httpd/Dockerfile .",
		"manual": "cd doc/help && npm run serve &"
	},
	"license": "GPL-3.0-or-later",
	"dependencies": {
		"@egjs/hammerjs": "^2.0.17",
		"@jaames/iro": "^5.5.2",
		"emoji-picker-element": "^1.16.0",
		"fabric": "^5.3.0",
		"fast-xml-parser": "^4.2.4",
		"luxon": "^3.2.1",
		"lz-string": "^1.4.4",
		"quill": "^1.3.7",
		"quill-delta-to-html": "^0.12.1",
		"tabulator-tables": "^5.4.4",
		"three-spritetext": "^1.8.0",
		"uniqolor": "^1.1.0",
		"uuid": "^8.3.2",
		"vis-data": "^7.1.4",
		"vis-network": "^9.1.4",
		"vis-util": "^5.0.3",
		"xlsx": "^0.18.5",
		"y-indexeddb": "^9.0.9",
		"y-websocket": "^1.4.6",
		"yjs": "^13.5.47"
	},
	"devDependencies": {
		"eslint": "^8.35.0",
		"eslint-config-prettier": "^8.6.0",
		"jsdoc": "^4.0.2",
		"microdiff": "^1.3.1",
		"parcel": "latest",
		"parcel-resolver-ignore": "^2.1.5",
		"prettier": "^2.8.4",
		"process": "^0.11.10"
	},
	"private": true
}

🤔 Expected Behavior

No runtime error

😯 Current Behavior

Copied from DevTools:

Uncaught error: Origin not found
 at bundle-url.js:33:15
 at bundle-url.js:36:1

## 🔦 Context

This has only just started happening with `parcel:latest` ([email protected])

## 💻 Code Sample

The code is at https://github.com/micrology/prsm 

To reproduce, 
1. Fork the code, 
2. Run `npm run build`
3. Access http://localhost/prsm.html or local equivalent and view the console in DevTools

## 🌍 Your Environment

| Software         | Version(s) |
| ---------------- | ---------- |
| Parcel           | 2.9.3
| Node             | 20.3.1
| npm/Yarn         | 9.6.7
| Operating System | Mac OS Ventura 13.4.1

Exactly the same bug occurs on AWS:

| Parcel           | 2.9.3
| Node             | 17.7.1
| npm/Yarn         | 8.19.2
| Operating System | Linux2  x86_64

micrology avatar Aug 04 '23 22:08 micrology

Further investigation shows that the error is generated in the parcel helper file bundle-url.js by:

// TODO: Replace uses with `new URL(url).origin` when ie11 is no longer supported.
function getOrigin(url) {
  var matches = ('' + url).match(/(https?|file|ftp|(chrome|moz|safari-web)-extension):\/\/[^/]+/);
  if (!matches) {
    throw new Error('Origin not found');
  }
  return matches[0];
}

and that getOrigin is called with

url: "undefined/betweenness.7abecd3a.js"

(./js/betweenness.js is a file in my code)

betweenness.js is referenced in my code at:

// set  up a web worker to calculate network statistics in parallel with whatever
// the user is doing
var worker = new Worker(new URL('./betweenness.js', import.meta.url), {type: 'module'})

which matches what is suggested in the parcel documentation

micrology avatar Aug 06 '23 11:08 micrology

Possibly related to the new SWC minifier introduced in v2.9 (also reported under #9124). If you swap to using terser or esbuild as the minifier, does the issue go away?

Install terser via npm i @parcel/optimizer-terser --save-dev and add this to your .parcelrc file:

{
  "optimizers": {
    "*.js": ["@parcel/optimizer-terser"]
  }
}

Then run a build and see if that fixes it.

47M18 avatar Aug 11 '23 01:08 47M18

Yes, it does fix it! Wonderful.

micrology avatar Aug 11 '23 08:08 micrology

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs.

github-actions[bot] avatar Feb 07 '24 12:02 github-actions[bot]