Skip to content

Commit 2e027a8

Browse files
thebanjomaticAdam Hines
andauthored
fix(sass-imports): resolve module imports relative to file being processed (#478)
* fix(sass-imports): resolve module imports relative to file being processed Currently sass-imports that use the module import syntax (eg. `~module-to-import/some.sass`) are resolved relative to the vue-jest package. If you only have one version of that package, and everything has been hoisted to the same level as vue-jest (or higher), then things work as expected. Where things start to fail is when you have more than one instance of the sass package you are trying to import as it is possible the wrong version will be loaded, and it is concievable that the file won't be found. Where I encountered this problem, we had something like the following: ``` node_modules/ component-library/ node_modules/ [email protected] mixins.sass some-component.vue: <style lang="sass"> @import '~sass-library/mixins.scss'; class foo { @use someMixinFromV3 } </style> [email protected] mixins.sass @vue/vue2-jest lib module-name-mapper-helper.js ``` Notice that "sass-library" v1 was hoisted to the top level, but "component-library" depends on v3 of sass-library so v3 is in a nested node_modules directory. When "@vue/vue2-jest" does `require.resolve('sass-library/mixins.scss')`, while processing `component-library/some-component.vue`, the module search path is relative to the vue2-jest source file and not relative to some-component.vue like you would expect. The end result is that it finds the top-level `[email protected]` and not the v3 version. To fix this, I am now passing in the optional "paths" value to require.resolve (available since node 8.9 https://nodejs.org/api/modules.html#requireresolverequest-options) and providing the path to the source file being processed instead. This will start the module resolution search at some-component.vue and the nested `node_modules/sass-library`` v3 will be found instead. * test(vue2): adding e2e tests for sass-importer * test(vue3): adding e2e tests for sass-importer Co-authored-by: Adam Hines <[email protected]>
1 parent e218c07 commit 2e027a8

File tree

24 files changed

+319
-4
lines changed

24 files changed

+319
-4
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const { createTransformer } = require('babel-jest').default
2+
module.exports = createTransformer({
3+
presets: ['@babel/preset-env']
4+
})
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<template>
2+
<div>
3+
<h1 class="entry">Entry</h1>
4+
<lib-component />
5+
</div>
6+
</template>
7+
8+
<script>
9+
import LibComponent from 'vue2-sass-importer-lib/index.vue'
10+
11+
export default {
12+
components: {
13+
LibComponent
14+
}
15+
}
16+
</script>
17+
18+
<style lang="scss" module>
19+
@import '~vue2-sass-importer-sass-lib/index.scss';
20+
21+
.entry {
22+
@include my-v2-mixin;
23+
}
24+
</style>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"name": "vue2-sass-importer-entry",
3+
"version": "1.0.0",
4+
"license": "MIT",
5+
"private": true,
6+
"scripts": {
7+
"test": "jest --no-cache --coverage test.js"
8+
},
9+
"dependencies": {
10+
"vue": "^2.5.21",
11+
"vue-template-compiler": "^2.5.21",
12+
"vue2-sass-importer-lib": "file:../lib",
13+
"vue2-sass-importer-sass-lib": "file:../sass-lib-v2"
14+
},
15+
"devDependencies": {
16+
"@babel/core": "^7.9.0",
17+
"@babel/preset-env": "^7.9.0",
18+
"@vue/test-utils": "^1.1.0",
19+
"babel-jest": "^28.0.2",
20+
"jest": "28.x",
21+
"jest-environment-jsdom": "28.0.2",
22+
"postcss": "^7.0.13",
23+
"postcss-color-function": "^4.0.1",
24+
"sass": "^1.23.7",
25+
"@vue/vue2-jest": "^28.0.0"
26+
},
27+
"jest": {
28+
"testEnvironment": "jsdom",
29+
"moduleFileExtensions": [
30+
"js",
31+
"json",
32+
"vue"
33+
],
34+
"transformIgnorePatterns": [
35+
"/node_modules/.*(?<!.vue)$"
36+
],
37+
"transform": {
38+
"^.+\\.js$": "./babel-transformer.js",
39+
"^.+\\.vue$": "@vue/vue2-jest"
40+
},
41+
"globals": {
42+
"vue-jest": {
43+
"transform": {
44+
"^js$": "./babel-transformer.js"
45+
}
46+
}
47+
}
48+
}
49+
}

e2e/2.x/sass-importer/entry/test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { mount } from '@vue/test-utils'
2+
import Entry from './components/Entry.vue'
3+
4+
test('processes sass imports relative to current file', () => {
5+
const wrapper = mount(Entry)
6+
expect(wrapper).toBeDefined()
7+
})

e2e/2.x/sass-importer/lib/index.vue

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<template>
2+
<div class="lib-class">Lib Component</div>
3+
</template>
4+
5+
<style lang="scss" module>
6+
@import '~vue2-sass-importer-sass-lib/index.scss';
7+
8+
.lib-class {
9+
@include my-v1-mixin;
10+
}
11+
</style>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "vue2-sass-importer-lib",
3+
"version": "1.0.0",
4+
"license": "MIT",
5+
"private": true,
6+
"main": "index.vue",
7+
"files": [
8+
"index.vue"
9+
],
10+
"scripts": {
11+
"test": "echo 'No tests found.'"
12+
},
13+
"dependencies": {
14+
"vue2-sass-importer-sass-lib": "file:../sass-lib-v1"
15+
},
16+
"peerDependencies": {
17+
"vue": "^2.5.21"
18+
}
19+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@mixin my-v1-mixin {
2+
color: blue;
3+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "vue2-sass-importer-sass-lib",
3+
"version": "1.0.0",
4+
"license": "MIT",
5+
"private": true,
6+
"files": [
7+
"index.scss"
8+
],
9+
"scripts": {
10+
"test": "echo 'No tests found.'"
11+
}
12+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@mixin my-v2-mixin {
2+
color: red;
3+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "vue2-sass-importer-sass-lib",
3+
"version": "2.0.0",
4+
"license": "MIT",
5+
"private": true,
6+
"files": [
7+
"index.scss"
8+
],
9+
"scripts": {
10+
"test": "echo 'No tests found.'"
11+
}
12+
}

0 commit comments

Comments
 (0)