Skip to content

Commit

Permalink
feat: Support loading ECMAScript modules, but drop sync API
Browse files Browse the repository at this point in the history
  • Loading branch information
michael42 committed Apr 6, 2022
1 parent b43f8de commit c8a7e26
Show file tree
Hide file tree
Showing 18 changed files with 160 additions and 243 deletions.
17 changes: 0 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,6 @@ module.exports = (ctx) => ({
}
```

### `Async`

```js
const { readFileSync } = require('fs')

Expand All @@ -343,21 +341,6 @@ postcssrc(ctx).then(({ plugins, options }) => {
})
```

### `Sync`

```js
const { readFileSync } = require('fs')

const postcss = require('postcss')
const postcssrc = require('postcss-load-config')

const css = readFileSync('index.sss', 'utf8')

const ctx = { parser: true, map: 'inline' }

const { plugins, options } = postcssrc.sync(ctx)
```

<div align="center">
<img width="80" height="80" halign="10" src="https://worldvectorlogo.com/logos/gulp.svg">
</div>
Expand Down
6 changes: 0 additions & 6 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ declare function postcssrc(
): Promise<postcssrc.Result>;

declare namespace postcssrc {
function sync(
ctx?: ConfigContext,
path?: string,
options?: ConfigOptions
): Result;

// In the ConfigContext, these three options can be instances of the
// appropriate class, or strings. If they are strings, postcss-load-config will
// require() them and pass the instances along.
Expand Down
40 changes: 19 additions & 21 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ const createContext = (ctx) => {
return ctx
}

const importOrRequire = async filepath => {
try {
return (await import(filepath)).default
} catch (error) {
// Node.js 10 does not support dynamic import(...) and throws 'Error: Not supported'.
// Can be removed when only Node.js >= 12 is supported.
if (error.message === 'Not supported') {
return require(filepath)
}
throw error
}
}

const addTypeScriptLoader = (options = {}, loader) => {
const moduleName = 'postcss'

Expand All @@ -81,14 +94,19 @@ const addTypeScriptLoader = (options = {}, loader) => {
`.${moduleName}rc.ts`,
`.${moduleName}rc.js`,
`.${moduleName}rc.cjs`,
`.${moduleName}rc.mjs`,
`${moduleName}.config.ts`,
`${moduleName}.config.js`,
`${moduleName}.config.cjs`
`${moduleName}.config.cjs`,
`${moduleName}.config.mjs`
],
loaders: {
...options.loaders,
'.yaml': (filepath, content) => yaml.parse(content),
'.yml': (filepath, content) => yaml.parse(content),
'.js': importOrRequire,
'.cjs': importOrRequire,
'.mjs': importOrRequire,
'.ts': loader
}
}
Expand Down Expand Up @@ -152,26 +170,6 @@ const rc = withTypeScriptLoader((ctx, path, options) => {
})
})

rc.sync = withTypeScriptLoader((ctx, path, options) => {
/**
* @type {Object} The full Config Context
*/
ctx = createContext(ctx)

/**
* @type {String} `process.cwd()`
*/
path = path ? resolve(path) : process.cwd()

const result = config.lilconfigSync('postcss', options).search(path)

if (!result) {
throw new Error(`No PostCSS Config found in: ${path}`)
}

return processResult(ctx, result)
})

/**
* Autoload Config for PostCSS
*
Expand Down
80 changes: 0 additions & 80 deletions test/Errors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@ test('Loading Config - {Error}', () => {
})
})

test('Loading Config - Sync - {Error}', () => {
try {
postcssrc.sync({}, 'test/err')
} catch (err) {
match(err.message, /^No PostCSS Config found in: (.*)$/)
}
})

describe('Loading Plugins - {Error}', test => {
test('Plugin - {Type} - Invalid', () => {
return postcssrc({}, 'test/err/plugins').catch(err => {
Expand Down Expand Up @@ -52,50 +44,6 @@ describe('Loading Plugins - {Error}', test => {
test.run()
})

describe('Loading Plugins - Sync - {Error}', test => {
test('Plugin - {Type} - Invalid', () => {
try {
postcssrc.sync({}, 'test/err/plugins')
} catch (err) {
match(err.message, /^Invalid PostCSS Plugin found at: (.*)\n\n\(@.*\)$/)
}
})

test('Plugin - {Object}', () => {
try {
postcssrc.sync({}, 'test/err/plugins/object')
} catch (err) {
match(err.message, /^Loading PostCSS Plugin failed: .*$/m)
}
})

test('Plugin - {Object} - Options', () => {
try {
postcssrc.sync({}, 'test/err/plugins/object/options')
} catch (err) {
match(err.message, /^Loading PostCSS Plugin failed: .*$/m)
}
})

test('Plugin - {Array}', () => {
try {
postcssrc.sync({}, 'test/err/plugins/array')
} catch (err) {
match(err.message, /^Cannot find (.*)$/m)
}
})

test('Plugin - {Array} - Options', () => {
try {
postcssrc.sync({}, 'test/err/plugins/array/options')
} catch (err) {
match(err.message, /^Cannot find (.*)$/m)
}
})

test.run()
})

describe('Loading Options - {Error}', test => {
test('Parser - {String}', () => {
return postcssrc({}, 'test/err/options/parser').catch(err => {
Expand All @@ -118,32 +66,4 @@ describe('Loading Options - {Error}', test => {
test.run()
})

describe('Loading Options - Sync - {Error}', test => {
test('Parser - {String}', () => {
try {
postcssrc.sync({}, 'test/err/options/parser')
} catch (err) {
match(err.message, /^Loading PostCSS Parser failed: .*$/m)
}
})

test('Syntax - {String}', () => {
try {
postcssrc.sync({}, 'test/err/options/syntax')
} catch (err) {
match(err.message, /^Loading PostCSS Syntax failed: .*$/m)
}
})

test('Stringifier - {String}', () => {
try {
postcssrc.sync({}, 'test/err/options/stringifier')
} catch (err) {
match(err.message, /^Loading PostCSS Stringifier failed: .*$/m)
}
})

test.run()
})

test.run()
Loading

0 comments on commit c8a7e26

Please sign in to comment.