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

RFC: Rollup static build #4218

Open
wants to merge 1 commit into
base: master
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -14,3 +14,4 @@ test/tmp
test/typescript/typings
perf/
.nyc_output/
dist-static/
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -12,6 +12,7 @@
"build": "shx rm -rf dist && git rev-parse HEAD > .commithash && rollup --config rollup.config.ts --configPlugin typescript && shx cp src/rollup/types.d.ts dist/rollup.d.ts && shx chmod a+x dist/bin/rollup",
"build:cjs": "shx rm -rf dist && rollup --config rollup.config.ts --configPlugin typescript --configTest && shx cp src/rollup/types.d.ts dist/rollup.d.ts && shx chmod a+x dist/bin/rollup",
"build:bootstrap": "node dist/bin/rollup --config rollup.config.ts --configPlugin typescript && shx cp src/rollup/types.d.ts dist/rollup.d.ts && shx chmod a+x dist/bin/rollup",
"build:static": "shx rm -rf dist-static && git rev-parse HEAD > .commithash && rollup --config rollup.config.static.ts --configPlugin typescript && shx chmod a+x dist-static/rollup",
"ci:lint": "npm run lint:nofix",
"ci:test": "npm run build:cjs && npm run build:bootstrap && npm run test:all",
"ci:test:only": "npm run build:cjs && npm run build:bootstrap && npm run test:only",
Expand Down
124 changes: 124 additions & 0 deletions rollup.config.static.ts
@@ -0,0 +1,124 @@
import fs from 'fs';
import path from 'path';
import alias from '@rollup/plugin-alias';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import resolve from '@rollup/plugin-node-resolve';
import { RollupOptions, WarningHandlerWithDefault } from 'rollup';
import { string } from 'rollup-plugin-string';
import typescript from 'rollup-plugin-typescript';
import conditionalFsEventsImport from './build-plugins/conditional-fsevents-import';
import emitModulePackageFile from './build-plugins/emit-module-package-file';
import esmDynamicImport from './build-plugins/esm-dynamic-import';
import getLicenseHandler from './build-plugins/generate-license-file';
import pkg from './package.json';

const commitHash = (function () {
try {
return fs.readFileSync('.commithash', 'utf-8');
} catch (err) {
return 'unknown';
}
})();

const now = new Date(
process.env.SOURCE_DATE_EPOCH
? 1000 * parseInt(process.env.SOURCE_DATE_EPOCH)
: new Date().getTime()
).toUTCString();

const banner = `#! /usr/bin/env node
/*
@license
Rollup.js v${pkg.version}
${now} - commit ${commitHash}

https://github.com/rollup/rollup

Released under the MIT License.
*/`;

const onwarn: WarningHandlerWithDefault = warning => {
// eslint-disable-next-line no-console
console.error(
'Building Rollup produced warnings that need to be resolved. ' +
'Please keep in mind that the browser build may never have external dependencies!'
);
throw new Error(warning.message);
};

const moduleAliases = {
entries: [
{ find: 'help.md', replacement: path.resolve('cli/help.md') },
{ find: 'package.json', replacement: path.resolve('package.json') },
{ find: 'acorn', replacement: path.resolve('node_modules/acorn/dist/acorn.mjs') }
],
resolve: ['.js', '.json', '.md']
};

const treeshake = {
moduleSideEffects: false,
propertyReadSideEffects: false,
tryCatchDeoptimization: false
};

const nodePlugins = [
alias(moduleAliases),
resolve(),
json(),
conditionalFsEventsImport(),
string({ include: '**/*.md' }),
commonjs({ include: 'node_modules/**' }),
typescript()
];

export default (command: Record<string, unknown>): RollupOptions | RollupOptions[] => {
const { collectLicenses, writeLicense } = getLicenseHandler();
const commonJSBuild: RollupOptions = {
// fsevents is a dependency of chokidar that cannot be bundled as it contains binary code
external: [
'buffer',
'@rollup/plugin-typescript',
'assert',
'crypto',
'events',
'fs',
'fsevents',
'module',
'path',
'os',
'stream',
'url',
'util'
],
input: 'cli/cli.ts',
onwarn,
output: {
banner,
// TODO Only loadConfigFile is using default exports mode; this should be changed in Rollup@3
exports: 'auto',
externalLiveBindings: false,
file: 'dist-static/rollup',
format: 'cjs',
freeze: false,
inlineDynamicImports: true,
interop: id => {
if (id === 'fsevents') {
return 'defaultOnly';
}
return 'default';
},
sourcemap: false
},
plugins: [
...nodePlugins,
esmDynamicImport(),
// TODO this relied on an unpublished type update
(!command.configTest && collectLicenses()) as Plugin
],
strictDeprecations: true,
treeshake
};

return [commonJSBuild];
};