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
Add resolve options for imports from css files. #474
Changes from 4 commits
b54ec7b
0f9657f
b9e42be
382bc5f
9525e7a
7895f63
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// There seems to be an issue with the sass-loader and prefixing | ||
// aliased directories with a tilde. The following line is how | ||
// this import should look and works for node_modules. | ||
// @import '~lib/test-pkg'; | ||
|
||
// Importing without the tilde seems to work for webpack aliases | ||
@import 'lib/test-pkg'; | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@import '~lib/test-pkg'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
$content:'Sass entrypoint'; | ||
|
||
body { | ||
content:$content; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
body { | ||
content:'Style entrypoint'; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
"use strict"; | ||
|
||
document.write("JavaScript entrypoint"); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name":"@symfony/webpack-encore-test-pkg", | ||
"description": "This is a test package for use by functional tests which use packages.", | ||
"author": { | ||
"name": "David Ellingsworth", | ||
"email": "david@desource.org" | ||
}, | ||
"main":"js/javascript_entry.js", | ||
"style":"css/style_entry.css", | ||
"sass":"css/sass_entry.scss" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -227,6 +227,10 @@ class ConfigGenerator { | |
use: babelLoaderUtil.getLoaders(this.webpackConfig) | ||
}, | ||
{ | ||
resolve: { | ||
mainFields: ['style', 'main'], | ||
extensions: ['.css'], | ||
}, | ||
test: /\.css$/, | ||
use: cssExtractLoaderUtil.prependLoaders(this.webpackConfig, cssLoaderUtil.getLoaders(this.webpackConfig)) | ||
} | ||
|
@@ -290,6 +294,10 @@ class ConfigGenerator { | |
|
||
if (this.webpackConfig.useSassLoader) { | ||
rules.push({ | ||
resolve: { | ||
mainFields: ['sass', 'style', 'main'], | ||
extensions: ['.scss', '.sass', '.css'] | ||
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. What exactly does this do in the process? I know what 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. The If you were to say 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. One more question: because this is being added to the sass loader, this resolution logic applies to files being imported from within a Sass file only, right? So, for example, if I were in What about less & stylus below? Do those also need these options? 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. Yes, these are loader specific options. So These options can be specified for any loaders where appropriate so long as that loader uses Webpack to perform the import resolution. |
||
}, | ||
test: /\.s[ac]ss$/, | ||
use: cssExtractLoaderUtil.prependLoaders(this.webpackConfig, sassLoaderUtil.getLoaders(this.webpackConfig)) | ||
}); | ||
|
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.
Can you walk me through this process for how this is resolved? I don't quite see the full picture yet:
lib/test-pkg
moduleresolve
key from the css rule?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.
When the sass-loader encounters
'lib/test-pkg'
it asks Webpack to perform the resolution of this import and it does so using the process outlined in the Resolve configuration. Webpack will notice that lib is an alias to./lib
and thattest-pkg
is a directory under it which contains a package.json file. To determine which file to import, Webpack evaluates each value in themainFields
resolve option in order to determine if it has been specified in the package.json file. If themainFields
resolve option is not specified, Webpack defaults tobrowser
,module
, and thenmain
when thetarget
property is set toweb
,webworker
, or is unspecified. If none of these properties exist in the package.json file, then Webpack will revert to using extension based resolution. In this case since'lib/text-pkg'
resolves to a directory, Webpack will look for a file in this directory using the names specified inmainFiles
resolve option having an extension of one of the values specified in theextensions
resolve option. ThemainFiles
resolve option defaults toindex
and theextensions
resolve options defaults to['.wasm', '.mjs', '.js', '.json']
. With the options below specified, Webpack will instead look forindex.scss
,index.sass
, and thenindex.css
.