Skip to content

Commit

Permalink
Merge pull request #749 from embroider-build/the-platform
Browse files Browse the repository at this point in the history
exposing some features for browser-based build tools
  • Loading branch information
ef4 authored Mar 31, 2021
2 parents f73b4b6 + 2f95b2c commit d36a045
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
3 changes: 2 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"default": "./src/index.js"
},
"./src/messages": "./src/messages.js",
"./src/babel-plugin-inline-hbs": "./src/babel-plugin-inline-hbs.js"
"./src/babel-plugin-inline-hbs": "./src/babel-plugin-inline-hbs.js",
"./src/mini-modules-polyfill": "./src/mini-modules-polyfill.js"
},
"files": [
"src/**/*.js",
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/babel-plugin-adjust-imports.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { emberVirtualPackages, emberVirtualPeerDeps, packageName as getPackageName } from '@embroider/shared-internals';
import { join, dirname, resolve } from 'path';
import { NodePath } from '@babel/traverse';
import type { NodePath } from '@babel/traverse';
import type * as t from '@babel/types';
import { PackageCache, Package, V2Package, explicitRelative } from '@embroider/shared-internals';
import { outputFileSync } from 'fs-extra';
import { Memoize } from 'typescript-memoize';
import { compile } from './js-handlebars';
import { emberModulesPolyfill } from './mini-modules-polyfill';
import { handleImportDeclaration } from './mini-modules-polyfill';

interface State {
adjustFile: AdjustFile;
Expand Down Expand Up @@ -418,7 +418,7 @@ function rewriteTopLevelImport(
}

if (opts.emberNeedsModulesPolyfill && path.isImportDeclaration()) {
let replacement = emberModulesPolyfill(t, path);
let replacement = handleImportDeclaration(t, path);
if (replacement) {
path.replaceWith(replacement);
return;
Expand Down
22 changes: 21 additions & 1 deletion packages/core/src/mini-modules-polyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,27 @@ let replacements: { [moduleSpecifier: string]: { [name: string]: ((t: BabelTypes
},
};

export function emberModulesPolyfill(t: BabelTypes, path: NodePath<t.ImportDeclaration>) {
export default function miniModulesPolyfill(babel: unknown) {
let t = (babel as any).types as BabelTypes;
return {
visitor: {
Program: {
exit(path: NodePath<t.Program>) {
for (let child of path.get('body')) {
if (child.isImportDeclaration()) {
let replacement = handleImportDeclaration(t, child);
if (replacement) {
path.replaceWith(replacement);
}
}
}
},
},
},
};
}

export function handleImportDeclaration(t: BabelTypes, path: NodePath<t.ImportDeclaration>) {
let match = replacements[path.node.source.value];
if (match) {
let specifiers = path.get('specifiers');
Expand Down
11 changes: 11 additions & 0 deletions packages/shared-internals/src/ember-standard-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,14 @@ export const emberVirtualPackages = new Set<string>(mappings.map((m: any) => m.m
// a v1 addon in between the app and a v2 addon might not declare the peerDep,
// breaking the deeper v2 addon.
export const emberVirtualPeerDeps = new Set<string>(['@glimmer/component']);

// this is a real package, even though it's still listed in rfc176
emberVirtualPackages.delete('@ember/string');
emberVirtualPeerDeps.add('@ember/string');

// these can also appear in ember code and should get compiled away by babel,
// but we may need to tell a build tool that is inspecting pre-transpiled code
// (like snowpack) not to worrya bout these packages.
emberVirtualPackages.add('@glimmer/env');
emberVirtualPackages.add('ember');
emberVirtualPackages.add('@embroider/macros');

0 comments on commit d36a045

Please sign in to comment.