This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 429451d
Showing
7 changed files
with
232 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"singleQuote": true, | ||
"trailingComma": "none", | ||
"arrowParens": "avoid", | ||
"printWidth": 90 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) → Pipe esbuild plugins output. | ||
|
||
[esbuild-plugin-babel](https://github.com/nativew/esbuild-plugin-babel) → Babel plugin for esbuild. | ||
|
||
<br> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |