Created
December 18, 2015 18:02
-
-
Save dreyescat/c3fd66ea0f7e97ba5c21 to your computer and use it in GitHub Desktop.
Webpack config to expose bundle in a variable in the global context
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
</head> | |
<body> | |
<script src="lib/yourlib.js"></script> | |
<script> | |
window.onload = function () { | |
EntryPoint.run(); | |
}; | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
run: function () { | |
console.log('run from library'); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "test-webpack-library", | |
"version": "1.0.0", | |
"description": "", | |
"main": "./lib/yourlib.js", | |
"scripts": { | |
"build": "webpack" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "MIT" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var webpack = require('webpack'); | |
module.exports = { | |
entry: './index.js', | |
output: { | |
path: './lib', | |
filename: 'yourlib.js', | |
libraryTarget: 'var', | |
library: 'EntryPoint' | |
} | |
}; |
Any solution ? Im getting the same as @darkworks with webpack 3
I got this working with the following config:
module.exports = {
entry: './index.js',
output: {
path: './lib',
filename: 'yourlib.js',
libraryTarget: 'umd',
library: 'YourLibraryName',
umdNamedDefine: true,
libraryExport: 'default'
}
};
The libraryExport
is needed if you want to expose the default export of your module, but you can set it a named export as well. In this case I had an export default class Foo
that I wanted to expose, so I just set the export to default
.
See more information about the output configs here.
@caroso1222 Thanks! Your solution helped me out
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
not working getting
Uncaught ReferenceError: EntryPoint is not defined