Skip to content

Commit

Permalink
chore: remove external from config (#4355)
Browse files Browse the repository at this point in the history
* chore: remove external from config

* simplify named imports

* nits

* fix: mark fsevents as external

* fix: set interop to default

* Revert "fix: mark fsevents as external"

This reverts commit 4a5fa73.

* chore: bump chokidar

* simplify alias plugin params

* Revert "Revert "fix: mark fsevents as external""

This reverts commit 2ef6c5b.

* add bogus types for fsevents

* exclude fsevents from import linting

* add back comment

* bump typescript

* re-use env var

Co-authored-by: Lukas Taegert-Atkinson <lukastaegert@users.noreply.github.com>
  • Loading branch information
dnalborczyk and lukastaegert committed Jan 21, 2022
1 parent d1079a3 commit 2256dcd
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 76 deletions.
5 changes: 4 additions & 1 deletion .eslintrc.js
Expand Up @@ -73,7 +73,10 @@ module.exports = {
'dot-notation': 'error',
'import/no-unresolved': [
'error',
{ ignore: ['package.json', 'is-reference', 'help.md', 'types'] }
{
// 'fsevents' is ony available on macOS, and not installed on linux/windows
ignore: ['fsevents', 'help.md', 'is-reference', 'package.json', 'types']
}
],
'import/order': ['error', { alphabetize: { order: 'asc' } }],
'no-constant-condition': ['error', { checkLoops: false }],
Expand Down
2 changes: 1 addition & 1 deletion build-plugins/conditional-fsevents-import.ts
@@ -1,5 +1,5 @@
import MagicString from 'magic-string';
import { Plugin } from 'rollup';
import type { Plugin } from 'rollup';

const FSEVENTS_REQUIRE = "require('fsevents')";
const REPLACEMENT = "require('../../../src/watch/fsevents-importer').getFsEvents()";
Expand Down
68 changes: 42 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -75,7 +75,7 @@
"acorn-jsx": "^5.3.2",
"acorn-walk": "^8.2.0",
"buble": "^0.20.0",
"chokidar": "^3.5.2",
"chokidar": "^3.5.3",
"colorette": "^2.0.16",
"core-js": "^3.20.3",
"date-time": "^4.0.0",
Expand Down Expand Up @@ -114,7 +114,7 @@
"systemjs": "^6.11.0",
"terser": "^5.10.0",
"tslib": "^2.3.1",
"typescript": "^4.5.4",
"typescript": "^4.5.5",
"weak-napi": "^2.0.2",
"yargs-parser": "^20.2.9"
},
Expand Down
59 changes: 18 additions & 41 deletions rollup.config.ts
@@ -1,11 +1,12 @@
import { readFileSync } from 'fs';
import path from 'path';
import { resolve } from 'path';
import process from 'process';
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 { nodeResolve } from '@rollup/plugin-node-resolve';
import typescript from '@rollup/plugin-typescript';
import { RollupOptions, WarningHandlerWithDefault } from 'rollup';
import type { RollupOptions, WarningHandlerWithDefault } from 'rollup';
import { string } from 'rollup-plugin-string';
import { terser } from 'rollup-plugin-terser';
import addCliEntry from './build-plugins/add-cli-entry';
Expand All @@ -14,7 +15,7 @@ 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 replaceBrowserModules from './build-plugins/replace-browser-modules';
import pkg from './package.json';
import { version } from './package.json';

const commitHash = (function () {
try {
Expand All @@ -24,15 +25,12 @@ const commitHash = (function () {
}
})();

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

const banner = `/*
@license
Rollup.js v${pkg.version}
Rollup.js v${version}
${now} - commit ${commitHash}
https://github.com/rollup/rollup
Expand All @@ -50,11 +48,11 @@ const onwarn: WarningHandlerWithDefault = warning => {
};

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') }
],
entries: {
acorn: resolve('node_modules/acorn/dist/acorn.mjs'),
'help.md': resolve('cli/help.md'),
'package.json': resolve('package.json')
},
resolve: ['.js', '.json', '.md']
};

Expand All @@ -66,7 +64,7 @@ const treeshake = {

const nodePlugins = [
alias(moduleAliases),
resolve(),
nodeResolve(),
json(),
conditionalFsEventsImport(),
string({ include: '**/*.md' }),
Expand All @@ -80,24 +78,8 @@ const nodePlugins = [
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',
'os',
'path',
'perf_hooks',
'process',
'stream',
'url',
'util'
],
// 'fsevents' is a dependency of 'chokidar' that cannot be bundled as it contains binary code
external: ['fsevents'],
input: {
'loadConfigFile.js': 'cli/run/loadConfigFile.ts',
'rollup.js': 'src/node-entry.ts'
Expand All @@ -114,12 +96,7 @@ export default (command: Record<string, unknown>): RollupOptions | RollupOptions
format: 'cjs',
freeze: false,
generatedCode: 'es2015',
interop: id => {
if (id === 'fsevents') {
return 'defaultOnly';
}
return 'default';
},
interop: 'default',
manualChunks: { rollup: ['src/node-entry.ts'] },
sourcemap: true
},
Expand Down Expand Up @@ -160,7 +137,7 @@ export default (command: Record<string, unknown>): RollupOptions | RollupOptions
plugins: [
replaceBrowserModules(),
alias(moduleAliases),
resolve({ browser: true }),
nodeResolve({ browser: true }),
json(),
commonjs(),
typescript(),
Expand Down
10 changes: 5 additions & 5 deletions src/watch/fsevents-importer.ts
@@ -1,18 +1,18 @@
let fsEvents: unknown;
import type FsEvents from 'fsevents';

let fsEvents: typeof FsEvents;
let fsEventsImportError: Error | undefined;

export async function loadFsEvents(): Promise<void> {
const moduleName = 'fsevents';

try {
({ default: fsEvents } = await import(moduleName));
({ default: fsEvents } = await import('fsevents'));
} catch (err: any) {
fsEventsImportError = err;
}
}

// A call to this function will be injected into the chokidar code
export function getFsEvents(): unknown {
export function getFsEvents(): typeof FsEvents {
if (fsEventsImportError) throw fsEventsImportError;
return fsEvents;
}
6 changes: 6 additions & 0 deletions typings/fsevents.d.ts
@@ -0,0 +1,6 @@
// 'fsevents' (which also has typings included) is an optional dependency installed on macOS,
// and not installed on linux/windows. this will provide (bogus) type information for
// linux/windows, and overwrite (replace) the types coming with the 'fsevents' module on macOS
declare module 'fsevents' {
export default {};
}

0 comments on commit 2256dcd

Please sign in to comment.