Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hoistUseStatements not working with multiline imports #140

Merged
merged 1 commit into from
Apr 14, 2021
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
4 changes: 3 additions & 1 deletion src/utils/processResources.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import logger from './logger';

// Matches opening and closing parenthesis across multiple lines
const multilineParenthesisRegex = '\\([\\s\\S]*?\\);?';
// Finds any @use statement
const useRegex = '^@use .*\n?$';
const useRegex = `^@use \\S*(?: with ${multilineParenthesisRegex}|.*)?\n?$`;
// Same as above, but adds the m (multiline) flag
const useRegexTest = new RegExp(useRegex, 'm');
// Makes sure that only the last instance of `useRegex` variable is found
Expand Down
26 changes: 26 additions & 0 deletions test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,32 @@ div {
"
`;

exports[`sass-resources-loader hoisting should work with multiline @use statements 1`] = `
"@use 'shared/index' with (
$text-color: blue
);

$text-color: $ccc;

@mixin my-mixin {
background-color: gray;
color: white;
}

@function always-blue() {
@return blue;
}

div {
@include secret.my-mixin;

p {
color: secret.always-blue();
}
}
"
`;

exports[`sass-resources-loader imports should preserve import method 1`] = `
"@use 'shared/index' as secret;
@import 'shared/variables';
Expand Down
11 changes: 11 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,17 @@ describe('sass-resources-loader', () => {
const output = require('./output/hoist3').default;
expect(output).toMatchSnapshot();
}));

it('should work with multiline @use statements', () => execTest('hoist-multiline', {
resources: [
path.resolve(__dirname, './scss/shared/_variables.scss'),
],
hoistUseStatements: true,
}).then(() => {
// eslint-disable-next-line global-require
const output = require('./output/hoist-multiline').default;
expect(output).toMatchSnapshot();
}));
});

describe('imports', () => {
Expand Down
11 changes: 11 additions & 0 deletions test/scss/hoist-multiline.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@use 'shared/index' with (
$text-color: blue
);

div {
@include secret.my-mixin;

p {
color: secret.always-blue();
}
}