From a850ff7a24ccb631d3bc3d34d04b439f85d44e8a Mon Sep 17 00:00:00 2001 From: Florian Rappl Date: Thu, 7 Jul 2022 14:00:22 +0200 Subject: [PATCH] Individual packing for more flexibility and lighter packages --- CHANGELOG.md | 1 + src/tooling/piral-cli/src/common/inspect.ts | 18 ++-- src/tooling/piral-cli/src/common/pack.ts | 98 ++++++++++++++++----- src/tooling/piral-cli/src/common/package.ts | 20 +---- 4 files changed, 91 insertions(+), 46 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f6c7fa3a0..49a1683f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Improved default metadata using `piletConfig` from *package.json* in `pilet debug` (#458) - Updated to React v18 (#501) +- Updated `importmap` with `inherit` key - Moved templates to dedicated repository (#458) - Changed the default bundler to be `piral-cli-webpack5` (#469) - Changed webpack configs to use `oneOf` for assets (#451) diff --git a/src/tooling/piral-cli/src/common/inspect.ts b/src/tooling/piral-cli/src/common/inspect.ts index 9856f62aa..05fe515cc 100644 --- a/src/tooling/piral-cli/src/common/inspect.ts +++ b/src/tooling/piral-cli/src/common/inspect.ts @@ -12,16 +12,14 @@ function getPackageJson(files: PackageFiles): PackageData { return jju.parse(content); } -function getPiletMainPath(data: PackageData, files: PackageFiles) { - const paths = [ - data.main, - `dist/${data.main}`, - `${data.main}/index.js`, - `dist/${data.main}/index.js`, - 'index.js', - 'dist/index.js', - ]; - return paths.map((filePath) => `${packageRoot}${filePath}`).filter((filePath) => !!files[filePath])[0]; +export function getPossiblePiletMainPaths(data: PackageData) { + const { main = 'index.js' } = data; + return [main, `dist/${main}`, `${main}/index.js`, `dist/${main}/index.js`, 'index.js', 'dist/index.js']; +} + +export function getPiletMainPath(data: PackageData, files: PackageFiles) { + const paths = getPossiblePiletMainPaths(data); + return paths.map((filePath) => `${packageRoot}${filePath}`).find((filePath) => !!files[filePath]); } export interface PiletPackageData extends PackageData { diff --git a/src/tooling/piral-cli/src/common/pack.ts b/src/tooling/piral-cli/src/common/pack.ts index 1bae933f8..6f0a11f18 100644 --- a/src/tooling/piral-cli/src/common/pack.ts +++ b/src/tooling/piral-cli/src/common/pack.ts @@ -1,20 +1,60 @@ -import { resolve, join } from 'path'; +import { tmpdir } from 'os'; +import { resolve, relative, join, dirname } from 'path'; +import { createWriteStream, mkdtemp } from 'fs'; import { log, progress, fail } from './log'; -import { readJson, move } from './io'; -import { createNpmPackage } from './npm'; -import { ForceOverwrite } from './enums'; - -async function getFile(root: string, name: string, dest: string) { - const proposed = join(root, name); - - if (dest !== root) { - log('generalDebug_0003', `Moving file from "${root}" to "${dest}" ...`); - const file = await move(proposed, dest, ForceOverwrite.yes); - log('generalDebug_0003', 'Successfully moved file.'); - return file; +import { readJson, copy, removeDirectory, checkExists } from './io'; +import { getPossiblePiletMainPaths } from './inspect'; +import { tar } from '../external'; + +async function getPiletContentDir(root: string, packageData: any) { + const paths = getPossiblePiletMainPaths(packageData); + + for (const path of paths) { + const file = resolve(root, path); + + if (await checkExists(file)) { + return dirname(file); + } } - return proposed; + return root; +} + +export function makeTempDir(prefix: string) { + return new Promise((resolve, reject) => + mkdtemp(prefix, (err, folder) => { + if (err) { + reject(err); + } else { + resolve(folder); + } + }), + ); +} + +export function packContent(file: string, cwd: string, files: Array) { + const stream = createWriteStream(file); + + tar + .c( + { + cwd, + prefix: 'package/', + portable: true, + follow: true, + gzip: { + level: 9, + }, + // @ts-ignore + mtime: new Date('1985-10-26T08:15:00.000Z'), + }, + files, + ) + .pipe(stream, { + end: true, + }); + + return new Promise((finish) => stream.on('close', finish)); } export async function createPiletPackage(baseDir: string, source: string, target: string) { @@ -37,12 +77,30 @@ export async function createPiletPackage(baseDir: string, source: string, target } progress(`Packing pilet in ${dest} ...`); - log('generalDebug_0003', 'Creating package ...'); - await createNpmPackage(root); - log('generalDebug_0003', 'Successfully created package.'); - const name = `${pckg.name}-${pckg.version}.tgz`.replace(/@/g, '').replace(/\//g, '-'); - log('generalDebug_0003', `Assumed package name "${name}".`); - const file = await getFile(root, name, dest); + const pckgName = pckg.name.replace(/@/g, '').replace(/\//g, '-'); + const id = `${pckgName}-${pckg.version}`; + const name = `${id}.tgz`; + log('generalDebug_0003', `Assume package name "${name}".`); + + const file = resolve(dest, name); + const content = await getPiletContentDir(root, pckg); + const files = [resolve(root, 'package.json'), content]; + const prefix = join(tmpdir(), `${id}-`); + const cwd = await makeTempDir(prefix); + log('generalDebug_0003', `Creating package with content from "${content}" ...`); + + await Promise.all(files.map((file) => copy(file, resolve(cwd, relative(root, file))))); + + await packContent( + file, + cwd, + files.map((f) => relative(root, f)), + ); + + log('generalDebug_0003', `Successfully created package from "${cwd}".`); + + await removeDirectory(cwd); + log('generalDebug_0003', `Packed file "${file}".`); return file; } diff --git a/src/tooling/piral-cli/src/common/package.ts b/src/tooling/piral-cli/src/common/package.ts index cfe9f6a4f..484654f75 100644 --- a/src/tooling/piral-cli/src/common/package.ts +++ b/src/tooling/piral-cli/src/common/package.ts @@ -518,21 +518,7 @@ export async function patchPiletPackage( ...info.scripts, } : info.scripts; - const peerModules = []; const allExternals = makePiletExternals(root, piralDependencies, externals, fromEmulator, piralInfo); - const peerDependencies = { - ...allExternals.reduce((deps, name) => { - const valid = isValidDependency(name); - deps[name] = valid ? '*' : undefined; - - if (!valid) { - peerModules.push(name); - } - - return deps; - }, {}), - [name]: `*`, - }; const devDependencies: Record = { ...Object.keys(typeDependencies).reduce((deps, name) => { deps[name] = piralDependencies[name] || typeDependencies[name]; @@ -564,9 +550,11 @@ export async function patchPiletPackage( const packageContent = deepMerge(packageOverrides, { piral, + importmap: { + imports: {}, + inherit: [name], + }, devDependencies, - peerDependencies, - peerModules, dependencies: { [name]: undefined, },