Skip to content

Commit

Permalink
refactor: code (#451)
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed Aug 20, 2020
1 parent 60e4f12 commit 2cd7614
Show file tree
Hide file tree
Showing 23 changed files with 102 additions and 62 deletions.
8 changes: 6 additions & 2 deletions src/utils.js
Expand Up @@ -129,8 +129,12 @@ async function loadConfig(config, context, configPath, loaderContext) {
} else {
result = await explorer.search(searchPath);
}
} catch (errorIgnore) {
throw new Error(`No PostCSS Config found in: ${searchPath}`);
} catch (error) {
throw error;
}

if (!result) {
return {};
}

const patchedContext = createContext(context);
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions test/fixtures/config-scope/css/index.js
@@ -0,0 +1,3 @@
import style from './style.css'

export default style
3 changes: 3 additions & 0 deletions test/fixtures/config-scope/css/index2.js
@@ -0,0 +1,3 @@
import style from './style2.css'

export default style
File renamed without changes.
7 changes: 7 additions & 0 deletions test/fixtures/config-scope/css/plugins.config.js
@@ -0,0 +1,7 @@
module.exports = {
plugins: {
'postcss-short': { prefix: 'x' },
'postcss-import': {},
'postcss-nested': {},
}
}
1 change: 1 addition & 0 deletions test/fixtures/config-scope/css/style.css
@@ -0,0 +1 @@
a { color: black }
4 changes: 4 additions & 0 deletions test/fixtures/config-scope/css/style2.css
@@ -0,0 +1,4 @@
a {
-x-border-color: blue blue *;
-x-color: * #fafafa;
}
File renamed without changes.
13 changes: 11 additions & 2 deletions test/options/__snapshots__/config.test.js.snap
Expand Up @@ -3,7 +3,7 @@
exports[`Config Options should emit error when invalid config : errors 1`] = `
Array [
"ModuleBuildError: Module build failed (from \`replaced original path\`):
Error: No PostCSS Config found in: /test/fixtures/css/invalid.config.js",
Error: invalid postcss config",
]
`;

Expand All @@ -12,7 +12,7 @@ exports[`Config Options should emit error when invalid config : warnings 1`] = `
exports[`Config Options should emit error when unresolved config : errors 1`] = `
Array [
"ModuleBuildError: Module build failed (from \`replaced original path\`):
Error: No PostCSS Config found in: /test/fixtures/css/unresolve.js",
Error: No PostCSS Config found in: /test/fixtures/config-scope/css/unresolve.js",
]
`;

Expand Down Expand Up @@ -103,6 +103,15 @@ exports[`Config Options should work Config – Context – Loader {Object}: erro

exports[`Config Options should work Config – Context – Loader {Object}: warnings 1`] = `Array []`;

exports[`Config Options should work if Config not found: css 1`] = `
"a { color: black }
"
`;

exports[`Config Options should work if Config not found: errors 1`] = `Array []`;

exports[`Config Options should work if Config not found: warnings 1`] = `Array []`;

