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

Bundle CSS files #205

Merged
merged 2 commits into from
Apr 18, 2019
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
8 changes: 6 additions & 2 deletions packages/ember-auto-import/ts/auto-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,12 @@ export default class AutoImport {

let mappings = new Map();
for (let name of this.bundles.names) {
let target = this.bundles.bundleEntrypoint(name);
mappings.set(`entrypoints/${name}`, target);
let byType = new Map();
mappings.set(`entrypoints/${name}`, byType);
for (let type of this.bundles.types) {
let target = this.bundles.bundleEntrypoint(name, type);
byType.set(type, target);
}
}

let passthrough = new Map();
Expand Down
31 changes: 20 additions & 11 deletions packages/ember-auto-import/ts/broccoli-append.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Plugin, { Tree } from 'broccoli-plugin';
import { join } from 'path';
import { join, extname } from 'path';
import walkSync, { WalkSyncEntry } from 'walk-sync';
import { unlinkSync, rmdirSync, mkdirSync, readFileSync, existsSync, writeFileSync, removeSync, readdirSync } from 'fs-extra';
import FSTree from 'fs-tree-diff';
Expand All @@ -17,10 +17,11 @@ import { insertBefore} from './source-map-url';
*/

export interface AppendOptions {
// map from a directory in the appendedTree (like `entrypoints/app`) to a file
// that may exists in the upstreamTree (like `assets/vendor.js`). Appends the
// JS files in the directory to that file, when it exists.
mappings: Map<string, string>;
// map from a directory in the appendedTree (like `entrypoints/app`) to a map
// keyed by file type (extension) containing file paths that may exists in the
// upstreamTree (like `assets/vendor.js`). Appends the JS/CSS files in the
// directory to that file, when it exists.
mappings: Map<string, Map<string, string>>;

// map from a directory in the appendedTree (like `lazy`) to a directory where
// we will output those files in the output (like `assets`).
Expand All @@ -30,7 +31,7 @@ export interface AppendOptions {
export default class Append extends Plugin {
private previousUpstreamTree = new FSTree();
private previousAppendedTree = new FSTree();
private mappings: Map<string, string>;
private mappings: Map<string, Map<string, string>>;
private reverseMappings: Map<string, string>;
private passthrough: Map<string, string>;

Expand All @@ -40,9 +41,13 @@ export default class Append extends Plugin {
persistentOutput: true
});

// mappings maps entry points to maps that map file types to output files.
// reverseMappings maps output files back to entry points.
let reverseMappings = new Map();
for (let [key, value] of options.mappings.entries( )) {
reverseMappings.set(value, key);
for (let [key, map] of options.mappings.entries( )) {
for (let value of map.values( )) {
reverseMappings.set(value, key);
}
}

this.mappings = options.mappings;
Expand All @@ -66,7 +71,10 @@ export default class Append extends Plugin {
for (let [, relativePath] of patchset) {
let match = findByPrefix(relativePath, this.mappings);
if (match) {
changed.add(match.mapsTo);
let ext = extname(relativePath).slice(1);
if (match.mapsTo.has(ext)) {
changed.add(match.mapsTo.get(ext));
}
}
}
return { needsUpdate: changed, passthroughEntries };
Expand Down Expand Up @@ -152,6 +160,7 @@ export default class Append extends Plugin {
private handleAppend(relativePath: string) {
let upstreamPath = join(this.upstreamDir, relativePath);
let outputPath = join(this.outputPath, relativePath);
let ext = extname(relativePath);

if (!existsSync(upstreamPath)) {
removeSync(outputPath);
Expand All @@ -165,7 +174,7 @@ export default class Append extends Plugin {
}

let appendedContent = readdirSync(sourceDir).map(name => {
if (/\.js$/.test(name)) {
if (name.endsWith(ext)) {
return readFileSync(join(sourceDir, name), 'utf8');
}
}).filter(Boolean).join(";\n");
Expand Down Expand Up @@ -200,7 +209,7 @@ interface PassthroughEntry extends WalkSyncEntry {

type AugmentedWalkSyncEntry = WalkSyncEntry | PassthroughEntry;

function findByPrefix(path: string, map: Map<string, string>) {
function findByPrefix<T>(path: string, map: Map<string, T>) {
let parts = path.split('/');
for (let i = 1; i < parts.length; i++) {
let candidate = parts.slice(0, i).join('/');
Expand Down
22 changes: 18 additions & 4 deletions packages/ember-auto-import/ts/bundle-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,27 @@ export default class BundleConfig {
return Object.freeze(['app', 'tests']);
}

get types(): ReadonlyArray<string> {
return Object.freeze(['js', 'css']);
}

// Which final JS file the given bundle's dependencies should go into.
bundleEntrypoint(name: string): string | undefined {
bundleEntrypoint(name: string, type: string): string | undefined {
switch (name) {
case 'tests':
return 'assets/test-support.js';
switch (type) {
case 'js':
return 'assets/test-support.js';
case 'css':
return 'assets/test-support.css';
}
case 'app':
return this.emberApp.options.outputPaths.vendor.js.replace(/^\//, '');
switch (type) {
case 'js':
return this.emberApp.options.outputPaths.vendor.js.replace(/^\//, '');
case 'css':
return this.emberApp.options.outputPaths.vendor.css.replace(/^\//, '');
}
}
}

Expand All @@ -36,6 +50,6 @@ export default class BundleConfig {
}

get lazyChunkPath() {
return dirname(this.bundleEntrypoint(this.names[0])!);
return dirname(this.bundleEntrypoint(this.names[0], 'js')!);
}
}
Loading