Skip to content

Commit 8b2d595

Browse files
committed
feat: Enable optimization for production builds by default.
1 parent a626962 commit 8b2d595

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

packages/@css-blocks/ember-cli/ember-cli-build.js

-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ module.exports = function(defaults) {
1212
rewriteIdents: true,
1313
mergeDeclarations: true,
1414
removeUnusedStyles: true,
15-
conflictResolution: true,
16-
enabled: false //process.env.EMBER_ENV !== 'development',
1715
},
1816
}
1917
});

packages/@css-blocks/ember-cli/index.js

+46-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { CSSBlocksAggregate, CSSBlocksAnalyze, Transport } = require("@css-blocks
55
const { GlimmerAnalyzer, GlimmerRewriter } = require("@css-blocks/glimmer");
66
const { NodeJsImporter, BlockFactory } = require("@css-blocks/core");
77
const config = require('@css-blocks/config');
8+
const merge = require("lodash.merge");
89

910
const BroccoliConcat = require("broccoli-concat");
1011
const BroccoliFunnel = require("broccoli-funnel");
@@ -31,6 +32,30 @@ const NOOP_PLUGIN = {
3132
cacheKey: () => { return 1; }
3233
};
3334

35+
class IDAllocator {
36+
constructor(startValue, defaultMaxCount) {
37+
this.modules = new Map();
38+
this.startValue = startValue;
39+
this.defaultMaxCount = defaultMaxCount;
40+
}
41+
allocateRange(maxCount = undefined) {
42+
maxCount = maxCount || this.defaultMaxCount;
43+
let startValue = this.startValue;
44+
this.startValue += maxCount;
45+
return {startValue, maxCount};
46+
}
47+
getRangeForModuleAndType(mod, type) {
48+
if (!this.modules.has(mod)) {
49+
this.modules.set(mod, new Map());
50+
}
51+
let ranges = this.modules.get(mod);
52+
if (!ranges.has(type)) {
53+
ranges.set(type, this.allocateRange());
54+
}
55+
return ranges.get(type);
56+
}
57+
}
58+
3459
module.exports = {
3560
name: '@css-blocks/ember-cli',
3661
outputFile: 'app.css',
@@ -187,10 +212,10 @@ module.exports = {
187212
parent.preprocessTree = (type, tree) => origPreprocessTree(type, type === "css" ? withoutCssBlockFiles(tree) : tree);
188213

189214
// Analyze all templates and block files from `/app` in addons.
190-
parent.treeForApp = this.genTreeWrapper(env, options, parent.treeForApp);
215+
parent.treeForApp = this.genTreeWrapper(env, options, 'addonApp', parent.treeForApp);
191216

192217
// Analyze all templates and block files from `/addon` in addons.
193-
parent.treeForAddon = this.genTreeWrapper(env, options, parent.treeForAddon);
218+
parent.treeForAddon = this.genTreeWrapper(env, options, 'addon', parent.treeForAddon);
194219

195220
// Analyze all templates and block files from `/app` in Ember apps.
196221
// Analyze all templates and block files from `/src` in Glimmer apps.
@@ -204,7 +229,7 @@ module.exports = {
204229
let packageJsonTree = BroccoliFunnel(env.rootDir, {include: ["package.json"]});
205230
tree = BroccoliMerge([tree, packageJsonTree]);
206231
}
207-
parent.trees[treeName] = this.genTreeWrapper(env, options)(tree);
232+
parent.trees[treeName] = this.genTreeWrapper(env, options, 'app')(tree);
208233
}
209234

210235
},
@@ -271,12 +296,14 @@ module.exports = {
271296

272297
getOptions(env) {
273298

274-
let { isEmber, app, rootDir } = env;
299+
let { isEmber, app, rootDir, modulePrefix } = env;
300+
301+
if (!app.options["css-blocks"]) {
302+
app.options["css-blocks"] = {};
303+
}
275304

276305
// Get CSS Blocks options provided by the application, if present.
277-
const options = app.options["css-blocks"]
278-
? app.options["css-blocks"] // Do not clone! Contains non-json-safe data.
279-
: {};
306+
const options = app.options["css-blocks"] // Do not clone! Contains non-json-safe data.
280307
options.aliases || (options.aliases = {});
281308
options.analysisOpts || (options.analysisOpts = {});
282309
options.optimization || (options.optimization = {});
@@ -289,7 +316,9 @@ module.exports = {
289316
options.parserOpts.importer = options.parserOpts.importer || new NodeJsImporter(options.aliases);
290317

291318
// Optimization is always disabled for now, until we get project-wide analysis working.
292-
options.optimization.enabled = false;
319+
if (typeof options.optimization.enabled === "undefined") {
320+
options.optimization.enabled = app.isProduction;
321+
}
293322

294323
// Update parserOpts to include the absolute path to our application code directory.
295324
if (!options.parserOpts.rootDir) {
@@ -332,7 +361,7 @@ module.exports = {
332361
};
333362
},
334363

335-
genTreeWrapper(env, options, prev = NOOP) {
364+
genTreeWrapper(env, options, type, prev = NOOP) {
336365
const { isEmber, app, parent, rootDir, moduleConfig, modulePrefix } = env;
337366

338367
// In Ember, we treat every template as an entry point. `BroccoliCSSBlocks` will
@@ -347,11 +376,18 @@ module.exports = {
347376
let analyzer = new GlimmerAnalyzer(new BlockFactory(options.parserOpts), options.analysisOpts, moduleConfig);
348377
analyzer.transport = transport;
349378

379+
if (!this.idAllocator) {
380+
let identifiers = options.optimization.identifiers || {};
381+
this.idAllocator = new IDAllocator(identifiers.startValue || 1, identifiers.maxCount || 500);
382+
}
383+
let identifiers = this.idAllocator.getRangeForModuleAndType(this.parent, type);
384+
let optimizationOptions = merge({}, options.optimization, {identifiers});
385+
DEBUG(`Optimization is ${optimizationOptions.enabled ? 'enabled' : 'disabled'}. Identifier allocation for ${modulePrefix}/${type} is ${identifiers.startValue} - ${identifiers.startValue + identifiers.maxCount - 1}`);
350386
const broccoliOptions = {
351387
analyzer,
352388
entry,
353389
output: options.output,
354-
optimization: options.optimization,
390+
optimization: optimizationOptions,
355391
root: rootDir,
356392
};
357393

packages/@css-blocks/ember-cli/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"debug": "^4.1.1",
3333
"ember-cli-babel": "7.11.0",
3434
"fs-extra": "^8.0.0",
35+
"lodash.merge": "^4.6.2",
3536
"symlink-or-copy": "^1.2.0"
3637
},
3738
"devDependencies": {

0 commit comments

Comments
 (0)