Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite node module handling (npm plugin) #874

Draft
wants to merge 41 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
f3fbb1b
Add foundation for a new npm plugin
marvinhagemeister Sep 11, 2021
fde2f51
Acorn: Add missing exportDefaultDeclaration
marvinhagemeister Sep 11, 2021
5908aed
Add support for commonjs default exports
marvinhagemeister Sep 11, 2021
4682b9e
Add support for legacy sub packages
marvinhagemeister Sep 12, 2021
f1dee81
Add support for auto installing npm dependencies
marvinhagemeister Sep 12, 2021
46bb3f2
Add basic support for "exports" field
marvinhagemeister Sep 12, 2021
bd516a1
Fix unable to resolve scoped packages
marvinhagemeister Sep 12, 2021
7624518
Watcher: Ignore `.cache/` folder
marvinhagemeister Sep 12, 2021
34c4f08
Cleanup npm-plugin logging
marvinhagemeister Sep 12, 2021
4f153e8
Experiment with npm autoInstall
marvinhagemeister Sep 12, 2021
7b52e2c
Fix incorrect npm auto-install cache directory
marvinhagemeister Sep 12, 2021
da3bb3b
Fix commonjs rewriting non "module.exports" assginments
marvinhagemeister Sep 12, 2021
ad65d49
Add support for commonjs proxy modules
marvinhagemeister Sep 12, 2021
2139c51
Fix duplicate download requeusts
marvinhagemeister Sep 12, 2021
ac47f47
Drop if-statement if it's unreachable
marvinhagemeister Sep 12, 2021
cea027b
Remove single top level IIFE in CommonJS bundles
marvinhagemeister Sep 12, 2021
121c772
Switch to a non-string based transpiler for commonjs
marvinhagemeister Sep 16, 2021
560c362
Fix CommonJS file not being detected
marvinhagemeister Sep 16, 2021
86ce5cb
Include json in npm bundles
marvinhagemeister Sep 19, 2021
a0aa34a
Improve npm module bundling
marvinhagemeister Sep 19, 2021
9b1d76e
NPM: Bring back disk cache
marvinhagemeister Sep 19, 2021
31419e6
Update to zecorn 0.8.1
marvinhagemeister Sep 19, 2021
d5b5a2a
Use `writeFile` helper
marvinhagemeister Sep 19, 2021
7aa7999
Remove `setCwd` hack
marvinhagemeister Sep 19, 2021
1862bbf
Fix unable to load json in commonjs package
marvinhagemeister Sep 16, 2021
65104d9
NPM: Remove file dependencies on old plugin
marvinhagemeister Sep 29, 2021
b8dd4ac
NPM: Support loading assets from node modules
marvinhagemeister Sep 29, 2021
944d74e
NPM: Add back size warning plugin
marvinhagemeister Sep 29, 2021
4a13701
NPM: Remove old npm plugin
marvinhagemeister Sep 29, 2021
c2adc8a
Bring back etag caching for npm packages
marvinhagemeister Sep 29, 2021
b791f9b
Upgrade zecorn to fix codegen issues
marvinhagemeister Oct 4, 2021
300d7fc
Allow loading assets from auto-installed packages
marvinhagemeister Oct 6, 2021
755826e
NPM: Use already extracted package if available
marvinhagemeister Oct 6, 2021
ee489af
Support auto installing versioned packages
marvinhagemeister Oct 6, 2021
151def0
Fix incorrect CLI argument casing
marvinhagemeister Oct 6, 2021
a9e22b7
Specify custom npm registry via `--registry`
marvinhagemeister Oct 6, 2021
efe3a9e
Add changeset
marvinhagemeister Oct 6, 2021
bedc4da
NPM: Default to `index.js` if no entry point is found
marvinhagemeister Oct 6, 2021
52950ec
Update zecorn to 0.9.5
marvinhagemeister Oct 6, 2021
bbe841c
Reduce test CLI noise
marvinhagemeister Oct 6, 2021
3043967
WIP
marvinhagemeister Oct 7, 2021
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Bring back etag caching for npm packages
  • Loading branch information
