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

Commit

Permalink
fix: cjs build
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathantneal committed May 2, 2021
1 parent 30f97f1 commit efc8b3d
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 53 deletions.
85 changes: 66 additions & 19 deletions .bin/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,65 @@ import esbuild from 'esbuild'
import fs from 'node:fs/promises'
import zlib from 'node:zlib'

/** @typedef {{ [name: string]: string }} Exports */
/** @typedef {{ extension: string, transform(code: string, exports: Exports): string }} Variant */
/** @type {{ [name: string]: Variant }} */
const variants = {
esm: {
extension: 'mjs',
transform(code, exports) {
/** @type {string[]} */
const esmExports = []
for (const name in exports) esmExports.push(`${exports[name]} as ${name}`)
return `${code}export{${esmExports.join(',')}}`
return (
esmExports.length
? `${code}export{${esmExports.join(',')}}`
: code
)
},
},
cjs: {
extension: 'cjs',
transform(code, exports) {
const cjsExports = ['__esModule:!0']
/** @type {string[]} */
const cjsExports = []
for (const name in exports) cjsExports.push(`${name}:${exports[name]}`)
return `${code}module.exports={${cjsExports.join(',')}}`
return (
cjsExports.length
? 'default' in exports
? `${code}module.exports=Object.assign(${exports.default},{${cjsExports.join(',')}})`
: `${code}module.exports={${cjsExports.join(',')}}`
: code
)
},
},
}

