Skip to content

Commit

Permalink
add support for relative imports in allowAppImports
Browse files Browse the repository at this point in the history
  • Loading branch information
mansona committed Nov 2, 2023
1 parent 6228d39 commit f3bc777
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
27 changes: 19 additions & 8 deletions packages/ember-auto-import/ts/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
WebpackPluginInstance,
Module,
} from 'webpack';
import { join, dirname, resolve, relative } from 'path';
import { join, dirname, resolve, relative, posix, isAbsolute } from 'path';
import { mergeWith, flatten, zip } from 'lodash';
import { writeFileSync, realpathSync, readFileSync } from 'fs';
import { compile, registerHelper } from 'handlebars';
Expand Down Expand Up @@ -302,18 +302,29 @@ export default class WebpackBundler extends Plugin implements Bundler {
if (request.startsWith('!')) {
return callback();
}
let name = packageName(request);
if (!name) {
// we're only interested in handling inter-package resolutions
return callback();
}
let pkg = packageCache.ownerOfFile(context);

let pkg = packageCache.ownerOfFile(context);
if (!pkg) {
// we couldn't find the package in the package cache
return callback();
}

let name = packageName(request);
if (!name) {
if (!isAbsolute(request) && pkg.root === this.opts.rootPackage.root) {
let appRelativeContext = relative(
resolve(this.opts.rootPackage.root, 'app'),
context
);

name = this.opts.rootPackage.name;
request = posix.join(name, appRelativeContext, request);
} else {
// we're only interested in handling inter-package resolutions
return callback();
}
}

// Handling full-name imports that point at the app itself e.g. app-name/lib/thingy
if (name === this.opts.rootPackage.name) {
if (this.importMatchesAppImports(request.slice(name.length + 1))) {
Expand Down Expand Up @@ -459,7 +470,7 @@ export default class WebpackBundler extends Plugin implements Bundler {
let nextModule = stats.compilation.moduleGraph.getModule(dep);
if (nextModule) {
if ((nextModule as any).externalType) {
ownExternals.add((dep as any).request);
ownExternals.add((nextModule as any).request);
} else {
gatherExternals(nextModule, ownExternals);
}
Expand Down
22 changes: 22 additions & 0 deletions test-scenarios/static-import-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ function staticImportTest(project: Project) {
export { default as example5 } from '@ef4/app-template/lib/example5.js';
// this will be externalised to amd and you cannont use an extension in this context :(
export { default as example6 } from '@ef4/app-template/utils/example6';
export { default as example7, secret_string as secret_string_7 } from '../utils/example7';
export default function () {
return 'example2 worked';
Expand All @@ -100,6 +101,8 @@ function staticImportTest(project: Project) {
utils: {
'example6.js':
'export default function() { return "example6 worked" }; export let dont_find_me = "2634a160bb3d83eae65ffd576f383dc35f77d6577402220d6f19e2eeea7e328a";',
'example7.js':
'export default function() { return "example7 worked" }; export let secret_string = "95c34c842bd06504a541559d0ebf104e0a135f9ebc42c7a9bbf99b70dd6a5c96";',
},
templates: {
'application.hbs': `{{hello-world}}`,
Expand Down Expand Up @@ -172,11 +175,14 @@ function staticImportTest(project: Project) {
example4,
example5,
example6,
example7,
please_find_me,
dont_find_me_4,
secret_string_7
} from '../../lib/example2';
import Service from '@ember/service';
import example6Direct, { dont_find_me } from '@ef4/app-template/utils/example6';
import example7Direct, { secret_string } from '@ef4/app-template/utils/example7';
async function checkScripts(scriptSrcPattern, needle) {
let scripts = [...document.querySelectorAll('script')];
Expand Down Expand Up @@ -237,6 +243,22 @@ function staticImportTest(project: Project) {
);
assert.strictEqual(example6, example6Direct);
});
test('local relative imports to files outside of appImports work and do show up in AMD loader', async function (assert) {
assert.equal(example7(), 'example7 worked');
assert.ok(
require.has('@ef4/app-template/utils/example7'),
'should have example7 in loader'
);
assert.strictEqual(example7, example7Direct, "example 7 object equality");
assert.notOk(
await checkScripts(/chunk/, secret_string_7),
"expect not to find the 'secret_string_7' sha in chunks"
);
assert.ok(
await checkScripts(/app-template.js/, secret_string_7),
"expect to find the 'secret_string_7' sha in app js asset"
);
});
test('make sure externalised import doesnt end up in the chunks', async function (assert) {
assert.ok(
await checkScripts(/chunk/, please_find_me),
Expand Down

0 comments on commit f3bc777

Please sign in to comment.