Skip to content
This repository has been archived by the owner on Mar 10, 2023. It is now read-only.

Remove on-demand bundling and use a single entry #193

Open
wants to merge 1 commit into
base: parcel-integration
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@
"babel-plugin-transform-flow-strip-types": "^6.22.0",
"bolt-check": "^0.2.0",
"command-line-args": "^5.0.2",
"ejs": "^2.6.2",
"emotion": "^9.1.3",
"emotion-server": "^9.2.12",
"eslint-plugin-emotion": "^10.0.7",
Expand Down Expand Up @@ -187,4 +186,4 @@
"ts-loader": "^5.3.3",
"tti-polyfill": "^0.2.2"
}
}
}
11 changes: 11 additions & 0 deletions packages/website/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<head>
<link
rel="stylesheet"
href="node_modules/@atlaskit/css-reset/dist/bundle.css"
/>
</head>
<body>
Basic webpage
</body>
</html>
25 changes: 0 additions & 25 deletions packages/website/src/_app.js

This file was deleted.

117 changes: 21 additions & 96 deletions packages/website/src/bin/parcel-server.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/* eslint-disable no-restricted-syntax */
const Parcel = require('@parcel/core').default;
const express = require('express');
const path = require('path');
const fs = require('fs').promises;

const extensions = ['.tsx', '.ts', '.js'];

const configPath = require.resolve('@parcel/config-default');
const defaultConfig = {
Expand All @@ -14,116 +10,45 @@ const defaultConfig = {
};

const DIST_DIR = path.resolve('.dist');

// Resolve ourselves since we need to quickly determine whether the page exists
async function resolve(pagesRoot, request) {
const base = path.resolve(
pagesRoot,
request.slice(1), // remove the leading `/`
);
if (!base.startsWith(pagesRoot)) {
// Do not resolve anything outside the pages root.
return;
}

for (const basename of [base, path.join(base, 'index')]) {
for (const extension of extensions) {
const filePath = basename + extension;
try {
// eslint-disable-next-line no-await-in-loop
await fs.stat(filePath);
// eslint-disable-next-line consistent-return
return filePath;
} catch (e) {
// continue
}
}
}
}
const builtEntryPage = path.resolve(DIST_DIR, 'index.html');

module.exports = async function createParcelServer({
port,
pagesRoot,
staticRoot,
}) {
const parcelWatchSubscription = await new Parcel({
entries: [path.resolve(__dirname, '..', '..', 'index.html')],
defaultConfig,
killWorkers: false,
sourceMaps: false,
targets: {
main: {
distDir: DIST_DIR,
engines: {
browsers: ['last 1 Chrome version'],
},
publicUrl: '/',
},
},
}).watch();

const app = express();
app.set('view engine', 'ejs');
app.use(express.static(DIST_DIR));
app.use('/static', express.static(staticRoot));
const pageToBundles = new Map();

app.get('*', async (req, res) => {
const pagePath = await resolve(pagesRoot, req.url);
if (pagePath == null) {
res.sendStatus(404);
return;
}

let bundlePaths = pageToBundles.get(pagePath);
if (bundlePaths == null) {
// TODO: Handle multiple concurrent parcels during multiple requests
const bundleGraph = await new Parcel({
entries: [pagePath, path.resolve(__dirname, '../_app.js')],
defaultConfig,
killWorkers: false,
sourceMaps: false,
targets: {
main: {
distDir: DIST_DIR,
engines: {
browsers: ['last 1 Chrome version'],
},
publicUrl: '/',
},
},
}).run();

const bundles = bundleGraph.getBundles();
const pageBundle = bundles.find(b => b.isEntry && b.type === 'js');

// TODO: Currently Pacel creates shared bundles for entry points, and the resulting
// bundles are not entries but belong to the same bundle group as entries.
const otherEntryBundles = bundleGraph
.getBundlesInBundleGroup(
bundleGraph.getBundleGroupsContainingBundle(pageBundle)[0],
)
.filter(b => b.id !== pageBundle.id);

const entries = otherEntryBundles.concat(
bundleGraph.getBundles().filter(b => b.isEntry && b.type === 'js'),
);

if (entries.length < 2) {
throw new Error('Expected page entry and app');
}

bundlePaths = {
pageEntryAsset: pageBundle.getEntryAssets()[0].id,
scripts: entries.map(e => e.filePath),
stylesheets: bundles
.filter(b => b.isEntry && b.type === 'css')
.map(b => b.filePath),
};
pageToBundles.set(pagePath, bundlePaths);
}

res.render('page', {
pageEntryAsset: bundlePaths.pageEntryAsset,
scripts: bundlePaths.scripts.map(
bundlePath => `/${path.posix.relative(DIST_DIR, bundlePath)}`,
),
stylesheets: bundlePaths.stylesheets.map(
bundlePath => `/${path.posix.relative(DIST_DIR, bundlePath)}`,
),
});
app.get('*', async (_, res) => {
res.sendFile(builtEntryPage);
});

const server = app.listen(port != null ? port : 3001);
return {
dispose() {
async dispose() {
// `close` is on the returned server object, not on `app`.
// https://stackoverflow.com/questions/8659011/how-do-i-programmatically-shut-down-an-instance-of-expressjs-for-testing
server.close();
await parcelWatchSubscription.unsubscribe();
},
};
};
17 changes: 0 additions & 17 deletions packages/website/views/page.ejs

This file was deleted.