Skip to content

Commit 8655b71

Browse files
committed
feat: environment variable
feat: tests fix: format
1 parent 6db4f83 commit 8655b71

File tree

9 files changed

+101
-68
lines changed

9 files changed

+101
-68
lines changed

.github/workflows/main.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ jobs:
44
build:
55
runs-on: ubuntu-latest
66
steps:
7-
- uses: actions/checkout@v2
8-
- name: Install
9-
run: npm install
10-
- name: Compile
11-
run: npm run build
12-
- name: Run tests
13-
run: npm run test
7+
- uses: actions/checkout@v2
8+
- name: Install
9+
run: npm install
10+
- name: Compile
11+
run: npm run build
12+
- name: Run tests
13+
run: npm run test

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.vscode
22
.DS_Store
33
node_modules
4-
test/onExtract
4+
test/extract
55
dist

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ export default {
2323

2424
## Options
2525

26-
| Property | Description |
27-
| -------------------- | ------------------------------------------------- |
28-
| include | Files to include |
29-
| exclude | Files to exclude |
30-
| styleRegex | Custom regex for selecting CSS in file |
31-
| configPath | Path to directory that contains postcss.config.js |
26+
| Property | Description |
27+
| ---------- | ------------------------------------------------------------------------ |
28+
| include | Files to include |
29+
| exclude | Files to exclude |
30+
| env | Environment variable, defaults to `process.env.NODE_ENV` where available |
31+
| styleRegex | Custom regex for selecting CSS in file |
32+
| configPath | Path to directory that contains postcss.config.js |
3233

3334
## Template Literals in JavaScript
3435

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"build:dist": "cp src/package.json dist/package.json && cp README.md dist/README.md && cp CHANGELOG.md dist/CHANGELOG.md",
88
"prettier": "prettier --write .",
99
"test": "jest",
10+
"test:clean": "rm -rf test/extract",
1011
"version": "./version.sh"
1112
},
1213
"repository": {
@@ -39,4 +40,4 @@
3940
"prettier": "^2.2.1",
4041
"typescript": "^4.1.3"
4142
}
42-
}
43+
}

src/index.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,14 @@ export default function inlinePostCSS(options: any = {}) {
2222
}
2323

