Can ES6's module loader also load assets (html/css/…)

前端 未结 2 1242
时光说笑
时光说笑 2020-12-24 01:09

ES6\'s modules are based on a flexible loader architecture (although the standard is not final, so ...).

Does this mean ES6\'s loader, based on system.js, can load <

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-24 01:45

    If you use SystemJS then you can load assets by using plugins:

    // Will generate a  element for my/file.css
    System.import('my/file.css!')
        .then(() => console.log('CSS file loaded'));
    

    Alternatively, you can use an import statement. This will make sure that the CSS file is loaded before the your script executes:

    import 'my/file.css!';
    

    Finally, you can retrieve the contents of the file using the text plugin:

    import cssContent from 'my/file.css!text';
    console.log('CSS file contents: ', cssContent);
    

    Another option is to add the css as a dependency in JSPM config files. Basically adding the dependency in the specific package .json file and then running 'jspm install' which will add the override to package.js & jspm.config.js

提交回复
热议问题