Skip to content
This repository has been archived by the owner on Dec 28, 2021. It is now read-only.

Commit

Permalink
8.0.0 (#75)
Browse files Browse the repository at this point in the history
* 8.0.0

* fix: Node v12 compatibility

* feat: add support for @container

* feat: postcss-7 compatibility
  • Loading branch information
jonathantneal committed Apr 27, 2021
1 parent 52ec2d9 commit 64ba9ac
Show file tree
Hide file tree
Showing 39 changed files with 567 additions and 244 deletions.
92 changes: 92 additions & 0 deletions .bin/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import esbuild from 'esbuild'
import fs from 'node:fs/promises'
import zlib from 'node:zlib'

const variants = {
esm: {
extension: 'mjs',
transform(code, exports) {
const esmExports = []
for (const name in exports) esmExports.push(`${exports[name]} as ${name}`)
return `${code}export{${esmExports.join(',')}}`
},
},
cjs: {
extension: 'cjs',
transform(code, exports) {
const cjsExports = ['__esModule:!0']
for (const name in exports) cjsExports.push(`${name}:${exports[name]}`)
return `${code}module.exports={${cjsExports.join(',')}}`
},
},
}

async function buildPackage(src) {
const packageUrl = src
const initPackageUrl = new URL('src/', packageUrl)
const distPackageUrl = new URL('dist/', packageUrl)

const packageJsonUrl = new URL(`package.json`, packageUrl)
const packageName = JSON.parse(await fs.readFile(packageJsonUrl, 'utf8')).name

console.log(packageName)
console.log()

const targetPathname = new URL('index.js', initPackageUrl).pathname
const outputPathname = new URL('index.js', distPackageUrl).pathname

// Build ESM version
const {
outputFiles: [cmapResult, codeResult],
} = await esbuild.build({
entryPoints: [targetPathname],
outfile: outputPathname,
bundle: true,
format: 'esm',
sourcemap: 'external',
external: ['postcss'],
write: false,
})

const code = codeResult.text
const map = cmapResult.text

// ensure empty dist directory
await fs.mkdir(distPackageUrl, { recursive: true })

// write map
fs.writeFile(new URL(`index.map`, distPackageUrl), map)

// prepare variations
const splitByExport = (code, index = code.indexOf('export')) => [code.slice(0, index), code.slice(index)]
const [lead, tail] = splitByExport(code)

const exports = Array.from(tail.matchAll(/([$\w]+) as (\w+)/g)).reduce((exports, each) => Object.assign(exports, { [each[2]]: each[1] }), Object.create(null))

// write variation builds
for (const variant in variants) {
const variantInfo = variants[variant]
const variantPath = new URL(`dist/index.${variantInfo.extension}`, packageUrl).pathname
const variantCode = variantInfo.transform(lead, exports)
const variantSize = (Buffer.byteLength(variantCode, 'utf8') / 1000).toFixed()
const variantGzip = Number((zlib.gzipSync(variantCode, { level: 9 }).length / 1000).toFixed(2))

console.log(' ', `\x1b[33m${variantSize} kB\x1b[0m \x1b[2m/\x1b[0m \x1b[33m${variantGzip} kB\x1b[0m`, `\x1b[2m(${variant})\x1b[0m`)

await fs.writeFile(variantPath, variantCode + `\n//# sourceMappingUrl=index.map`)
}
}

export default buildPackage

const metaUrl = new URL(import.meta.url)
const argvUrl = new URL(process.argv[1], 'file:')

if (metaUrl.href === argvUrl.href) {
/** Root directory. */
const rootUrl = new URL('../', metaUrl)

console.log()

await buildPackage(rootUrl)
}
35 changes: 35 additions & 0 deletions .bin/test-lint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import process from 'node:process'
import { ESLint } from 'eslint'

export default async function lint() {
try {
const eslint = new ESLint({ fix: true })

const results = await eslint.lintFiles(['src/**/*.js'])

await ESLint.outputFixes(results)

const formatter = await eslint.loadFormatter('stylish')
const resultText = formatter.format(results)

console.log(`\x1b[42m\x1b[30m PASS \x1b[0m`, 'linting')

if (resultText) {
console.log(resultText)
}

return true
} catch (error) {
console.error(`\x1b[41m\x1b[30m FAIL \x1b[0m`, 'linting')
console.error(error)

return false
}
}

if (new URL(process.argv[1], 'file:').href === import.meta.url) {
lint().then(
process.exit.bind(process, 0),
process.exit.bind(process, 1)
)
}
58 changes: 58 additions & 0 deletions .bin/test-tape.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { strict as assert } from 'node:assert'
import { promises as fs } from 'node:fs'
import postcss from 'postcss'
import process from 'node:process'

import plugin from '../src/index.js'

let workingUrl = new URL(`${process.cwd()}/`, `file:`)

export default async function tape() {
let failures = 0

failures += await test('supports basic usage', { basename: 'basic' })
failures += await test('supports at-rule usage', { basename: 'at-rule' })
failures += await test('supports direct usage', { basename: 'direct' })
failures += await test('removes empty rules', { basename: 'empty' })
failures += await test('supports nested @container', { basename: 'container' })
failures += await test('supports nested @media', { basename: 'media' })
failures += await test('ignores invalid entries', { basename: 'ignore' })
failures += await test('supports complex entries', { basename: 'complex' })

return failures === 0
}

async function test(name, init) {
const { basename } = Object(init)

let sourceUrl = new URL(`test/${basename}.css`, workingUrl)
let expectUrl = new URL(`test/${basename}.expect.css`, workingUrl)
let resultUrl = new URL(`test/${basename}.result.css`, workingUrl)

let plugins = [plugin]

let sourceCss = await fs.readFile(sourceUrl, 'utf8')
let expectCss = await fs.readFile(expectUrl, 'utf8')
let resultCss = await postcss(plugins).process(sourceCss, { from: sourceCss, to: resultUrl }).css

try {
assert.equal(resultCss, expectCss)

console.log(`\x1b[42m\x1b[30m PASS \x1b[0m`, name)

return 0
} catch (error) {
console.error(`\x1b[41m\x1b[30m FAIL \x1b[0m`, name)
console.error(error.stack)

return 1
}
}


if (new URL(process.argv[1], 'file:').href === import.meta.url) {
tape().then(
() => process.exit(0),
() => process.exit(1)
)
}
26 changes: 26 additions & 0 deletions .bin/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import process from 'node:process'
import lint from './test-lint.js'
import tape from './test-tape.js'

export default async function test() {
let failures = 0
try {
if (!await lint()) {
++failures
}
if (!await tape()) {
++failures
}
} catch(error) {
++failures
}

return failures === 0
}

if (new URL(process.argv[1], 'file:').href === import.meta.url) {
test().then(
process.exit.bind(process, 0),
process.exit.bind(process, 1)
)
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
dist
node_modules
index.*.*
package-lock.json
yarn.lock
*.log*
*.result.css
.*
!.bin
!.editorconfig
!.gitignore
!.rollup.js
Expand Down
20 changes: 0 additions & 20 deletions .tape.js

This file was deleted.

6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@
language: node_js

node_js:
- 10

install:
- npm install --ignore-scripts
- 12
- 14
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changes to PostCSS Nesting

### 8.0.0 (April 26, 2021)

- Updated: Support for PostCSS 8+.
- Updated: Support for Node v12+.
- Updated: Support for the `@container` at-rule.

### 7.0.1 (July 29, 2018)

- Fixed: Non-breaking issue with rules being unnecessarily split
Expand Down
14 changes: 0 additions & 14 deletions index.js

This file was deleted.

23 changes: 0 additions & 23 deletions lib/atrule-within-atrule.js

This file was deleted.

28 changes: 0 additions & 28 deletions lib/atrule-within-rule.js

This file was deleted.

11 changes: 0 additions & 11 deletions lib/merge-params.js

This file was deleted.

12 changes: 0 additions & 12 deletions lib/merge-selectors.js

This file was deleted.

32 changes: 0 additions & 32 deletions lib/nest-rule-within-rule.js

This file was deleted.

Loading

0 comments on commit 64ba9ac

Please sign in to comment.