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

Add es-compat to make asset loaders work as expected #605

Merged
merged 2 commits into from
Dec 23, 2023
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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions packages/ember-auto-import/ts/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,20 @@ module.exports = (function(){
{{! this is only used for synchronous importSync() using a template string }}
return r('_eai_sync_' + specifier)(Array.prototype.slice.call(arguments, 1))
};
{{!- es compatibility. Where we're using "require", webpack doesn't necessarily know to apply its own ES compatibility stuff -}}
function esc(m) {
return m && m.__esModule ? m : Object.assign({ default: m }, m);
}
{{#each staticImports as |module|}}
d('{{js-string-escape module.specifier}}', EAI_DISCOVERED_EXTERNALS('{{module-to-id module.specifier}}'), function() { return require('{{js-string-escape module.specifier}}'); });
d('{{js-string-escape module.specifier}}', EAI_DISCOVERED_EXTERNALS('{{module-to-id module.specifier}}'), function() { return esc(require('{{js-string-escape module.specifier}}')); });
{{/each}}
{{#each dynamicImports as |module|}}
d('_eai_dyn_{{js-string-escape module.specifier}}', [], function() { return import('{{js-string-escape module.specifier}}'); });
{{/each}}
{{#each staticTemplateImports as |module|}}
d('_eai_sync_{{js-string-escape module.key}}', [], function() {
return function({{module.args}}) {
return require({{{module.template}}});
return esc(require({{{module.template}}}));
}
});
{{/each}}
Expand Down
2 changes: 1 addition & 1 deletion test-scenarios/leader-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Scenarios.fromProject(baseApp)
unit: {
'asset-test.js': `
import { module, test } from 'qunit';
import * as example from 'images/thing.png';
import example from 'images/thing.png';

module('Unit | webpack5', function () {
test('can use webpack5 asset loading', function (assert) {
Expand Down
27 changes: 27 additions & 0 deletions test-scenarios/static-import-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function staticImportTest(project: Project) {
],
allowAppImports: [
'lib/**',
'**/*.txt',
'assets/*.specialfile',
],
webpack: {
Expand All @@ -52,6 +53,10 @@ function staticImportTest(project: Project) {
test: /\.specialfile/,
use: 'specialfile-loader',
},
{
test: /\.txt/,
type: 'asset/resource',
},
],
},
},
Expand All @@ -64,6 +69,17 @@ function staticImportTest(project: Project) {
'reexport.js': `
export { default as innerLib } from 'inner-lib';
`,
'uses-an-asset.js': `
import txtURL from './images/example.txt';
export default async function fetchTxt() {
let response = await fetch(txtURL);
let body = await response.text();
return body;
}
`,
images: {
'example.txt': 'here is some text',
},
components: {
'hello-world.js': `
import Component from '@ember/component';
Expand Down Expand Up @@ -191,6 +207,17 @@ function staticImportTest(project: Project) {
});
});
`,
'asset-test.js': `
import { module, test } from 'qunit';
import loadTxt from '@ef4/app-template/uses-an-asset';

module('Unit | reexports are found', function () {
test('can load an asset URL', async function (assert) {
let txt = await loadTxt();
assert.equal(txt, 'here is some text');
});
});
`,
'import-into-tests-test.js': `
import { module, test } from 'qunit';
import { capitalize } from 'lodash-es';
Expand Down
Loading