/*eslint-disable no-multi-str */ 'use strict'; var envify = require('loose-envify/custom'); var grunt = require('grunt'); var UglifyJS = require('uglify-js'); var uglifyify = require('uglifyify'); var derequire = require('derequire'); var aliasify = require('aliasify'); var collapser = require('bundle-collapser/plugin'); var envifyDev = envify({NODE_ENV: process.env.NODE_ENV || 'development'}); var envifyProd = envify({NODE_ENV: process.env.NODE_ENV || 'production'}); var shimSharedModules = aliasify.configure({ 'aliases': { 'react/lib/React': 'react/lib/ReactUMDShim', 'react/lib/ReactCurrentOwner': 'react/lib/ReactCurrentOwnerUMDShim', 'react/lib/ReactComponentTreeHook': 'react/lib/ReactComponentTreeHookUMDShim', 'react/lib/getNextDebugID': 'react/lib/getNextDebugIDUMDShim', }, }); var shimDOMModules = aliasify.configure({ 'aliases': { './ReactAddonsDOMDependencies': {relative: './ReactAddonsDOMDependenciesUMDShim'}, }, }); var SIMPLE_TEMPLATE = grunt.file.read('./grunt/data/header-template-short.txt'); var LICENSE_TEMPLATE = grunt.file.read('./grunt/data/header-template-extended.txt'); function minify(src) { return UglifyJS.minify(src, {fromString: true}).code; } // TODO: move this out to another build step maybe. function bannerify(src) { var version = grunt.config.data.pkg.version; var packageName = this.data.packageName || this.data.standalone; return ( grunt.template.process( LICENSE_TEMPLATE, {data: {package: packageName, version: version}} ) + src ); } function simpleBannerify(src) { var version = grunt.config.data.pkg.version; var packageName = this.data.packageName || this.data.standalone; return ( grunt.template.process( SIMPLE_TEMPLATE, {data: {package: packageName, version: version}} ) + src ); } // What is happening here??? // I'm glad you asked. It became really hard to make our bundle splitting work. // Everything is fine in node and when bundling with those packages, but when // using our pre-packaged files, the splitting didn't work. Specifically due to // the UMD wrappers defining their own require and creating their own encapsulated // "registry" scope, we couldn't require across the boundaries. Webpack tries to // be smart and looks for top-level requires (even when aliasing to a bundle), // but since we didn't have those, we couldn't require 'react' from 'react-dom'. // But we are already shimming in some modules that look for a global React // variable. So we replace the UMD wrapper that browserify creates with out own, // in 2 steps. // 1. We swap out the browserify UMD with a plain function call. This ensures // that the internal wrapper doesn't interact with the external state. By the // time we're in the internal wrapper it doesn't matter what the external wrapper // detected. Browserify insulates its CommonJS system inside closures so can just // call that function and return it. // 2. We put our own UMD wrapper around that fixed internal function, ensuring // React is in scope. This outer wrapper is essentially the same UMD wrapper // browserify would create, just handling the scope issue. // Is this insane? Yes. // Does it work? Yes. // Should it go away ASAP? Yes. function wrapperify(src) { var toReplace = `function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.${this.data.standalone} = f()}}`; if (src.indexOf(toReplace) === -1) { throw new Error('wrapperify failed to find code to replace'); } src = src.replace( toReplace, `function(f){return f()}` ); return ` ;(function(f) { // CommonJS if (typeof exports === "object" && typeof module !== "undefined") { module.exports = f(require('react')); // RequireJS } else if (typeof define === "function" && define.amd) { define(['react'], f); //