Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port to ESM #401

Merged
merged 4 commits into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
env:
node: true
parserOptions:
sourceType: module
extends: problems
rules:
no-console: off
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,3 @@ jobs:
node-version: ${{ matrix.node }}
- run: npm install
- run: npm run ci
- run: nyc report --reporter=text-lcov | coveralls
continue-on-error: true
3 changes: 0 additions & 3 deletions bin/postcss

This file was deleted.

90 changes: 50 additions & 40 deletions index.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,49 +1,59 @@
'use strict'

const fs = require('fs-extra')
const path = require('path')

const prettyHrtime = require('pretty-hrtime')
const stdin = require('get-stdin')
const read = require('read-cache')
const { bold, dim, red, cyan, green } = require('colorette')
const globber = require('globby')
const slash = require('slash')
const chokidar = require('chokidar')

const postcss = require('postcss')
const postcssrc = require('postcss-load-config')
const reporter = require('postcss-reporter/lib/formatter')()

const argv = require('./lib/args')
const createDependencyGraph = require('./lib/DependencyGraph')
const getMapfile = require('./lib/getMapfile')
#!/usr/bin/env node

import fs from 'fs-extra'
import path from 'path'

import prettyHrtime from 'pretty-hrtime'
import stdin from 'get-stdin'
import read from 'read-cache'
import { bold, dim, red, cyan, green } from 'colorette'
import globber from 'globby'
import slash from 'slash'
import chokidar from 'chokidar'

import postcss from 'postcss'
import postcssrc from 'postcss-load-config'
import postcssReporter from 'postcss-reporter/lib/formatter.js'

import argv from './lib/args.js'
import createDependencyGraph from './lib/DependencyGraph.js'
import getMapfile from './lib/getMapfile.js'

const reporter = postcssReporter()
const depGraph = createDependencyGraph()

let input = argv._
const { dir, output } = argv

if (argv.map) argv.map = { inline: false }