exports[`Config Options should work package.json - {Object} - Process CSS: css 1`] = `
".import {
color: red;
Expand Down
2 changes: 1 addition & 1 deletion test/options/__snapshots__/plugins.test.js.snap
Expand Up @@ -79,7 +79,7 @@ exports[`Options Plugins should work Plugins - {Object without require} + option
exports[`Options Plugins should work Plugins - {Object without require} + options: warnings 1`] = `Array []`;
exports[`Options Plugins should work Plugins - {Object without require}: css 1`] = `
"a { color: rgba(255, 0, 0, 1.0) }
"a { color: black }
"
`;
Expand Down
10 changes: 5 additions & 5 deletions test/options/__snapshots__/sourceMap.test.js.snap
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Options Sourcemap should work Sourcemap - {Boolean}: css 1`] = `
"a { color: rgba(255, 0, 0, 1.0) }
"a { color: black }
"
`;

Expand All @@ -10,7 +10,7 @@ exports[`Options Sourcemap should work Sourcemap - {Boolean}: errors 1`] = `Arra
exports[`Options Sourcemap should work Sourcemap - {Boolean}: map 1`] = `
Object {
"file": "x",
"mappings": "AAAA,IAAI,4BAAa",
"mappings": "AAAA,IAAI,aAAa",
"names": Array [],
"sources": Array [
"xxx",
Expand All @@ -26,17 +26,17 @@ Object {
exports[`Options Sourcemap should work Sourcemap - {Boolean}: warnings 1`] = `Array []`;

exports[`Options Sourcemap should work Sourcemap - {String}: css 1`] = `
"a { color: rgba(255, 0, 0, 1.0) }
"a { color: black }
/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QvZml4dHVyZXMvY3NzL3N0eWxlLmNzcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxJQUFJLDRCQUFhIiwiZmlsZSI6InRlc3QvZml4dHVyZXMvY3NzL3N0eWxlLmNzcyIsInNvdXJjZXNDb250ZW50IjpbImEgeyBjb2xvcjogYmxhY2sgfVxuIl19 */"
/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QvZml4dHVyZXMvY3NzL3N0eWxlLmNzcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxJQUFJLGFBQWEiLCJmaWxlIjoidGVzdC9maXh0dXJlcy9jc3Mvc3R5bGUuY3NzIiwic291cmNlc0NvbnRlbnQiOlsiYSB7IGNvbG9yOiBibGFjayB9XG4iXX0= */"
`;

exports[`Options Sourcemap should work Sourcemap - {String}: errors 1`] = `Array []`;

exports[`Options Sourcemap should work Sourcemap - {String}: warnings 1`] = `Array []`;

exports[`Options Sourcemap should work disable Sourcemap - {Boolean}: css 1`] = `
"a { color: rgba(255, 0, 0, 1.0) }
"a { color: black }
"
`;

Expand Down
63 changes: 43 additions & 20 deletions test/options/config.test.js
Expand Up @@ -12,7 +12,7 @@ const testDirectory = path.resolve(__dirname, '../fixtures', 'config-autoload');

describe('Config Options', () => {
it('should work Config - false', async () => {
const compiler = getCompiler('./css/index.js', {
const compiler = getCompiler('./config-scope/css/index.js', {
config: false,
});
const stats = await compile(compiler);
Expand All @@ -25,7 +25,7 @@ describe('Config Options', () => {
});

it('should work Config - true', async () => {
const compiler = getCompiler('./css/index.js', {
const compiler = getCompiler('./config-scope/css/index.js', {
config: true,
});
const stats = await compile(compiler);
Expand All @@ -38,8 +38,11 @@ describe('Config Options', () => {
});

it('should work Config - "string"', async () => {
const compiler = getCompiler('./css/index.js', {
config: path.resolve(__dirname, '../fixtures/css/custom.config.js'),
const compiler = getCompiler('./config-scope/css/index.js', {
config: path.resolve(
__dirname,
'../fixtures/config-scope/css/custom.config.js'
),
});
const stats = await compile(compiler);

Expand All @@ -51,8 +54,8 @@ describe('Config Options', () => {
});

it('should work Config - "string" with relative path', async () => {
const compiler = getCompiler('./css/index.js', {
config: 'test/fixtures/css/custom.config.js',
const compiler = getCompiler('./config-scope/css/index.js', {
config: 'test/fixtures/config-scope/css/custom.config.js',
});
const stats = await compile(compiler);

Expand All @@ -64,8 +67,8 @@ describe('Config Options', () => {
});

it('should work Config - "string" with path directory', async () => {
const compiler = getCompiler('./css/index.js', {
config: 'test/fixtures',
const compiler = getCompiler('./config-scope/css/index.js', {
config: 'test/fixtures/config-scope',
});
const stats = await compile(compiler);

Expand All @@ -77,9 +80,12 @@ describe('Config Options', () => {
});

it('should work Config - Object - path file', async () => {
const compiler = getCompiler('./css/index.js', {
const compiler = getCompiler('./config-scope/css/index.js', {
config: {
path: path.resolve(__dirname, '../fixtures/css/custom.config.js'),
path: path.resolve(
__dirname,
'../fixtures/config-scope/css/custom.config.js'
),
},
});
const stats = await compile(compiler);
Expand All @@ -92,7 +98,7 @@ describe('Config Options', () => {
});

it('should work Config - {Object}', async () => {
const compiler = getCompiler('./css/index.js', {});
const compiler = getCompiler('./config-scope/css/index.js', {});
const stats = await compile(compiler);

const codeFromBundle = getCodeFromBundle('style.css', stats);
Expand All @@ -103,8 +109,8 @@ describe('Config Options', () => {
});

it('should work Config - Path - {String}', async () => {
const compiler = getCompiler('./css/index.js', {
config: { path: 'test/fixtures/config/postcss.config.js' },
const compiler = getCompiler('./config-scope/css/index.js', {
config: { path: 'test/fixtures/config-scope/config/postcss.config.js' },
});
const stats = await compile(compiler);

Expand All @@ -116,9 +122,9 @@ describe('Config Options', () => {
});

it('should work Config - Context - {Object}', async () => {
const compiler = getCompiler('./css/index.js', {
const compiler = getCompiler('./config-scope/css/index.js', {
config: {
path: 'test/fixtures/config/postcss.config.js',
path: 'test/fixtures/config-scope/config/postcss.config.js',
ctx: { plugin: true },
},
});
Expand All @@ -134,7 +140,7 @@ describe('Config Options', () => {
it('should work Config – Context – Loader {Object}', async () => {
const compiler = getCompiler('./css/index.js', {
config: {
path: 'test/fixtures/config/context/postcss.config.js',
path: 'test/fixtures/config-scope/config/context/postcss.config.js',
},
});
const stats = await compile(compiler);
Expand Down Expand Up @@ -196,8 +202,11 @@ describe('Config Options', () => {
});

it('should emit error when unresolved config ', async () => {
const compiler = getCompiler('./css/index.js', {
config: path.resolve(__dirname, '../fixtures/css/unresolve.js'),
const compiler = getCompiler('./config-scope/css/index.js', {
config: path.resolve(
__dirname,
'../fixtures/config-scope/css/unresolve.js'
),
});
const stats = await compile(compiler);

Expand All @@ -206,12 +215,26 @@ describe('Config Options', () => {
});

it('should emit error when invalid config ', async () => {
const compiler = getCompiler('./css/index.js', {
config: path.resolve(__dirname, '../fixtures/css/invalid.config.js'),
const compiler = getCompiler('./config-scope/css/index.js', {
config: path.resolve(
__dirname,
'../fixtures/config-scope/css/invalid.config.js'
),
});
const stats = await compile(compiler);

expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats, true)).toMatchSnapshot('errors');
});

it('should work if Config not found', async () => {
const compiler = getCompiler('./css/index.js', {});
const stats = await compile(compiler);

const codeFromBundle = getCodeFromBundle('style.css', stats);

expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
});
6 changes: 2 additions & 4 deletions test/options/parser.test.js
Expand Up @@ -25,7 +25,7 @@ describe('Options Parser', () => {
},
{
loader: path.resolve(__dirname, '../../src'),
options: { parser: 'sugarss', config: false },
options: { parser: 'sugarss' },
},
],
},
Expand Down Expand Up @@ -62,7 +62,6 @@ describe('Options Parser', () => {
options: {
// eslint-disable-next-line global-require,import/no-dynamic-require
parser: require('sugarss'),
config: false,
},
},
],
Expand Down Expand Up @@ -100,7 +99,6 @@ describe('Options Parser', () => {
options: {
// eslint-disable-next-line global-require,import/no-dynamic-require
parser: require('sugarss').parse,
config: false,
},
},
],
Expand Down Expand Up @@ -134,7 +132,7 @@ describe('Options Parser', () => {
},
{
loader: path.resolve(__dirname, '../../src'),
options: { parser: 'unresolve', config: false },
options: { parser: 'unresolve' },
},
],
},
Expand Down
15 changes: 6 additions & 9 deletions test/options/plugins.test.js
Expand Up @@ -10,7 +10,7 @@ describe('Options Plugins', () => {
it('should work Plugins - {Array}', async () => {
const compiler = getCompiler('./css/index.js', {
// eslint-disable-next-line global-require
plugins: [require('../fixtures/config/plugin')()],
plugins: [require('../fixtures/config-scope/config/plugin')()],
});
const stats = await compile(compiler);

Expand All @@ -24,7 +24,7 @@ describe('Options Plugins', () => {
it('should work Plugins - {Object}', async () => {
const compiler = getCompiler('./css/index.js', {
// eslint-disable-next-line global-require
plugins: require('../fixtures/config/plugin'),
plugins: require('../fixtures/config-scope/config/plugin'),
});
const stats = await compile(compiler);

Expand All @@ -38,7 +38,7 @@ describe('Options Plugins', () => {
it('should work Plugins - {Function} - {Array}', async () => {
const compiler = getCompiler('./css/index.js', {
// eslint-disable-next-line global-require
plugins: () => [require('../fixtures/config/plugin')()],
plugins: () => [require('../fixtures/config-scope/config/plugin')()],
});
const stats = await compile(compiler);

Expand All @@ -52,7 +52,7 @@ describe('Options Plugins', () => {
it('should work Plugins - {Function} - {Object}', async () => {
const compiler = getCompiler('./css/index.js', {
// eslint-disable-next-line global-require
plugins: () => require('../fixtures/config/plugin')(),
plugins: () => require('../fixtures/config-scope/config/plugin')(),
});
const stats = await compile(compiler);

Expand Down Expand Up @@ -80,7 +80,7 @@ describe('Options Plugins', () => {
});

it('should work Plugins - {empty Object}', async () => {
const compiler = getCompiler('./css/index.js', {
const compiler = getCompiler('./config-scope/css/index.js', {
plugins: {},
});
const stats = await compile(compiler);
Expand All @@ -97,7 +97,6 @@ describe('Options Plugins', () => {
plugins: {
'postcss-short': { prefix: 'x' },
},
config: false,
});
const stats = await compile(compiler);

Expand All @@ -112,7 +111,6 @@ describe('Options Plugins', () => {
const compiler = getCompiler('./css/index2.js', {
// eslint-disable-next-line global-require
plugins: require('postcss-short')({ prefix: 'x' }),
config: false,
});
const stats = await compile(compiler);

Expand All @@ -127,7 +125,6 @@ describe('Options Plugins', () => {
const compiler = getCompiler('./css/index2.js', {
// eslint-disable-next-line global-require
plugins: [require('postcss-short')({ prefix: 'x' })],
config: false,
});
const stats = await compile(compiler);

Expand All @@ -140,7 +137,7 @@ describe('Options Plugins', () => {

it('should disables plugin from config', async () => {
const compiler = getCompiler('./css/index2.js', {
config: 'test/fixtures/css/plugins.config.js',
config: 'test/fixtures/config-scope/css/plugins.config.js',
plugins: {
'postcss-short': false,
},
Expand Down

0 comments on commit 2cd7614

Please sign in to comment.