[英]How to export a symbol from a webpack bundle to use in another javascript file?
There are some JS modules which I'd like to use on the browser. 我想在浏览器中使用一些JS模块。 I have created a webpack project for that.
我为此创建了一个webpack项目。 However I am unable to use them in my target JS file.
但是,我无法在目标JS文件中使用它们。
I'd like to create a bundle that I could use like the following: 我想创建一个可以如下使用的包:
<html>
<head>
<script src='ndarray-bundle.js'></script>
</head>
<body>
<script>
let ndx = ndarray(x, x_dims.slice(0)).transpose(0, 2, 3, 1);
</script>
</body>
</html>
For that I creates a folder with the following webpack and package.json files: 为此,我使用以下webpack和package.json文件创建一个文件夹:
Webpack file: Webpack文件:
const path = require('path');
const webpack = require('webpack');
module.exports = {
entry: './index.js',
mode: 'development',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'ndarray-bundle.js'
}
};
Package.json: 的package.json:
{
"name": "packstuff-js",
"version": "0.0.1",
"description": "Just so I can pack ndarray stuff as one JS file",
"main": "./lib/index.js",
"scripts": {},
"repository": {
"type": "git",
"url": "https://nothing/"
},
"keywords": [
"ndarray"
],
"author": "jeff",
"license": "MIT",
"dependencies": {
"ndarray": "^1.0.18"
},
"devDependencies": {
"webpack": "^4.19.1",
"webpack-cli": "^3.1.0"
}
}
I run webpack and get a bundled file however everytime I ran that I get the following error: ndarray is not defined
我运行webpack并得到一个捆绑文件,但是每次运行都会出现以下错误:
ndarray is not defined
I have used the following index.js contents all to no avail: 我已使用以下index.js的内容全部无效:
version 1: 版本1:
import ndarray from 'ndarray';
version 2: 版本2:
require('ndarray');
export const ndarray = 'ndarray';
version 3: 版本3:
const ndarray = require('ndarray');
version 4: 版本4:
export ndarray from 'ndarray';
I followed the advice here and defined global variables: 我按照这里的建议定义了全局变量:
https://stackoverflow.com/a/37862805/5669031 https://stackoverflow.com/a/37862805/5669031
My index.js now looks like: 我的index.js现在看起来像:
import ndarray from 'ndarray';
window.ndarray = ndarray;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.