const cliConfig = {
options: {
map: argv.map !== undefined ? argv.map : { inline: true },
parser: argv.parser ? require(argv.parser) : undefined,
syntax: argv.syntax ? require(argv.syntax) : undefined,
stringifier: argv.stringifier ? require(argv.stringifier) : undefined,
},
plugins: argv.use
? argv.use.map((plugin) => {
try {
return require(plugin)()
} catch (e) {
const msg = e.message || `Cannot find module '${plugin}'`
let prefix = msg.includes(plugin) ? '' : ` (${plugin})`
if (e.name && e.name !== 'Error') prefix += `: ${e.name}`
return error(`Plugin Error${prefix}: ${msg}'`)
}
})
: [],
let cliConfig

async function buildCliConfig() {
cliConfig = {
options: {
map: argv.map !== undefined ? argv.map : { inline: true },
parser: argv.parser ? await import(argv.parser) : undefined,
syntax: argv.syntax ? await import(argv.syntax) : undefined,
stringifier: argv.stringifier
? await import(argv.stringifier)
: undefined,
},
plugins: argv.use
? await Promise.all(
argv.use.map(async (plugin) => {
try {
return (await import(plugin)).default()
} catch (e) {
const msg = e.message || `Cannot find module '${plugin}'`
let prefix = msg.includes(plugin) ? '' : ` (${plugin})`
if (e.name && e.name !== 'Error') prefix += `: ${e.name}`
return error(`Plugin Error${prefix}: ${msg}'`)
}
})
)
: [],
}
}

let configFile
Expand All @@ -61,7 +71,7 @@ if (parseInt(postcss().version) < 8) {
error('Please install PostCSS 8 or above')
}

Promise.resolve()
buildCliConfig()
.then(() => {
if (argv.watch && !(argv.output || argv.replace || argv.dir)) {
error('Cannot write to stdout in watch mode')
Expand Down
7 changes: 3 additions & 4 deletions lib/DependencyGraph.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict'
const path = require('path')
const { DepGraph } = require('dependency-graph')
import path from 'path'
import { DepGraph } from 'dependency-graph'

module.exports = function () {
export default function createDependencyGraph() {
const graph = new DepGraph()
return {
add(message) {
Expand Down
7 changes: 3 additions & 4 deletions lib/DependencyGraph.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'
const test = require('ava')
const path = require('path')
const createDependencyGraph = require('./DependencyGraph.js')
import test from 'ava'
import path from 'path'
import createDependencyGraph from './DependencyGraph.js'

function resolveArray(arr) {
return arr.map((p) => path.resolve(p))
Expand Down
8 changes: 4 additions & 4 deletions lib/args.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict'
const { bold, red } = require('colorette')
import yargs from 'yargs'
import { bold, red } from 'colorette'

const logo = `
/|\\
Expand All @@ -13,7 +13,7 @@ const logo = `
//_____||___*_________*___||_____//
`

const { argv } = require('yargs')
const { argv } = yargs(process.argv.slice(2))
.usage(
`${bold(red(logo))}
Usage:
Expand Down Expand Up @@ -126,4 +126,4 @@ For more details, please see https://github.com/postcss/postcss-cli`

if (argv.ext && argv.ext.indexOf('.') !== 0) argv.ext = `.${argv.ext}`

module.exports = argv
export default argv
5 changes: 2 additions & 3 deletions lib/getMapfile.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'
const path = require('path')
module.exports = function getMapfile(options) {
import path from 'path'
export default function getMapfile(options) {
if (options.map && typeof options.map.annotation === 'string') {
return `${path.dirname(options.to)}/${options.map.annotation}`
}
Expand Down
27 changes: 27 additions & 0 deletions lib/getMapfile.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import test from 'ava'
import getMapfile from './getMapfile.js'

test('mapFile path is properly resolved', async (t) => {
const paths = [
{
input: { to: '/foo/bar.css/baz/index.css' },
want: '/foo/bar.css/baz/index.css.map',
},
{
input: { to: '/foo/bar.sss/baz/index.sss' },
want: '/foo/bar.sss/baz/index.sss.map',
},
{
input: { to: '/foo/bar.css/baz/bar.css' },
want: '/foo/bar.css/baz/bar.css.map',
},
{
input: { map: { annotation: 'foo.map' }, to: '/foo/bar.css/baz/bar.css' },
want: '/foo/bar.css/baz/foo.map',
},
]

for (const p of paths) {
t.is(getMapfile(p.input), p.want)
}
})
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
"name": "postcss-cli",
"version": "8.3.1",
"description": "CLI for PostCSS",
"main": "index.js",
"type": "module",
"engines": {
"node": ">=12"
},
"bin": {
"postcss": "./bin/postcss"
"postcss": "./index.js"
},
"scripts": {
"ci": "eslint . && nyc ava -v && npm run prettier -- --list-different",
"ci": "eslint . && c8 ava -v && npm run prettier -- --list-different",
"clean": "node test/helpers/clean.js",
"prettier": "prettier --single-quote --no-semi \"**/*.{js,md}\"",
"format": "npm run prettier -- --write && eslint . --fix",
"pretest": "npm run clean && npm run format",
"test": "nyc ava -v"
"test": "c8 ava -v"
},
"dependencies": {
"chokidar": "^3.3.0",
Expand All @@ -33,10 +33,10 @@
},
"devDependencies": {
"ava": "^3.1.0",
"c8": "^7.9.0",
"coveralls": "^3.0.0",
"eslint": "^7.8.0",
"eslint-config-problems": "5.0.0",
"nyc": "^15.0.0",
"postcss": "^8.0.4",
"postcss-import": "^14.0.0",
"prettier": "~2.4.0",
Expand Down
11 changes: 5 additions & 6 deletions test/base.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use strict'
const test = require('ava')
const path = require('path')
import test from 'ava'
import path from 'path'

const cli = require('./helpers/cli.js')
const tmp = require('./helpers/tmp.js')
const read = require('./helpers/read.js')
import cli from './helpers/cli.js'
import tmp from './helpers/tmp.js'
import read from './helpers/read.js'

test('--base --dir works', async (t) => {
const dir = tmp()
Expand Down
9 changes: 4 additions & 5 deletions test/cli.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
'use strict'
const test = require('ava')
import test from 'ava'

const cli = require('./helpers/cli.js')
const tmp = require('./helpers/tmp.js')
const read = require('./helpers/read.js')
import cli from './helpers/cli.js'
import tmp from './helpers/tmp.js'
import read from './helpers/read.js'

test('works with defaults', async (t) => {
const output = tmp('output.css')
Expand Down
11 changes: 5 additions & 6 deletions test/config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict'
const test = require('ava')
const path = require('path')
import test from 'ava'
import path from 'path'

const ENV = require('./helpers/env.js')
import ENV from './helpers/env.js'

const cli = require('./helpers/cli.js')
const read = require('./helpers/read.js')
import cli from './helpers/cli.js'
import read from './helpers/read.js'

test('supports common config', async (t) => {
const env = `module.exports = {
Expand Down
11 changes: 5 additions & 6 deletions test/dir.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use strict'
const test = require('ava')
const path = require('path')
import test from 'ava'
import path from 'path'

const cli = require('./helpers/cli.js')
const tmp = require('./helpers/tmp.js')
const read = require('./helpers/read.js')
import cli from './helpers/cli.js'
import tmp from './helpers/tmp.js'
import read from './helpers/read.js'

test('--dir works', async (t) => {
const dir = tmp()
Expand Down
13 changes: 6 additions & 7 deletions test/error.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
'use strict'
const test = require('ava')
import test from 'ava'

const tmp = require('./helpers/tmp.js')
const cli = require('./helpers/cli.js')
import tmp from './helpers/tmp.js'
import cli from './helpers/cli.js'

test('multiple input files && --output', (t) => {
return cli(['test/fixtures/*.css', '-o', tmp()]).then(({ error, code }) => {
Expand Down Expand Up @@ -34,7 +33,7 @@ test('plugin not found', (t) => {
t.is(code, 1, 'expected non-zero error code')
t.regex(
error.toString(),
/Plugin Error: Cannot find module 'postcss-plugin'/
/Plugin Error: Cannot find package 'postcss-plugin'/
)
}
)
Expand All @@ -44,12 +43,12 @@ test('plugin throws on require', (t) => {
return cli([
'test/fixtures/a.css',
'-u',
'./test/fixtures/_bad-plugin',
'./test/fixtures/_bad-plugin.js',
'-o',
tmp(),
]).then(({ error, code }) => {
t.is(code, 1, 'expected non-zero error code')
t.regex(error.toString(), /Plugin Error \(.*bad-plugin\): This fails/)
t.regex(error.toString(), /Plugin Error \(.*bad-plugin.js\): This fails/)
})
})

Expand Down
11 changes: 5 additions & 6 deletions test/ext.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict'
const test = require('ava')
import test from 'ava'

const fs = require('fs-extra')
const path = require('path')
import fs from 'fs-extra'
import path from 'path'

const cli = require('./helpers/cli.js')
const tmp = require('./helpers/tmp.js')
import cli from './helpers/cli.js'
import tmp from './helpers/tmp.js'

test('--ext works', async (t) => {
const dir = tmp()
Expand Down
1 change: 0 additions & 1 deletion test/fixtures/_bad-plugin.js
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
'use strict'
throw new Error('This fails')
11 changes: 5 additions & 6 deletions test/glob.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use strict'
const test = require('ava')
const path = require('path')
import test from 'ava'
import path from 'path'

const cli = require('./helpers/cli.js')
const tmp = require('./helpers/tmp.js')
const read = require('./helpers/read.js')
import cli from './helpers/cli.js'
import tmp from './helpers/tmp.js'
import read from './helpers/read.js'

test('works with glob patterns', async (t) => {
const output = tmp()
Expand Down
4 changes: 1 addition & 3 deletions test/helpers/clean.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict'
const fs = require('fs-extra')
import fs from 'fs-extra'

Promise.all([
fs.emptyDir('./test/fixtures/.tmp/'),
fs.remove('./coverage'),
fs.remove('./.nyc_output'),
]).catch((err) => {
console.error(err)
process.exit(1)
Expand Down
Loading