Skip to content

Commit 33aebed

Browse files
akki-jatmichael-ciniawsky
authored andcommittedOct 8, 2018
fix(addStyles): support exports of transpiled transforms (options.transform) (#333)
1 parent e821fe8 commit 33aebed

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed
 

‎README.md

+2
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ A `transform` is a function that can modify the css just before it is loaded int
246246
This function will be called on the css that is about to be loaded and the return value of the function will be loaded into the page instead of the original css.
247247
If the return value of the `transform` function is falsy, the css will not be loaded into the page at all.
248248

249+
> :Warning: In case you are using ES Module syntax in `tranform.js` then, you **need to transpile** it or otherwise it will throw an `{Error}`.
250+
249251
**webpack.config.js**
250252
```js
251253
{

‎lib/addStyles.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,9 @@ function addStyle (obj, options) {
254254

255255
// If a transform function was defined, run it on the css
256256
if (options.transform && obj.css) {
257-
result = options.transform(obj.css);
257+
result = typeof options.transform === 'function'
258+
? options.transform(obj.css)
259+
: options.transform.default(obj.css);
258260

259261
if (result) {
260262
// If transform returns a value, use that instead of the original css.

‎test/basic.test.js

+25
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,31 @@ describe("basic tests", function() {
536536

537537
runCompilerTest(expected, done);
538538
});
539+
540+
it("es6 export: should throw error transform is not a function", function(done) {
541+
const transform = require('./transforms/transform_es6');
542+
styleLoaderOptions.transform = 'test/transforms/transform_es6';
543+
544+
// const expectedTansformedStyle = transform(requiredStyle);
545+
const expected = new TypeError('transform is not a function').message;
546+
547+
runCompilerTest(expected, done, function() {
548+
try {
549+
let test = transform(requiredStyle);
550+
} catch(error) {
551+
return error.message;
552+
} });
553+
});
554+
555+
it("es6 export: should not throw any error", function(done) {
556+
const transform = require('./transforms/transform_es6');
557+
styleLoaderOptions.transform = 'test/transforms/transform_es6';
558+
559+
const expectedTansformedStyle = transform[Object.keys(transform)[0]](requiredStyle);
560+
const expected = [existingStyle, expectedTansformedStyle].join("\n");
561+
562+
runCompilerTest(expected, done);
563+
});
539564
});
540565

541566
describe("HMR", function() {

‎test/transforms/transform_es6.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
exports.default = function (css) {
2+
return css.replace('.required', '.transformed');
3+
}

0 commit comments

Comments
 (0)
Please sign in to comment.