2016-08-23 164 views
1

我们使用TypeScript,webpack和ArcGIS JS API将自定义npm包our-map包装为一个React组件的esri映射。我们已经确认该模块按照预期在模块内的测试页面上工作。也就是说,我们可以正确显示我们的React Map Component并获取Web地图。将our-map npm包写入文件共享,以便我们可以将npm安装到我们的其他应用程序中。未捕获的ReferenceError:__WEBPACK_EXTERNAL_MODULE_XX__未定义

我们有npm install ed our-mapour-app,另一个TypeScript应用程序。我们使用webpack捆绑应用程序。但是,在运行时我们收到以下错误。

Uncaught ReferenceError: __WEBPACK_EXTERNAL_MODULE_61__ is not defined 

虽然在铬调试这一点,我们找到有问题的模块是

module.exports = __WEBPACK_EXTERNAL_MODULE_61__; 

////////////////// 
// WEBPACK FOOTER 
// external "esri/arcgis/utils" 
// module id = 61 
// module chunks = 0 

如果我们从our-map模块移除esri/arcgis/utils模块并重新发布,我们有一个类似的错误,但是,它引用了下一个ESRI模块。

our-app的代码是:

的index.html

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Our Application</title> 
    </head> 

    <body> 
    <div id="header"></div> 
    <div id="content"></div> 
    <div id="footer"></div> 
    <script src="https://js.arcgis.com/3.16/"></script> 
    <script> 
      require(["dist/main.bundle.js"], function (main) {}); 
     </script> 
    </body> 
</html> 

app.tsx 为了清楚而简化

import * as React from "react"; 
import {MapContainer} from "our-map"; 

export const App = (props: IApp) => { 
    return <div style={{height: "100%"}}> 
     <div style={mapStyle}> 
      <MapContainer /> 
     </div> 
    </div>; 
}; 

个webpack.config.js

var webpack = require("webpack"); 

module.exports = { 
    entry: { 
     main: ['./src/app/main.ts', 'esri', 'esri/map', 'esri/urlUtils', 'esri/geometry/Point', 'esri/arcgis/utils'] 
    }, 
    output: { 
     filename: 'dist/[name].bundle.js' 
    }, 
    resolve: { 
     extensions: ['', '.webpack.js', '.web.js', '.ts', '.tsx', '.js'] 
    }, 
    module: { 
     loaders: [ 
      //typescript 
      { 
       test: /\.tsx?$/, 
       loader: 'ts-loader' 
      }, 
      // css 
      { 
       test: /\.css$/, 
       loader: "style-loader!css-loader" 
      }, 
      // images 
      { 
       test: /\.png$/, 
       loader: "url-loader", 
       query: { mimetype: "image/png" } 
      }, 
      // fonts 
      { 
       test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, 
       loader: "url-loader?limit=10000&name=dist/fa/[hash].[ext]&mimetype=application/font-woff" 
      }, 
      { 
       test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, 
       loader: "file-loader?name=dist/fa/[hash].[ext]" 
      } 
     ] 
    }, 
    externals: [ 
     function(context, request, callback) { 
      if (/^dojo/.test(request) || 
       /^dijit/.test(request) || 
       /^esri/.test(request) 
      ) { 
       return callback(null, "amd " + request); 
      } 
      callback(); 
     } 
    ], 
    devtool: 'source-map' 
}; 

任何想法,以什么可能会造成这个问题,如何解决呢?

回答

2

解决的办法是设置libraryTarget: 'amd'在webpack.config.js文件中像这样:

output: { 
    filename: 'dist/[name].bundle.js', 
    libraryTarget: 'amd' 
}, 
相关问题