@@ -48,6 +48,7 @@ appScenarios
48
48
'rollup.config.mjs' : `
49
49
import { babel } from '@rollup/plugin-babel';
50
50
import { Addon } from '@embroider/addon-dev/rollup';
51
+ import { resolve, dirname } from 'path';
51
52
52
53
const addon = new Addon({
53
54
srcDir: 'src',
@@ -64,6 +65,7 @@ appScenarios
64
65
plugins: [
65
66
addon.publicEntrypoints([
66
67
'components/**/*.js',
68
+ 'asset-examples/**/*.js',
67
69
], {
68
70
exclude: ['**/-excluded/**/*'],
69
71
}),
@@ -79,6 +81,37 @@ appScenarios
79
81
addon.gjs(),
80
82
addon.dependencies(),
81
83
addon.publicAssets('public'),
84
+ addon.keepAssets(["**/*.css"]),
85
+
86
+ // this works with custom-asset plugin below to exercise whether we can keepAssets
87
+ // for generated files that have exports
88
+ addon.keepAssets(["**/*.xyz"], "default"),
89
+ {
90
+ name: 'virtual-css',
91
+ resolveId(source, importer) {
92
+ if (source.endsWith('virtual.css')) {
93
+ return { id: resolve(dirname(importer), source) }
94
+ }
95
+ },
96
+ load(id) {
97
+ if (id.endsWith('virtual.css')) {
98
+ return '.my-blue-example { color: blue }'
99
+ }
100
+ }
101
+ },
102
+ {
103
+ name: 'custom-plugin',
104
+ resolveId(source, importer) {
105
+ if (source.endsWith('.xyz')) {
106
+ return { id: resolve(dirname(importer), source) }
107
+ }
108
+ },
109
+ load(id) {
110
+ if (id.endsWith('.xyz')) {
111
+ return 'Custom Content';
112
+ }
113
+ }
114
+ },
82
115
83
116
babel({ babelHelpers: 'bundled', extensions: ['.js', '.hbs', '.gjs'] }),
84
117
@@ -156,6 +189,23 @@ appScenarios
156
189
` ,
157
190
} ,
158
191
} ,
192
+ 'asset-examples' : {
193
+ 'has-css-import.js' : `
194
+ import "./styles.css";
195
+ ` ,
196
+ 'styles.css' : `
197
+ .my-red-example { color: red }
198
+ ` ,
199
+ 'has-virtual-css-import.js' : `
200
+ import "./my-virtual.css";
201
+ ` ,
202
+ 'has-custom-asset-import.js' : `
203
+ import value from './custom.xyz';
204
+ export function example() {
205
+ return value;
206
+ }
207
+ ` ,
208
+ } ,
159
209
} ,
160
210
public : {
161
211
'thing.txt' : 'hello there' ,
@@ -286,8 +336,54 @@ appScenarios
286
336
});
287
337
});
288
338
` ,
339
+ 'asset-test.js' : `
340
+ import { module, test } from 'qunit';
341
+
342
+ module('keepAsset', function (hooks) {
343
+ let initialClassList;
344
+ hooks.beforeEach(function() {
345
+ initialClassList = document.body.classList;
346
+ });
347
+
348
+ hooks.afterEach(function() {
349
+ document.body.classList = initialClassList;
350
+ });
351
+
352
+ test('Normal CSS', async function (assert) {
353
+ await import("v2-addon/asset-examples/has-css-import");
354
+ document.body.classList.add('my-red-example');
355
+ assert.strictEqual(getComputedStyle(document.querySelector('body')).color, 'rgb(255, 0, 0)');
356
+ });
357
+
358
+ test("Virtual CSS", async function (assert) {
359
+ await import("v2-addon/asset-examples/has-virtual-css-import");
360
+ document.body.classList.add('my-blue-example');
361
+ assert.strictEqual(getComputedStyle(document.querySelector('body')).color, 'rgb(0, 0, 255)');
362
+ });
363
+
364
+ test("custom asset with export", async function(assert) {
365
+ let { example } = await import("v2-addon/asset-examples/has-custom-asset-import");
366
+ assert.strictEqual(example(), "Custom Content");
367
+ });
368
+ })
369
+ ` ,
289
370
} ,
290
371
} ) ;
372
+
373
+ project . files [ 'vite.config.mjs' ] = ( project . files [ 'vite.config.mjs' ] as string ) . replace (
374
+ 'contentFor(),' ,
375
+ `
376
+ contentFor(),
377
+ {
378
+ name: "xyz-handler",
379
+ transform(code, id) {
380
+ if (id.endsWith('.xyz')) {
381
+ return \`export default "\${code}"\`
382
+ }
383
+ }
384
+ },
385
+ `
386
+ ) ;
291
387
} )
292
388
. forEachScenario ( scenario => {
293
389
Qmodule ( scenario . name , function ( hooks ) {
@@ -399,6 +495,28 @@ export { SingleFileComponent as default };
399
495
'./public/other.txt' : '/other.txt' ,
400
496
} ) ;
401
497
} ) ;
498
+
499
+ test ( 'keepAssets works for real css files' , async function ( ) {
500
+ expectFile ( 'dist/asset-examples/has-css-import.js' ) . equalsCode ( `import './styles.css'` ) ;
501
+ expectFile ( 'dist/asset-examples/styles.css' ) . matches ( '.my-red-example { color: red }' ) ;
502
+ } ) ;
503
+
504
+ test ( 'keepAssets works for css generated by another plugin' , async function ( ) {
505
+ expectFile ( 'dist/asset-examples/has-virtual-css-import.js' ) . equalsCode ( `import './my-virtual.css'` ) ;
506
+ expectFile ( 'dist/asset-examples/my-virtual.css' ) . matches ( '.my-blue-example { color: blue }' ) ;
507
+ } ) ;
508
+
509
+ test ( 'keepAssets tolerates non-JS content that is interpreted as having a default export' , async function ( ) {
510
+ expectFile ( 'dist/asset-examples/has-custom-asset-import.js' ) . equalsCode ( `
511
+ import _asset_0_ from './custom.xyz'
512
+ var value = _asset_0_;
513
+ function example() {
514
+ return value;
515
+ }
516
+ export { example }
517
+ ` ) ;
518
+ expectFile ( 'dist/asset-examples/custom.xyz' ) . matches ( `Custom Content` ) ;
519
+ } ) ;
402
520
} ) ;
403
521
404
522
Qmodule ( 'Consuming app' , function ( ) {
0 commit comments