Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ node_modules

# Build files
dist
/test/**/output.*
test/**/output
25 changes: 22 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,32 @@ import './layout.css'

### Options

There is 1 option: `output`.
By default the plugin will use `output.assetFileNames` to decide the filename.

```js
css({
// Optional: filename to write all styles to
output: 'bundle.css'
exclude, // [optional] - Array of glob/Strings like what `include` uses.
fileName, // [optional] - File name of emitted asset.
include, // [optional] - Array of glob/Strings - default: ['**/*.css'].
name, // [optional] - Name of the emitted asset.
output, // [optional] - Below are the possible values for `output`:

// ---------------------------------------------------------------------------
// Filename to write all styles to
output: 'bundle.css',

// Callback that will be called on generate with two arguments:
// - styles: the contents of all style tags combined: 'body { color: green }'
// - styleNodes: an array of style objects: [{ lang: 'css', content: 'body { color: green }' }]
output: (styles, styleNodes) => {
writeFileSync('bundle.css', styles)
},

// Disable any style output or callbacks
output: false,

// Default behaviour is to write all styles to the bundle destination where .js is replaced by .css
output: null
})
```

Expand Down
73 changes: 73 additions & 0 deletions bin/test-runner.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/usr/bin/env node

const { promisify } = require('node:util');
const exec = promisify(require('node:child_process').exec);

const args = process.argv.slice(2);
let exclude;
while (args.length) {
switch (args[0]) {
case '--exclude': { // NOTE: Use pipe `|` to add multiple exclusions: ':win|:unique'
exclude = args[1];
args.splice(0, 2);
break;
}
default: args.splice(0, args.length);
}
}

// TODO: Switch to `node:util -> styleText` if support is considered efficient.
const color = (col, txt) => {
const cols = {
cyan: "\x1b[36m",
green: "\x1b[32m",
red: "\x1b[31m",
reset: "\x1b[0m",
yellow: "\x1b[33m",
};

return `${cols[col] || ''}${txt}${cols.reset}`;
};

const tests = Object
.keys(require('../package.json').scripts)
.filter(s => (
/^test:/.test(s)
&& ((exclude.length) ? !(new RegExp(`(${exclude})`)).test(s) : true)
));

const printMsg = (msg, overwrite = false) => {
if (overwrite) {
process.stdout.clearLine(0);
process.stdout.cursorTo(0);
}
process.stdout.write(msg);
};

(async () => {
for (let t of tests) {

try {
printMsg(` Testing: ${color('cyan', t)}`);
await exec(`npm run ${t}`);
printMsg(` ${color('green', '✔')} ${color('cyan', t)}`, true);
}
catch (err) {
printMsg(` ${color('red', '✘')} ${color('yellow', t)}`, true);

if (err.stdout) {
const lines = err.stdout
.split('\n')
.filter(l => !!l && !l.startsWith('> '))
.map(l => color('red', l));

printMsg(`\n\n ${lines.join('\n')}`);
break;
}
else { throw err; }
}
finally {
printMsg('\n');
}
}
})();
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
"test:nested": "cd test/nested && rm -rf output && rollup -c && cmp output/output.js expected.js && cmp output/output.css expected.css && cd ../..",
"test:empty": "cd test/empty && rm -rf output && rollup -c && cmp output/output.js expected.js && cmp output/output.css expected.css && cd ../..",
"test:simple": "cd test/simple && rm -rf output && rollup -c && cmp output/output.js expected.js && cmp output/output.css expected.css && cd ../..",
"test:unique": "cd test/unique && rm -rf output && rollup -c && cmp output/main.js expected.js && cmp output/output.css expected.css && cd ../..",
"test:token-names": "cd test/token-names && rm -rf output && rollup -c && cmp output/app_sQGiL2.js expected.js && cmp output/app_QeMiKt.css expected.css && cd ../..",
"test:unique": "cd test/unique && rm -rf output && rollup -c && cmp output/dependency-a.js expected-dep.js && cmp output/main.js expected.js && cmp output/output.css expected.css && cd ../..",
"test:win:simple": "cd .\\test\\simple && del -f output.* && rollup -c && cd .. && ECHO n|comp simple\\output.js expected.js && ECHO n|comp simple\\output.css simple\\expected.css && cd ..",
"test": "npm run test:simple && npm run test:nested && npm run test:empty && npm run test:circular && npm run test:unique",
"test:win": "npm run test:win:simple",
"test": "./bin/test-runner.cjs --exclude ':win'",
"lint": "prettier rollup.config.js src/**",
"prepare": "npm run build",
"prepublish": "npm run build",
Expand Down
4 changes: 4 additions & 0 deletions test/token-names/expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.rollup {
color: green;
user-select: none;
}
1 change: 1 addition & 0 deletions test/token-names/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('css imported');
4 changes: 4 additions & 0 deletions test/token-names/input.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.rollup {
color: green;
user-select: none;
}
3 changes: 3 additions & 0 deletions test/token-names/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import './input.css'

console.log('css imported')
13 changes: 13 additions & 0 deletions test/token-names/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import css from '../../src/index.mjs'

export default {
input: { app: 'input.js' },
output: {
assetFileNames: '[name]_[hash:6][extname]',
dir: 'output',
entryFileNames: '[name]_[hash:6].js',
format: 'esm',
// hashCharacters: 'hex', // TODO should be added at rollup@4.10.0 or above
},
plugins: [css()]
}
File renamed without changes.
1 change: 0 additions & 1 deletion test/unique/output/main.js

This file was deleted.