import { BroccoliConcatOptions , CSSBlocksEmberOptions , getConfig } from "@css-blocks/ember-utils" ; import broccoliConcat = require ( "broccoli-concat" ) ; import BroccoliDebug = require ( "broccoli-debug" ) ; import funnel = require ( "broccoli-funnel" ) ; import mergeTrees = require ( "broccoli-merge-trees" ) ; import EmberApp from "ember-cli/lib/broccoli/ember-app" ; import type Addon from "ember-cli/lib/models/addon" ; import type { AddonImplementation , ThisAddon } from "ember-cli/lib/models/addon" ; import Project from "ember-cli/lib/models/project" ; import { CSSBlocksApplicationPlugin , CSSBlocksStylesPostprocessorPlugin , CSSBlocksStylesPreprocessorPlugin } from "./broccoli-plugin" ; import { appStylesPostprocessFilename , cssBlocksPostprocessFilename , optimizedStylesPostprocessFilepath } from "./utils/filepaths" ; import { AddonEnvironment , CSSBlocksApplicationAddon } from "./utils/interfaces" ; css-blocks css-blocks todo const EMBER_ADDON = { /**
     * The name of this addon. Generally matches the package name in package.json.
     */ name : "@css-blocks/ember-app" , env : undefined , /**
     * The instance of the CSSBlocksApplicationPlugin. This instance is
     * generated during the JS tree and is needed for the CSS tree.
     */ broccoliAppPluginInstance : undefined , param parent param project init ( parent , project ) { this . _super . init && this . _super . init . call ( this , parent , project ) ; this . treePaths . app = "../runtime/app" ; this . treePaths . addon = "../runtime/addon" ; } , _modulePrefix ( ) { const parent = this . parent ; const config = typeof parent . config === "function" ? parent . config ( ) || { } : { } ; const name = typeof parent . name === "function" ? parent . name ( ) : parent . name ; const moduleName = typeof parent . moduleName === "function" ? parent . moduleName ( ) : parent . moduleName ; return moduleName || parent . modulePrefix || config . modulePrefix || name || "" ; } , getEnv ( this , parent ) { let current = this ; let app ; do { app = ( < Addon > current ) . app || app ; } while ( ( < Addon > ( < Addon > current ) . parent ) . parent && ( current = ( < Addon > current ) . parent ) ) ; let isApp = parent === app ; let rootDir = ( < Addon > parent ) . root || ( < EmberApp > parent ) . project . root ; let modulePrefix = this . _modulePrefix ( ) ; let appOptions = app ! . options ; if ( ! appOptions [ "css-blocks" ] ) { appOptions [ "css-blocks" ] = { } ; } let config = getConfig ( rootDir , app ! . isProduction , < CSSBlocksEmberOptions > appOptions [ "css-blocks" ] ) ; return { parent , app : app ! , rootDir , isApp , modulePrefix , config , } ; } , param parent included ( parent ) { this . _super . included . apply ( this , [ parent ] ) ; this . env = this . getEnv ( parent ) ; } , param type param tree returns preprocessTree ( type , tree ) { let env = this . env ! ; if ( type === "js" ) { if ( env . isApp ) { let lazyAddons = this . project . addons . filter ( ( a ) => a . lazyLoading && a . lazyLoading . enabled === true ) ; let jsOutputTrees = lazyAddons . map ( ( a ) => { let addon = a . addons . find ( ( child ) => child . name === "@css-blocks/ember" ) ; return addon && ( < any > addon ) . templateCompiler ; } ) . filter ( Boolean ) ; let lazyOutput = funnel ( mergeTrees ( jsOutputTrees ) , { destDir : "lazy-tree-output" } ) ; this . broccoliAppPluginInstance = new CSSBlocksApplicationPlugin ( env . modulePrefix , env . app . isProduction , [ env . app . addonTree ( ) , tree , lazyOutput ] , env . config ) ; let debugTree = new BroccoliDebug ( this . broccoliAppPluginInstance , `css-blocks:optimized` ) ; return funnel ( debugTree , { srcDir : env . modulePrefix , destDir : env . modulePrefix } ) ; } else { return tree ; } } else if ( type === "css" ) { if ( ! env . isApp ) { return tree ; } if ( ! this . broccoliAppPluginInstance ) { throw new Error ( "[css-blocks/ember-app] The CSS tree ran before the JS tree, so the CSS tree doesn't have the contents for CSS Blocks files. This shouldn't ever happen, but if it does, please file an issue with us!" ) ; } const cssBlocksContentsTree = new CSSBlocksStylesPreprocessorPlugin ( env . modulePrefix , env . config , [ this . broccoliAppPluginInstance , tree ] ) ; return new BroccoliDebug ( mergeTrees ( [ tree , cssBlocksContentsTree ] , { overwrite : true } ) , "css-blocks:css-preprocess" ) ; } else { return tree ; } } , param type param tree returns postprocessTree ( type , tree ) { let env = this . env ! ; if ( type === "css" ) { if ( ! env . isApp || env . config . broccoliConcat === false ) { return tree ; } let scannerTree ; if ( env . config . optimization . enabled ) { scannerTree = new BroccoliDebug ( new CSSBlocksStylesPostprocessorPlugin ( env , [ tree ] ) , "css-blocks:css-postprocess-preconcat" ) ; } else { scannerTree = tree ; } const concatTree = broccoliConcat ( scannerTree , buildBroccoliConcatOptions ( env ) ) ; const mergedTree = funnel ( mergeTrees ( [ tree , concatTree ] , { overwrite : true } ) , { exclude : [ cssBlocksPostprocessFilename ( env . config ) , optimizedStylesPostprocessFilepath , ] , } ) ; return new BroccoliDebug ( mergedTree , "css-blocks:css-postprocess" ) ; } return tree ; } , } ; param env returns function buildBroccoliConcatOptions ( env ) { const overrides = env . config . broccoliConcat || { } ; let sourceMapConfig ; sourceMapConfig = Object . assign ( { } , { enabled : true , } , overrides . sourceMapConfig , { extensions : [ "css" ] , mapCommentType : "block" , } ) ; return Object . assign ( { } , buildDefaultBroccoliConcatOptions ( env ) , env . config . broccoliConcat , { sourceMapConfig , } ) ; } param env returns function buildDefaultBroccoliConcatOptions ( env ) { const appCssPath = appStylesPostprocessFilename ( env ) ; const blocksCssPath = cssBlocksPostprocessFilename ( env . config ) ; return { inputFiles : [ blocksCssPath , appCssPath ] , outputFile : appCssPath , sourceMapConfig : { enabled : true , extensions : [ "css" ] , mapCommentType : "block" , } , } ; } type MaybeCSSBlocksTree = MaybeCSSBlocksTreePlugin | string ; interface MaybeCSSBlocksTreePlugin { _inputNodes : Array < MaybeCSSBlocksTree > | undefined ; isCssBlocksTemplateCompiler : boolean | undefined ; } module . exports = EMBER_ADDON ;