@eik/postcss-import-map

PostCSS plugin that uses Eik defined import map files to transform bare import specifiers to absolute URLs in @import rules

Usage no npm install needed!

<script type="module">
  import eikPostcssImportMap from 'https://cdn.skypack.dev/@eik/postcss-import-map';
</script>

README

postcss-import-map

PostCSS Eik plugin to support the use of import maps to map "bare" import specifiers in CSS @import rules.

Notes:

  • This plugin should probably be used as the first plugin of your list.
  • If you use postcss-import please read this.

Installation

$ npm install @eik/postcss-import-map

Usage

// dependencies
var fs = require('fs');
var postcss = require('postcss');
var eikImportMapPlugin = require('@eik/postcss-import-map');

// css to be processed
var css = fs.readFileSync('css/input.css', 'utf8');

// process css
postcss()
    .use(
        eikImportMapPlugin({
            imports: {
                'normalize.css':
                    'https://unpkg.com/normalize.css@8/normalize.css',
            },
        })
    )
    .process(css, {
        // `from` option is needed here
        from: 'css/input.css',
    })
    .then(function (result) {
        var output = result.css;

        console.log(output);
    });

css/input.css:

@import 'normalize.css';

body {
    background: black;
}

will give you:

@import 'https://unpkg.com/normalize.css@8/normalize.css';

body {
    background: black;
}

PostCSS Import Usage

If you're using postcss-import make sure you update the plugins option. postcss.config.js

module.exports = (ctx) => ({
    plugins: [
        require('@eik/postcss-import-map')(),
        require('postcss-import')({
            // It needs to be added here as well to ensure everything is mapped
            plugins: [require('@eik/postcss-import-map')],
        }),
    ],
});

Options

This plugin takes an import map as options:

option default type required details
path cwd/eik.json string false Path to eik.json file.
urls [] array false Array of import map URLs to fetch from.
imports {} object false Mapping between "bare" import specifiers and absolute URLs.

This module only cares about "bare" import specifies which map to absolute URLs in the import map. Any other import specifiers defined in the import map are ignored.

License

Copyright (c) 2020 Finn.no

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.