parcel: Cannot use functions in HTML from referenced script
π bug report
When attempting to use a function (in an onclick
and onload
, specifically) from an imported script in HTML (i.e <script src="test.js"></script>
), I get ReferenceError: x is not defined
for every function. This has been tested only when running locally in the default parcel dev mode.
π Configuration (.babelrc, package.json, cli command)
{
"scripts": {
"build": "parcel build src/index.html --detailed-report",
"dev": "parcel src/index.html"
},
"dependencies": {},
"devDependencies": {
"babel-plugin-transform-runtime": "^6.23.0",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.7.0",
"parcel-bundler": "^1.9.2"
},
"babel": {
"presets": [
[
"env",
{
"targets": {
"browsers": [
"last 3 versions",
"not ie < 11"
]
}
}
]
],
"plugins": [
"transform-runtime"
],
"comments": false
},
"browserlist": [
"last 3 versions",
"not ie < 11"
]
}
π€ Expected Behavior
When I call a function in HTML that is defined in an imported file, I should run that function
π― Current Behavior
Uncaught ReferenceError: login is not at HTMLButtonElement.onclick ((index):27)
π Possible Solution
Not really sure. Iβve tried using export
on my functions as well. Only directly inlining the script seems to work.
π¦ Context
Iβd like to be able to define some simple JS functions in a separate file from my HTML. Currently, Iβm building a static login page, but the login logic is complex enough that Iβd like for it to live in a separate file.
π» Code Sample
https://github.com/JonathanTroyer/Parcel-html-import
π Your Environment
Software | Version(s) |
---|---|
Parcel | 1.9.3 |
Node | 8.11.1 OR 10.5.0 |
npm/Yarn | 5.6.0/1.7.0 |
Operating System | Windows 10 Pro Version 1803 Build 17134.112 |
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 1
- Comments: 17 (3 by maintainers)
Following this helped me https://stackoverflow.com/questions/57602686/javascript-function-wont-trigger-when-called-in-html-file-during-parcel-build
Could you refer to this example: https://github.com/buronnie/parcel-global-var-test? The class name is sitting in the namespace of
SpaceInvaders
. Still not exactly what you want, but at least it is working π@buronnie I tried your example, it works:
In my example, adding
.ts
seems to have fixed the initial issue:I use postcss imports
Isnβt this already possible with
βglobal <some variable name>
?Sent with GitHawk