Skip to content
This repository has been archived by the owner on Jan 3, 2023. It is now read-only.

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
hum-n committed Jan 22, 2021
0 parents commit 429451d
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
root = true

[*]
indent_style = tab
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.yml]
indent_style = space
indent_size = 2

[*.md]
indent_style = space
trim_trailing_whitespace = false
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
npm-debug.log
yarn.lock
package-lock.json
.DS_Store
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"singleQuote": true,
"trailingComma": "none",
"arrowParens": "avoid",
"printWidth": 90
}
15 changes: 15 additions & 0 deletions license
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ISC License

Copyright (c) Antoine Boulanger (https://github.com/antoineboulanger)

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
37 changes: 37 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "esbuild-plugin-postcss-literal",
"version": "0.1.0",
"description": "PostCSS tagged template literals plugin for esbuild.",
"repository": "nativew/esbuild-plugin-postcss-literal",
"author": "Antoine Boulanger (https://github.com/antoineboulanger)",
"license": "ISC",
"exports": "./src/index.js",
"main": "src/index.js",
"type": "module",
"scripts": {
"format": "prettier --write '**/*'"
},
"peerDependencies": {
"postcss": "^8.0.0",
"postcss-load-config": "^3.0.0"
},
"devDependencies": {
"postcss": "^8.2.4",
"postcss-load-config": "^3.0.0",
"prettier": "^2.2.1"
},
"files": [
"src"
],
"keywords": [
"postcss",
"postcss-runner",
"esbuild",
"esbuild-plugin",
"css",
"style",
"literal",
"css-in-js",
"web-components"
]
}
78 changes: 78 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# esbuild-plugin-postcss-literal

[PostCSS](https://github.com/postcss/postcss) tagged template literals plugin for [esbuild](https://github.com/evanw/esbuild).

<br>

### Install

```zsh
npm install esbuild-plugin-postcss-literal --save-dev
```

<br>

### Use

`esbuild.config.json`

```js
import esbuild from 'esbuild';
import postcssLiteral from 'esbuild-plugin-postcss-literal';

esbuild
.build({
entryPoints: ['index.js'],
bundle: true,
outfile: 'main.js',
plugins: [postcssLiteral()]
})
.catch(() => process.exit(1));
```

`package.json`

```json
{
"type": "module",
"scripts": {
"start": "node esbuild.config.js"
}
}
```

<br>

### Configure

`esbuild.config.json`

```js
postcssLiteral({
filter: /.*/,
namespace: '',
tag: 'css',
minify: false, // esbuild is used to minify and parse errors
config: {} // postcss config here or in .postcssrc
});
```

[`.postcssrc`](https://github.com/postcss/postcss-load-config)

```json
{
"plugins": {
"postcss-plugin": {}
}
}
```

<br>

### Check

[esbuild-plugin-merge](https://github.com/nativew/esbuild-plugin-merge) &nbsp;&nbsp; Pipe esbuild plugins output.

[esbuild-plugin-babel](https://github.com/nativew/esbuild-plugin-babel) &nbsp;&nbsp; Babel plugin for esbuild.

<br>
74 changes: 74 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import esbuild from 'esbuild';
import postcss from 'postcss';
import postcssrc from 'postcss-load-config';
import fs from 'fs';
import path from 'path';

const pluginPostcssLiteral = (options = {}) => ({
name: 'postcss-literal',
setup(build, transform) {
const {
filter = /.*/,
namespace = '',
tag = 'css',
minify = false,
config = {}
} = options;
let warnings;

const parse = css => {
const result = esbuild.transformSync(css, {
loader: 'css',
minify
});

if (result.warnings.length) return (warnings = result.warnings);

return result.code;
};

const transformContents = ({ args, contents }) => {
const index = contents.indexOf(tag + '`');

if (index == -1) return { contents };

const start = index + tag.length + 1;
const end = contents.indexOf('`', start);
const css = contents.slice(start, end);
const from = path.relative(process.cwd(), args.path);

return postcssrc(config).then(({ plugins, options }) =>
postcss(plugins)
.process(css, { ...options, from })
.then(result => {
const css = parse(result.css);

if (warnings) return { warnings };

contents = contents.slice(0, start) + css + contents.slice(end);

result
.warnings()
.forEach(warn => process.stderr.write(warn.toString()));

return { contents };
})
.catch(error => {
if (error.name != 'CssSyntaxError') throw error;

process.stderr.write(error.message + error.showSourceCode());
})
);
};

if (transform) return transformContents(transform);

build.onLoad({ filter, namespace }, async args => {
const contents = await fs.promises.readFile(args.path, 'utf8');

return transformContents({ args, contents });
});
}
});

export default pluginPostcssLiteral;

0 comments on commit 429451d

Please sign in to comment.