Skip to content

Commit 2220894

Browse files
committed
Convert to ESM TypeScript build
1 parent 4f20ea9 commit 2220894

11 files changed

Lines changed: 124 additions & 86 deletions

File tree

.github/workflows/release_package.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ jobs:
5858
- name: Setup pnpm
5959
uses: pnpm/action-setup@v4
6060
with:
61-
version: "10"
6261
run_install: false
6362

6463
# Setup Node.js environment
@@ -86,6 +85,9 @@ jobs:
8685
- name: Install dependencies
8786
run: pnpm install --frozen-lockfile
8887

88+
- name: Build
89+
run: pnpm build
90+
8991
# Configure Git
9092
- name: Git configuration
9193
run: |

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
.output
3+
dist
34
.DS_Store

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v10.16.0
1+
v22.0.0

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ import "./test.css";
3939
Create file `build.js`:
4040

4141
```js
42-
const esbuild = require("esbuild");
43-
const autoprefixer = require("autoprefixer");
44-
const postCssPlugin = require("@deanc/esbuild-plugin-postcss");
42+
import esbuild from "esbuild";
43+
import autoprefixer from "autoprefixer";
44+
import postCssPlugin from "@deanc/esbuild-plugin-postcss";
4545

4646
esbuild
4747
.build({

index.d.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

index.js

Lines changed: 0 additions & 55 deletions
This file was deleted.

package.json

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@
22
"name": "@deanc/esbuild-plugin-postcss",
33
"version": "1.0.1",
44
"description": "Plugin for esbuild to support postcss",
5-
"main": "index.js",
6-
"exports": "./index.js",
7-
"types": "./index.d.ts",
5+
"type": "module",
6+
"main": "./dist/index.js",
7+
"exports": {
8+
".": {
9+
"types": "./dist/index.d.ts",
10+
"default": "./dist/index.js"
11+
}
12+
},
13+
"types": "./dist/index.d.ts",
814
"packageManager": "pnpm@10.0.0",
915
"files": [
10-
"index.js",
11-
"index.d.ts",
12-
"README.md"
16+
"dist",
17+
"README.md",
18+
"CHANGELOG.md"
1319
],
1420
"scripts": {
15-
"test": "node --test tests/*.js"
21+
"build": "tsc -p tsconfig.json",
22+
"test": "pnpm build && node --test tests/*.js"
1623
},
1724
"author": {
1825
"name": "Dean Clatworthy",
@@ -36,7 +43,8 @@
3643
"esbuild": "^0.27.0"
3744
},
3845
"devDependencies": {
39-
"esbuild": "^0.27.0"
46+
"esbuild": "^0.27.0",
47+
"typescript": "^5.7.3"
4048
},
4149
"engines": {
4250
"node": ">=22"

pnpm-lock.yaml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import fs from "node:fs/promises";
2+
import path from "node:path";
3+
import postcss, { type AcceptedPlugin } from "postcss";
4+
import type { Plugin } from "esbuild";
5+
6+
export interface PostCssPluginOptions {
7+
plugins?: AcceptedPlugin[];
8+
}
9+
10+
export default function postCssPlugin(
11+
options: PostCssPluginOptions = {},
12+
): Plugin {
13+
const plugins = options.plugins ?? [];
14+
15+
return {
16+
name: "postcss",
17+
setup(build) {
18+
build.onResolve({ filter: /\.css$/, namespace: "file" }, async (args) => {
19+
// Use esbuild path resolution for node_modules, tsconfig paths, etc.
20+
// https://esbuild.github.io/plugins/#resolve
21+
const resolution = await build.resolve(args.path, {
22+
resolveDir: args.resolveDir,
23+
kind: args.kind,
24+
});
25+
26+
if (resolution.errors.length > 0) {
27+
return { errors: resolution.errors };
28+
}
29+
30+
return {
31+
path: resolution.path,
32+
namespace: "postcss",
33+
};
34+
});
35+
36+
build.onLoad(
37+
{ filter: /\.css$/, namespace: "postcss" },
38+
async (args) => {
39+
const sourceFullPath = args.path;
40+
const css = await fs.readFile(sourceFullPath, "utf8");
41+
42+
if (plugins.length === 0) {
43+
return {
44+
contents: css,
45+
loader: "css",
46+
resolveDir: path.dirname(sourceFullPath),
47+
watchFiles: [sourceFullPath],
48+
};
49+
}
50+
51+
const result = await postcss(plugins).process(css, {
52+
from: sourceFullPath,
53+
});
54+
55+
return {
56+
contents: result.css,
57+
loader: "css",
58+
resolveDir: path.dirname(sourceFullPath),
59+
watchFiles: [sourceFullPath],
60+
};
61+
},
62+
);
63+
},
64+
};
65+
}

tests/basic.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
const test = require("node:test");
2-
const assert = require("node:assert/strict");
3-
const path = require("node:path");
4-
const autoprefixer = require("autoprefixer");
5-
const fs = require("node:fs");
1+
import test from "node:test";
2+
import assert from "node:assert/strict";
3+
import path from "node:path";
4+
import fs from "node:fs";
5+
import { fileURLToPath } from "node:url";
6+
import autoprefixer from "autoprefixer";
7+
import esbuild from "esbuild";
8+
import postCssPlugin from "../dist/index.js";
69

710
const autoPrefixerPlugin = autoprefixer({
811
overrideBrowserslist: ["last 2 versions", "chrome >= 4"],
912
});
1013

14+
const __filename = fileURLToPath(import.meta.url);
15+
const __dirname = path.dirname(__filename);
1116
process.chdir(path.resolve(__dirname));
1217

13-
const postCssPlugin = require("../index.js");
14-
1518
test("simplest case", async () => {
1619
fs.rmSync(".output", { recursive: true, force: true });
1720

18-
const esbuild = require("esbuild");
19-
2021
await esbuild.build({
2122
entryPoints: ["basic/index.js"],
2223
bundle: true,

0 commit comments

Comments
 (0)