Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
7 changes: 7 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "node-important-stuff",
"parser": "@babel/eslint-parser",
"rules": {
"node/no-unsupported-features/es-syntax": "off"
}
}
17 changes: 17 additions & 0 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
on:
- push
- pull_request

jobs:
build-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: pnpm/action-setup@v2.0.1
with:
version: 6.11.5
run_install: |
- recursive: true
args: [--frozen-lockfile, --strict-peer-dependencies]
- run: pnpm run lint
- run: pnpm run test
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

pnpm run lint && pnpm exec pretty-quick --staged
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,29 @@
# node-loader-postcss
# @node-loader/postcss

A [nodejs loader](https://nodejs.org/dist/latest-v13.x/docs/api/esm.html#esm_experimental_loaders) for [postcss](https://postcss.org/). This allows you to load CSS files into NodeJS programs by converting them into Javascript modules. The returned ES module has a `default` property whose value is the compiled CSS string.

## Installation

```sh
npm install --save @node-loader/postcss
```

## Usage

Now run node with the `--loader` (or `--experimental-loader` for older NodeJS version) flag:

```sh
node --loader @node-loader/postcss file.js
```

## Configuration

By default, node-loader postcss looks for a configuration file called `postcss.config.js` in the current working directory. To specify the file path to the configuration file, provide the `POSTCSS_CONFIG` environment variable:

```sh
POSTCSS_CONFIG=/Users/name/some/dir/.postcssrc node --loader @node-loader/postcss file.js
```

## Composition

If you wish to combine postcss loading with other NodeJS loaders, you may do so by using [node-loader-core](https://github.com/node-loader/node-loader-core).
62 changes: 62 additions & 0 deletions lib/node-loader-postcss.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import postcss from "postcss";
import path from "path";

export async function transformSource(
originalSource,
context,
defaultTransformSource
) {
if (useLoader(context.url)) {
const postCssConfigFilePath = path.resolve(
process.cwd(),
process.env.POSTCSS_CONFIG || "postcss.config.js"
);

const { default: postCssConfig } = await import(postCssConfigFilePath);
let css;
try {
const result = await postcss(postCssConfig.plugins).process(
originalSource.toString(),
{
from: context.url,
to: context.url,
}
);

css = result.css;
} catch (err) {
console.error("@node-loader/postcss: PostCSS compilation error");
console.error(err);
throw err;
}

const source =
"export default `" +
css.replace(/\\/g, "\\\\").replace(/`/g, "\\`") +
"`";

return {
source,
};
}

return defaultTransformSource(
originalSource,
context,
defaultTransformSource
);
}

export function getFormat(url, context, defaultGetFormat) {
if (useLoader(url)) {
return {
format: "module",
};
} else {
return defaultGetFormat(url, context, defaultGetFormat);
}
}

function useLoader(url) {
return url.endsWith(".css");
}
47 changes: 47 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "@node-loader/postcss",
"version": "1.0.0",
"description": "A NodeJS loader for processing PostCSS ",
"type": "module",
"main": "lib/node-loader-postcss.js",
"scripts": {
"test": "node --loader ./lib/node-loader-postcss.js ./test/run-tests.js",
"lint": "eslint lib",
"prepare": "husky install"
},
"files": [
"lib"
],
"engines": {
"node": ">=14"
},
"repository": {
"type": "git",
"url": "git+https://github.com/node-loader/node-loader-postcss.git"
},
"keywords": [
"node",
"loader",
"postcss"
],
"author": "Joel Denning",
"license": "MIT",
"bugs": {
"url": "https://github.com/node-loader/node-loader-postcss/issues"
},
"homepage": "https://github.com/node-loader/node-loader-postcss#readme",
"dependencies": {
"postcss": "^8.3.6"
},
"devDependencies": {
"@babel/core": "^7.14.8",
"@babel/eslint-parser": "^7.14.9",
"autoprefixer": "^10.3.1",
"eslint": "^7.32.0",
"eslint-config-node-important-stuff": "^1.1.0",
"husky": "^7.0.0",
"mocha": "^9.0.3",
"prettier": "^2.3.2",
"pretty-quick": "^3.1.1"
}
}
Loading