代码之家  ›  专栏  ›  技术社区  ›  Jeff Saremi

如何从一个webpack包中导出一个符号以在另一个javascript文件中使用?

  •  0
  • Jeff Saremi  · 技术社区  · 6 年前

    我想在浏览器上使用一些JS模块。我已经为此创建了一个网页包项目。但是我不能在我的目标JS文件中使用它们。

    我想创建一个可以使用的包,如下所示:

    <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>
    

    为此,我创建了一个包含以下webpack和package.json文件的文件夹:

    网页包文件:

    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文件:

    {
      "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"
      }
    }
    

    我运行webpack并获得一个捆绑文件,但每次运行时都会出现以下错误: ndarray is not defined

    我使用了以下index.js内容都没有用:

    import ndarray from 'ndarray';
    

    版本2:

    require('ndarray');
    export const ndarray = 'ndarray';
    

    const ndarray = require('ndarray');
    

    版本4:

    export ndarray from 'ndarray';
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Jeff Saremi    6 年前

    https://stackoverflow.com/a/37862805/5669031

    我的index.js现在看起来像:

    import ndarray from 'ndarray';
    
    window.ndarray = ndarray;