Skip to content

Commit

Permalink
Merge pull request #83 from igor-ribeiro/master
Browse files Browse the repository at this point in the history
Fixes #12: Add `extensions` config to support other css file extensions
  • Loading branch information
wbyoung committed Jul 18, 2018
2 parents ce92e02 + f5b4535 commit 59849d5
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 8 deletions.
34 changes: 26 additions & 8 deletions src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,20 @@ const launchServer = () => {
process.on('exit', stopServer);
};

const extensions = ['.css'];

const getStylesFromStylesheet = (stylesheetPath: string, file: any,
config: any): any => {
const defaultExtensions = ['.css'];

const getStylesFromStylesheet = (
stylesheetPath: string,
file: any,
config: any,
configExtensions: string[],
): any => {
const stylesheetExtension = extname(stylesheetPath);

const extensions = Array.isArray(configExtensions)
? configExtensions
: defaultExtensions;

if (extensions.indexOf(stylesheetExtension) !== -1) {
launchServer();
const requiringFile = file.opts.filename;
Expand Down Expand Up @@ -88,8 +96,13 @@ export default function transformPostCSS({ types: t }: any): any {
}

const [{ value: stylesheetPath }] = args;
const { config } = this.opts;
const tokens = getStylesFromStylesheet(stylesheetPath, file, config);
const { config, extensions } = this.opts;
const tokens = getStylesFromStylesheet(
stylesheetPath,
file,
config,
extensions
);

if (tokens !== undefined) {
const expression = path.findParent((test) => (
Expand Down Expand Up @@ -118,8 +131,13 @@ export default function transformPostCSS({ types: t }: any): any {
return;
}

const { config } = this.opts;
const tokens = getStylesFromStylesheet(stylesheetPath, file, config);
const { config, extensions } = this.opts;
const tokens = getStylesFromStylesheet(
stylesheetPath,
file,
config,
extensions
);

if (tokens) {
const styles = t.objectExpression(
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/import.scss.expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";

var styles = {
"simple": "_simple_jvai8_1"
}; // @related-file ./simple.scss

console.log(styles);
3 changes: 3 additions & 0 deletions test/fixtures/import.scss.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import styles from "./simple.scss";

console.log(styles);
3 changes: 3 additions & 0 deletions test/fixtures/simple.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.simple {
color: magenta;
}
2 changes: 2 additions & 0 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const babelNoModules = {
export const transform = (
filename: string,
babelOptionOverrides: ?{ [string]: mixed },
extensions: ?string[],
): Promise<string> => {
const file = path.join(fixtures, filename);

Expand All @@ -22,6 +23,7 @@ export const transform = (
plugins: [
['../../src/plugin.js', {
config: 'fixtures/postcss.config.js',
extensions,
}],
],
}, babelOptionOverrides);
Expand Down
20 changes: 20 additions & 0 deletions test/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,26 @@ describe('babel-plugin-transform-postcss', () => {
shouldBehaveLikeSeverIsRunning();
});

describe('when transforming import.scss.js', () => {
let result;

beforeEach(async() => {
result = await transform(
'import.scss.js',
null,
['.scss']
);
});

it('launches the server', testServerLaunched);
it('launches a client', () => testClientLaunched('simple.scss'));
it('compiles correctly', async() => {
expect(result).to.eql((await read('import.scss.expected.js')).trim());
});

shouldBehaveLikeSeverIsRunning();
});

describe('when transforming nocss.js', () => {
beforeEach(() => transform('nocss.js'));

Expand Down

0 comments on commit 59849d5

Please sign in to comment.