async function buildPackage(src) {
const packageUrl = src
const initPackageUrl = new URL('src/', packageUrl)
const distPackageUrl = new URL('dist/', packageUrl)
/** @type {(pkgUrl: URL, name: string) => Promise<void>} */
async function buildPackage(pkgUrl, base) {
const srcDirUrl = new URL(`src/`, pkgUrl)
const outDirUrl = new URL(`${base}/`, pkgUrl)

const packageJsonUrl = new URL(`package.json`, packageUrl)
const packageName = JSON.parse(await fs.readFile(packageJsonUrl, 'utf8')).name
/** @type {{ name: string }} */
const { name } = JSON.parse(
await fs.readFile(
new URL('package.json', pkgUrl),
'utf8'
)
)

console.log(packageName)
console.log(base)
console.log()

const targetPathname = new URL('index.js', initPackageUrl).pathname
const outputPathname = new URL('index.js', distPackageUrl).pathname
const srcPath = new URL(`${base}.js`, srcDirUrl).pathname
const outPath = new URL(`${base}.js`, outDirUrl).pathname

// Build ESM version
const {
outputFiles: [cmapResult, codeResult],
} = await esbuild.build({
entryPoints: [targetPathname],
outfile: outputPathname,
entryPoints: [srcPath],
outfile: outPath,
bundle: true,
format: 'esm',
sourcemap: 'external',
Expand All @@ -52,28 +72,53 @@ async function buildPackage(src) {
const map = cmapResult.text

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

// write map
fs.writeFile(new URL(`index.map`, distPackageUrl), map)
await fs.writeFile(new URL(`${name}.map`, outDirUrl), map)

// prepare variations
/** @type {(code: string, index?: number) => [string, string]} */
const splitByExport = (code, index = code.indexOf('export')) => [code.slice(0, index), code.slice(index)]
const [lead, tail] = splitByExport(code)

/** @type {{ [name: string]: string }} */
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) {
/** @type {Variant} */
const variantInfo = variants[variant]
const variantPath = new URL(`dist/index.${variantInfo.extension}`, packageUrl).pathname
const variantPath = new URL(`${base}/${name}.${variantInfo.extension}`, pkgUrl).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`)

const packageJSON = JSON.stringify({
private: true,
type: 'module',
main: `${name}.cjs`,
module: `${name}.mjs`,
jsdelivr: `${name}.mjs`,
unpkg: `${name}.mjs`,
files: [
`${name}.cjs`,
`${name}.mjs`
],
exports: {
'.': {
import: `./${name}.mjs`,
require: `./${name}.cjs`,
default: `./${name}.mjs`
}
}
}, null, ' ')

await fs.writeFile(new URL('package.json', outDirUrl), packageJSON)
}
}

Expand All @@ -84,9 +129,11 @@ const argvUrl = new URL(process.argv[1], 'file:')

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

console.log()
await buildPackage(pkgUrl, 'postcss-8-nesting')

await buildPackage(rootUrl)
console.log()
await buildPackage(pkgUrl, 'postcss-7-nesting')
}
2 changes: 1 addition & 1 deletion .bin/test-tape.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { promises as fs } from 'node:fs'
import postcss from 'postcss'
import process from 'node:process'

import plugin from '../src/index.js'
import plugin from '../src/postcss-8-nesting.js'

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

Expand Down
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
dist
node_modules
index.*.*
postcss-7-nesting
postcss-8-nesting
package-lock.json
yarn.lock
*.log*
Expand Down
19 changes: 0 additions & 19 deletions .rollup.js

This file was deleted.

31 changes: 25 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,37 @@
"license": "CC0-1.0",
"version": "8.0.0",
"type": "module",
"main": "dist/index.cjs",
"module": "dist/index.mjs",
"typings": "dist/index.d.ts",
"main": "postcss-8-nesting/postcss-nesting.cjs",
"module": "postcss-8-nesting/postcss-nesting.mjs",
"jsdelivr": "postcss-8-nesting/postcss-nesting.mjs",
"unpkg": "postcss-8-nesting/postcss-nesting.mjs",
"publishConfig": {
"access": "public"
},
"author": "Jonathan Neal <[email protected]>",
"bugs": "https://github.com/csstools/postcss-nesting/issues",
"homepage": "https://github.com/csstools/postcss-nesting#readme",
"repository": "csstools/postcss-nesting",
"exports": {
".": {
"import": "./postcss-8-nesting/postcss-nesting.mjs",
"require": "./postcss-8-nesting/postcss-nesting.cjs",
"default": "./postcss-8-nesting/postcss-nesting.mjs"
},
"./postcss-7-nesting": {
"import": "./postcss-7-nesting/postcss-nesting.mjs",
"require": "./postcss-7-nesting/postcss-nesting.cjs",
"default": "./postcss-7-nesting/postcss-nesting.mjs"
},
"./postcss-8-nesting": {
"import": "./postcss-8-nesting/postcss-nesting.mjs",
"require": "./postcss-8-nesting/postcss-nesting.cjs",
"default": "./postcss-8-nesting/postcss-nesting.mjs"
}
},
"files": [
"dist",
"postcss-7"
"postcss-7-nesting",
"postcss-8-nesting"
],
"scripts": {
"prepublishOnly": "npm test",
Expand All @@ -26,9 +44,10 @@
"test:tape": "node .bin/test-tape.js"
},
"devDependencies": {
"esbuild": "0.11.15",
"esbuild": "0.11.17",
"eslint": "7.25.0",
"eslint-config-dev": "2.0.0",
"nodemon": "2.0.7",
"postcss": "8.2.6",
"pre-commit": "1.2.2"
},
Expand Down
4 changes: 0 additions & 4 deletions postcss-7/package.json

This file was deleted.

4 changes: 2 additions & 2 deletions postcss-7/index.js → src/postcss-7-nesting.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const postcssNesting = require('../dist/index.cjs').default
import postcssNesting from './postcss-8-nesting.js'

module.exports = Object.defineProperties(postcssNesting, Object.getOwnPropertyDescriptors({
export default Object.defineProperties(postcssNesting, Object.getOwnPropertyDescriptors({
get postcss() {
function postcssPlugin(cssRoot) {
const visitors = postcssNesting()
Expand Down
File renamed without changes.

0 comments on commit efc8b3d

Please sign in to comment.