diff --git a/src/utils.js b/src/utils.js index a3f922b6..ceec88b4 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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); diff --git a/test/fixtures/config/context/plugin.js b/test/fixtures/config-scope/config/context/plugin.js similarity index 100% rename from test/fixtures/config/context/plugin.js rename to test/fixtures/config-scope/config/context/plugin.js diff --git a/test/fixtures/config/context/postcss.config.js b/test/fixtures/config-scope/config/context/postcss.config.js similarity index 100% rename from test/fixtures/config/context/postcss.config.js rename to test/fixtures/config-scope/config/context/postcss.config.js diff --git a/test/fixtures/config/plugin.js b/test/fixtures/config-scope/config/plugin.js similarity index 100% rename from test/fixtures/config/plugin.js rename to test/fixtures/config-scope/config/plugin.js diff --git a/test/fixtures/config/postcss.config.js b/test/fixtures/config-scope/config/postcss.config.js similarity index 100% rename from test/fixtures/config/postcss.config.js rename to test/fixtures/config-scope/config/postcss.config.js diff --git a/test/fixtures/css/custom.config.js b/test/fixtures/config-scope/css/custom.config.js similarity index 100% rename from test/fixtures/css/custom.config.js rename to test/fixtures/config-scope/css/custom.config.js diff --git a/test/fixtures/config-scope/css/index.js b/test/fixtures/config-scope/css/index.js new file mode 100644 index 00000000..61e122b2 --- /dev/null +++ b/test/fixtures/config-scope/css/index.js @@ -0,0 +1,3 @@ +import style from './style.css' + +export default style diff --git a/test/fixtures/config-scope/css/index2.js b/test/fixtures/config-scope/css/index2.js new file mode 100644 index 00000000..9415e9b0 --- /dev/null +++ b/test/fixtures/config-scope/css/index2.js @@ -0,0 +1,3 @@ +import style from './style2.css' + +export default style diff --git a/test/fixtures/css/invalid.config.js b/test/fixtures/config-scope/css/invalid.config.js similarity index 100% rename from test/fixtures/css/invalid.config.js rename to test/fixtures/config-scope/css/invalid.config.js diff --git a/test/fixtures/config-scope/css/plugins.config.js b/test/fixtures/config-scope/css/plugins.config.js new file mode 100644 index 00000000..47734fec --- /dev/null +++ b/test/fixtures/config-scope/css/plugins.config.js @@ -0,0 +1,7 @@ +module.exports = { + plugins: { + 'postcss-short': { prefix: 'x' }, + 'postcss-import': {}, + 'postcss-nested': {}, + } +} diff --git a/test/fixtures/config-scope/css/style.css b/test/fixtures/config-scope/css/style.css new file mode 100644 index 00000000..fa33ad5f --- /dev/null +++ b/test/fixtures/config-scope/css/style.css @@ -0,0 +1 @@ +a { color: black } diff --git a/test/fixtures/config-scope/css/style2.css b/test/fixtures/config-scope/css/style2.css new file mode 100644 index 00000000..3bab8470 --- /dev/null +++ b/test/fixtures/config-scope/css/style2.css @@ -0,0 +1,4 @@ +a { + -x-border-color: blue blue *; + -x-color: * #fafafa; +} diff --git a/test/fixtures/postcss.config.js b/test/fixtures/config-scope/postcss.config.js similarity index 100% rename from test/fixtures/postcss.config.js rename to test/fixtures/config-scope/postcss.config.js diff --git a/test/options/__snapshots__/config.test.js.snap b/test/options/__snapshots__/config.test.js.snap index 84e5c4fc..8ccf858d 100644 --- a/test/options/__snapshots__/config.test.js.snap +++ b/test/options/__snapshots__/config.test.js.snap @@ -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", ] `; @@ -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", ] `; @@ -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; diff --git a/test/options/__snapshots__/plugins.test.js.snap b/test/options/__snapshots__/plugins.test.js.snap index eb29f24c..3534e56b 100644 --- a/test/options/__snapshots__/plugins.test.js.snap +++ b/test/options/__snapshots__/plugins.test.js.snap @@ -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 } " `; diff --git a/test/options/__snapshots__/sourceMap.test.js.snap b/test/options/__snapshots__/sourceMap.test.js.snap index 4b8c3977..d9f7b6aa 100644 --- a/test/options/__snapshots__/sourceMap.test.js.snap +++ b/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 } " `; @@ -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", @@ -26,9 +26,9 @@ 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 []`; @@ -36,7 +36,7 @@ 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 } " `; diff --git a/test/options/config.test.js b/test/options/config.test.js index df3900df..16fcf102 100644 --- a/test/options/config.test.js +++ b/test/options/config.test.js @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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 }, }, }); @@ -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); @@ -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); @@ -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'); + }); }); diff --git a/test/options/parser.test.js b/test/options/parser.test.js index cdb2175e..2ae63c1c 100644 --- a/test/options/parser.test.js +++ b/test/options/parser.test.js @@ -25,7 +25,7 @@ describe('Options Parser', () => { }, { loader: path.resolve(__dirname, '../../src'), - options: { parser: 'sugarss', config: false }, + options: { parser: 'sugarss' }, }, ], }, @@ -62,7 +62,6 @@ describe('Options Parser', () => { options: { // eslint-disable-next-line global-require,import/no-dynamic-require parser: require('sugarss'), - config: false, }, }, ], @@ -100,7 +99,6 @@ describe('Options Parser', () => { options: { // eslint-disable-next-line global-require,import/no-dynamic-require parser: require('sugarss').parse, - config: false, }, }, ], @@ -134,7 +132,7 @@ describe('Options Parser', () => { }, { loader: path.resolve(__dirname, '../../src'), - options: { parser: 'unresolve', config: false }, + options: { parser: 'unresolve' }, }, ], }, diff --git a/test/options/plugins.test.js b/test/options/plugins.test.js index 6021b108..2e7d5de3 100644 --- a/test/options/plugins.test.js +++ b/test/options/plugins.test.js @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -97,7 +97,6 @@ describe('Options Plugins', () => { plugins: { 'postcss-short': { prefix: 'x' }, }, - config: false, }); const stats = await compile(compiler); @@ -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); @@ -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); @@ -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, }, diff --git a/test/options/sourceMap.test.js b/test/options/sourceMap.test.js index 9ada77a0..d3574dbc 100644 --- a/test/options/sourceMap.test.js +++ b/test/options/sourceMap.test.js @@ -56,9 +56,7 @@ describe('Options Sourcemap', () => { it('should work with prev sourceMap', async () => { const compiler = getCompiler( './scss/index.js', - { - config: false, - }, + {}, { devtool: 'source-map', module: { @@ -72,9 +70,7 @@ describe('Options Sourcemap', () => { }, { loader: path.resolve(__dirname, '../../src'), - options: { - config: false, - }, + options: {}, }, { loader: 'sass-loader', diff --git a/test/options/stringifier.test.js b/test/options/stringifier.test.js index 45cd8963..5b2fa09a 100644 --- a/test/options/stringifier.test.js +++ b/test/options/stringifier.test.js @@ -10,7 +10,6 @@ describe('Options Stringifier', () => { it('should work Stringifier - {String}', async () => { const compiler = getCompiler('./css/index.js', { stringifier: 'sugarss', - config: false, }); const stats = await compile(compiler); @@ -25,7 +24,6 @@ describe('Options Stringifier', () => { const compiler = getCompiler('./css/index.js', { // eslint-disable-next-line global-require stringifier: require('sugarss'), - config: false, }); const stats = await compile(compiler); @@ -44,7 +42,6 @@ describe('Options Stringifier', () => { const compiler = getCompiler('./css/index.js', { // eslint-disable-next-line global-require stringifier: midas.stringifier, - config: false, }); const stats = await compile(compiler); @@ -59,7 +56,6 @@ describe('Options Stringifier', () => { const compiler = getCompiler('./css/index.js', { // eslint-disable-next-line global-require stringifier: 'unresolved', - config: false, }); const stats = await compile(compiler); diff --git a/test/options/syntax.test.js b/test/options/syntax.test.js index 576d84f0..eb2471f6 100644 --- a/test/options/syntax.test.js +++ b/test/options/syntax.test.js @@ -25,7 +25,7 @@ describe('Options Syntax', () => { }, { loader: path.resolve(__dirname, '../../src'), - options: { syntax: 'sugarss', config: false }, + options: { syntax: 'sugarss' }, }, ], }, @@ -61,7 +61,6 @@ describe('Options Syntax', () => { options: { // eslint-disable-next-line global-require syntax: require('sugarss'), - config: false, }, }, ], @@ -96,7 +95,7 @@ describe('Options Syntax', () => { }, { loader: path.resolve(__dirname, '../../src'), - options: { syntax: 'unresolve', config: false }, + options: { syntax: 'unresolve' }, }, ], }, diff --git a/test/validate-options.test.js b/test/validate-options.test.js index 34dd77bd..8ecff34e 100644 --- a/test/validate-options.test.js +++ b/test/validate-options.test.js @@ -7,14 +7,14 @@ describe('validate options', () => { const tests = { config: { success: [ - { path: 'test/fixtures/config/postcss.config.js' }, + { path: 'test/fixtures/config-scope/config/postcss.config.js' }, { - path: 'test/fixtures/config/postcss.config.js', + path: 'test/fixtures/config-scope/config/postcss.config.js', ctx: { plugin: true }, }, true, false, - 'test/fixtures/config/postcss.config.js', + 'test/fixtures/config-scope/config/postcss.config.js', ], failure: [[], { foo: 'bar' }], }, @@ -40,9 +40,9 @@ describe('validate options', () => { }, plugins: { success: [ - [require('./fixtures/config/plugin')()], - require('./fixtures/config/plugin'), - () => [require('./fixtures/config/plugin')()], + [require('./fixtures/config-scope/config/plugin')()], + require('./fixtures/config-scope/config/plugin'), + () => [require('./fixtures/config-scope/config/plugin')()], ], failure: [1, true, false], },