This repository has been archived by the owner on Dec 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 8.0.0 * fix: Node v12 compatibility * feat: add support for @container * feat: postcss-7 compatibility
- Loading branch information
1 parent
52ec2d9
commit 64ba9ac
Showing
39 changed files
with
567 additions
and
244 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,5 @@ | |
language: node_js | ||
|
||
node_js: | ||
- 10 | ||
|
||
install: | ||
- npm install --ignore-scripts | ||
- 12 | ||
- 14 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.