Created
December 8, 2019 15:46
-
-
Save mfdeveloper/9e0153fe08e1a56af74135e1190d2c64 to your computer and use it in GitHub Desktop.
Webpack: How to load non module scripts into global scope | window
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
// Import or require the module to includes global variables from the lib | |
import "p5.scenemanager" |
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
// Use the lib | |
function myFunction() { | |
var manager = new window.SceneManager(); | |
} |
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
{ | |
"script-loader": "^0.7.2", | |
"webpack": "^4.41.2", | |
"webpack-cli": "^3.3.10", | |
"webpack-dev-server": "^3.9.0", | |
"webpack-merge": "^4.2.2" | |
} |
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
/* | |
* Uses 'script-loader' npm package to add a JS dependency from node_modules to "window" | |
* | |
* Obs: Using webpack >= 4.0 | |
* | |
* @see https://medium.com/@yushulx/react-load-global-javascript-library-with-webpack-fa3e1a33d8bc | |
* @see https://webpack.js.org/guides/shimming/ | |
* @see https://stackoverflow.com/questions/48998102/webpack-how-to-load-non-module-scripts-into-global-scope-window | |
* @see https://webpack.js.org/configuration/externals/ | |
*/ | |
module.exports = { | |
entry: path.resolve(__dirname, 'index.js'), | |
module: { | |
rules: [ | |
{ | |
loader: 'babel-loader', | |
exclude: /node_modules/, | |
test: /\.js$/ | |
}, | |
{ | |
test: /\.js$/, | |
include: path.resolve(__dirname, 'node_modules', 'p5.scenemanager'), | |
use: [ 'script-loader' ] | |
} | |
], | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment