Skip to content

Commit

Permalink
Merge pull request #605 from embroider-build/es-compat
Browse files Browse the repository at this point in the history
Add es-compat to make asset loaders work as expected
  • Loading branch information
ef4 authored Dec 23, 2023
2 parents ad83213 + d1be0e5 commit 7562cfd
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
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

0 comments on commit 7562cfd

Please sign in to comment.