Skip to content

Commit

Permalink
publish from dist directory
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Oct 1, 2021
1 parent d02e45a commit 6e2b29e
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 116 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
.cache/
.DS_Store
node_modules/
/integration/
/lib/
/react/
/dist/
/tests/~partytown/
/tests/videos/
/tsc/
1 change: 0 additions & 1 deletion index.js

This file was deleted.

23 changes: 1 addition & 22 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,14 @@
"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",
"build.watch": "rollup -c scripts/rollup.config.js -w --configDev",
"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",
Expand Down
10 changes: 4 additions & 6 deletions scripts/build-atomics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] {
Expand All @@ -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: [
Expand All @@ -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;
}
Expand Down Expand Up @@ -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: [
Expand Down
15 changes: 11 additions & 4 deletions scripts/build-integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
];
Expand All @@ -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;`;
}
Expand All @@ -38,7 +45,7 @@ export function buildIntegration(opts: BuildOptions): RollupOptions {
submodulePackageJson(
'@builder.io/partytown/integration',
opts.srcIntegrationDir,
opts.buildIntegrationDir,
opts.distIntegrationDir,
opts
),
],
Expand Down
4 changes: 2 additions & 2 deletions scripts/build-main-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()],
Expand Down
4 changes: 2 additions & 2 deletions scripts/build-main-snippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
6 changes: 3 additions & 3 deletions scripts/build-react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
],
Expand All @@ -21,7 +21,7 @@ export function buildReact(opts: BuildOptions): RollupOptions {
submodulePackageJson(
'@builder.io/partytown/react',
opts.srcReactDir,
opts.buildReactDir,
opts.distReactDir,
opts
),
],
Expand Down
10 changes: 4 additions & 6 deletions scripts/build-service-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)],
Expand All @@ -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()],
Expand Down Expand Up @@ -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 = `<!DOCTYPE html><html><head><meta charset="utf-8"><script src="./partytown-sandbox-${msgType}.js"></script></head></html>`;
} else {
sandboxHtml = `<!DOCTYPE html><html><head><meta charset="utf-8"><script type="module">${sandboxJsCode}</script></head></html>`;
Expand Down
6 changes: 2 additions & 4 deletions scripts/build-web-worker.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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;
Expand Down
106 changes: 78 additions & 28 deletions scripts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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');
Expand All @@ -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;
}
Loading

1 comment on commit 6e2b29e

@vercel
Copy link

@vercel vercel bot commented on 6e2b29e Oct 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.