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

Address misleading warnings from rollup about externals #1015

Merged
merged 1 commit into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/addon-dev/sample-rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),

Expand Down
54 changes: 54 additions & 0 deletions packages/addon-dev/src/rollup-addon-dependencies.ts
Original file line number Diff line number Diff line change
@@ -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 };
}
},
};
}
5 changes: 5 additions & 0 deletions packages/addon-dev/src/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -63,4 +64,8 @@ export class Addon {
output() {
return { dir: this.#destDir, format: 'es', entryFileNames: '[name]' };
}

dependencies() {
return dependencies();
}
}