marvinhagemeister committed Oct 6, 2021
commit c2adc8ae79d8076afee932f93a69e92bc42408c6
188 changes: 20 additions & 168 deletions packages/wmr/src/lib/npm-middleware.js
Original file line number Diff line number Diff line change
@@ -1,184 +1,36 @@
import * as rollup from 'rollup';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
// import unpkgPlugin from '../plugins/unpkg-plugin.js';
import npmPlugin, { normalizeSpecifier } from '../plugins/npm-plugin/index.js';
import { resolvePackageVersion, loadPackageFile } from '../plugins/npm-plugin/registry.js';
import { getCachedBundle, setCachedBundle, sendCachedBundle, enqueueCompress } from './npm-middleware-cache.js';
import processGlobalPlugin from '../plugins/process-global-plugin.js';
import aliasPlugin from '../plugins/aliases-plugin.js';
import { getMimeType } from './mimetypes.js';
import nodeBuiltinsPlugin from '../plugins/node-builtins-plugin.js';
import * as kl from 'kolorist';
import { hasDebugFlag, onWarn } from './output-utils.js';
import path from 'path';
import { getPackageInfo, isValidPackageName } from '../plugins/npm-plugin/utils.js';

/**
* Serve a "proxy module" that uses the WMR runtime to load CSS.
* @param {ReturnType<typeof normalizeSpecifier>} meta
* @param {import('http').ServerResponse} res
* @param {boolean} [isModule]
*/
async function handleAsset(meta, res, isModule) {
let code = '';
let type = null;

if (isModule) {
type = 'application/javascript;charset=utf-8';
const specifier = JSON.stringify('/@npm/' + meta.specifier + '?asset');
code = `import{style}from '/_wmr.js';\nstyle(${specifier});`;
} else {
type = getMimeType(meta.path);
code = await loadPackageFile(meta);
}
res.writeHead(200, {
'content-type': type || 'text/plain',
'content-length': Buffer.byteLength(code)
});
res.end(code);
}

