Skip to content

Commit

Permalink
Merge pull request #102 from Rich-Harris/esm-format
Browse files Browse the repository at this point in the history
support ?format=esm query
  • Loading branch information
Rich-Harris authored Jan 7, 2020
2 parents 8752342 + d2a3e71 commit f69123f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
27 changes: 16 additions & 11 deletions server/child-processes/create-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,24 +131,25 @@ function installDependencies(cwd) {
function bundle(cwd, deep, query) {
const pkg = require(`${cwd}/package.json`);
const moduleName = query.name || makeLegalIdentifier(pkg.name);
const format = query.format || 'umd';

const entry = deep
? path.resolve(cwd, deep)
: findEntry(
path.resolve(
cwd,
pkg.module || pkg['jsnext:main'] || pkg.main || 'index.js'
)
path.resolve(
cwd,
pkg.module || pkg['jsnext:main'] || pkg.main || 'index.js'
)
);

const code = sander.readFileSync(entry, { encoding: 'utf-8' });

if (isModule(code)) {
info(`[${pkg.name}] ES2015 module found, using Rollup`);
return bundleWithRollup(cwd, pkg, entry, moduleName);
return bundleWithRollup(cwd, pkg, entry, moduleName, format);
} else {
info(`[${pkg.name}] No ES2015 module found, using Browserify`);
return bundleWithBrowserify(pkg, entry, moduleName);
return bundleWithBrowserify(pkg, entry, moduleName, format);
}
}

Expand All @@ -162,7 +163,7 @@ function findEntry(file) {
}
}

async function bundleWithRollup(cwd, pkg, moduleEntry, name) {
async function bundleWithRollup(cwd, pkg, moduleEntry, name, format) {
const bundle = await rollup.rollup({
input: path.resolve(cwd, moduleEntry),
plugins: [
Expand All @@ -171,13 +172,13 @@ async function bundleWithRollup(cwd, pkg, moduleEntry, name) {
});

const result = await bundle.generate({
format: 'umd',
format,
name
});

if (result.output.length > 1) {
info(`[${pkg.name}] generated multiple chunks, trying Browserify instead`);
return bundleWithBrowserify(pkg, moduleEntry, name);
return bundleWithBrowserify(pkg, moduleEntry, name, format);
}

if (result.output[0].imports.length > 0) {
Expand All @@ -191,15 +192,19 @@ async function bundleWithRollup(cwd, pkg, moduleEntry, name) {
});

fs.writeFileSync(intermediate, code);
return bundleWithBrowserify(pkg, intermediate, name);
return bundleWithBrowserify(pkg, intermediate, name, format);
}

info(`[${pkg.name}] bundled using Rollup`);

return result.output[0].code;
}

function bundleWithBrowserify(pkg, main, moduleName) {
function bundleWithBrowserify(pkg, main, moduleName, format) {
if (format === 'esm') {
throw new Error(`Failed to generate ES module`);
}

const b = browserify(main, {
standalone: moduleName
});
Expand Down
4 changes: 4 additions & 0 deletions server/serve-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ module.exports = function servePackage(req, res, next) {
return query;
}, {});

if (query.format && (query.format !== 'umd' && query.format !== 'esm')) {
return sendBadRequest(res, 'Invalid format (must be umd or esm)');
}

get(`${registry}/${encodeURIComponent(qualified).replace('%40', '@')}`)
.then(JSON.parse)
.then(meta => {
Expand Down

0 comments on commit f69123f

Please sign in to comment.