Skip to content

Commit

Permalink
Merge pull request #530 from mansona/template-import-runtime
Browse files Browse the repository at this point in the history
Move Dynamic Template Import error to runtime instead of a build error
  • Loading branch information
ef4 committed Oct 13, 2022
2 parents 008e165 + 50ceb06 commit a9f5b9e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
7 changes: 1 addition & 6 deletions packages/ember-auto-import/ts/splitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,7 @@ export default class Splitter {
}

if (target.type === 'local') {
throw new Error(
`ember-auto-import does not support dynamic relative imports. "${leadingQuasi}" is relative. To make this work, you need to upgrade to Embroider. ` +
`The attempted import of '${imp.cookedQuasis.join(
''
)}' is located in ${imp.path}`
);
return;
}

if (target.type === 'imprecise') {
Expand Down
16 changes: 0 additions & 16 deletions packages/ember-auto-import/ts/tests/splitter-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,22 +343,6 @@ Qmodule('splitter', function (hooks) {
}
});

test('dynamic template relative imports are forbidden', async function (assert) {
assert.expect(1);
let src = 'import(`./thing/${foo}`)';
outputFileSync(join(project.baseDir, 'sample.js'), src);
await builder.build();
try {
await splitter.deps();
throw new Error(`expected not to get here, build was supposed to fail`);
} catch (err) {
assert.contains(
err.message,
`ember-auto-import does not support dynamic relative imports. "./thing/" is relative. To make this work, you need to upgrade to Embroider.`
);
}
});

test('exact alias remaps package name and root', async function (assert) {
setup({
alias: {
Expand Down
26 changes: 26 additions & 0 deletions test-scenarios/import-sync-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ appScenarios
Router.map(function () {
this.route('import-sync');
this.route('import-sync-relative-template');
this.route('import-sync-flavor', { path: '/flavor/:which' });
});
Expand All @@ -46,6 +47,7 @@ appScenarios
`,
templates: {
'import-sync.hbs': `<div data-test="import-sync-result">{{this.model}}</div>`,
'import-sync-relative-template.hbs': `<div data-test="import-sync-result">{{this.model.message}}</div>`,
'import-sync-flavor.hbs': `<div data-test="import-sync-result">{{this.model.name}}</div>`,
},
routes: {
Expand All @@ -68,6 +70,20 @@ appScenarios
` },
});
`,
'import-sync-relative-template.js':
`
import Route from '@ember/routing/route';
import { importSync } from '@embroider/macros';
export default Route.extend({
model() {
try {` +
' return importSync(`/a-dependency/${42}`); ' +
` throw new Error('you should not reach this point');
} catch (err) {
return { message: err.message }
}
},
});`
},
},
tests: {
Expand All @@ -94,6 +110,16 @@ appScenarios
assert.equal(currentURL(), '/flavor/vanilla');
assert.equal(document.querySelector('[data-test="import-sync-result"]').textContent.trim(), 'vanilla');
});
test('import-sync relative template string import', async function (assert) {
await visit('/import-sync-relative-template');
assert.equal(currentURL(), '/import-sync-relative-template');
assert.equal(
document.querySelector('[data-test="import-sync-result"]').textContent.trim(), ` +
"'Could not find module `_eai_sync_/a-dependency/${e}` imported from `(require)`'" +
`
);
})
});
`,
},
Expand Down

0 comments on commit a9f5b9e

Please sign in to comment.