From 39d8921059715fc24e453210460ac6927e935bae Mon Sep 17 00:00:00 2001 From: Jason Mitchell Date: Mon, 3 Aug 2015 00:45:57 -0700 Subject: [PATCH] the ability to import paths relative to app root --- README.md | 17 +++++++++++------ index.js | 44 ++++++++++++++++++++++---------------------- lib/broccoli-myth.js | 16 ++++++++-------- package.json | 6 +++--- 4 files changed, 44 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index e484595..ccdcf61 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Preprocess your CSS using Myth. ## Installation ``` -npm install --save-dev ember-myth +ember install ember-myth ``` ## Usage @@ -18,11 +18,16 @@ By default, this addon will compile `app/styles/.css` into `dist/asset Options can be configured within your `Brocfile.js` or from within `config/environment.js`: ```javascript -// Brocfile.js -var app = new EmberApp({ - mythOptions: {...} - ... -}); +// ember-cli-build.js +var EmberApp = require('ember-cli/lib/broccoli/ember-app'); + +module.exports = function(defaults) { + var app = new EmberApp(defaults, { + mythOptions: {...} + }); + + return app.toTree(); +}; ``` ```javascript diff --git a/index.js b/index.js index 77da027..7aa361e 100644 --- a/index.js +++ b/index.js @@ -4,42 +4,37 @@ var defaults = require('lodash.defaults'); var checker = require('ember-cli-version-checker'); var path = require('path'); - -var BroccoliMyth = require('./lib/broccoli-myth'); +var MythCompiler = require('./lib/broccoli-myth'); module.exports = EmberMyth; -function MythPlugin (mythOptions) { +function MythPlugin(options) { this.name = 'ember-myth'; this.ext = 'css'; - this.mythOptions = defaults({}, mythOptions); + this.options = defaults({}, options); } MythPlugin.prototype = { constructor: MythPlugin, toTree: function (tree, inputPath, outputPath) { + var options = this.options; var trees = []; if (tree) { trees.push(tree); } - // todo: look into this `paths` - if (this.mythOptions.paths) { - trees = trees.concat(this.mythOptions.paths); - } - - inputPath += '/' + this.mythOptions.inputFile; - outputPath += '/' + this.mythOptions.outputFile; + inputPath = path.join(inputPath, options.inputFile); + outputPath = path.join(outputPath, options.outputFile); - return new BroccoliMyth(trees, inputPath, outputPath, this.mythOptions); + return new MythCompiler(trees, inputPath, outputPath, options); } }; function EmberMyth (project) { this.project = project; - this.name = 'Ember CLI Myth'; + this.name = 'Ember CLI Myth'; } EmberMyth.prototype = { @@ -51,18 +46,18 @@ EmberMyth.prototype = { options: function(app) { var config = this.project.config(app.env) || {}; - var stylesDir; + var root = app.project.root; var mythOptions = defaults({}, app.options.mythOptions, config.mythOptions, { - paths: 'app/styles', + paths: [root], inputFile: 'app.css', - outputFile: 'app.css', + outputFile: this.project.name() + '.css', compress: app.env === 'production' }); if (!mythOptions.source) { - stylesDir = app.options.trees.styles.dir || app.options.trees.styles - mythOptions.source = path.join(app.project.root, stylesDir, mythOptions.inputFile); + var styles = app.options.trees.styles.dir || app.options.trees.styles + mythOptions.source = path.join(root, styles, mythOptions.inputFile); } return mythOptions; @@ -72,15 +67,20 @@ EmberMyth.prototype = { var app = registry.app; var mythOptions = this.options(app); registry.add('css', new MythPlugin(mythOptions)); - }, - included: function (app) { - this.app = app; + // prevent conflict with broccoli-myth if it's installed + if (registry.remove) { + registry.remove('css', 'broccoli-myth'); + } + }, - if (this._super) { + included: function(app) { + if (this._super && this._super.included) { this._super.included.apply(this, arguments); } + this.app = app; + if (this.shouldSetupRegistryInIncluded()) { this.setupPreprocessorRegistry('parent', app.registry); } diff --git a/lib/broccoli-myth.js b/lib/broccoli-myth.js index 7d288bf..587c811 100644 --- a/lib/broccoli-myth.js +++ b/lib/broccoli-myth.js @@ -27,21 +27,21 @@ function MythCompiler (sourceTrees, inputFile, outputFile, options) { } this.sourceTrees = sourceTrees; - this.outputFile = outputFile; - this.inputFile = inputFile; - this.mythOptions = options; + this.outputFile = outputFile; + this.inputFile = inputFile; + this.options = options; } MythCompiler.prototype.updateCache = function (srcDir, destDir) { - var destFile = destDir + '/' + this.outputFile; + var destFile = path.join(destDir, this.outputFile); - var mythOptions = defaults({}, this.mythOptions, { - filename: pathSearcher.findFileSync(this.inputFile, srcDir) + var options = defaults({}, this.options, { + filename: pathSearcher.findFileSync(this.inputFile, srcDir) }); mkdirp.sync(path.dirname(destFile)); - return readFile(mythOptions.filename, { encoding: 'utf8' }).then(function (input) { - return writeFile(destFile, myth(input, mythOptions).trim(), { encoding: 'utf8' }); + return readFile(options.filename, { encoding: 'utf8' }).then(function (input) { + return writeFile(destFile, myth(input, options).trim(), { encoding: 'utf8' }); }); } diff --git a/package.json b/package.json index d98720f..7794de8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ember-myth", - "version": "0.1.1", + "version": "0.2.0", "description": "Myth for ember-cli - css the way it was imagined", "repository": { "type": "git", @@ -20,12 +20,12 @@ "ember" ], "dependencies": { - "broccoli-caching-writer": "^0.5.5", + "broccoli-caching-writer": "^1.0.0", "ember-cli-version-checker": "^1.0.2", "include-path-searcher": "^0.1.0", "lodash.defaults": "^3.1.1", "mkdirp": "^0.5.1", - "myth": "^1.4.0", + "myth": "jasonmit/myth#92753f767f162a2e59627a5ea23a4053cdd20782", "rsvp": "^3.0.18" } }