-
Notifications
You must be signed in to change notification settings - Fork 156
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: allow importing relative paths in global resources #373
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@primary-color: "red"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
$primary-color: #333; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
@primary-color: "red"; | ||
@import "./colors.less"; | ||
|
||
@font-size: 16px; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
$primary-color: #333; | ||
@import './colors.scss'; | ||
|
||
$font-size: 16px; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
const path = require('path') | ||
const fs = require('fs') | ||
const cssExtract = require('extract-from-css') | ||
const getVueJestConfig = require('./utils').getVueJestConfig | ||
const compileStyle = require('@vue/component-compiler-utils').compileStyle | ||
|
@@ -12,14 +11,23 @@ function getGlobalResources(resources, lang) { | |
let globalResources = '' | ||
if (resources && resources[lang]) { | ||
globalResources = resources[lang] | ||
.map(resource => path.resolve(process.cwd(), resource)) | ||
.filter(resourcePath => fs.existsSync(resourcePath)) | ||
.map(resourcePath => fs.readFileSync(resourcePath).toString()) | ||
.join('\n') | ||
.map(resource => { | ||
const absolutePath = path.resolve(process.cwd(), resource) | ||
return `${getImportLine(lang, absolutePath)}\n` | ||
}) | ||
.join('') | ||
} | ||
return globalResources | ||
} | ||
|
||
function getImportLine(lang, filePath) { | ||
const importLines = { | ||
default: `@import "${filePath}";`, | ||
sass: `@import "${filePath}"` | ||
} | ||
return importLines[lang] || importLines.default | ||
} | ||
|
||
function extractClassMap(cssCode) { | ||
const cssNames = cssExtract.extractClasses(cssCode) | ||
const cssMap = {} | ||
|
@@ -32,6 +40,7 @@ function extractClassMap(cssCode) { | |
function getPreprocessOptions(lang, filePath, jestConfig) { | ||
if (lang === 'scss' || lang === 'sass') { | ||
return { | ||
filename: filePath, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After rebasing, all the tests for vue files including a relative |
||
importer: (url, prev, done) => ({ | ||
file: applyModuleNameMapper( | ||
url, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I forgot what this does exactly but it looks like it does something different to the code you added. It looks like this line handles
<style src="/something.css">
. Did I misunderstand this? Do you know what is going on here by any chance?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can provide more details indeed. This function is only used to inject the global resources provided in the jest config. If you check where it is being used:
It basically define the final content of the
<style>
block by prepending the global resources but only if there are resources defined for the current stylelang
(i.e.lang="scss"
).Now back to the code I replaced: it used to get the content of each global resource with
fs.readFileSync
, join them with a line break and return it to be injected as a string in the<style>
block. This is the reason why the bug I fixed was happening because it makes the relative imports relative to the.vue
file instead of relative to the global resource. For instance, if we have styles/global.scss as global resource in the Jest config:styles/global.scss
components/Something.vue
It will be compiled to:
which is wrong since fonts.scss is within the
styles
directory, notcomponents
. I fixed it by injecting an actual import statement instead which will make the processor understand the relative import as it should.My code change will produce:
Does that make sense?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so. I'll merge and release this now. Let's keep an eye after people start upgrading to see if anything was negatively impacted by this change.
It looks like the script you added for the tests isn't running on CI 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lmiller1990 It's running on CircleCI, but not on Github actions since the
26.x
branch is not targeted. It would need to be added to node.yml:By the way, I removed the test-runner I previously added since it was useless: the e2e tests were already running since each of them is a package of the monorepo and their test scripts were called by yarn workspaces. I'm sorry for the confusion.