/**
* @param {object} [options]
* @param {'npm'|'unpkg'} [options.source = 'npm'] How to fetch package files
* @param {Record<string,string>} [options.alias]
* @param {boolean} [options.optimize = true] Progressively minify and compress dependency bundles?
* @param {string} [options.cwd] Virtual cwd
* @returns {import('polka').Middleware}
*/
export default function npmMiddleware({ source = 'npm', alias, optimize, cwd } = {}) {
export function npmEtagCache() {
return async (req, res, next) => {
const url = new URL(req.url, 'https://localhost');
// @ts-ignore
const mod = url.pathname.replace(/^\//, '');
let id = path.posix.normalize(url.pathname);

const meta = normalizeSpecifier(mod);
if (!id.startsWith('/@npm/@id/')) {
return next();
}

try {
await resolvePackageVersion(meta);
} catch (e) {
return next(e);
id = id.slice('/@npm/@id/'.length);
if (!isValidPackageName(id)) {
return next();
}

const { name, version, pathname } = getPackageInfo(id);

try {
// The package name + path + version is a strong ETag since versions are immutable
const etag = Buffer.from(`${meta.specifier}${meta.version}`).toString('base64');
// The package name + version + pathname is a strong ETag since versions are immutablew
const etag = Buffer.from(`${name}${version}${pathname}`).toString('base64');
const ifNoneMatch = String(req.headers['if-none-match']).replace(/-(gz|br)$/g, '');

if (ifNoneMatch === etag) {
return res.writeHead(304).end();
}
res.setHeader('etag', etag);

// CSS files and proxy modules don't use Rollup.
if (/\.((css|s[ac]ss|less)|wasm|txt|json)$/.test(meta.path)) {
return handleAsset(meta, res, url.searchParams.has('module'));
}

res.setHeader('content-type', 'application/javascript;charset=utf-8');
if (hasDebugFlag()) {
// eslint-disable-next-line no-console
console.log(` ${kl.dim('middleware:') + kl.bold(kl.magenta('npm'))} ${JSON.stringify(meta.specifier)}`);
}
// serve from memory and disk caches:
const cached = await getCachedBundle(etag, meta, cwd);
if (cached) return sendCachedBundle(req, res, cached);

// const start = Date.now();
const code = await bundleNpmModule(mod, { source, alias, cwd });
// console.log(`Bundle dep: ${mod}: ${Date.now() - start}ms`);

// send it!
res.writeHead(200, { 'content-length': Buffer.byteLength(code) }).end(code);

// store the bundle in memory and disk caches
setCachedBundle(etag, code, meta, cwd);

// this is a new bundle, we'll compress it with terser and brotli shortly
if (optimize !== false) {
enqueueCompress(etag);
}
} catch (e) {
console.error(`Error bundling ${mod}: `, e);
next(e);
res.setHeader('etag', etag);
} catch (err) {
next(err);
}
};
}

let npmCache;

/**
* Bundle am npm module entry path into a single file
* @param {string} mod The module to bundle, including subpackage/path
* @param {object} options
* @param {'npm'|'unpkg'} [options.source]
* @param {Record<string,string>} [options.alias]
* @param {string} [options.cwd]
*/
async function bundleNpmModule(mod, { source, alias, cwd }) {
let npmProviderPlugin;

if (source === 'unpkg') {
throw Error('unpkg plugin is disabled');
// npmProviderPlugin = unpkgPlugin({
// publicPath: '/@npm',
// perPackage: true
// });
} else {
npmProviderPlugin = npmPlugin({
publicPath: '/@npm'
});
}

const bundle = await rollup.rollup({
input: mod,
onwarn: onWarn,
// input: '\0entry',
cache: npmCache,
shimMissingExports: true,
treeshake: false,
// inlineDynamicImports: true,
// shimMissingExports: true,
preserveEntrySignatures: 'allow-extension',
plugins: [
nodeBuiltinsPlugin({}),
aliasPlugin({ alias, cwd }),
npmProviderPlugin,
processGlobalPlugin({
sourcemap: false,
NODE_ENV: 'development'
}),
commonjs({
extensions: ['.js', '.cjs', ''],
sourceMap: false,
transformMixedEsModules: true
}),
json(),
{
name: 'no-builtins',
load(s) {
if (s === 'fs' || s === 'path') {
return 'export default {};';
}
}
},
{
name: 'never-disk',
load(s) {
throw Error('local access not allowed');
}
}
]
});

npmCache = bundle.cache;

const { output } = await bundle.generate({
format: 'es',
indent: false,
// entryFileNames: '[name].js',
// chunkFileNames: '[name].js',
// Don't transform paths at all:
paths: String
});

return output[0].code;
next();
};
}
1 change: 1 addition & 0 deletions packages/wmr/src/plugins/npm-plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export function npmPlugin({ cwd, autoInstall, production }) {
/** @type {Map<string, { code: string, map: any }>} */
const chunkCache = new Map();

/** @type {Map<string, string>} */
const entryToChunk = new Map();

/** @type {Map<string, import('./utils').Deferred>} */
Expand Down
7 changes: 5 additions & 2 deletions packages/wmr/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import WebSocketServer from './lib/websocket-server.js';
import * as kl from 'kolorist';
import * as errorstacks from 'errorstacks';
import { hasDebugFlag } from './lib/output-utils.js';
import { npmEtagCache } from './lib/npm-middleware.js';

/**
* @typedef CustomServer
Expand All @@ -17,8 +18,8 @@ import { hasDebugFlag } from './lib/output-utils.js';

/**
* @param {object} options
* @param {string} [options.cwd = ''] Directory to serve
* @param {string} [options.root] Virtual process.cwd
* @param {string} options.cwd Directory to serve
* @param {string} options.root Virtual process.cwd
* @param {string} [options.publicDir] A directory containing public files, relative to cwd
* @param {string} [options.overlayDir] A directory of generated files to serve if present, relative to cwd
* @param {polka.Middleware[]} [options.middleware] Additional Polka middlewares to inject
Expand Down Expand Up @@ -97,6 +98,8 @@ export default async function server({ cwd, root, overlayDir, middleware, http2,

app.ws = new WebSocketServer(app.server, '/_hmr');

app.use(npmEtagCache());

if (compress) {
// @TODO: reconsider now that npm deps are compressed AOT
const threshold = compress === true ? 1024 : compress;
Expand Down