Skip to content

Support Sass Loader #26

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,14 @@ function getWebpackOpts(opts, callback) {
css = spmcss + css;
var cssLoader = 'style!' + css;
var lessLoader = 'style!' + normalize(css+'!less', opts, 'less');

var sassOutputStyle = opts.build.sass && opts.build.sass.outputStyle || 'expanded';
var sassLoader = 'style!' + normalize(css+'!resolve-url!sass?sourceMap&outputStyle='+sassOutputStyle, opts, 'scss');

if (opts.build.extractCSS || files.extractCSS) {
cssLoader = ExtractTextPlugin.extract('style', css);
lessLoader = ExtractTextPlugin.extract('style', normalize(css+'!less', opts, 'less'));
sassLoader = ExtractTextPlugin.extract('style', normalize(css+'!resolve-url!sass?sourceMap&outputStyle='+sassOutputStyle, opts, 'scss'));
}

var babelOpts = decodeURI(qs.stringify(opts.build.babel, {arrayFormat:'brackets'}));
Expand Down Expand Up @@ -174,6 +179,7 @@ function getWebpackOpts(opts, callback) {
{ test: /\.handlebars$/, loader: normalize('handlebars?helperDirs[]=' + __dirname + '/../helpers', opts, 'handlebars') },
{ test: /\.css$/, loader: cssLoader },
{ test: /\.less$/, loader: lessLoader },
{ test: /\.scss$/, loader: sassLoader },
{ test: /\.(png|jpe?g|gif|eot|svg|ttf|woff|woff2)$/, loader: loader }
]
},
Expand Down
4 changes: 2 additions & 2 deletions lib/getFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ function map(files, cwd, pathmap) {
key = file.name.replace(new RegExp(ext + '$'), '');
key = formatOutputPath(key, pathmap);
js[key] = formatFiles(file.files, cwd);
} else if (ext === '.css' || ext === '.less') {
} else if (ext === '.css' || ext === '.less' || ext === '.scss') {
// Create js file to require css
var f = file.name.replace(/\.(css|less)$/, '');
var f = file.name.replace(/\.(css|less|scss)$/, '');
f = formatOutputPath(f, pathmap);
var fileName = '_webpackcssentry_' + f;
var jsFile = join(require('os').tmpdir(), fileName + '.js');
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
"mkdirp": "~0.5.1",
"node-libs-browser": "~0.5.2",
"qs": "~2.4.1",
"resolve-url-loader": "~1.2.0",
"rimraf": "~2.3.2",
"sass-loader": "~2.0.1",
"scripts-hook": "~0.1.3",
"semver": "~4.3.1",
"spm-argv": "~0.1.0",
Expand Down
40 changes: 39 additions & 1 deletion test/build-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,16 @@ describe('lib/build.js', function() {
assert(dest, 'require-less-extract');
});

it('require sass extract', function*() {
yield build({
debug: true,
cwd: join(fixtures, 'require-sass'),
dest: dest,
extractCSS: true
});
assert(dest, 'require-sass-extract', true);
});

it('jsx', function*() {
yield build({
debug: true,
Expand Down Expand Up @@ -396,6 +406,30 @@ describe('lib/build.js', function() {

});

describe('custom-loader-sass', function() {

var oldCwd;

before(function() {
oldCwd = process.cwd();
process.chdir(join(fixtures, 'custom-loader-sass'));
});

after(function() {
process.chdir(oldCwd);
});

it('custom-loader-sass', function*() {
yield build({
debug: true,
cwd: join(fixtures, 'custom-loader-sass'),
dest: dest
});
assert(dest, 'custom-loader-sass', true);
});

});

describe('scripts', function() {

afterEach(function() {
Expand All @@ -412,7 +446,7 @@ describe('lib/build.js', function() {

});

function assert(actual, expect) {
function assert(actual, expect, isSass) {
expect = join(fixtures, '../expected', expect);
glob.sync('**/*', {cwd: actual})
.forEach(function(file) {
Expand All @@ -424,6 +458,10 @@ function assert(actual, expect) {
} else {
var c = fs.readFileSync(filepath).toString();
var ec = fs.readFileSync(join(expect, file)).toString();
if (isSass && /\.css$/.test(filepath)) {
c = c.replace(/\/\*# sourceMappingURL=[^\*]+\*\//, '');
ec = ec.replace(/\/\*# sourceMappingURL=[^\*]+\*\//, '');
}
c.should.eql(ec);
}
}
Expand Down
4 changes: 4 additions & 0 deletions test/expected/custom-loader-sass/a.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
a {
color: #121212;
}

66 changes: 66 additions & 0 deletions test/expected/custom-loader-sass/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};

/******/ // The require function
/******/ function __webpack_require__(moduleId) {

/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;

/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };

/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);

/******/ // Flag the module as loaded
/******/ module.loaded = true;

/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }


/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;

/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;

/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";

/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {

module.exports = __webpack_require__(2);


/***/ },
/* 1 */
/***/ function(module, exports) {

// removed by extract-text-webpack-plugin

/***/ },
/* 2 */
/***/ function(module, exports, __webpack_require__) {

__webpack_require__(1);
console.log(1);



/***/ }
/******/ ]);
9 changes: 9 additions & 0 deletions test/expected/require-sass-extract/a.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
b {
color: red;
background: url(bd98d83ae100b3e9a2fd351c5a2178e9.png);
}

a {
color: #121212;
}

66 changes: 66 additions & 0 deletions test/expected/require-sass-extract/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};

/******/ // The require function
/******/ function __webpack_require__(moduleId) {

/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;

/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };

/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);

/******/ // Flag the module as loaded
/******/ module.loaded = true;

/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }


/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;

/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;

/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";

/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {

module.exports = __webpack_require__(2);


/***/ },
/* 1 */
/***/ function(module, exports) {

// removed by extract-text-webpack-plugin

/***/ },
/* 2 */
/***/ function(module, exports, __webpack_require__) {


__webpack_require__(1);
console.log(1);


/***/ }
/******/ ]);
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions test/fixtures/custom-loader-sass/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require('./a.scss');
console.log(1);

6 changes: 6 additions & 0 deletions test/fixtures/custom-loader-sass/a.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

¥color: #121212;

a {
color: ¥color;
}
7 changes: 7 additions & 0 deletions test/fixtures/custom-loader-sass/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = function(source) {
this.cacheable && this.cacheable();
return source
.replace(/¥/g, '$');
};
11 changes: 11 additions & 0 deletions test/fixtures/custom-loader-sass/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"spm": {
"output": ["a.js"],
"build": {
"loader": {
".scss": "+./foo$"
},
"extractCSS": true
}
}
}
3 changes: 3 additions & 0 deletions test/fixtures/require-sass/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

require('./a.scss');
console.log(1);
8 changes: 8 additions & 0 deletions test/fixtures/require-sass/a.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

@import '~b';

$color: #121212;

a {
color: $color;
}
8 changes: 8 additions & 0 deletions test/fixtures/require-sass/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"spm": {
"output": [
"a.js"
],
"extractCSS": true
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions test/fixtures/require-sass/spm_modules/b/0.1.0/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
b {
color: red;
background: url(b.png);
}
7 changes: 7 additions & 0 deletions test/fixtures/require-sass/spm_modules/b/0.1.0/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "b",
"version": "0.1.0",
"spm": {
"main": "index.css"
}
}