【问题标题】:System.import returns empty objectSystem.import 返回空对象
【发布时间】:2017-01-01 13:50:47
【问题描述】:

我有一个简单的模块可以使用 SystemJS 和 javascript 中的动态模块加载:

export default function test() {
    alert('test');
}

该文件使用我的 webpack 配置处理为以下内容(我说的对吗?这就是所谓的“模块”?):

/******/ (function(modules) { // webpackBootstrap
/******/    // The module cache
/******/    var installedModules = {};

/******/    // The require function
/******/    function __webpack_require__(moduleId) {

/******/        // Check if module is in cache
/******/        if(installedModules[moduleId])
/******/            return installedModules[moduleId].exports;

/******/        // Create a new module (and put it into the cache)
/******/        var module = installedModules[moduleId] = {
/******/            i: moduleId,
/******/            l: false,
/******/            exports: {}
/******/        };

/******/        // Execute the module function
/******/        modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);

/******/        // Flag the module as loaded
/******/        module.l = true;

/******/        // Return the exports of the module
/******/        return module.exports;
/******/    }


/******/    // expose the modules object (__webpack_modules__)
/******/    __webpack_require__.m = modules;

/******/    // expose the module cache
/******/    __webpack_require__.c = installedModules;

/******/    // identity function for calling harmony imports with the correct context
/******/    __webpack_require__.i = function(value) { return value; };

/******/    // define getter function for harmony exports
/******/    __webpack_require__.d = function(exports, name, getter) {
/******/        if(!__webpack_require__.o(exports, name)) {
/******/            Object.defineProperty(exports, name, {
/******/                configurable: false,
/******/                enumerable: true,
/******/                get: getter
/******/            });
/******/        }
/******/    };

/******/    // getDefaultExport function for compatibility with non-harmony modules
/******/    __webpack_require__.n = function(module) {
/******/        var getter = module && module.__esModule ?
/******/            function getDefault() { return module['default']; } :
/******/            function getModuleExports() { return module; };
/******/        __webpack_require__.d(getter, 'a', getter);
/******/        return getter;
/******/    };

/******/    // Object.prototype.hasOwnProperty.call
/******/    __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };

/******/    // __webpack_public_path__
/******/    __webpack_require__.p = "";

/******/    // Load entry module and return exports
/******/    return __webpack_require__(__webpack_require__.s = 445);
/******/ })
/************************************************************************/
/******/ ({

/***/ 445:
/***/ function(module, exports) {

"use strict";
'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.default = test;
function test() {
    alert('test');
}

/***/ }

/******/ });

我已使该文件可从我的 Web 应用程序访问:

Request URL:http://localhost:8080/test
Request Method:GET
Status Code:200 
Remote Address:[::1]:8080
Response Headers
view source
Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Content-Length:2672
Date:Sun, 01 Jan 2017 16:34:54 GMT
Expires:0
Pragma:no-cache
X-Content-Type-Options:nosniff
X-Frame-Options:DENY
X-XSS-Protection:1; mode=block
Request Headers
view source
Accept:application/x-es-module, */*
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Cookie:XSRF-TOKEN=ac781d41-fdd4-4bae-9290-e54e960dfa8d; JSESSIONID=57B0C1DDA88DE1D6D4031CC8BCBCCBC9
Host:localhost:8080
Referer:http://localhost:8080/
User-Agent:Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36

然后我在页面<head>标签内添加<script src="js/system.js" type="text/javascript"></script>,并在控制台中写入:

SystemJS.import('./test').then(m => console.log(m))

它下载文件(我通过浏览器开发工具看到它)但将空对象输出到控制台。 m.defaultm.test 都不会返回除 undefined 之外的任何内容。

我希望在导入后能够调用test,我做错了什么?

【问题讨论】:

    标签: javascript webpack systemjs


    【解决方案1】:

    webpack 使用System.import 而不是SystemJS.import。此外,如果您使用的是最新的 webpack RC,我们现在建议您使用原生规范 import()

    以下是我们的 github 存储库中有关如何使用它的示例。 https://github.com/webpack/webpack/blob/master/examples/code-splitting-harmony/README.md

    【讨论】:

    • 感谢您的回答!似乎它需要使用 wepack 处理所有可能的候选人(我的 test.js 模块变成了 0.bundle.js)。然后,当我导入时,它会从 /0.bundle.js 下载模块。现在很清楚了。但是,如果我不想公开我的所有模块并从自定义 URL 导入它们,做一些服务器逻辑,例如访问检查?有可能吗?
    • 您可以使用 publicPath 并设置 CDN URL。如果这就是你的意思。
    • 对,publicPath 允许我设置它动态加载块的 URL。但我无法确定它对应的源 .js 文件:test.js 变成了0.bundle.js。我怎么知道原始源文件是什么?
    • 啊,你希望能够使用named chunks。好的,由于加载器规范的限制,没有办法通过 webpack 开箱即用地做到这一点。因此,如果您想拥有块名称,则可以使用 require.ensure
    • 你能提供一些例子吗?我尝试了 this 中的 require.ensure 示例,但输出块名称仍然是 0.bundle.js。
    猜你喜欢
    • 2014-09-29
    • 2015-02-11
    • 2020-12-21
    • 2019-04-08
    • 2012-02-22
    • 2019-07-17
    • 2018-08-03
    • 2022-01-11
    • 1970-01-01
    相关资源
    最近更新 更多