From ced84a027297770d465da7c8fdb62756e53e0717 Mon Sep 17 00:00:00 2001 From: Edward Faulkner Date: Thu, 4 Nov 2021 15:23:14 -0400 Subject: [PATCH] Address misleading warnings from rollup about externals When building a v2 addon we deliberately *don't* want rollup to do anything with imports of our dependencies or the various ember-provided package names we might refer to. And that is what already happens, except rollup emits warnings about them. This adds a plugin that handles these dependenices correctly, making them explicitly as external so there's no warning, while ensuring that if you stray from the allowed resolvable dependencies you will still see warnings. --- packages/addon-dev/sample-rollup.config.js | 6 +++ .../src/rollup-addon-dependencies.ts | 54 +++++++++++++++++++ packages/addon-dev/src/rollup.ts | 5 ++ 3 files changed, 65 insertions(+) create mode 100644 packages/addon-dev/src/rollup-addon-dependencies.ts diff --git a/packages/addon-dev/sample-rollup.config.js b/packages/addon-dev/sample-rollup.config.js index c2cb808bf..6dc9bf9d1 100644 --- a/packages/addon-dev/sample-rollup.config.js +++ b/packages/addon-dev/sample-rollup.config.js @@ -26,8 +26,14 @@ export default { // template colocation. babel({ plugins: ['@embroider/addon-dev/template-colocation-plugin'], + babelHelpers: 'bundled', }), + // Follow the V2 Addon rules about dependencies. Your code can import from + // `dependencies` and `peerDependencies` as well as standard Ember-provided + // package names. + addon.dependencies(), + // Ensure that standalone .hbs files are properly integrated as Javascript. addon.hbs(), diff --git a/packages/addon-dev/src/rollup-addon-dependencies.ts b/packages/addon-dev/src/rollup-addon-dependencies.ts new file mode 100644 index 000000000..d7bfa5468 --- /dev/null +++ b/packages/addon-dev/src/rollup-addon-dependencies.ts @@ -0,0 +1,54 @@ +import type { Plugin } from 'rollup'; +import { readJsonSync } from 'fs-extra'; +import { + emberVirtualPackages, + emberVirtualPeerDeps, + packageName, + templateCompilationModules, +} from '@embroider/shared-internals'; + +const compilationModules = new Set( + templateCompilationModules.map((m) => m.module) +); + +function resolvableDependencies() { + let deps = new Set(); + let pkg = readJsonSync('package.json'); + if (pkg.dependencies) { + for (let name of Object.keys(pkg.dependencies)) { + deps.add(name); + } + } + if (pkg.peerDependencies) { + for (let name of Object.keys(pkg.peerDependencies)) { + deps.add(name); + } + } + return deps; +} + +export default function emberExternals(): Plugin { + let deps = resolvableDependencies(); + + return { + name: 'ember-externals', + + async resolveId(source) { + let pkgName = packageName(source); + if (!pkgName) { + // No package name found means this is a relative import, which we don't + // need to deal with. + return; + } + + if ( + deps.has(pkgName) || + emberVirtualPeerDeps.has(pkgName) || + emberVirtualPackages.has(pkgName) || + compilationModules.has(pkgName) + ) { + return { id: source, external: true }; + } + }, + }; +} diff --git a/packages/addon-dev/src/rollup.ts b/packages/addon-dev/src/rollup.ts index bbc93d9f9..70661b3b7 100644 --- a/packages/addon-dev/src/rollup.ts +++ b/packages/addon-dev/src/rollup.ts @@ -3,6 +3,7 @@ import { default as publicEntrypoints } from './rollup-public-entrypoints'; import { default as appReexports } from './rollup-app-reexports'; import { default as clean } from 'rollup-plugin-delete'; import { default as keepAssets } from './rollup-keep-assets'; +import { default as dependencies } from './rollup-addon-dependencies'; import type { Plugin } from 'rollup'; export class Addon { @@ -63,4 +64,8 @@ export class Addon { output() { return { dir: this.#destDir, format: 'es', entryFileNames: '[name]' }; } + + dependencies() { + return dependencies(); + } }