Skip to content

Commit 9ebe90b

Browse files
committed
feature #474 Add resolve options for imports from css files. (David Ellingsworth, weaverryan)
This PR was merged into the master branch. Discussion ---------- Add resolve options for imports from css files. These options change the default resolver functionality when import is used in css files. This reduces the burden on the developer to locate styles provided by npm packages. E.g. With these changes the follow become valid: sass-test.scss: ```css // Resolution via "sass" property in package.json file in npm package @import '~bootstrap'; // Resolution via file extension to css-test.css @import 'css-test'; ``` css-test.css ```css // Resolution via "style" property in package.json file in npm package @import '~@fortawesome/fontawesome-free'; ``` Commits ------- 7895f63 fixing CS 9525e7a Merge branch 'master' into resolve 382bc5f Add functional test for css imports via package.json style property b9e42be Add functional test for sass imports via package.json sass property 0f9657f Specify resolve option to be used with the css-loader. This allows @import in css files to locate a file to import based on the package.json file and to look for local files with the appropriate extensions when not specified. b54ec7b Specify resolve option to be used with the sass-loader. This allows @import in sass files to locate a file to import based on the package.json file and to look for local files with the appropriate extentions when not specified.
2 parents 46ceeff + 7895f63 commit 9ebe90b

File tree

8 files changed

+75
-0
lines changed

8 files changed

+75
-0
lines changed

fixtures/css/sass_package_import.scss

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// There seems to be an issue with the sass-loader and prefixing
2+
// aliased directories with a tilde. The following line is how
3+
// this import should look and works for node_modules.
4+
// @import '~lib/test-pkg';
5+
6+
// Importing without the tilde seems to work for webpack aliases
7+
@import 'lib/test-pkg';

fixtures/css/style_package_import.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import '~lib/test-pkg';
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
$content:'Sass entrypoint';
2+
3+
body {
4+
content:$content;
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
content:'Style entrypoint';
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use strict";
2+
3+
document.write("JavaScript entrypoint");

fixtures/lib/test-pkg/package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name":"@symfony/webpack-encore-test-pkg",
3+
"description": "This is a test package for use by functional tests which use packages.",
4+
"author": {
5+
"name": "David Ellingsworth",
6+
"email": "david@desource.org"
7+
},
8+
"main":"js/javascript_entry.js",
9+
"style":"css/style_entry.css",
10+
"sass":"css/sass_entry.scss"
11+
}

lib/config-generator.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ class ConfigGenerator {
234234
use: babelLoaderUtil.getLoaders(this.webpackConfig)
235235
}),
236236
applyRuleConfigurationCallback('css', {
237+
resolve: {
238+
mainFields: ['style', 'main'],
239+
extensions: ['.css'],
240+
},
237241
test: /\.css$/,
238242
oneOf: [
239243
{
@@ -311,6 +315,10 @@ class ConfigGenerator {
311315

312316
if (this.webpackConfig.useSassLoader) {
313317
rules.push(applyRuleConfigurationCallback('sass', {
318+
resolve: {
319+
mainFields: ['sass', 'style', 'main'],
320+
extensions: ['.scss', '.sass', '.css']
321+
},
314322
test: /\.s[ac]ss$/,
315323
oneOf: [
316324
{

test/functional.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2214,6 +2214,43 @@ module.exports = {
22142214
});
22152215
});
22162216

2217+
describe('Package entrypoint imports', () => {
2218+
it('Import via "sass" package property', (done) => {
2219+
const config = createWebpackConfig('web/build', 'dev');
2220+
2221+
config.setPublicPath('/build');
2222+
config.addAliases({
2223+
lib: path.resolve('./lib')
2224+
});
2225+
config.enableSassLoader();
2226+
config.addStyleEntry('sass', './css/sass_package_import.scss');
2227+
2228+
testSetup.runWebpack(config, () => {
2229+
// A successful compile is all that is needed to pass this test.
2230+
// If this test fails then the import in the above sass file
2231+
// is not loading the package's sass file.
2232+
done();
2233+
});
2234+
});
2235+
2236+
it('Import via "style" package property', (done) => {
2237+
const config = createWebpackConfig('web/build', 'dev');
2238+
2239+
config.setPublicPath('/build');
2240+
config.addAliases({
2241+
lib: path.resolve('./lib')
2242+
});
2243+
config.addStyleEntry('style', './css/style_package_import.css');
2244+
2245+
testSetup.runWebpack(config, () => {
2246+
// A successful compile is all that is needed to pass this test.
2247+
// If this test fails then the import in the above css file
2248+
// is not loading the package's style file.
2249+
done();
2250+
});
2251+
});
2252+
});
2253+
22172254
describe('CSS extraction', () => {
22182255
it('With CSS extraction enabled', (done) => {
22192256
const config = createWebpackConfig('build', 'dev');

0 commit comments

Comments
 (0)