Javascript 如何使用jspm2创建导入映射

Javascript 如何使用jspm2创建导入映射,javascript,npm,module,node-modules,jspm,Javascript,Npm,Module,Node Modules,Jspm,我正在尝试使用JSPM模块加载器从npm导入javascript包,并使用脱机包加载器而不是CDN。 现在我想添加一个importmap脚本,以便导入react或模块中的另一个模块,如: 从“React”导入React 我能做些什么来实现这一点?您可以使用importmap作为标准的新方法来实现您的目标,并在js模块文件中导入npm模块,而无需编写像节点模块这样的完整路径/react@16.x.x/js,只需编写从“React”导入React,就像以前我们在webpack或汇总。 要实现这一点,

我正在尝试使用JSPM模块加载器从npm导入javascript包,并使用脱机包加载器而不是CDN。
现在我想添加一个
importmap
脚本,以便导入
react
或模块中的另一个模块,如:
从“React”导入React


我能做些什么来实现这一点?

您可以使用
importmap
作为标准的新方法来实现您的目标,并在js模块文件中导入npm模块,而无需编写像
节点模块这样的完整路径/react@16.x.x/js
,只需编写
从“React”导入React
,就像以前我们在webpack或汇总。
要实现这一点,请在安装jspm并使用jspm安装所需的软件包后,首先运行以下命令:

jspm map -o importmap.json --flat-scope 
它将为jspm模块生成当前的importmap,然后创建一个包含importmap数据的文件。使用--flat作用域是必要的,因为chrome目前不支持导入映射作用域。您必须获取JSON文件并将其注入HTML文件。首先创建一个
bootstrap.js
文件,并在其中粘贴以下代码:

class bootstrap{
    constructor(){
        this.initMapper().then(()=>{
            this.lunchApp();
        })
    }
     async initMapper() {
        document.head.appendChild(Object.assign(document.createElement('script'), {
            type: 'importmap',
            innerHTML: await (await fetch('/importmap.json')).text()
        }));
    }
    lunchApp(){
        import('/Client/Assets/js/index.js');
    }

}
var app = new bootstrap();
然后将
bootrstrap.js
添加到
index.html
文件中:

<script defer src="/Client/Assets/js/bootstrap.js"></script>

您可以使用
importmap
作为实现目标的标准新方法,在js模块文件中导入npm模块,而无需编写像
节点模块这样的完整路径/react@16.x.x/index.js
只需编写
从“React”导入React
,就像以前我们在webpack或汇总中编写的一样。
要实现这一点,请在安装jspm并使用jspm安装所需的软件包后,首先运行以下命令:

jspm map -o importmap.json --flat-scope 
它将为jspm模块生成当前的importmap,然后创建一个包含importmap数据的文件。使用--flat作用域是必要的,因为chrome目前不支持导入映射作用域。您必须获取JSON文件并将其注入HTML文件。首先创建一个
bootstrap.js
文件,并在其中粘贴以下代码:

class bootstrap{
    constructor(){
        this.initMapper().then(()=>{
            this.lunchApp();
        })
    }
     async initMapper() {
        document.head.appendChild(Object.assign(document.createElement('script'), {
            type: 'importmap',
            innerHTML: await (await fetch('/importmap.json')).text()
        }));
    }
    lunchApp(){
        import('/Client/Assets/js/index.js');
    }

}
var app = new bootstrap();
然后将
bootrstrap.js
添加到
index.html
文件中:

<script defer src="/Client/Assets/js/bootstrap.js"></script>