Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@eik/postcss-import-map

Package Overview
Dependencies
Maintainers
4
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@eik/postcss-import-map - npm Package Compare versions

Comparing version
1.2.3
to
2.0.0
+17
-0
CHANGELOG.md

@@ -0,1 +1,18 @@

# [2.0.0](https://github.com/eik-lib/postcss-import-map/compare/v1.2.3...v2.0.0) (2020-11-13)
### Bug Fixes
* **deps:** update dependency node-fetch to ^2.6.1 ([#33](https://github.com/eik-lib/postcss-import-map/issues/33)) ([8b0455c](https://github.com/eik-lib/postcss-import-map/commit/8b0455c5c812d7db15d5f39d8d067fcd42f966a8))
### Features
* support PostCSS v8 ([#36](https://github.com/eik-lib/postcss-import-map/issues/36)) ([5156ba5](https://github.com/eik-lib/postcss-import-map/commit/5156ba5a01aab1781b01a0756772cb0804ea0740))
### BREAKING CHANGES
* PostCSS v7 and longer isn't supported
## [1.2.3](https://github.com/eik-lib/postcss-import-map/compare/v1.2.2...v1.2.3) (2020-11-13)

@@ -2,0 +19,0 @@

+24
-35
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
/* eslint-disable no-restricted-syntax, no-shadow */
var fs = require('fs');
var path = require('path');
var fetch = require('node-fetch');
var postcss = require('postcss');
var parseCssUrls = require('css-url-parser');
const fs = require('fs');
const path = require('path');
const fetch = require('node-fetch');
const parseCssUrls = require('css-url-parser');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
var fetch__default = /*#__PURE__*/_interopDefaultLegacy(fetch);
var postcss__default = /*#__PURE__*/_interopDefaultLegacy(postcss);
var parseCssUrls__default = /*#__PURE__*/_interopDefaultLegacy(parseCssUrls);
/* eslint-disable no-restricted-syntax, no-shadow */
const notUrl = (url) => url.substr(0, 4) !== 'http';

@@ -28,3 +17,3 @@

try {
const contents = await fs__default['default'].promises.readFile(eikJSONPath);
const contents = await fs.promises.readFile(eikJSONPath);
const eikJSON = JSON.parse(contents);

@@ -42,3 +31,3 @@ if (typeof eikJSON['import-map'] === 'string')

const maps = urls.map((map) =>
fetch__default['default'](map).then((result) => {
fetch(map).then((result) => {
if (result.status === 404) {

@@ -70,3 +59,3 @@ throw new Error('Import map could not be found on server');

async function populateImportMap({
path: eikPath = path__default['default'].join(process.cwd(), 'eik.json'),
path: eikPath = path.join(process.cwd(), 'eik.json'),
urls = [],

@@ -101,12 +90,13 @@ imports = {},

var plugin = postcss__default['default'].plugin(
'@eik/postcss-import-map',
({ path, urls, imports } = {}) => {
// Work with options here
return async (root) => {
await populateImportMap({ path, urls, imports });
root.walkAtRules('import', (decl) => {
module.exports = ({ path, urls, imports } = {}) => {
// Eagerly start resolving
const mapFetch = populateImportMap({ path, urls, imports });
return {
postcssPlugin: '@eik/postcss-import-map',
AtRule: {
import: async (decl) => {
await mapFetch;
let key;
// First check if it's possibly using syntax like url()
const parsedUrls = parseCssUrls__default['default'](decl.params);
const parsedUrls = parseCssUrls(decl.params);
if (parsedUrls.length > 0) {

@@ -127,13 +117,12 @@ // eslint-disable-next-line prefer-destructuring

}
});
};
}
);
},
},
};
};
// Useful for integrating with other plugins such as postcss-import
function filter(url) {
module.exports.filter = function filter(url) {
return !mapping.has(url);
}
};
exports.default = plugin;
exports.filter = filter;
module.exports.postcss = true;
{
"name": "@eik/postcss-import-map",
"version": "1.2.3",
"version": "2.0.0",
"description": "PostCSS plugin that uses Eik defined import map files to transform bare import specifiers to absolute URLs in @import rules",
"main": "dist/plugin.js",
"files": [
"src",
"dist",

@@ -51,3 +50,3 @@ "CHANGELOG.md"

"fastify": "3.7.0",
"postcss": "8.1.6",
"postcss": "8.1.7",
"rollup": "2.33.1",

@@ -59,4 +58,7 @@ "semantic-release": "17.2.2",

"css-url-parser": "^1.1.3",
"node-fetch": "^2.6.0"
"node-fetch": "^2.6.1"
},
"peerDependencies": {
"postcss": "^8.1.7"
},
"prettier": {

@@ -63,0 +65,0 @@ "singleQuote": true,

/* eslint-disable no-restricted-syntax, no-shadow */
import fs from 'fs';
import path from 'path';
import fetch from 'node-fetch';
import postcss from 'postcss';
import parseCssUrls from 'css-url-parser';
const notUrl = (url) => url.substr(0, 4) !== 'http';
const notBare = (str) =>
str.startsWith('/') || str.startsWith('./') || str.startsWith('../');
async function readEikJSONMaps(eikJSONPath) {
try {
const contents = await fs.promises.readFile(eikJSONPath);
const eikJSON = JSON.parse(contents);
if (typeof eikJSON['import-map'] === 'string')
return [eikJSON['import-map']];
return eikJSON['import-map'] || [];
} catch (err) {
return [];
}
}
async function fetchImportMaps(urls = []) {
try {
const maps = urls.map((map) =>
fetch(map).then((result) => {
if (result.status === 404) {
throw new Error('Import map could not be found on server');
} else if (result.status >= 400 && result.status < 500) {
throw new Error('Server rejected client request');
} else if (result.status >= 500) {
throw new Error('Server error');
}
return result.json();
})
);
const results = await Promise.all(maps);
const dependencies = results.map((result) => result.imports);
return Object.assign({}, ...dependencies);
} catch (err) {
throw new Error(
`Unable to load import map file from server: ${err.message}`
);
}
}
// The resolve option in postcss-import doesn't support async functions or promises,
// thus we have to workaround it
const mapping = new Map();
// @TODO this could be a @eik/import-map-utils package
async function populateImportMap({
path: eikPath = path.join(process.cwd(), 'eik.json'),
urls = [],
imports = {},
} = {}) {
// Reset the map to avoid pollution
mapping.clear();
const importmapUrls = await readEikJSONMaps(eikPath);
for (const map of importmapUrls) {
urls.push(map);
}
let imprts = {};
if (urls.length > 0) {
imprts = { ...(await fetchImportMaps(urls)) };
}
Object.assign(imprts, imports);
Object.keys(imprts).forEach((key) => {
const value = Array.isArray(imprts[key]) ? imprts[key][0] : imprts[key];
if (notBare(key)) return;
if (notUrl(value))
throw Error('Target for import specifier must be an absolute URL.');
mapping.set(key, value);
});
}
export default postcss.plugin(
'@eik/postcss-import-map',
({ path, urls, imports } = {}) => {
// Work with options here
return async (root) => {
await populateImportMap({ path, urls, imports });
root.walkAtRules('import', (decl) => {
let key;
// First check if it's possibly using syntax like url()
const parsedUrls = parseCssUrls(decl.params);
if (parsedUrls.length > 0) {
// eslint-disable-next-line prefer-destructuring
key = parsedUrls[0];
} else {
// Handle the common cases where it's not wrapped in url() but may have quotes
key = decl.params.replace(/["']/g, '');
}
// Webpack interop
key = key.replace(/^~/, '');
if (mapping.has(key)) {
// eslint-disable-next-line no-param-reassign
decl.params = `'${mapping.get(key)}'`;
}
});
};
}
);
// Useful for integrating with other plugins such as postcss-import
export function filter(url) {
return !mapping.has(url);
}