From 6e2b29e24d1b1918d9e0a029775d9b8aabbfaad0 Mon Sep 17 00:00:00 2001 From: Adam Bradley Date: Thu, 30 Sep 2021 20:39:38 -0500 Subject: [PATCH] publish from dist directory --- .gitignore | 4 +- index.js | 1 - package.json | 23 +------ scripts/build-atomics.ts | 10 ++- scripts/build-integration.ts | 15 ++-- scripts/build-main-loader.ts | 4 +- scripts/build-main-snippet.ts | 4 +- scripts/build-react.ts | 6 +- scripts/build-service-worker.ts | 10 ++- scripts/build-web-worker.ts | 6 +- scripts/index.ts | 106 +++++++++++++++++++++-------- scripts/utils.ts | 45 +++++------- src/integration/api-extractor.json | 2 +- src/integration/package.json | 6 +- src/react/api-extractor.json | 2 +- src/react/package.json | 6 +- 16 files changed, 134 insertions(+), 116 deletions(-) delete mode 100644 index.js diff --git a/.gitignore b/.gitignore index e8c8f077..5f24703e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,7 @@ .cache/ .DS_Store node_modules/ -/integration/ -/lib/ -/react/ +/dist/ /tests/~partytown/ /tests/videos/ /tsc/ \ No newline at end of file diff --git a/index.js b/index.js deleted file mode 100644 index 2c65486b..00000000 --- a/index.js +++ /dev/null @@ -1 +0,0 @@ -// @builder.io/partytown diff --git a/package.json b/package.json index bd401158..05c0070d 100644 --- a/package.json +++ b/package.json @@ -3,27 +3,6 @@ "version": "0.0.7", "description": "Relocate resource intensive 3rd-party scripts off of the main thread and into a web worker.", "license": "MIT", - "main": "./index.js", - "exports": { - "./integration": { - "import": "./integration/index.mjs", - "require": "./integration/index.cjs" - }, - "./react": { - "import": "./react/index.mjs", - "require": "./react/index.cjs" - }, - ".": { - "import": "./index.mjs", - "require": "./index.cjs" - } - }, - "files": [ - "/integration/", - "/lib/", - "/react/", - "index.js" - ], "scripts": { "build": "tsc && rollup -c scripts/rollup.config.js", "build.prod": "tsc && rollup -c scripts/rollup.config.js --configApi", @@ -31,7 +10,7 @@ "dev": "tsc && concurrently \"npm:build.watch\" \"npm:tsc.watch\" -n build,tsc -c magenta,yellow", "playwright": "playwright test --browser=chromium", "playwright.webkit": "playwright test --browser=webkit", - "release": "np --no-2fa", + "release": "npm run build && cd dist && np --no-2fa", "serve": "sirv tests --port 4000 --dev", "serve.test": "sirv tests --port 5000 --dev --quiet", "test": "start-server-and-test serve.test http://localhost:5000/ playwright", diff --git a/scripts/build-atomics.ts b/scripts/build-atomics.ts index 26c98a04..57aa5451 100644 --- a/scripts/build-atomics.ts +++ b/scripts/build-atomics.ts @@ -8,7 +8,7 @@ import { } from './utils'; import { join } from 'path'; import { minifyPlugin } from './minify'; -import { ensureDir, writeFile } from 'fs-extra'; +import { writeFile } from 'fs-extra'; import { webWorkerBlobUrlPlugin } from './build-web-worker'; export function buildAtomics(opts: BuildOptions): RollupOptions[] { @@ -25,7 +25,7 @@ function buildAtomicsDebug(opts: BuildOptions): RollupOptions { return { input: join(opts.tscLibDir, 'sandbox', 'index.js'), output: { - file: join(opts.buildLibDir, 'debug', 'partytown-sandbox-atomics.html'), + file: join(opts.distLibDebugDir, 'partytown-sandbox-atomics.html'), format: 'es', exports: 'none', plugins: [ @@ -36,9 +36,7 @@ function buildAtomicsDebug(opts: BuildOptions): RollupOptions { const b = bundle[f]; if (b.type === 'chunk') { const jsCode = b.code; - const libDebugPath = join(opts.buildLibDir, 'debug'); - const debugJsPath = join(libDebugPath, 'partytown-sandbox-atomics.js'); - await ensureDir(libDebugPath); + const debugJsPath = join(opts.distLibDebugDir, 'partytown-sandbox-atomics.js'); await writeFile(debugJsPath, jsCode); b.code = debugHtml; } @@ -71,7 +69,7 @@ function buildAtomicsMin(opts: BuildOptions): RollupOptions { return { input: join(opts.tscLibDir, 'sandbox', 'index.js'), output: { - file: join(opts.buildLibDir, 'partytown-sandbox-atomics.html'), + file: join(opts.distLibDir, 'partytown-sandbox-atomics.html'), format: 'es', exports: 'none', plugins: [ diff --git a/scripts/build-integration.ts b/scripts/build-integration.ts index ddf65493..eb76c4e2 100644 --- a/scripts/build-integration.ts +++ b/scripts/build-integration.ts @@ -6,11 +6,11 @@ import { readFile } from 'fs-extra'; export function buildIntegration(opts: BuildOptions): RollupOptions { const output: OutputOptions[] = [ { - file: join(opts.buildIntegrationDir, 'index.cjs'), + file: join(opts.distIntegrationDir, 'index.cjs'), format: 'cjs', }, { - file: join(opts.buildIntegrationDir, 'index.mjs'), + file: join(opts.distIntegrationDir, 'index.mjs'), format: 'es', }, ]; @@ -29,7 +29,14 @@ export function buildIntegration(opts: BuildOptions): RollupOptions { async load(id) { if (id === '@snippet') { const codeFileName = 'partytown-snippet.js'; - const codeFilePath = join(opts.buildLibDir, codeFileName); + + let codeFilePath: string; + if (opts.isDev) { + codeFilePath = join(opts.distLibDebugDir, codeFileName); + } else { + codeFilePath = join(opts.distLibDir, codeFileName); + } + const code = JSON.stringify((await readFile(codeFilePath, 'utf-8')).trim()); return `const PartytownSnippet = ${code}; export default PartytownSnippet;`; } @@ -38,7 +45,7 @@ export function buildIntegration(opts: BuildOptions): RollupOptions { submodulePackageJson( '@builder.io/partytown/integration', opts.srcIntegrationDir, - opts.buildIntegrationDir, + opts.distIntegrationDir, opts ), ], diff --git a/scripts/build-main-loader.ts b/scripts/build-main-loader.ts index 4bc6db6e..df33e278 100644 --- a/scripts/build-main-loader.ts +++ b/scripts/build-main-loader.ts @@ -5,14 +5,14 @@ import type { OutputOptions, RollupOptions } from 'rollup'; export function buildMainLoader(opts: BuildOptions): RollupOptions { const partytownDebug: OutputOptions = { - file: join(opts.buildLibDir, 'debug', 'partytown.js'), + file: join(opts.distLibDebugDir, 'partytown.js'), format: 'es', exports: 'none', plugins: [...minifyPlugin(true)], }; const partytownMin: OutputOptions = { - file: join(opts.buildLibDir, 'partytown.js'), + file: join(opts.distLibDir, 'partytown.js'), format: 'es', exports: 'none', plugins: [...minifyPlugin(false), fileSize()], diff --git a/scripts/build-main-snippet.ts b/scripts/build-main-snippet.ts index 13d3c1c6..13862d40 100644 --- a/scripts/build-main-snippet.ts +++ b/scripts/build-main-snippet.ts @@ -5,14 +5,14 @@ import type { OutputOptions, RollupOptions } from 'rollup'; export function buildMainSnippet(opts: BuildOptions): RollupOptions { const partytownDebug: OutputOptions = { - file: join(opts.buildLibDir, 'debug', 'partytown-snippet.js'), + file: join(opts.distLibDebugDir, 'partytown-snippet.js'), format: 'es', exports: 'none', plugins: [...minifyPlugin(true)], }; const partytownMin: OutputOptions = { - file: join(opts.buildLibDir, 'partytown-snippet.js'), + file: join(opts.distLibDir, 'partytown-snippet.js'), format: 'es', exports: 'none', plugins: [ diff --git a/scripts/build-react.ts b/scripts/build-react.ts index 54b19649..bed6bdb0 100644 --- a/scripts/build-react.ts +++ b/scripts/build-react.ts @@ -7,11 +7,11 @@ export function buildReact(opts: BuildOptions): RollupOptions { input: join(opts.tscReactDir, 'index.js'), output: [ { - file: join(opts.buildReactDir, 'index.cjs'), + file: join(opts.distReactDir, 'index.cjs'), format: 'cjs', }, { - file: join(opts.buildReactDir, 'index.mjs'), + file: join(opts.distReactDir, 'index.mjs'), format: 'es', }, ], @@ -21,7 +21,7 @@ export function buildReact(opts: BuildOptions): RollupOptions { submodulePackageJson( '@builder.io/partytown/react', opts.srcReactDir, - opts.buildReactDir, + opts.distReactDir, opts ), ], diff --git a/scripts/build-service-worker.ts b/scripts/build-service-worker.ts index 533e4a40..4304f950 100644 --- a/scripts/build-service-worker.ts +++ b/scripts/build-service-worker.ts @@ -10,12 +10,12 @@ import { } from './utils'; import { join } from 'path'; import { minifyPlugin } from './minify'; -import { ensureDir, writeFile } from 'fs-extra'; +import { writeFile } from 'fs-extra'; import { webWorkerBlobUrlPlugin } from './build-web-worker'; export function buildServiceWorker(opts: BuildOptions): RollupOptions { const swDebug: OutputOptions = { - file: join(opts.buildLibDir, 'debug', 'partytown-sw.js'), + file: join(opts.distLibDebugDir, 'partytown-sw.js'), format: 'es', exports: 'none', plugins: [...minifyPlugin(true)], @@ -24,7 +24,7 @@ export function buildServiceWorker(opts: BuildOptions): RollupOptions { const output: OutputOptions[] = [swDebug]; if (!opts.isDev) { output.push({ - file: join(opts.buildLibDir, 'partytown-sw.js'), + file: join(opts.distLibDir, 'partytown-sw.js'), format: 'es', exports: 'none', plugins: [...minifyPlugin(false), fileSize()], @@ -65,9 +65,7 @@ async function buildSandboxServiceWorker(opts: BuildOptions, msgType: MessageTyp let sandboxHtml: string; if (debug) { - const buildLibDebugDir = join(opts.buildLibDir, 'debug'); - await ensureDir(buildLibDebugDir); - await writeFile(join(buildLibDebugDir, `partytown-sandbox-${msgType}.js`), sandboxJsCode); + await writeFile(join(opts.distLibDebugDir, `partytown-sandbox-${msgType}.js`), sandboxJsCode); sandboxHtml = ``; } else { sandboxHtml = ``; diff --git a/scripts/build-web-worker.ts b/scripts/build-web-worker.ts index 622193e1..19801dac 100644 --- a/scripts/build-web-worker.ts +++ b/scripts/build-web-worker.ts @@ -1,7 +1,7 @@ import { OutputOptions, Plugin, rollup } from 'rollup'; import { BuildOptions, MessageType, onwarn, syncCommunicationModulesPlugin } from './utils'; import { join } from 'path'; -import { ensureDir, writeFile } from 'fs-extra'; +import { writeFile } from 'fs-extra'; import { minifyPlugin } from './minify'; export async function buildWebWorker(opts: BuildOptions, msgType: MessageType, debug: boolean) { @@ -23,9 +23,7 @@ export async function buildWebWorker(opts: BuildOptions, msgType: MessageType, d const webWorkerCode = generated.output[0].code; if (debug) { - const debugDir = join(opts.buildLibDir, 'debug'); - await ensureDir(debugDir); - await writeFile(join(debugDir, `partytown-ww-${msgType}.js`), webWorkerCode); + await writeFile(join(opts.distLibDebugDir, `partytown-ww-${msgType}.js`), webWorkerCode); } return webWorkerCode; diff --git a/scripts/index.ts b/scripts/index.ts index 3619d8e5..6bf5fdb3 100644 --- a/scripts/index.ts +++ b/scripts/index.ts @@ -6,22 +6,13 @@ import { buildMainSnippet } from './build-main-snippet'; import { buildIntegration } from './build-integration'; import { buildReact } from './build-react'; import { buildServiceWorker } from './build-service-worker'; -import { emptyDir } from 'fs-extra'; +import { emptyDir, ensureDir, readJsonSync, writeFile, writeJson } from 'fs-extra'; import { join } from 'path'; export async function runBuild(rootDir: string, isDev: boolean, generateApi: boolean) { const opts = createBuildOptions(rootDir, isDev, generateApi); - if (!opts.isDev) { - const emptyDirs = [ - opts.buildIntegrationDir, - opts.buildLibDir, - opts.buildReactDir, - opts.buildTestsDir, - opts.testsVideosDir, - ]; - await Promise.all(emptyDirs.map(emptyDir)); - } + await createRootPackage(opts); if (opts.generateApi) { buildApi(opts); @@ -37,12 +28,62 @@ export async function runBuild(rootDir: string, isDev: boolean, generateApi: boo ]; } +async function createRootPackage(opts: BuildOptions) { + await emptyDir(opts.distDir); + await emptyDir(opts.distTestsDir); + + await ensureDir(opts.distIntegrationDir); + await ensureDir(opts.distLibDir); + await ensureDir(opts.distLibDebugDir); + await ensureDir(opts.distReactDir); + + const indexJsCode = `// @builder.io/partytown`; + const indexJsPath = join(opts.distDir, 'index.js'); + await writeFile(indexJsPath, indexJsCode); + + const rootPkg = opts.packageJson; + const packageJson = { + name: rootPkg.name, + version: rootPkg.version, + description: rootPkg.description, + license: rootPkg.license, + author: rootPkg.author, + main: './index.js', + exports: { + './integration': { + import: './integration/index.mjs', + require: './integration/index.cjs', + }, + './react': { + import: './react/index.mjs', + require: './react/index.cjs', + }, + '.': { + require: './index.js', + }, + }, + files: ['./integration/', './lib/', './react/', './index.js'], + homepage: rootPkg.homepage, + keywords: rootPkg.keywords, + repository: rootPkg.repository, + publishConfig: rootPkg.publishConfig, + }; + + if (opts.isDev) { + const version = `dev.0.${Date.now()}`; + rootPkg.version = packageJson.version = version; + } + + const packageJsonPath = join(opts.distDir, 'package.json'); + await writeJson(packageJsonPath, packageJson, { spaces: 2 }); +} + function createBuildOptions(rootDir: string, isDev: boolean, generateApi: boolean) { - const tscDir = join(rootDir, 'tsc'); - const tscSrcDir = join(tscDir, 'src'); - const tscIntegrationDir = join(tscSrcDir, 'integration'); - const tscLibDir = join(tscSrcDir, 'lib'); - const tscReactDir = join(tscSrcDir, 'react'); + const distDir = join(rootDir, 'dist'); + const distIntegrationDir = join(distDir, 'integration'); + const distLibDir = join(distDir, 'lib'); + const distLibDebugDir = join(distLibDir, 'debug'); + const distReactDir = join(distDir, 'react'); const srcDir = join(rootDir, 'src'); const srcIntegrationDir = join(srcDir, 'integration'); @@ -51,30 +92,39 @@ function createBuildOptions(rootDir: string, isDev: boolean, generateApi: boolea const testsDir = join(rootDir, 'tests'); const testsVideosDir = join(testsDir, 'videos'); - const buildTestsDir = join(testsDir, '~partytown'); + const distTestsDir = join(testsDir, '~partytown'); - const buildIntegrationDir = join(rootDir, 'integration'); - const buildLibDir = join(rootDir, 'lib'); - const buildReactDir = join(rootDir, 'react'); + const tscDir = join(rootDir, 'tsc'); + const tscSrcDir = join(tscDir, 'src'); + const tscIntegrationDir = join(tscSrcDir, 'integration'); + const tscLibDir = join(tscSrcDir, 'lib'); + const tscReactDir = join(tscSrcDir, 'react'); + + const packageJsonPath = join(rootDir, 'package.json'); + const packageJson = readJsonSync(packageJsonPath); const opts: BuildOptions = { isDev, generateApi, rootDir, - tscDir, - tscIntegrationDir, - tscLibDir, - tscReactDir, + distDir, + distIntegrationDir, + distLibDir, + distLibDebugDir, + distReactDir, + distTestsDir, srcDir, srcIntegrationDir, srcLibDir, srcReactDir, - buildIntegrationDir, - buildLibDir, - buildReactDir, - buildTestsDir, testsDir, testsVideosDir, + tscDir, + tscIntegrationDir, + tscLibDir, + tscReactDir, + packageJson, }; + return opts; } diff --git a/scripts/utils.ts b/scripts/utils.ts index 189e3e1f..6d00cf3f 100644 --- a/scripts/utils.ts +++ b/scripts/utils.ts @@ -1,14 +1,6 @@ import gzipSize from 'gzip-size'; import { basename, join } from 'path'; -import { - copy, - ensureDir, - readdirSync, - readFileSync, - readJson, - statSync, - writeJson, -} from 'fs-extra'; +import { copy, readdirSync, readFileSync, readJson, statSync, writeJson } from 'fs-extra'; import type { Plugin, RollupWarning } from 'rollup'; export function syncCommunicationModulesPlugin(opts: BuildOptions, msgType: MessageType): Plugin { @@ -84,14 +76,11 @@ export function submodulePackageJson( return { name: 'submodulePackageJson', async writeBundle() { - if (opts.generateApi) { - const rootPkg = await readJson(join(opts.rootDir, 'package.json')); - const pkg = await readJson(join(submoduleSrcDir, 'package.json')); - pkg.name = submoduleName; - pkg.version = rootPkg.version; - pkg.private = true; - await writeJson(join(submoduleBuildDir, 'package.json'), pkg, { spaces: 2 }); - } + const pkg = await readJson(join(submoduleSrcDir, 'package.json')); + pkg.name = submoduleName; + pkg.version = opts.packageJson.version; + pkg.private = true; + await writeJson(join(submoduleBuildDir, 'package.json'), pkg, { spaces: 2 }); }, }; } @@ -100,8 +89,7 @@ export function copyBuildToTestSite(opts: BuildOptions): Plugin { return { name: 'copyBuildToTestSite', async writeBundle() { - await ensureDir(opts.buildTestsDir); - await copy(opts.buildLibDir, opts.buildTestsDir); + await copy(opts.distLibDir, opts.distTestsDir); }, }; } @@ -134,20 +122,23 @@ export interface BuildOptions { isDev: boolean; generateApi: boolean; rootDir: string; - tscDir: string; - tscIntegrationDir: string; - tscLibDir: string; - tscReactDir: string; + distDir: string; + distIntegrationDir: string; + distLibDir: string; + distLibDebugDir: string; + distReactDir: string; + distTestsDir: string; srcDir: string; srcIntegrationDir: string; srcLibDir: string; srcReactDir: string; - buildIntegrationDir: string; - buildLibDir: string; - buildReactDir: string; - buildTestsDir: string; testsDir: string; testsVideosDir: string; + tscDir: string; + tscIntegrationDir: string; + tscLibDir: string; + tscReactDir: string; + packageJson: any; } export type MessageType = 'sw' | 'atomics'; diff --git a/src/integration/api-extractor.json b/src/integration/api-extractor.json index eab712cf..6f51d348 100644 --- a/src/integration/api-extractor.json +++ b/src/integration/api-extractor.json @@ -13,7 +13,7 @@ }, "dtsRollup": { "enabled": true, - "untrimmedFilePath": "/integration/index.d.ts" + "untrimmedFilePath": "/dist/integration/index.d.ts" }, "docModel": { "enabled": false diff --git a/src/integration/package.json b/src/integration/package.json index cf642ca0..40341634 100644 --- a/src/integration/package.json +++ b/src/integration/package.json @@ -1,6 +1,6 @@ { "name": "@builder.io/partytown-integration", - "main": "index.cjs", - "module": "index.mjs", - "types": "index.d.ts" + "main": "./index.cjs", + "module": "./index.mjs", + "types": "./index.d.ts" } diff --git a/src/react/api-extractor.json b/src/react/api-extractor.json index 84cf2241..ac328bfa 100644 --- a/src/react/api-extractor.json +++ b/src/react/api-extractor.json @@ -13,7 +13,7 @@ }, "dtsRollup": { "enabled": true, - "untrimmedFilePath": "/react/index.d.ts" + "untrimmedFilePath": "/dist/react/index.d.ts" }, "docModel": { "enabled": false diff --git a/src/react/package.json b/src/react/package.json index 2e14fd0e..db651424 100644 --- a/src/react/package.json +++ b/src/react/package.json @@ -1,6 +1,6 @@ { "name": "@builder.io/partytown-react", - "main": "index.cjs", - "module": "index.mjs", - "types": "index.d.ts" + "main": "./index.cjs", + "module": "./index.mjs", + "types": "./index.d.ts" }