admin管理员组文章数量:1189403
TLDR: How can I make a module (imported via ES6 syntax) globally scoped (or reference an imported class inside another class)?
I'm importing a module from a package which wasn't implemented properly (no export etc) but am running into some issues.
What I am doing is using var
to set the module to global (not great) e.g.
var Example = require('./node_modules/example/long_path_to_file.js');
As I need to use it like so in my class (the module takes control of this
and class instances aren't available in the global scope so I can't use my class as I normally would):
new window.Example(...)
This works but it isn't great as I'm using webpack and would prefer to use the proper es6 syntax
import Example from './example';
and then in example.js
export default Example = require('./node_modules/example/long_path_to_file.js');
However this means it is no longer global scoped, and I'm unable to find a fix.
I've tried things like window.Example = Example
but it doesn't work.
TLDR: How can I make a module (imported via ES6 syntax) globally scoped (or reference an imported class inside another class)?
I'm importing a module from a package which wasn't implemented properly (no export etc) but am running into some issues.
What I am doing is using var
to set the module to global (not great) e.g.
var Example = require('./node_modules/example/long_path_to_file.js');
As I need to use it like so in my class (the module takes control of this
and class instances aren't available in the global scope so I can't use my class as I normally would):
new window.Example(...)
This works but it isn't great as I'm using webpack and would prefer to use the proper es6 syntax
import Example from './example';
and then in example.js
export default Example = require('./node_modules/example/long_path_to_file.js');
However this means it is no longer global scoped, and I'm unable to find a fix.
I've tried things like window.Example = Example
but it doesn't work.
2 Answers
Reset to default 13If you are using webpack
it's easy to setup it. So here is a simple example how to implement it.
webpack.config.js
module.exports = {
entry: 'test.js',
output: {
filename: 'bundle.js',
path: 'home',
library: 'home' // it assigns this module to the global (window) object
}
...
}
some.html
<script>console.log(home)</script>
Also if you open your bundle.js
file you will see how webpack did it for you.
var home = // main point
/*****/ (function(modules) blablabla)
...
Also i suggest look at webpack library configuration.
I hope it will help you.
Thanks
I've done some testing and this works correctly:
import './middleman';
// './middleman.js'
window.Example = require('./example.js').default
// OR
window.Example = require('./example.js').Example
// './example.js'
export function Example() {
this.name = 'Example'
}
export { Example as default }
本文标签: javascriptImport ES6 module into global scopeStack Overflow
版权声明:本文标题:javascript - Import ES6 module into global scope - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738361245a2080773.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
example/long_path_to_file.js
is setting a variablewindow.Example
which you want to use via webpack? – CodingIntrigue Commented Feb 24, 2016 at 11:27window.aaa = require(....)
\ – Royi Namir Commented Feb 24, 2016 at 11:28window.Example
by usingvar Example
in the outermost scope (not sure ifstrict
mode even allows that). I need it to be global because this module takes over the context in my class (and es6 class instances aren't referenced in the global scope) – user5786934 Commented Feb 24, 2016 at 11:30require
) already works but I want to know how to make a module global using ES6 – user5786934 Commented Feb 24, 2016 at 11:33