@eik/postcss-plugin 3.0.13 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -0
- package/README.md +8 -10
- package/package.json +58 -61
- package/src/plugin.js +33 -36
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,25 @@
|
|
|
1
|
+
# [4.0.0](https://github.com/eik-lib/postcss-import-map/compare/v3.0.14...v4.0.0) (2024-07-29)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Code Refactoring
|
|
5
|
+
|
|
6
|
+
* migrate to esm ([4e8bc12](https://github.com/eik-lib/postcss-import-map/commit/4e8bc1299323afb2ee7e74eb4d97d85869f5c904))
|
|
7
|
+
* remove node-fetch dependency ([8113805](https://github.com/eik-lib/postcss-import-map/commit/81138057c767bdecfeec96db017ddf25942e8458))
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
### BREAKING CHANGES
|
|
11
|
+
|
|
12
|
+
* this requires using Node 18 or newer without the
|
|
13
|
+
--no-experimental-fetch flag.
|
|
14
|
+
* module is now ESM
|
|
15
|
+
|
|
16
|
+
## [3.0.14](https://github.com/eik-lib/postcss-import-map/compare/v3.0.13...v3.0.14) (2024-03-06)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
### Bug Fixes
|
|
20
|
+
|
|
21
|
+
* **deps:** update dependency @eik/common to v4.0.0-next.7 ([#299](https://github.com/eik-lib/postcss-import-map/issues/299)) ([23eaca9](https://github.com/eik-lib/postcss-import-map/commit/23eaca92e3d641f4d8a1ecd365f72e982b3495c9))
|
|
22
|
+
|
|
1
23
|
## [3.0.13](https://github.com/eik-lib/postcss-import-map/compare/v3.0.12...v3.0.13) (2023-08-23)
|
|
2
24
|
|
|
3
25
|
|
package/README.md
CHANGED
|
@@ -19,9 +19,7 @@ const fs = require('fs');
|
|
|
19
19
|
const css = fs.readFileSync('css/input.css', 'utf8');
|
|
20
20
|
|
|
21
21
|
postcss()
|
|
22
|
-
.use(
|
|
23
|
-
plugin()
|
|
24
|
-
)
|
|
22
|
+
.use(plugin())
|
|
25
23
|
.process(css, {
|
|
26
24
|
// `from` option is needed here
|
|
27
25
|
from: 'css/input.css',
|
|
@@ -55,8 +53,8 @@ when applaying the following Import Map:
|
|
|
55
53
|
```json
|
|
56
54
|
{
|
|
57
55
|
"imports": {
|
|
58
|
-
"normalize.css": "https://cdn.eik.dev/normalize.css@8/normalize.css"
|
|
59
|
-
}
|
|
56
|
+
"normalize.css": "https://cdn.eik.dev/normalize.css@8/normalize.css"
|
|
57
|
+
}
|
|
60
58
|
}
|
|
61
59
|
```
|
|
62
60
|
|
|
@@ -74,11 +72,11 @@ body {
|
|
|
74
72
|
|
|
75
73
|
This plugin takes the following as options:
|
|
76
74
|
|
|
77
|
-
| option
|
|
78
|
-
|
|
|
79
|
-
| path
|
|
80
|
-
| urls
|
|
81
|
-
| maps
|
|
75
|
+
| option | default | type | required | details |
|
|
76
|
+
| ------ | --------------- | -------- | -------- | ----------------------------------------------------------------------------- |
|
|
77
|
+
| path | `process.cwd()` | `string` | `false` | Path to directory containing a eik.json file or package.json with eik config. |
|
|
78
|
+
| urls | `[]` | `array` | `false` | Array of import map URLs to fetch from. |
|
|
79
|
+
| maps | `[]` | `array` | `false` | Array of import map as objects. |
|
|
82
80
|
|
|
83
81
|
The plugin will attempt to read import map URLs from [`eik.json` or `package.json`](https://eik.dev/docs/overview_eik_json) files in the root of the current working directory if present.
|
|
84
82
|
|
package/package.json
CHANGED
|
@@ -1,63 +1,60 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
"
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
"singleQuote": true,
|
|
61
|
-
"tabWidth": 4
|
|
62
|
-
}
|
|
2
|
+
"name": "@eik/postcss-plugin",
|
|
3
|
+
"version": "4.0.0",
|
|
4
|
+
"description": "PostCSS plugin that uses Eik defined import map files to transform bare import specifiers to absolute URLs in @import rules",
|
|
5
|
+
"main": "src/plugin.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"files": [
|
|
8
|
+
"CHANGELOG.md",
|
|
9
|
+
"package.json",
|
|
10
|
+
"src/"
|
|
11
|
+
],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"test": "tap test/*.js --disable-coverage --allow-empty-coverage",
|
|
14
|
+
"test:snapshot": "TAP_SNAPSHOT=1 tap test/*.js --disable-coverage --allow-empty-coverage",
|
|
15
|
+
"lint": "eslint .",
|
|
16
|
+
"lint:fix": "eslint . --fix"
|
|
17
|
+
},
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/eik-lib/postcss-import-map.git"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"css",
|
|
24
|
+
"postcss",
|
|
25
|
+
"postcss-plugin",
|
|
26
|
+
"import",
|
|
27
|
+
"url"
|
|
28
|
+
],
|
|
29
|
+
"author": "Finn.no",
|
|
30
|
+
"license": "MIT",
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/eik-lib/postcss-import-map/issues"
|
|
33
|
+
},
|
|
34
|
+
"homepage": "https://github.com/eik-lib/postcss-import-map#readme",
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@semantic-release/changelog": "6.0.3",
|
|
37
|
+
"@semantic-release/commit-analyzer": "13.0.0",
|
|
38
|
+
"@semantic-release/git": "10.0.1",
|
|
39
|
+
"@semantic-release/github": "10.1.3",
|
|
40
|
+
"@semantic-release/npm": "12.0.1",
|
|
41
|
+
"@semantic-release/release-notes-generator": "14.0.1",
|
|
42
|
+
"eslint": "9.8.0",
|
|
43
|
+
"eslint-config-prettier": "9.1.0",
|
|
44
|
+
"eslint-plugin-prettier": "5.2.1",
|
|
45
|
+
"fastify": "4.28.1",
|
|
46
|
+
"globals": "15.8.0",
|
|
47
|
+
"postcss": "8.4.40",
|
|
48
|
+
"rollup": "4.19.1",
|
|
49
|
+
"semantic-release": "24.0.0",
|
|
50
|
+
"prettier": "3.3.3",
|
|
51
|
+
"tap": "20.0.3"
|
|
52
|
+
},
|
|
53
|
+
"dependencies": {
|
|
54
|
+
"@eik/common-config-loader": "4.0.0-next.12",
|
|
55
|
+
"css-url-parser": "1.1.3"
|
|
56
|
+
},
|
|
57
|
+
"peerDependencies": {
|
|
58
|
+
"postcss": "^8.0.0"
|
|
59
|
+
}
|
|
63
60
|
}
|
package/src/plugin.js
CHANGED
|
@@ -1,23 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const parseCssUrls = require('css-url-parser');
|
|
4
|
-
const { helpers } = require('@eik/common');
|
|
5
|
-
const fetch = require('node-fetch');
|
|
1
|
+
import parseCssUrls from 'css-url-parser';
|
|
2
|
+
import { getDefaults } from '@eik/common-config-loader';
|
|
6
3
|
|
|
7
4
|
const notUrl = (url) => url.substr(0, 4) !== 'http';
|
|
8
5
|
|
|
9
6
|
async function fetchImportMaps(urls = []) {
|
|
10
7
|
try {
|
|
11
|
-
const maps = urls.map((map) =>
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
8
|
+
const maps = urls.map((map) =>
|
|
9
|
+
fetch(map).then((result) => {
|
|
10
|
+
if (result.status === 404) {
|
|
11
|
+
throw new Error('Import map could not be found on server');
|
|
12
|
+
} else if (result.status >= 400 && result.status < 500) {
|
|
13
|
+
throw new Error('Server rejected client request');
|
|
14
|
+
} else if (result.status >= 500) {
|
|
15
|
+
throw new Error('Server error');
|
|
16
|
+
}
|
|
17
|
+
return result.json();
|
|
18
|
+
}),
|
|
19
|
+
);
|
|
21
20
|
return await Promise.all(maps);
|
|
22
21
|
} catch (err) {
|
|
23
22
|
throw new Error(
|
|
@@ -26,21 +25,20 @@ async function fetchImportMaps(urls = []) {
|
|
|
26
25
|
}
|
|
27
26
|
}
|
|
28
27
|
|
|
29
|
-
const validate = (map) =>
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
if (notUrl(value)) {
|
|
33
|
-
throw Error(`Import specifier can NOT be mapped to a bare import statement. Import specifier "${key}" is being wrongly mapped to "${value}"`);
|
|
34
|
-
}
|
|
28
|
+
const validate = (map) =>
|
|
29
|
+
Object.keys(map.imports).map((key) => {
|
|
30
|
+
const value = map.imports[key];
|
|
35
31
|
|
|
36
|
-
|
|
37
|
-
|
|
32
|
+
if (notUrl(value)) {
|
|
33
|
+
throw Error(
|
|
34
|
+
`Import specifier can NOT be mapped to a bare import statement. Import specifier "${key}" is being wrongly mapped to "${value}"`,
|
|
35
|
+
);
|
|
36
|
+
}
|
|
38
37
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
} = {}) => {
|
|
38
|
+
return { key, value };
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
export default ({ path = process.cwd(), maps = [], urls = [] } = {}) => {
|
|
44
42
|
const pMaps = Array.isArray(maps) ? maps : [maps];
|
|
45
43
|
const pUrls = Array.isArray(urls) ? urls : [urls];
|
|
46
44
|
|
|
@@ -63,7 +61,6 @@ module.exports = ({
|
|
|
63
61
|
// First check if it's possibly using syntax like url()
|
|
64
62
|
const parsedUrls = parseCssUrls(decl.params);
|
|
65
63
|
if (parsedUrls.length > 0) {
|
|
66
|
-
// eslint-disable-next-line prefer-destructuring
|
|
67
64
|
key = parsedUrls[0];
|
|
68
65
|
} else {
|
|
69
66
|
// Handle the common cases where it's not wrapped in url() but may have quotes
|
|
@@ -76,7 +73,6 @@ module.exports = ({
|
|
|
76
73
|
if (replaced.has(key)) {
|
|
77
74
|
decl.remove();
|
|
78
75
|
} else if (mapping.has(key)) {
|
|
79
|
-
// eslint-disable-next-line no-param-reassign
|
|
80
76
|
decl.params = `'${mapping.get(key)}'`;
|
|
81
77
|
replaced.add(key);
|
|
82
78
|
}
|
|
@@ -91,18 +87,21 @@ module.exports = ({
|
|
|
91
87
|
// Run initially once, this is to ensure it runs before postcss-import
|
|
92
88
|
async Once(root) {
|
|
93
89
|
// Load eik config from eik.json or package.json
|
|
94
|
-
const config = await
|
|
90
|
+
const config = await getDefaults(path);
|
|
95
91
|
|
|
96
92
|
// Fetch import maps from the server
|
|
97
|
-
const fetched = await fetchImportMaps([
|
|
98
|
-
|
|
93
|
+
const fetched = await fetchImportMaps([
|
|
94
|
+
...config.map,
|
|
95
|
+
...pUrls,
|
|
96
|
+
]);
|
|
97
|
+
|
|
99
98
|
const allImportMaps = [...fetched, ...pMaps];
|
|
100
99
|
allImportMaps.forEach((item) => {
|
|
101
100
|
const i = validate(item);
|
|
102
101
|
i.forEach((obj) => {
|
|
103
102
|
mapping.set(obj.key, obj.value);
|
|
104
103
|
});
|
|
105
|
-
})
|
|
104
|
+
});
|
|
106
105
|
|
|
107
106
|
root.walkAtRules('import', (decl) => {
|
|
108
107
|
applyImportMap(mapping, decl);
|
|
@@ -117,5 +116,3 @@ module.exports = ({
|
|
|
117
116
|
},
|
|
118
117
|
};
|
|
119
118
|
};
|
|
120
|
-
|
|
121
|
-
module.exports.postcss = true;
|