Skip to content

Commit

Permalink
feat: add support "hoistAtrules" option (#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed Sep 28, 2020
1 parent 5bb8640 commit 90ff982
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -144,6 +144,16 @@ module.exports = {
*/
resolveURL: true,
// resolveURL: { nocheck: true },

/**
* Move @import and @charset to the top.
*
* @see https://stylus-lang.com/docs/executable.html
*
* @type {boolean}
* @default false
*/
hoistAtrules: true,
},
},
},
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Expand Up @@ -41,6 +41,10 @@ export default async function stylusLoader(source) {
styl.set('include css', true);
}

if (stylusOptions.hoistAtrules) {
styl.set('hoist atrules', true);
}

if (stylusOptions.disableCache) {
styl.set('cache', false);
}
Expand Down
12 changes: 12 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Expand Up @@ -689,6 +689,18 @@ exports[`loader should work "define" option: errors 1`] = `Array []`;

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

exports[`loader should work "hoistAtrules" option: css 1`] = `
"@charset 'utf-8';
body {
color: #f00;
}
"
`;

exports[`loader should work "hoistAtrules" option: errors 1`] = `Array []`;

exports[`loader should work "hoistAtrules" option: warnings 1`] = `Array []`;

exports[`loader should work "include" option: css 1`] = `
".other {
font-family: serif;
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/hoist-atrules.styl
@@ -0,0 +1,4 @@
body
color: red

@charset 'utf-8';
4 changes: 4 additions & 0 deletions test/helpers/getCodeFromStylus.js
Expand Up @@ -111,6 +111,10 @@ async function getCodeFromStylus(testId, options = {}) {
: `${options.additionalData}\n${data}`;
}

if (typeof stylusOptions.hoistAtrules === 'boolean') {
stylusOptions['hoist atrules'] = stylusOptions.hoistAtrules;
}

const mergedOptions = {
...defaultOptions,
...stylusOptions,
Expand Down
21 changes: 21 additions & 0 deletions test/loader.test.js
Expand Up @@ -1263,6 +1263,27 @@ describe('loader', () => {
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('should work "hoistAtrules" option', async () => {
const testId = './hoist-atrules.styl';
const compiler = getCompiler(testId, {
stylusOptions: {
hoistAtrules: true,
},
});
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
const codeFromStylus = await getCodeFromStylus(testId, {
stylusOptions: {
hoistAtrules: true,
},
});

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

it('should use .json file', async () => {
const testId = './json/index.styl';
const compiler = getCompiler(testId, {
Expand Down

0 comments on commit 90ff982

Please sign in to comment.