-
Notifications
You must be signed in to change notification settings - Fork 414
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add esbuild integration (#765)
* feat: add esbuild integration * fix: ignore node_modules in file transform
- Loading branch information
Showing
7 changed files
with
230 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
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,35 @@ | ||
<p align="center"> | ||
<img alt="Linaria" src="https://raw.githubusercontent.com/callstack/linaria/HEAD/website/assets/linaria-logo@2x.png" width="496"> | ||
</p> | ||
|
||
<p align="center"> | ||
Zero-runtime CSS in JS library. | ||
</p> | ||
|
||
--- | ||
|
||
### 📖 Please refer to the [GitHub](https://github.com/callstack/linaria#readme) for full documentation. | ||
|
||
## Features | ||
|
||
- Write CSS in JS, but with **zero runtime**, CSS is extracted to CSS files during build | ||
- Familiar **CSS syntax** with Sass like nesting | ||
- Use **dynamic prop based styles** with the React bindings, uses CSS variables behind the scenes | ||
- Easily find where the style was defined with **CSS sourcemaps** | ||
- **Lint your CSS** in JS with [stylelint](https://github.com/stylelint/stylelint) | ||
- Use **JavaScript for logic**, no CSS preprocessor needed | ||
- Optionally use any **CSS preprocessor** such as Sass or PostCSS | ||
|
||
**[Why use Linaria](../../docs/BENEFITS.md)** | ||
|
||
## Installation | ||
|
||
```sh | ||
npm install @linaria/core @linaria/react @linaria/babel-preset @linaria/shaker | ||
``` | ||
|
||
or | ||
|
||
```sh | ||
yarn add @linaria/core @linaria/react @linaria/babel-preset @linaria/shaker | ||
``` |
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,3 @@ | ||
const config = require('../../babel.config'); | ||
|
||
module.exports = config; |
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,48 @@ | ||
{ | ||
"name": "@linaria/esbuild", | ||
"version": "3.0.0-beta.5", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"description": "Blazing fast zero-runtime CSS in JS library", | ||
"main": "lib/index.js", | ||
"module": "esm/index.js", | ||
"types": "types", | ||
"files": [ | ||
"types/", | ||
"lib/", | ||
"esm/" | ||
], | ||
"license": "MIT", | ||
"repository": "git@github.com:callstack/linaria.git", | ||
"bugs": { | ||
"url": "https://github.com/callstack/linaria/issues" | ||
}, | ||
"homepage": "https://github.com/callstack/linaria#readme", | ||
"keywords": [ | ||
"react", | ||
"linaria", | ||
"css", | ||
"css-in-js", | ||
"styled-components", | ||
"babel-plugin", | ||
"babel", | ||
"esbuild" | ||
], | ||
"scripts": { | ||
"build:lib": "cross-env NODE_ENV=legacy babel src --out-dir lib --extensions '.js,.jsx,.ts,.tsx' --source-maps --delete-dir-on-start", | ||
"build:esm": "babel src --out-dir esm --extensions '.js,.jsx,.ts,.tsx' --source-maps --delete-dir-on-start", | ||
"build": "yarn build:lib && yarn build:esm", | ||
"build:declarations": "tsc --emitDeclarationOnly --outDir types", | ||
"prepare": "yarn build && yarn build:declarations", | ||
"typecheck": "tsc --noEmit --composite false", | ||
"watch": "yarn build --watch" | ||
}, | ||
"dependencies": { | ||
"esbuild": "^0.12.5", | ||
"@linaria/babel-preset": "^3.0.0-beta.5" | ||
}, | ||
"peerDependencies": { | ||
"@babel/core": ">=7" | ||
} | ||
} |
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,95 @@ | ||
/** | ||
* This file contains an esbuild loader for Linaria. | ||
* It uses the transform.ts function to generate class names from source code, | ||
* returns transformed code without template literals and attaches generated source maps | ||
*/ | ||
|
||
import path from 'path'; | ||
import fs from 'fs'; | ||
import type { PluginOptions, Preprocessor } from '@linaria/babel-preset'; | ||
import { slugify, transform } from '@linaria/babel-preset'; | ||
import esbuild, { Plugin, TransformOptions, Loader } from 'esbuild'; | ||
|
||
type EsbuildPluginOptions = { | ||
sourceMap?: boolean; | ||
preprocessor?: Preprocessor; | ||
esbuildOptions?: TransformOptions; | ||
} & Partial<PluginOptions>; | ||
|
||
const nodeModulesRegex = /^(?:.*[\\/])?node_modules(?:[\\/].*)?$/; | ||
|
||
export default function linaria({ | ||
sourceMap, | ||
preprocessor, | ||
esbuildOptions, | ||
...rest | ||
}: EsbuildPluginOptions = {}): Plugin { | ||
return { | ||
name: 'linaria', | ||
setup(build) { | ||
const cssLookup = new Map<string, string>(); | ||
|
||
build.onResolve({ filter: /\.linaria\.css$/ }, (args) => { | ||
return { | ||
namespace: 'linaria', | ||
path: args.path, | ||
}; | ||
}); | ||
|
||
build.onLoad({ filter: /.*/, namespace: 'linaria' }, (args) => { | ||
return { | ||
contents: cssLookup.get(args.path), | ||
loader: 'css', | ||
}; | ||
}); | ||
|
||
build.onLoad({ filter: /\.(js|jsx|ts|tsx)$/ }, (args) => { | ||
const rawCode = fs.readFileSync(args.path, 'utf8'); | ||
const { ext, name: filename } = path.parse(args.path); | ||
const loader = ext.replace(/^\./, '') as Loader; | ||
|
||
if (nodeModulesRegex.test(args.path)) { | ||
return { | ||
loader, | ||
contents: rawCode, | ||
}; | ||
} | ||
|
||
const { code } = esbuild.transformSync(rawCode, { | ||
...esbuildOptions, | ||
loader, | ||
}); | ||
const result = transform(code, { | ||
filename: args.path, | ||
preprocessor, | ||
pluginOptions: rest, | ||
}); | ||
|
||
if (!result.cssText) { | ||
return { | ||
contents: code, | ||
}; | ||
} | ||
|
||
let { cssText } = result; | ||
|
||
const slug = slugify(cssText); | ||
const cssFilename = `${filename}_${slug}.linaria.css`; | ||
|
||
if (sourceMap && result.cssSourceMapText) { | ||
const map = Buffer.from(result.cssSourceMapText).toString('base64'); | ||
cssText += `/*# sourceMappingURL=data:application/json;base64,${map}*/`; | ||
} | ||
|
||
cssLookup.set(cssFilename, cssText); | ||
|
||
return { | ||
contents: ` | ||
import ${JSON.stringify(cssFilename)}; | ||
${result.code} | ||
`, | ||
}; | ||
}); | ||
}, | ||
}; | ||
} |
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,12 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"paths": {}, | ||
"rootDir": "src/" | ||
}, | ||
"references": [ | ||
{ | ||
"path": "../babel" | ||
} | ||
] | ||
} |
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