Skip to content

Commit e53770d

Browse files
authored
feat: support "hoistUseStatements" loader option (#162)
1 parent 1c993e6 commit e53770d

File tree

7 files changed

+42
-36
lines changed

7 files changed

+42
-36
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616
* Share variables, mixins, functions across all style files (no `@import` needed)
1717
* Support for SASS, LESS and Stylus
1818
* Aliases (`~/assets/variables.css`) and globbing as supported
19+
* Support for hoisting `@use` imports
1920
* Compatible with Nuxt's `build.styleResources` (and will take them over directly if included!)
2021
* Blazing fast:tm:
2122

2223
## Warning
2324

2425
**Do not import actual styles**.
25-
Use this module only to import variables, mixins, functions (et cetera) as they won't exist in the actual build. Importing actual styles will include them in every component and will also make your build/HMR magnitudes slower.
26+
Use this module only to import variables, mixins, functions (et cetera) as they won't exist in the actual build. Importing actual styles will include them in every component and will also make your build/HMR magnitudes slower.
2627
**Do not do this!**
2728

2829
## Setup
@@ -45,7 +46,8 @@ export default {
4546
sass: [],
4647
scss: [],
4748
less: [],
48-
stylus: []
49+
stylus: [],
50+
hoistUseStatements: true // Hoists the "@use" imports. Applies only to "sass", "scss" and "less". Default: false.
4951
}
5052
}
5153
```

index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import NuxtConfiguration from '@nuxt/config'
1+
import '@nuxt/config'
22

33
declare module '@nuxt/config/types/index' {
44
export default interface NuxtConfiguration {
@@ -7,6 +7,7 @@ declare module '@nuxt/config/types/index' {
77
scss?: string[] | string
88
less?: string[] | string
99
stylus?: string[] | string
10+
hoistUseStatements?: boolean
1011
}
1112
}
1213
}

lib/module.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ export default function nuxtStyledResources(moduleOptions) {
1616
// A bit messy. Check for truthyness and keys and return
1717
const legacyResources = legacyStyledResources && Object.keys(legacyStyledResources).length && legacyStyledResources
1818

19-
const resources = legacyResources || styleResources
19+
const resources = Object.assign({}, legacyResources || styleResources)
20+
const hoistUseStatements = resources.hoistUseStatements || false
21+
delete resources.hoistUseStatements
2022

2123
const styleResourcesEntries = Object.entries(resources)
2224

@@ -52,11 +54,11 @@ export default function nuxtStyledResources(moduleOptions) {
5254
}
5355

5456
if (sass.length) {
55-
this.extendBuild(extendSass(sass))
57+
this.extendBuild(extendSass({ resources: sass, hoistUseStatements }))
5658
}
5759

5860
if (scss.length) {
59-
this.extendBuild(extendScss(scss))
61+
this.extendBuild(extendScss({ resources: scss, hoistUseStatements }))
6062
}
6163

6264
if (stylus.length) {
@@ -67,15 +69,15 @@ export default function nuxtStyledResources(moduleOptions) {
6769
}
6870

6971
if (less.length) {
70-
this.extendBuild(extendLess(less))
72+
this.extendBuild(extendLess({ resources: less, hoistUseStatements }))
7173
}
7274
}
7375

74-
const extendWithSassResourcesLoader = matchRegex => resources => (config) => {
76+
const extendWithSassResourcesLoader = matchRegex => options => (config) => {
7577
// Yes, using sass-resources-loader is **intended here**
7678
// Despite it's name it can be used for less as well!
7779
const sassResourcesLoader = {
78-
loader: 'sass-resources-loader', options: { resources }
80+
loader: 'sass-resources-loader', options
7981
}
8082

8183
// Gather all loaders that test against scss or sass files

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"dependencies": {
6060
"consola": "^2.4.0",
6161
"glob-all": "^3.1.0",
62-
"sass-resources-loader": "^2.0.0"
62+
"sass-resources-loader": "^2.2.1"
6363
},
6464
"devDependencies": {
6565
"@commitlint/cli": "^7.5.0",

test/fixture/sass/components/Sass.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
</template>
66

77
<style lang="scss">
8+
@use 'sass:math';
9+
810
.ymca {
911
color: $gray;
1012
line-height: pow(4, 2);

test/fixture/sass/nuxt.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ module.exports = {
99
},
1010
styleResources: {
1111
scss: ['@/assets/nested/index.scss', 'mathsass'],
12-
sass: ['@/assets/nested/index.sass']
12+
sass: ['@/assets/nested/index.sass'],
13+
hoistUseStatements: true
1314
},
1415
buildModules: ['@@'],
1516
build: {

yarn.lock

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2458,12 +2458,10 @@ async-each@^1.0.1:
24582458
resolved "https://registry.yarnpkg.com/async-each/-/async-each-1.0.3.tgz#b727dbf87d7651602f06f4d4ac387f47d91b0cbf"
24592459
integrity sha512-z/WhQ5FPySLdvREByI2vZiTWwCnF0moMJ1hK9YQwDTHKh6I7/uSckMetoRGb5UBZPC1z0jlw+n/XCgjeH7y1AQ==
24602460

2461-
async@^2.1.4:
2462-
version "2.6.3"
2463-
resolved "https://registry.yarnpkg.com/async/-/async-2.6.3.tgz#d72625e2344a3656e3a3ad4fa749fa83299d82ff"
2464-
integrity sha512-zflvls11DCy+dQWzTW2dzuilv8Z5X/pjfmZOWba6TNIVDm+2UDaJmXSOXlasHKfNBs8oo3M0aT50fDEWfKZjXg==
2465-
dependencies:
2466-
lodash "^4.17.14"
2461+
async@^3.2.0:
2462+
version "3.2.0"
2463+
resolved "https://registry.yarnpkg.com/async/-/async-3.2.0.tgz#b3a2685c5ebb641d3de02d161002c60fc9f85720"
2464+
integrity sha512-TR2mEZFVOj2pLStYxLht7TyfuRzaydfpxr3k9RpHIzMgw7A64dzsdqCxH1WJyQdoe8T10nDXd9wnEigmiuHIZw==
24672465

24682466
asynckit@^0.4.0:
24692467
version "0.4.0"
@@ -5429,10 +5427,10 @@ glob-to-regexp@^0.4.1:
54295427
resolved "https://registry.yarnpkg.com/glob-to-regexp/-/glob-to-regexp-0.4.1.tgz#c75297087c851b9a578bd217dd59a92f59fe546e"
54305428
integrity sha512-lkX1HJXwyMcprw/5YUZc2s7DrpAiHB21/V+E1rHUrVNokkvB6bqMzT0VfV6/86ZNabt1k14YOIaT7nDvOX3Iiw==
54315429

5432-
glob@^7.1.1, glob@^7.1.2:
5433-
version "7.1.6"
5434-
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.6.tgz#141f33b81a7c2492e125594307480c46679278a6"
5435-
integrity sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==
5430+
glob@^7.1.1, glob@^7.1.3, glob@^7.1.4, glob@^7.1.6, glob@^7.1.7:
5431+
version "7.1.7"
5432+
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.7.tgz#3b193e9233f01d42d0b3f78294bbeeb418f94a90"
5433+
integrity sha512-OvD9ENzPLbegENnYP5UUfJIirTg4+XwMWGaQfQTY0JenxNvvIKP3U3/tAQSPIu/lHxXYSZmpXlUHeqAIdKzBLQ==
54365434
dependencies:
54375435
fs.realpath "^1.0.0"
54385436
inflight "^1.0.4"
@@ -5441,10 +5439,10 @@ glob@^7.1.1, glob@^7.1.2:
54415439
once "^1.3.0"
54425440
path-is-absolute "^1.0.0"
54435441

5444-
glob@^7.1.3, glob@^7.1.4, glob@^7.1.6, glob@^7.1.7:
5445-
version "7.1.7"
5446-
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.7.tgz#3b193e9233f01d42d0b3f78294bbeeb418f94a90"
5447-
integrity sha512-OvD9ENzPLbegENnYP5UUfJIirTg4+XwMWGaQfQTY0JenxNvvIKP3U3/tAQSPIu/lHxXYSZmpXlUHeqAIdKzBLQ==
5442+
glob@^7.1.2:
5443+
version "7.1.6"
5444+
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.6.tgz#141f33b81a7c2492e125594307480c46679278a6"
5445+
integrity sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==
54485446
dependencies:
54495447
fs.realpath "^1.0.0"
54505448
inflight "^1.0.4"
@@ -7120,7 +7118,7 @@ loader-runner@^4.1.0:
71207118
resolved "https://registry.yarnpkg.com/loader-runner/-/loader-runner-4.2.0.tgz#d7022380d66d14c5fb1d496b89864ebcfd478384"
71217119
integrity sha512-92+huvxMvYlMzMt0iIOukcwYBFpkYJdpl2xsZ7LrlayO7E8SOv+JJUEK17B/dJIHAOLMfh2dZZ/Y18WgmGtYNw==
71227120

7123-
loader-utils@^1.0.1, loader-utils@^1.0.2, loader-utils@^1.0.4, loader-utils@^1.1.0, loader-utils@^1.2.3, loader-utils@^1.4.0:
7121+
loader-utils@^1.0.1, loader-utils@^1.0.2, loader-utils@^1.1.0, loader-utils@^1.2.3, loader-utils@^1.4.0:
71247122
version "1.4.0"
71257123
resolved "https://registry.yarnpkg.com/loader-utils/-/loader-utils-1.4.0.tgz#c579b5e34cb34b1a74edc6c1fb36bfa371d5a613"
71267124
integrity sha512-qH0WSMBtn/oHuwjy/NucEgbx5dbxxnxup9s4PVXJUDHZBQY+s0NWA9rJf53RBnQZxfch7euUui7hpoAPvALZdA==
@@ -7216,12 +7214,12 @@ lodash@4.17.11:
72167214
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.11.tgz#b39ea6229ef607ecd89e2c8df12536891cac9b8d"
72177215
integrity sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg==
72187216

7219-
lodash@^4.15.0, lodash@^4.17.13, lodash@^4.17.19, lodash@^4.17.20, lodash@^4.17.21, lodash@^4.17.5, lodash@^4.7.0:
7217+
lodash@^4.15.0, lodash@^4.17.13, lodash@^4.17.14, lodash@^4.17.19, lodash@^4.17.20, lodash@^4.17.21, lodash@^4.17.5, lodash@^4.7.0:
72207218
version "4.17.21"
72217219
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
72227220
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
72237221

7224-
lodash@^4.17.11, lodash@^4.17.12, lodash@^4.17.14, lodash@^4.2.1:
7222+
lodash@^4.17.11, lodash@^4.17.12, lodash@^4.2.1:
72257223
version "4.17.15"
72267224
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.15.tgz#b447f6670a0455bbfeedd11392eff330ea097548"
72277225
integrity sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==
@@ -9831,15 +9829,15 @@ sass-loader@^7.1.0:
98319829
pify "^4.0.1"
98329830
semver "^6.3.0"
98339831

9834-
sass-resources-loader@^2.0.0:
9835-
version "2.0.1"
9836-
resolved "https://registry.yarnpkg.com/sass-resources-loader/-/sass-resources-loader-2.0.1.tgz#c8427f3760bf7992f24f27d3889a1c797e971d3a"
9837-
integrity sha512-UsjQWm01xglINC1kPidYwKOBBzOElVupm9RwtOkRlY0hPA4GKi2KFsn4BZypRD1kudaXgUnGnfbiVOE7c+ybAg==
9832+
sass-resources-loader@^2.2.1:
9833+
version "2.2.1"
9834+
resolved "https://registry.yarnpkg.com/sass-resources-loader/-/sass-resources-loader-2.2.1.tgz#d7dbc36ccb25b2d8ffa508b1b8630b987df1e5c3"
9835+
integrity sha512-WlofxbWOVnxad874IHZdWbP9eW1pbyqsTJKBsMbeB+YELvLSrZQNDTpH70s6F19BwtanR3NEFnyGwJ9WyotJTQ==
98389836
dependencies:
9839-
async "^2.1.4"
9840-
chalk "^1.1.3"
9841-
glob "^7.1.1"
9842-
loader-utils "^1.0.4"
9837+
async "^3.2.0"
9838+
chalk "^4.1.0"
9839+
glob "^7.1.6"
9840+
loader-utils "^2.0.0"
98439841

98449842
sass@^1.34.0:
98459843
version "1.34.0"

0 commit comments

Comments
 (0)