Skip to content

Commit

Permalink
update project build
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathantneal committed Aug 5, 2021
1 parent 3cb55e0 commit ca4e244
Show file tree
Hide file tree
Showing 9 changed files with 529 additions and 95 deletions.
137 changes: 48 additions & 89 deletions .task/build.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import * as fs from './internal/fs.js'
import * as js from './internal/js.js'
import URL from './internal/URL.js'
import { box } from './internal/color.js'
import { transformDestructuring } from './internal/js.transformDestructuring.js'
import { transformModulesToCJS } from './internal/js.transformModulesToCJS.js'
import { transformOptionalCatchToParam } from './internal/js.transformOptionalCatchToParam.js'
import { transformIIFE } from './internal/js.transformIIFE.js'
import { corePackageUrl, reactPackageUrl, stringifyPackageUrl } from './internal/dirs.js'
import { isProcessMeta, getProcessArgOf } from './internal/process.js'
import esbuild from 'esbuild'
import fs from './internal/fs.js'
import nodemon from 'nodemon'
import zlib from 'zlib'
import { minify } from 'terser'
Expand All @@ -14,65 +20,48 @@ const matchNamings = /[{,]?([$\w]+)(?:\s+as\s+(\w+))?/gy
const variants = {
esm: {
extension: 'mjs',
transformImports(allImports, from) {
const defaultImports = []
const imports = []
for (const name in allImports) {
const as = allImports[name]
if (name === 'default') defaultImports.push(as)
else imports.push(`${name} as ${as}`)
}
return `import${defaultImports.length ? ` ${defaultImports.join(',')} ` : ``}${imports.length ? `{${imports.join(',')}}` : ''}from"${from}";`
},
transformExports(allExports) {
const exports = []
for (const as in allExports) {
const name = allExports[as]
exports.push(`${name} as ${as}`)
}
return `export{${exports.join(',')}};`
async transform(code, smap) {
return await minify(code, {
sourceMap: { content: smap },
compress: true,
module: true,
mangle: true,
toplevel: true,
})
},
},
cjs: {
extension: 'cjs',
transformImports(allImports, from) {
const defaultImports = []
const imports = []
for (const name in allImports) {
const as = allImports[name]
if (name === 'default') defaultImports.push(as)
else imports.push(`${name}:${as}`)
}
return `const${defaultImports.length ? ` ${defaultImports.join(',')}` : imports.length ? `{${imports.join(',')}}` : ''}=require("${from}");`
},
transformExports(allExports) {
const exports = []
for (const as in allExports) {
const name = allExports[as]
exports.push(`${as}:${name}`)
}
return `module.exports={${exports.join(',')}};`
async transform(code, smap) {
let cjsast = js.parse(code)

transformModulesToCJS(cjsast)
transformOptionalCatchToParam(cjsast)
transformDestructuring(cjsast)

return await minify(cjsast.toString(), {
sourceMap: { content: smap },
compress: true,
module: true,
mangle: true,
toplevel: true,
})
},
},
iife: {
extension: 'iife.js',
transformImports(allImports, from) {
const defaultImports = []
const imports = []
for (const name in allImports) {
const as = allImports[name]
if (name === 'default') defaultImports.push(as)
else imports.push(`${name}:${as}`)
}
return `const${defaultImports.length ? ` ${defaultImports.join(',')}` : imports.length ? `{${imports.join(',')}}` : ''}=${from[0].toUpperCase() + from.slice(1)};`
},
transformExports(allExports) {
const exports = []
for (const as in allExports) {
const name = allExports[as]
exports.push(`${as}:${name}`)
}
return `globalThis.stitches={${exports.join(',')}};`
gjs: {
extension: 'global.js',
async transform(code, smap) {
let cjsast = js.parse(code)

transformIIFE(cjsast)

return await minify(cjsast.toString(), {
sourceMap: { content: smap },
compress: true,
module: true,
mangle: true,
toplevel: true,
})
},
},
}
Expand All @@ -88,11 +77,11 @@ export const build = async (packageUrl, opts) => {

if (!opts.only.length || opts.only.includes(packageName)) {
const targetPathname = new URL('index.js', initPackageUrl).pathname
const outputPathname = new URL('stitches.js', distPackageUrl).pathname
const outputPathname = new URL('index.js', distPackageUrl).pathname

// Build ESM version
const {
outputFiles: [cmapResult, codeResult],
let {
outputFiles: [{ text: smap }, { text: code }],
} = await esbuild.build({
entryPoints: [targetPathname],
outfile: outputPathname,
Expand All @@ -103,20 +92,11 @@ export const build = async (packageUrl, opts) => {
write: false,
})

// Minify ESM version
const { code, map } = await minify(codeResult.text, {
sourceMap: { content: cmapResult.text },
compress: true,
module: true,
mangle: true,
toplevel: true,
})

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

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

// prepare variations
const size = {
Expand All @@ -129,28 +109,7 @@ export const build = async (packageUrl, opts) => {
const variantInfo = variants[variant]
const variantPath = new URL(`dist/index.${variantInfo.extension}`, packageUrl).pathname

matchImports.lastIndex = 0
matchExports.lastIndex = 0
const variantCode =
code.replace(matchImports, (_, statement, from) => {
matchNamings.lastIndex = 0
const allImports = Array.from(
statement.matchAll(matchNamings)
).reduce(
(allImports, each) => Object.assign(allImports, { [each[2] || 'default']: each[1] }),
{}
)
return variantInfo.transformImports(allImports, from)
}).replace(matchExports, (_, statement) => {
matchNamings.lastIndex = 0
const allExports = Array.from(
statement.matchAll(matchNamings)
).reduce(
(allExports, each) => Object.assign(allExports, { [each[2] || each[1]]: each[1] }),
{}
)
return variantInfo.transformExports(allExports)
}) // prettier-ignore
let { code: variantCode } = await variantInfo.transform(code, smap)

const variantMins = (Buffer.byteLength(variantCode) / 1000).toFixed(2)
const variantGzip = Number(zlib.gzipSync(variantCode, { level: 9 }).length / 1000).toFixed(2)
Expand Down
Loading

0 comments on commit ca4e244

Please sign in to comment.