Skip to content

Commit

Permalink
feat: allow to define raw parameter
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the `define` option is `Array` now
  • Loading branch information
cap-Bernardito committed Sep 10, 2020
1 parent 739d46b commit b5c75ed
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 11 deletions.
8 changes: 5 additions & 3 deletions README.md
Expand Up @@ -85,9 +85,11 @@ module.exports = {
use: ['nib'],
include: [path.join(__dirname, 'src/styl/config')],
import: ['nib', path.join(__dirname, 'src/styl/mixins')],
define: {
$development: process.env.NODE_ENV === 'development',
},
define: [
// [key, value, raw]
['$development', process.env.NODE_ENV === 'development'],
['rawVar', 42, true],
],
includeCSS: false,
resolveUrl: false,
},
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Expand Up @@ -54,8 +54,8 @@ export default async function stylusLoader(source) {
}

if (typeof stylusOptions.define !== 'undefined') {
for (const entry of Object.entries(stylusOptions.define)) {
styl.define(...entry);
for (const defined of stylusOptions.define) {
styl.define(...defined);
}
}

Expand Down
22 changes: 22 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Expand Up @@ -241,6 +241,28 @@ exports[`loader should import stylus: errors 1`] = `Array []`;

exports[`loader should import stylus: warnings 1`] = `Array []`;

exports[`loader should work "define" option with raw: css 1`] = `
"section.current-stylus-support {
raw: \\"hash property access must use brackets\\";
raw: \\"hash property access must be last in selector :/\\";
answer: 42;
}
ul.frisbee-golf {
list-style: disc;
}
section.type-checks {
castedType: 'object';
rawType: 'object';
answer: 42;
hooray: \\"mixins can work around limits\\";
}
"
`;

exports[`loader should work "define" option with raw: errors 1`] = `Array []`;

exports[`loader should work "define" option with raw: warnings 1`] = `Array []`;

exports[`loader should work "define" option: css 1`] = `
"body {
width: 100%;
Expand Down
19 changes: 19 additions & 0 deletions test/fixtures/defineRaw.styl
@@ -0,0 +1,19 @@
unquoteList(strings)
unquote(join(' ', strings))

hashLookup(obj, key)
obj[key]

section.current-stylus-support
raw "hash property access must use brackets"
raw "hash property access must be last in selector :/"
answer rawVar['nestedVar']

ul.frisbee-golf
list-style unquoteList(castedVar)

section.type-checks
castedType type(castedVar)
rawType type(rawVar)
answer hashLookup(rawVar, 'nestedVar')
hooray "mixins can work around limits"
23 changes: 20 additions & 3 deletions test/loader.test.js
Expand Up @@ -251,9 +251,26 @@ describe('loader', () => {
const testId = './webpack.config-plugin.styl';
const compiler = getCompiler(testId, {
stylusOptions: {
define: {
add: (a, b) => a.operate('+', b),
},
define: [['add', (a, b) => a.operate('+', b)]],
},
});
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);

expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('should work "define" option with raw', async () => {
const testId = './defineRaw.styl';
const compiler = getCompiler(testId, {
stylusOptions: {
define: [
['rawVar', { nestedVar: 42 }, true],
['castedVar', { disc: 'outside' }, true],
['rawDefine', ['rawVar'], true],
],
},
});
const stats = await compile(compiler);
Expand Down
7 changes: 4 additions & 3 deletions test/validate-options.test.js
Expand Up @@ -8,9 +8,10 @@ describe('validate options', () => {
{ resolveCss: true },
{ includeCSS: false },
{
define: {
$development: process.env.NODE_ENV === 'development',
},
define: [
['$development', process.env.NODE_ENV === 'development'],
['rawVar', 42, true],
],
},
() => {},
() => {
Expand Down

0 comments on commit b5c75ed

Please sign in to comment.