2424
try {
25-
2625
let configPath;
2726

27+
const env = process.env.NODE_ENV
28+
? options.env
29+
? options.env
30+
: null
31+
: null;
32+
2833
const postcssOptions = {
2934
from: options.from ? path.join(process.cwd(), options.from) : id,
3035
to: options.to ? path.join(process.cwd(), options.to) : id,
@@ -48,29 +53,32 @@ export default function inlinePostCSS(options: any = {}) {
4853

4954
if (typeof config === 'function') {
5055
config = config({
51-
env: process.env.NODE_ENV,
56+
env,
5257
});
5358
}
5459

5560
const outputConfig = options.plugins
5661
? options.plugins
5762
: Object.keys(config.plugins)
58-
.filter((key) => config.plugins[key])
59-
.map((key) => require(key));
63+
.filter((key) => config.plugins[key])
64+
.map((key) => require(key));
6065

6166
const matches = code.match(styleRegex);
6267

63-
return Promise.all(matches.map(css => postcss(outputConfig)
64-
.process(css.split('`')[1], postcssOptions))).then((transforms: any) => {
65-
let mappings = '';
66-
transforms.forEach((transform, index) => {
67-
code = code.replace(matches[index].split('`')[1], transform.css);
68-
});
69-
return {
70-
code,
71-
map: null,
72-
};
68+
return Promise.all(
69+
matches.map((css) =>
70+
postcss(outputConfig).process(css.split('`')[1], postcssOptions)
71+
)
72+
).then((transforms: any) => {
73+
let mappings = '';
74+
transforms.forEach((transform, index) => {
75+
code = code.replace(matches[index].split('`')[1], transform.css);
7376
});
77+
return {
78+
code,
79+
map: null,
80+
};
81+
});
7482
} catch (error) {
7583
if (options.failOnError) {
7684
this.error(error.message);

test/fixtures/multiple.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
21
const containerStyle = css`
3-
.container {
4-
display: block;
5-
background: #abcefe;
6-
color: white;
7-
}
2+
.container {
3+
display: block;
4+
background: #abcefe;
5+
color: white;
6+
}
87
`;
98

109
const fontStyle = css`
11-
* {
12-
font-size: 1.0em;
13-
font-weight: 400;
14-
line-height: 1.2em;
15-
}
16-
`
10+
* {
11+
font-size: 1em;
12+
font-weight: 400;
13+
line-height: 1.2em;
14+
}
15+
`;
1716

1817
const horizontalListStyle = css`
19-
.horizontal {
20-
display: flex;
21-
}
18+
.horizontal {
19+
display: flex;
20+
}
2221
`;

test/index.html

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
<!doctype html>
1+
<!DOCTYPE html>
22
<html class="no-js" lang="">
3+
<head>
4+
<meta charset="utf-8" />
5+
<title></title>
6+
<script src="extract/bundle.js"></script>
7+
</head>
38

4-
<head>
5-
<meta charset="utf-8">
6-
<title></title>
7-
<script src="onExtract/bundle.js"></script>
8-
</head>
9-
10-
<body>
11-
12-
<button is="my-button"></button>
13-
</body>
9+
<body>
10+
<button is="my-button"></button>
11+
</body>
1412
</html>

test/index.test.js

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,19 @@ function fixture(...args) {
99
}
1010

1111
async function write({ input, output, plugin, outDir, options }) {
12-
1312
const jsCodePath = path.join(outDir, 'bundle.js');
1413

1514
const bundle = await rollup({
1615
input: fixture(input),
17-
plugins: [
18-
plugin,
19-
],
16+
plugins: [plugin],
2017
});
2118

2219
await bundle.write({
2320
format: 'esm',
2421
file: path.join(outDir, output),
2522
sourcemap: true,
2623
sourcemapFile: path.join(outDir, `${output}.map`),
27-
...options
24+
...options,
2825
});
2926

3027
return {
@@ -44,21 +41,20 @@ async function write({ input, output, plugin, outDir, options }) {
4441
hasSourceMap(bundle) {
4542
const file = fs.readFileSync(path.join(outDir, bundle), 'utf8');
4643
return file.includes('sourceMappingURL');
47-
}
44+
},
4845
};
49-
5046
}
5147

5248
test('should process with plugins declared in rollup.config.js', async () => {
5349
const res = await write({
5450
input: 'component.js',
5551
output: 'bundle.js',
56-
outDir: 'test/onExtract',
52+
outDir: 'test/extract',
5753
plugin: inlinePostCSS({
5854
plugins: [require('postcss-csso'), require('postcss-rgb-plz')],
5955
}),
6056
options: {
61-
sourcemap: false
57+
sourcemap: false,
6258
},
6359
});
6460
expect(await res.hasRGBColorValues('bundle.js')).toBe(true);
@@ -70,7 +66,7 @@ test('should process file with custom regex', async () => {
7066
const res = await write({
7167
input: 'custom.js',
7268
output: 'custom.js',
73-
outDir: 'test/onExtract',
69+
outDir: 'test/extract',
7470
plugin: inlinePostCSS({
7571
styleRegex: /(?:foo`)((.|\n)+?)(?=(`(\n|;|,)))/gi,
7672
}),
@@ -84,7 +80,7 @@ test('should reference postcss.config.js', async () => {
8480
const res = await write({
8581
input: 'component.js',
8682
output: 'config.js',
87-
outDir: 'test/onExtract',
83+
outDir: 'test/extract',
8884
plugin: inlinePostCSS({
8985
configPath: path.join(__dirname, 'config'),
9086
}),
@@ -98,13 +94,38 @@ test('should process multiple css declarations', async () => {
9894
const res = await write({
9995
input: 'multiple.js',
10096
output: 'multiple.js',
101-
outDir: 'test/onExtract',
97+
outDir: 'test/extract',
10298
plugin: inlinePostCSS(),
10399
options: {},
104100
});
105101
expect(await res.hasRGBColorValues('multiple.js')).toBe(true);
106102
expect(await res.hasSourceMap('multiple.js')).toBe(true);
107-
// expect(await res.isMinified('multiple.js')).toBe(true);
108103
});
109104

105+
test('should not minify css when environnment is development', async () => {
106+
const res = await write({
107+
input: 'custom.js',
108+
output: 'unminified.js',
109+
outDir: 'test/extract',
110+
plugin: inlinePostCSS({
111+
configPath: path.join(__dirname, 'unminified'),
112+
env: 'DEV',
113+
}),
114+
options: {},
115+
});
116+
expect(await res.isMinified('unminified.js')).toBe(false);
117+
});
110118

119+
test('should minify css when environnment is production', async () => {
120+
const res = await write({
121+
input: 'custom.js',
122+
output: 'minified.js',
123+
outDir: 'test/extract',
124+
plugin: inlinePostCSS({
125+
configPath: path.join(__dirname, 'unminified'),
126+
env: 'PROD',
127+
}),
128+
options: {},
129+
});
130+
expect(await res.isMinified('minified.js')).toBe(false);
131+
});

test/unminified/postcss.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = (ctx) => ({
2+
plugins: {
3+
'postcss-csso': ctx.env === 'PROD' ? {} : null,
4+
},
5+
});

0 commit comments

Comments
 (0)