diff --git a/index.js b/index.js index e0b1b27..ed914f1 100644 --- a/index.js +++ b/index.js @@ -2,74 +2,106 @@ 'use strict'; var path = require('path'); var fs = require('fs'); -var babel = require('babel-core'); var VersionChecker = require('ember-cli-version-checker'); var Funnel = require('broccoli-funnel'); var mergeTrees = require('broccoli-merge-trees'); +var fastbootTransform = require('fastboot-transform'); +var esTranspiler = require('broccoli-babel-transpiler'); + module.exports = { name: 'ember-cli-foundation-6-sass', + + jsFilesToInclude: [], + + treeForVendor(vendorTree) { + let foundationTree; + let options = this.app.options['ember-cli-foundation-6-sass']; + let foundationPath; + let checker = new VersionChecker(this); + let isGTE6_3_0 = checker.for('foundation-sites', 'bower').satisfies('>=6.3.0'); + + if (options && options.foundationJs) { + if ((typeof options.foundationJs === 'string') || + (options.foundationJs instanceof String)) { + if (options.foundationJs === 'all') { + // >=6.3.0 changed some paths. + if (isGTE6_3_0) { + foundationPath = path.join(this.app.bowerDirectory, 'foundation-sites', 'dist', 'js'); + } else { + foundationPath = path.join(this.app.bowerDirectory, 'foundation-sites', 'js'); + } + } + } else if (options.foundationJs instanceof Array) { + foundationPath = path.join(this.app.bowerDirectory, 'foundation-sites', 'js'); + } + } + + if ( this.jsFilesToInclude === ['foundation.js'] ) { + foundationTree = new Funnel(foundationPath, { + destDir: 'foundation-sites', + files: this.jsFilesToInclude + }) + } else { + foundationTree = new Funnel(foundationPath, { + destDir: 'foundation-sites', + files: this.jsFilesToInclude + }); + + foundationTree = esTranspiler(foundationTree, { + plugins: [ + require.resolve('babel-plugin-transform-es2015-arrow-functions'), + require.resolve('babel-plugin-transform-es2015-block-scoped-functions'), + require.resolve('babel-plugin-transform-es2015-block-scoping'), + require.resolve('babel-plugin-transform-es2015-classes'), + require.resolve('babel-plugin-transform-es2015-destructuring'), + require.resolve('babel-plugin-transform-es2015-modules-commonjs'), + require.resolve('babel-plugin-transform-es2015-parameters'), + require.resolve('babel-plugin-transform-es2015-shorthand-properties'), + require.resolve('babel-plugin-transform-es2015-spread'), + require.resolve('babel-plugin-transform-es2015-template-literals') + ] + }); + } + + foundationTree = fastbootTransform(foundationTree); + + return new mergeTrees([vendorTree, foundationTree]); + }, + included: function included(app) { this._super.included(app); var options = app.options['ember-cli-foundation-6-sass']; - var foundationPath = path.join(app.bowerDirectory, 'foundation-sites'); - var checker = new VersionChecker(this); - var isGTE6_3_0 = checker.for('foundation-sites', 'bower').satisfies('>=6.3.0'); app.options.sassOptions = app.options.sassOptions || {}; app.options.sassOptions.includePaths = app.options.sassOptions.includePaths || []; - // >=6.3.0 changed some paths. - if (isGTE6_3_0) { - foundationPath = mergeTrees([new Funnel(foundationPath, { - include: ['_vendor/**/*'] - }), new Funnel(path.join(foundationPath, 'scss'), { - destDir: 'foundation-sites', - include: ['**/*'] - })]); - } + foundationPath = mergeTrees([new Funnel(foundationPath, { + include: ['_vendor/**/*'] + }), new Funnel(path.join(foundationPath, 'scss'), { + destDir: 'foundation-sites', + include: ['**/*'] + })]); app.options.sassOptions.includePaths.push(foundationPath); - // Include the js paths - if ( !process.env.EMBER_CLI_FASTBOOT ) { - if (options && options.foundationJs) { - if ((typeof options.foundationJs === 'string') || - (options.foundationJs instanceof String)) { - if (options.foundationJs === 'all') { - - // >=6.3.0 changed some paths. - if (isGTE6_3_0) { - app.import(path.join(app.bowerDirectory, 'foundation-sites', 'dist', 'js', 'foundation.js')); - } else { - app.import(path.join(app.bowerDirectory, 'foundation-sites', 'js', 'foundation.js')); - } - } - } - else if (options.foundationJs instanceof Array) { - options.foundationJs.forEach(function(componentName) { - var foundationJsPath = path.join(app.bowerDirectory, 'foundation-sites', 'js'); - var es5code = babel.transformFileSync(path.join(foundationJsPath, 'foundation.' + componentName + '.js'), { - 'plugins': [ - require.resolve('babel-plugin-transform-es2015-arrow-functions'), - require.resolve('babel-plugin-transform-es2015-block-scoped-functions'), - require.resolve('babel-plugin-transform-es2015-block-scoping'), - require.resolve('babel-plugin-transform-es2015-classes'), - require.resolve('babel-plugin-transform-es2015-destructuring'), - require.resolve('babel-plugin-transform-es2015-modules-commonjs'), - require.resolve('babel-plugin-transform-es2015-parameters'), - require.resolve('babel-plugin-transform-es2015-shorthand-properties'), - require.resolve('babel-plugin-transform-es2015-spread'), - require.resolve('babel-plugin-transform-es2015-template-literals') - ] - }).code; - var filenameAndPath = path.join(foundationJsPath, 'foundation.' + componentName + '.es5.js'); - fs.writeFileSync(filenameAndPath, es5code); - app.import(filenameAndPath); - }); + if (options && options.foundationJs) { + if ((typeof options.foundationJs === 'string') || + (options.foundationJs instanceof String)) { + if (options.foundationJs === 'all') { + this.jsFilesToInclude = ['foundation.js']; + app.import(path.join('vendor/foundation-sites', 'foundation.js')); } } + else if (options.foundationJs instanceof Array) { + options.foundationJs.forEach((componentName) => { + let filename = 'foundation.' + componentName + '.js'; + this.jsFilesToInclude.push(filename); + app.import(path.join('vendor/foundation-sites', filename)); + }); + } } } + }; diff --git a/package.json b/package.json index 9d0a5ef..47d7ecc 100644 --- a/package.json +++ b/package.json @@ -57,13 +57,15 @@ "babel-plugin-transform-es2015-spread": "^6.8.0", "babel-plugin-transform-es2015-template-literals": "^6.8.0", "babel-register": "^6.18.0", + "broccoli-babel-transpiler": "^6.1.1", "broccoli-funnel": "^1.2.0", "broccoli-merge-trees": "^2.0.0", "ember-cli-babel": "^6.1.0", "ember-cli-htmlbars": "^1.0.2", + "ember-cli-htmlbars-inline-precompile": "^0.4.2", "ember-cli-sass": "^6.1.3", "ember-cli-version-checker": "^1.3.1", - "ember-cli-htmlbars-inline-precompile": "^0.4.2" + "fastboot-transform": "^0.1.1" }, "ember-addon": { "configPath": "tests/dummy/config"