Skip to content

Commit c737bbc

Browse files
authored
Merge pull request #20582 from emberjs/internal-coordination
Reducing internal usage of AMD loader
2 parents 74aa87f + f679f03 commit c737bbc

File tree

17 files changed

+449
-437
lines changed

17 files changed

+449
-437
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ module.exports = {
5555
'@typescript-eslint/ban-types': 'off',
5656
'@typescript-eslint/no-empty-function': 'off',
5757
'@typescript-eslint/no-this-alias': 'off',
58-
'@typescript-eslint/no-var-requires': 'warn',
58+
'@typescript-eslint/no-var-requires': 'error',
5959
'@typescript-eslint/consistent-type-imports': 'error',
6060

6161
// TODO: Enable and fix these rules

ember-cli-build.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,40 @@ function templateCompilerBundle(emberPackages, transpileTree) {
241241

242242
return concatBundle(new MergeTrees([templateCompilerFiles, emberHeaderFiles()]), {
243243
outputFile: 'ember-template-compiler.js',
244-
footer:
245-
'(function (m) { if (typeof module === "object" && module.exports) { module.exports = m } }(require("ember-template-compiler")));',
244+
footer: `
245+
try {
246+
// in the browser, the ember-template-compiler.js and ember.js bundles find each other via globalThis.require.
247+
require('@ember/template-compilation');
248+
} catch (err) {
249+
// in node, that coordination is a no-op
250+
define('@ember/template-compilation', ['exports'], function (e) {
251+
e.__registerTemplateCompiler = function () {};
252+
});
253+
define('ember', [
254+
'exports',
255+
'@ember/-internals/environment',
256+
'@ember/canary-features',
257+
'ember/version',
258+
], function (e, env, fea, ver) {
259+
e.default = {
260+
ENV: env.ENV,
261+
FEATURES: fea.FEATURES,
262+
VERSION: ver.default,
263+
};
264+
});
265+
define('@ember/-internals/glimmer', ['exports'], function(e) {
266+
e.template = undefined;
267+
});
268+
define('@ember/application', ['exports'], function(e) {});
269+
}
270+
271+
(function (m) {
272+
if (typeof module === 'object' && module.exports) {
273+
module.exports = m;
274+
}
275+
})(require('ember-template-compiler'));
276+
277+
`,
246278
});
247279
}
248280

lib/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,14 @@ module.exports = {
211211
return new MergeTrees([
212212
concatBundle(emberFiles, {
213213
outputFile: 'ember.js',
214-
footer: `require('@ember/-internals/bootstrap')`,
214+
footer: `
215+
(function bootstrap() {
216+
// Bootstrap Node module
217+
if (typeof module === 'object' && typeof module.require === 'function') {
218+
module.exports = require('ember').default;
219+
}
220+
})();
221+
`,
215222
}),
216223

217224
concatBundle(emberTestingFiles, {

packages/@ember/-internals/bootstrap/index.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

packages/@ember/template-compilation/index.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { DEBUG } from '@glimmer/env';
22
import type { TemplateFactory } from '@glimmer/interfaces';
3-
4-
export { compile as compileTemplate } from 'ember-template-compiler';
3+
import type * as ETC from 'ember-template-compiler';
54

65
interface CommonOptions {
76
moduleName?: string;
@@ -27,6 +26,16 @@ interface PrecompileTemplate {
2726
(templateString: string, options: StrictModeOptions): TemplateFactory;
2827
}
2928

29+
export let __emberTemplateCompiler: undefined | typeof ETC;
30+
export const compileTemplate: typeof ETC.compile = (...args: Parameters<typeof ETC.compile>) => {
31+
if (!__emberTemplateCompiler) {
32+
throw new Error(
33+
'Attempted to call `compileTemplate` without first loading the runtime template compiler.'
34+
);
35+
}
36+
return __emberTemplateCompiler.compile(...args);
37+
};
38+
3039
export let precompileTemplate: PrecompileTemplate;
3140

3241
if (DEBUG) {
@@ -36,3 +45,7 @@ if (DEBUG) {
3645
);
3746
};
3847
}
48+
49+
export function __registerTemplateCompiler(c: typeof ETC) {
50+
__emberTemplateCompiler = c;
51+
}

packages/@ember/test/index.ts

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
1-
import require, { has } from 'require';
2-
import { type Test as TestNS } from 'ember-testing';
1+
import type * as EmberTesting from 'ember-testing';
32

4-
export let registerAsyncHelper: (typeof TestNS)['registerAsyncHelper'];
5-
export let registerHelper: (typeof TestNS)['registerHelper'];
6-
export let registerWaiter: (typeof TestNS)['registerWaiter'];
7-
export let unregisterHelper: (typeof TestNS)['unregisterHelper'];
8-
export let unregisterWaiter: (typeof TestNS)['unregisterWaiter'];
3+
export let registerAsyncHelper: (typeof EmberTesting.Test)['registerAsyncHelper'];
4+
export let registerHelper: (typeof EmberTesting.Test)['registerHelper'];
5+
export let registerWaiter: (typeof EmberTesting.Test)['registerWaiter'];
6+
export let unregisterHelper: (typeof EmberTesting.Test)['unregisterHelper'];
7+
export let unregisterWaiter: (typeof EmberTesting.Test)['unregisterWaiter'];
8+
export let _impl: typeof EmberTesting | undefined;
99

10-
if (has('ember-testing')) {
11-
// SAFETY: since `require` is opaque to TS, we need to inform it that this is
12-
// the actual type of what we import. This `require` needs to stay in sync
13-
// with the `import type` statement above. (This cast *increases* safety,
14-
// because the result of `require` is `any`.)
15-
let Test = require('ember-testing').Test as typeof TestNS;
10+
let testingNotAvailableMessage = () => {
11+
throw new Error('Attempted to use test utilities, but `ember-testing` was not included');
12+
};
1613

14+
registerAsyncHelper = testingNotAvailableMessage;
15+
registerHelper = testingNotAvailableMessage;
16+
registerWaiter = testingNotAvailableMessage;
17+
unregisterHelper = testingNotAvailableMessage;
18+
unregisterWaiter = testingNotAvailableMessage;
19+
20+
export function registerTestImplementaiton(impl: typeof EmberTesting) {
21+
let { Test } = impl;
1722
registerAsyncHelper = Test.registerAsyncHelper;
1823
registerHelper = Test.registerHelper;
1924
registerWaiter = Test.registerWaiter;
2025
unregisterHelper = Test.unregisterHelper;
2126
unregisterWaiter = Test.unregisterWaiter;
22-
} else {
23-
let testingNotAvailableMessage = () => {
24-
throw new Error('Attempted to use test utilities, but `ember-testing` was not included');
25-
};
26-
27-
registerAsyncHelper = testingNotAvailableMessage;
28-
registerHelper = testingNotAvailableMessage;
29-
registerWaiter = testingNotAvailableMessage;
30-
unregisterHelper = testingNotAvailableMessage;
31-
unregisterWaiter = testingNotAvailableMessage;
27+
_impl = impl;
3228
}

packages/ember-template-compiler/index.ts

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,8 @@
1-
import { ENV } from '@ember/-internals/environment';
2-
import { FEATURES } from '@ember/canary-features';
3-
import * as _GlimmerSyntax from '@glimmer/syntax';
4-
import VERSION from 'ember/version';
5-
import require from 'require';
6-
7-
export let _Ember: unknown;
8-
9-
try {
10-
_Ember = require('ember');
11-
} catch (e) {
12-
_Ember = {
13-
ENV,
14-
FEATURES,
15-
VERSION,
16-
};
17-
}
18-
19-
export { default as precompile } from './lib/system/precompile';
20-
export { default as compile } from './lib/system/compile';
21-
export {
22-
default as compileOptions,
23-
buildCompileOptions as _buildCompileOptions,
24-
transformsFor as _transformsFor,
25-
} from './lib/system/compile-options';
26-
export { RESOLUTION_MODE_TRANSFORMS, STRICT_MODE_TRANSFORMS } from './lib/plugins';
27-
export { EmberPrecompileOptions } from './lib/types';
28-
29-
export { preprocess as _preprocess, print as _print } from '@glimmer/syntax';
30-
export { precompile as _precompile } from '@glimmer/compiler';
31-
32-
export { _GlimmerSyntax, VERSION };
1+
export * from './lib/public-api';
2+
import * as ETC from './lib/public-api';
3+
import { __registerTemplateCompiler } from '@ember/template-compilation';
334

5+
__registerTemplateCompiler(ETC);
346
// used to bootstrap templates
357
import './lib/system/bootstrap';
368

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export { default as _Ember } from 'ember';
2+
3+
import VERSION from 'ember/version';
4+
import * as _GlimmerSyntax from '@glimmer/syntax';
5+
6+
export { default as precompile } from './system/precompile';
7+
export { default as compile } from './system/compile';
8+
export {
9+
default as compileOptions,
10+
buildCompileOptions as _buildCompileOptions,
11+
transformsFor as _transformsFor,
12+
} from './system/compile-options';
13+
export { RESOLUTION_MODE_TRANSFORMS, STRICT_MODE_TRANSFORMS } from './plugins';
14+
export { EmberPrecompileOptions } from './types';
15+
16+
export { preprocess as _preprocess, print as _print } from '@glimmer/syntax';
17+
export { precompile as _precompile } from '@glimmer/compiler';
18+
19+
export { _GlimmerSyntax, VERSION };

packages/ember-template-compiler/lib/system/compile.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
/**
22
@module ember
33
*/
4-
import require, { has } from 'require';
54
import type { EmberPrecompileOptions } from '../types';
65
import precompile from './precompile';
76
import type { SerializedTemplateWithLazyBlock, TemplateFactory } from '@glimmer/interfaces';
8-
import type { templateFactory } from '@glimmer/opcode-compiler';
9-
10-
let template: typeof templateFactory;
7+
import { template } from '@ember/-internals/glimmer';
118

129
/**
1310
Uses HTMLBars `compile` function to process a string into a compiled template.
@@ -21,10 +18,6 @@ export default function compile(
2118
templateString: string,
2219
options: Partial<EmberPrecompileOptions> = {}
2320
): TemplateFactory {
24-
if (!template && has('@ember/-internals/glimmer')) {
25-
template = require('@ember/-internals/glimmer').template;
26-
}
27-
2821
if (!template) {
2922
throw new Error(
3023
'Cannot call `compile` with only the template compiler loaded. Please load `ember.debug.js` or `ember.prod.js` prior to calling `compile`.'

packages/ember-template-compiler/lib/system/initializer.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
import require, { has } from 'require';
21
import bootstrap from './bootstrap';
2+
import * as emberEnv from '@ember/-internals/browser-environment';
3+
import * as emberGlimmer from '@ember/-internals/glimmer';
4+
import * as emberApp from '@ember/application';
35

46
// Globals mode template compiler
5-
if (
6-
has('@ember/application') &&
7-
has('@ember/-internals/browser-environment') &&
8-
has('@ember/-internals/glimmer')
9-
) {
10-
let emberEnv = require('@ember/-internals/browser-environment');
11-
let emberGlimmer = require('@ember/-internals/glimmer');
12-
let emberApp = require('@ember/application');
7+
if (emberApp.default) {
138
let Application = emberApp.default;
149
let { hasTemplate, setTemplate } = emberGlimmer;
1510
let { hasDOM } = emberEnv;

packages/ember-testing/index.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
export { default as Test } from './lib/test';
2-
export { default as Adapter } from './lib/adapters/adapter';
3-
export { default as setupForTesting } from './lib/setup_for_testing';
4-
export { default as QUnitAdapter } from './lib/adapters/qunit';
1+
export * from './lib/public-api';
2+
import * as EmberTesting from './lib/public-api';
3+
import { registerTestImplementaiton } from '@ember/test';
54

6-
import './lib/ext/application';
7-
import './lib/ext/rsvp'; // setup RSVP + run loop integration
8-
import './lib/helpers'; // adds helpers to helpers object in Test
9-
import './lib/initializers'; // to setup initializer
5+
registerTestImplementaiton(EmberTesting);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export { default as Test } from './test';
2+
export { default as Adapter } from './adapters/adapter';
3+
export { default as setupForTesting } from './setup_for_testing';
4+
export { default as QUnitAdapter } from './adapters/qunit';
5+
6+
import './ext/application';
7+
import './ext/rsvp'; // setup RSVP + run loop integration
8+
import './helpers'; // adds helpers to helpers object in Test
9+
import './initializers'; // to setup initializer

packages/ember-testing/tests/reexports_test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Ember from 'ember';
22
import { confirmExport } from 'internal-test-helpers';
33
import { moduleFor, AbstractTestCase } from 'internal-test-helpers';
4+
import * as emberTesting from 'ember-testing';
45

56
class ReexportsTestCase extends AbstractTestCase {}
67

@@ -19,7 +20,7 @@ class ReexportsTestCase extends AbstractTestCase {}
1920
}
2021

2122
ReexportsTestCase.prototype[`@test Ember.${path} exports correctly`] = function (assert) {
22-
confirmExport(Ember, assert, path, moduleId, exportName);
23+
confirmExport(Ember, assert, path, moduleId, exportName, emberTesting);
2324
};
2425
});
2526

0 commit comments

Comments
 (0)