Skip to content

Commit

Permalink
fix(deps): update dependency @eik/common to v4.0.0-next.7 (#299)
Browse files Browse the repository at this point in the history
* fix(deps): update dependency @eik/common to v4.0.0-next.7

* chore: migrate to new submodule for config

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: William Killerud <william.killerud@schibsted.com>
  • Loading branch information
renovate[bot] and wkillerud committed Mar 6, 2024
1 parent ed27f67 commit 23eaca9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 30 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -49,7 +49,7 @@
"tap": "18.7.0"
},
"dependencies": {
"@eik/common": "4.0.0-next.5",
"@eik/common-config-loader": "4.0.0-next.12",
"css-url-parser": "1.1.3",
"node-fetch": "2.7.0"
},
Expand Down
62 changes: 33 additions & 29 deletions src/plugin.js
@@ -1,46 +1,47 @@
/* eslint-disable no-restricted-syntax, no-shadow */

const parseCssUrls = require('css-url-parser');
const { helpers } = require('@eik/common');
const { getDefaults } = require('@eik/common-config-loader');
const fetch = require('node-fetch');

const notUrl = (url) => url.substr(0, 4) !== 'http';

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 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();
})
);
return await Promise.all(maps);
} catch (err) {
throw new Error(
`Unable to load import map file from server: ${err.message}`,
`Unable to load import map file from server: ${err.message}`
);
}
}

const validate = (map) => Object.keys(map.imports).map((key) => {
const value = map.imports[key];

if (notUrl(value)) {
throw Error(`Import specifier can NOT be mapped to a bare import statement. Import specifier "${key}" is being wrongly mapped to "${value}"`);
}
const validate = (map) =>
Object.keys(map.imports).map((key) => {
const value = map.imports[key];

return { key, value };
});
if (notUrl(value)) {
throw Error(
`Import specifier can NOT be mapped to a bare import statement. Import specifier "${key}" is being wrongly mapped to "${value}"`
);
}

module.exports = ({
path = process.cwd(),
maps = [],
urls = [],
} = {}) => {
return { key, value };
});

module.exports = ({ path = process.cwd(), maps = [], urls = [] } = {}) => {
const pMaps = Array.isArray(maps) ? maps : [maps];
const pUrls = Array.isArray(urls) ? urls : [urls];

Expand Down Expand Up @@ -91,18 +92,21 @@ module.exports = ({
// Run initially once, this is to ensure it runs before postcss-import
async Once(root) {
// Load eik config from eik.json or package.json
const config = await helpers.getDefaults(path);
const config = await getDefaults(path);

// Fetch import maps from the server
const fetched = await fetchImportMaps([...config.map, ...pUrls]);

const fetched = await fetchImportMaps([
...config.map,
...pUrls,
]);

const allImportMaps = [...fetched, ...pMaps];
allImportMaps.forEach((item) => {
const i = validate(item);
i.forEach((obj) => {
mapping.set(obj.key, obj.value);
});
});;
});

root.walkAtRules('import', (decl) => {
applyImportMap(mapping, decl);
Expand Down

0 comments on commit 23eaca9

Please sign in to comment.