简体   繁体   English

如何从Webpack捆绑包导出符号以在另一个JavaScript文件中使用?

[英]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.

相关问题 导出Webpack捆绑文件中的所有react组件并在html文件中使用 - Export all react components in Webpack bundle file and use in html file 如何导入从webpack.config生成的javascript导出文件? - How to import javascript export file generated from the webpack.config? 包含使用 webpack 编译的包以在另一个 js 文件中使用 - include bundle compiled with webpack to use in another js file 如何从服务文件返回或导出函数 javascript 的结果以在另一个文件中使用 - How to return or export result of function javascript from service file to use in another file 如何使用JavaScript Webpack将常量从配置文件导入到另一个文件 - How to import constant from config file to another file with JavaScript Webpack 如何使用Webpack将非混乱的javascript文件捆绑到文件中 - how to bundle non mudular javascript file into a file using webpack 如何在另一个包中重用 webpack 包? - How to reuse webpack bundle in another bundle? 如何将 webpack 包导出为全局脚本(不是模块)? - How to export webpack bundle as a global script (not module)? 如何在Webpack中有条件地使用捆绑软件 - How to conditionally use a bundle in webpack 如何在javascript中从另一个文件调用模块导出函数 - How to call module export function from another file in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM