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
1.2.4
+3
-8
dist/plugin.js
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var fs = require('fs');

@@ -98,3 +96,3 @@ var path = require('path');

var plugin = postcss__default['default'].plugin(
module.exports = postcss__default['default'].plugin(
'@eik/postcss-import-map',

@@ -130,7 +128,4 @@ ({ path, urls, imports } = {}) => {

// 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;
};
{
"name": "@eik/postcss-import-map",
"version": "1.2.3",
"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",
"CHANGELOG.md"
],
"directories": {
"dist": "dist"
},
"scripts": {
"prepare": "npm run -s build",
"test": "tap test/*.js --no-coverage",
"test:snapshot": "TAP_SNAPSHOT=1 tap test/*.js --no-coverage",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"build": "rollup -c"
},
"repository": {
"type": "git",
"url": "git+https://github.com/eik-lib/postcss-import-map.git"
},
"keywords": [
"css",
"postcss",
"postcss-plugin",
"import",
"url"
],
"author": "Finn.no",
"license": "MIT",
"bugs": {
"url": "https://github.com/eik-lib/postcss-import-map/issues"
},
"homepage": "https://github.com/eik-lib/postcss-import-map#readme",
"devDependencies": {
"@semantic-release/changelog": "5.0.1",
"@semantic-release/commit-analyzer": "8.0.1",
"@semantic-release/git": "9.0.0",
"@semantic-release/github": "7.1.1",
"@semantic-release/npm": "7.0.6",
"@semantic-release/release-notes-generator": "9.0.1",
"eslint": "7.13.0",
"eslint-config-airbnb-base": "14.2.1",
"eslint-plugin-import": "2.22.1",
"eslint-config-prettier": "^6.12.0",
"fastify": "3.7.0",
"postcss": "8.1.6",
"rollup": "2.33.1",
"semantic-release": "17.2.2",
"tap": "14.10.8"
},
"dependencies": {
"css-url-parser": "^1.1.3",
"node-fetch": "^2.6.0"
},
"prettier": {
"singleQuote": true,
"tabWidth": 4
}
"name": "@eik/postcss-import-map",
"version": "1.2.4",
"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": [
"dist",
"CHANGELOG.md"
],
"directories": {
"dist": "dist"
},
"scripts": {
"prepare": "npm run -s build",
"test": "tap test/*.js --no-coverage",
"test:snapshot": "TAP_SNAPSHOT=1 tap test/*.js --no-coverage",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"build": "rollup -c"
},
"repository": {
"type": "git",
"url": "git+https://github.com/eik-lib/postcss-import-map.git"
},
"keywords": [
"css",
"postcss",
"postcss-plugin",
"import",
"url"
],
"author": "Finn.no",
"license": "MIT",
"bugs": {
"url": "https://github.com/eik-lib/postcss-import-map/issues"
},
"homepage": "https://github.com/eik-lib/postcss-import-map#readme",
"devDependencies": {
"@semantic-release/changelog": "5.0.1",
"@semantic-release/commit-analyzer": "8.0.1",
"@semantic-release/git": "9.0.0",
"@semantic-release/github": "7.1.1",
"@semantic-release/npm": "7.0.6",
"@semantic-release/release-notes-generator": "9.0.1",
"eslint": "7.13.0",
"eslint-config-airbnb-base": "14.2.1",
"eslint-plugin-import": "2.22.1",
"eslint-config-prettier": "^6.12.0",
"fastify": "3.7.0",
"postcss": "8.1.6",
"rollup": "2.33.1",
"semantic-release": "17.2.2",
"tap": "14.10.8"
},
"dependencies": {
"css-url-parser": "^1.1.3",
"node-fetch": "^2.6.0"
},
"peerDependencies": {
"postcss": "^8.0.0"
},
"prettier": {
"singleQuote": true,
"tabWidth": 4
}
}
/* 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);
}