WebPack + SystemJS - 如何添加JavaScript文件?

时间:2016-09-29 07:03:13

标签: javascript webpack systemjs

我想在我的项目中包含一个名为/assets/js/clusterfeaturelayer.js的文件,其中我使用SystemJS和WebPack,它具有以下结构。

  • / app< - angular app code
  • / node_modules< - npm packages
  • / assets / js< - 不在NPM中的其他第三方库

该文件以AMD风格定义,如下所示:

define([
  'dojo/_base/declare',
  'dojo/_base/array',
  'dojo/_base/lang',  
  'esri/SpatialReference',
  'esri/geometry/Point',
  'esri/geometry/Polygon',
  'esri/geometry/Multipoint',
  'esri/geometry/Extent',
  'esri/graphic',

], function (declare, arrayUtils, lang, SpatialReference, Point, Polygon, Multipoint, Extent, Graphic) 
{

});

我想在import ClusterFeatureLayer = require("ClusterFeatureLayer");

的代码中使用该组件

但无论我如何尝试将此文件包含在我的systemjs和webpack配置中,它都找不到它:

这是我的systemjs config:

(function(global) {

  // map tells the System loader where to look for things.
  var map = {
    'app':                        'app', // 'dist',
    '@angular':                   'node_modules/@angular',
    'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api',
    'rxjs':                       'node_modules/rxjs',
    'ClusterFeatureLayer':        'assets/js'
  };

  // packages tells the System loader how to load when no filename and/or no extension
  var packages = {
    'app':                        { main: 'boot.js',  defaultExtension: 'js' },
    'angular2-in-memory-web-api': { main: './index.js',defaultExtension:'js' },
    'rxjs':                       { defaultExtension: 'js' },
    'esri':                       { defaultExtension: 'js' },
    'ClusterFeatureLayer':        { main: 'clusterfeaturelayer.js', defaultExtension: 'js' }
  };
  var ngPackageNames = [
    'common',
    'compiler',
    'core',
    'http',
    'platform-browser',
    'platform-browser-dynamic',
    'router'
  ];
  // Add package entries for angular packages
  ngPackageNames.forEach(function(pkgName) {
    packages['@angular/'+pkgName] = { main: pkgName + '.umd.js', defaultExtension: 'js' };
  });

  var config = {
    map: map,
    packages: packages
  }
  System.config(config);

})(this);

这是webpack配置:

var webpack = require("webpack");

module.exports = {
    entry: {
        main: [
            './app/boot.ts' // entry point for your application code
        ],
        vendor: [
            // put your third party libs here
            "core-js",
            "reflect-metadata", // order is important here
            "rxjs",
            "zone.js",
            '@angular/core',
            '@angular/common',
            "@angular/compiler",
            "@angular/core",
            "@angular/forms",
            "@angular/http",
            "@angular/platform-browser",
            "@angular/platform-browser-dynamic",
            "@angular/router",
            "@angular/upgrade",
            "ng2-slim-loading-bar",
            "ng2-toasty",              
            "ClusterFeatureLayer"
]
    },
    output: {
        filename: './dist/[name].bundle.js',
        publicPath: './',
        libraryTarget: "amd"
    },
    resolve: {
        extensions: ['', '.webpack.js', '.web.js', '.ts', '.tsx', '.js']
    },
    module: {
        loaders: [
            {
                test: /\.tsx?$/,
                loader: 'ts-loader',
                exclude: ''
            },
            // css
            {
                test: /\.css$/,
                loader: "style-loader!css-loader"
            }
        ]
    },
    plugins: [
        new webpack.optimize.CommonsChunkPlugin({
            name: 'vendor',
            minChunks: Infinity
        })
    ],
    externals: [
        function(context, request, callback) {
            if (/^dojo/.test(request) ||
                /^dojox/.test(request) ||
                /^dijit/.test(request) ||
                /^esri/.test(request)
            ) {
                return callback(null, "amd " + request);
            }
            callback();
        }
    ],
    devtool: 'source-map'
};

1 个答案:

答案 0 :(得分:6)

您的clusterlayerfeature文件中是否有导出?

// My Module
var myModule = {
    myFunction: function() {
        return "Hello!";
    }
}
module.exports = myModule;

如果您没有导出模块,则对其的任何引用都将是未定义的。