-
-
Notifications
You must be signed in to change notification settings - Fork 198
Add resolve options for imports from css files. #474
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
Changes from all 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": "[email protected]" | ||
| }, | ||
| "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 |
|---|---|---|
|
|
@@ -234,6 +234,10 @@ class ConfigGenerator { | |
| use: babelLoaderUtil.getLoaders(this.webpackConfig) | ||
| }), | ||
| applyRuleConfigurationCallback('css', { | ||
| resolve: { | ||
| mainFields: ['style', 'main'], | ||
| extensions: ['.css'], | ||
| }, | ||
| test: /\.css$/, | ||
| oneOf: [ | ||
| { | ||
|
|
@@ -311,6 +315,10 @@ class ConfigGenerator { | |
|
|
||
| if (this.webpackConfig.useSassLoader) { | ||
| rules.push(applyRuleConfigurationCallback('sass', { | ||
| resolve: { | ||
| mainFields: ['sass', 'style', 'main'], | ||
| extensions: ['.scss', '.sass', '.css'] | ||
|
Member
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
Author
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
Member
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?
Author
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$/, | ||
| oneOf: [ | ||
| { | ||
|
|
||
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-pkgmoduleresolvekey 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./liband thattest-pkgis a directory under it which contains a package.json file. To determine which file to import, Webpack evaluates each value in themainFieldsresolve option in order to determine if it has been specified in the package.json file. If themainFieldsresolve option is not specified, Webpack defaults tobrowser,module, and thenmainwhen thetargetproperty 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 inmainFilesresolve option having an extension of one of the values specified in theextensionsresolve option. ThemainFilesresolve option defaults toindexand theextensionsresolve options defaults to['.wasm', '.mjs', '.js', '.json']. With the options below specified, Webpack will instead look forindex.scss,index.sass, and thenindex.css.