Skip to content

Commit 52662fb

Browse files
author
Joel Denning
authored
Support Node 16.12 (#2)
* Support Node 16.12 * Documentation
1 parent 73177b5 commit 52662fb

File tree

4 files changed

+43
-32
lines changed

4 files changed

+43
-32
lines changed

.github/workflows/build_and_test.yml

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
1+
name: Build and Test
2+
13
on:
2-
- push
3-
- pull_request
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
branches:
9+
- "*"
410

511
jobs:
6-
build-test:
12+
build:
713
runs-on: ubuntu-latest
14+
815
steps:
916
- uses: actions/checkout@v2
17+
- uses: actions/setup-node@v2
18+
with:
19+
node-version: "16.12"
1020
- uses: pnpm/action-setup@v2.0.1
1121
with:
12-
version: 6.11.5
13-
run_install: |
14-
- recursive: true
15-
args: [--frozen-lockfile, --strict-peer-dependencies]
22+
version: 6.20.3
23+
- run: pnpm install --frozen-lockfile
24+
- run: pnpm test
25+
- run: pnpm run check-format
1626
- run: pnpm run lint
17-
- run: pnpm run test

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ A [nodejs loader](https://nodejs.org/dist/latest-v13.x/docs/api/esm.html#esm_exp
88
npm install --save @node-loader/postcss
99
```
1010

11+
Node 16.12 changed the hooks used in NodeJS loaders. If using Node<16.12, use `@node-loader/postcss@1`. Otherwise, use `@node-loader/postcss@latest`.
12+
1113
## Usage
1214

13-
Create a `postcss.config.js` file in your current working directory. Now run node with the `--loader` (or `--experimental-loader` for older NodeJS version) flag:
15+
Create a `postcss.config.js` file in your current working directory. Now run node with the `--loader` (or `--experimental-loader` for older NodeJS version) flag:
1416

1517
```sh
1618
node --loader @node-loader/postcss file.js
@@ -19,9 +21,12 @@ node --loader @node-loader/postcss file.js
1921
Now you can import CSS files as ES modules in your NodeJS project:
2022

2123
```js
22-
import cssString from './main.css';
24+
import cssString from "./main.css";
2325

24-
console.log("After PostCSS processing, the main.css file content is", cssString);
26+
console.log(
27+
"After PostCSS processing, the main.css file content is",
28+
cssString
29+
);
2530
```
2631

2732
## Configuration

lib/node-loader-postcss.js

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
import postcss from "postcss";
22
import path from "path";
3+
import fs from "fs/promises";
4+
import urlModule from "url";
35

4-
export async function transformSource(
5-
originalSource,
6-
context,
7-
defaultTransformSource
8-
) {
9-
if (useLoader(context.url)) {
6+
export async function load(url, context, defaultLoad) {
7+
if (useLoader(url)) {
108
const postCssConfigFilePath = path.resolve(
119
process.cwd(),
1210
process.env.POSTCSS_CONFIG || "postcss.config.js"
1311
);
1412

1513
const { default: postCssConfig } = await import(postCssConfigFilePath);
14+
15+
let originalSource;
16+
17+
try {
18+
originalSource = (await defaultLoad(url, context, defaultLoad)).source;
19+
} catch (err) {
20+
originalSource = await fs.readFile(urlModule.fileURLToPath(url), {
21+
encoding: "utf-8",
22+
});
23+
}
24+
1625
let css;
1726
try {
1827
const result = await postcss(postCssConfig.plugins).process(
@@ -37,24 +46,11 @@ export async function transformSource(
3746

3847
return {
3948
source,
40-
};
41-
}
42-
43-
return defaultTransformSource(
44-
originalSource,
45-
context,
46-
defaultTransformSource
47-
);
48-
}
49-
50-
export function getFormat(url, context, defaultGetFormat) {
51-
if (useLoader(url)) {
52-
return {
5349
format: "module",
5450
};
55-
} else {
56-
return defaultGetFormat(url, context, defaultGetFormat);
5751
}
52+
53+
return defaultLoad(url, context, defaultLoad);
5854
}
5955

6056
function useLoader(url) {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"scripts": {
88
"test": "node --loader ./lib/node-loader-postcss.js ./test/run-tests.js",
99
"lint": "eslint lib",
10+
"check-format": "prettier --check .",
1011
"prepare": "husky install"
1112
},
1213
"files": [

0 commit comments

Comments
 (0)