|
| 1 | +// TODO: remove lodash and other dependencies |
| 2 | +var _ = require('lodash') |
| 3 | +var path = require('path') |
| 4 | +var url = require('url') |
| 5 | +var fs = require('fs') |
| 6 | + |
| 7 | +function PhpOutputPlugin(options) { |
| 8 | + var defaults = { |
| 9 | + outPutPath: false, // false for default webpack path of pass string to specify |
| 10 | + assetsPathPrefix: '', |
| 11 | + phpClassName: 'WebpackBuiltFiles', // |
| 12 | + phpFileName: 'WebpackBuiltFiles', |
| 13 | + nameSpace: false, // false {nameSpace: 'name', use: ['string'] or empty property or don't pass "use" property} |
| 14 | + path: '', |
| 15 | + extraPurePatches: [], |
| 16 | + } |
| 17 | + |
| 18 | + this.options = _.defaults(options, defaults) |
| 19 | +} |
| 20 | + |
| 21 | +PhpOutputPlugin.prototype.apply = function apply(compiler) { |
| 22 | + var options = this.options |
| 23 | + |
| 24 | + var getCssFiles = function(filelist, filepath) { |
| 25 | + return _.map( |
| 26 | + _.filter(filelist, filename => filename.endsWith('.css') && !filename.startsWith('editor-style')), // filtering |
| 27 | + filename => path.join(options.assetsPathPrefix, filepath, filename) // mapping filtered |
| 28 | + ) |
| 29 | + } |
| 30 | + |
| 31 | + var getJsFiles = function(filelist, filepath) { |
| 32 | + let files = _.map( |
| 33 | + _.filter(filelist, filename => filename.endsWith('.js') && !filename.startsWith('editor-style')), // filtering |
| 34 | + filename => path.join(options.assetsPathPrefix, filepath, filename) // mapping filtered |
| 35 | + ) |
| 36 | + |
| 37 | + if (options.extraPurePatches.length) { |
| 38 | + files = files.concat(options.extraPurePatches) |
| 39 | + } |
| 40 | + |
| 41 | + // return files.sort().reverse() |
| 42 | + return files |
| 43 | + } |
| 44 | + |
| 45 | + var arrayToPhpStatic = function(list, varname) { |
| 46 | + var out = ' static $' + varname + ' = [\n' |
| 47 | + _.forEach(list, function(item) { |
| 48 | + out += " '" + item + "'," |
| 49 | + }) |
| 50 | + out += '\n ];\n' |
| 51 | + return out |
| 52 | + } |
| 53 | + |
| 54 | + var objectToPhpClass = function(obj) { |
| 55 | + // Create a header string for the generated file: |
| 56 | + var out = '<?php\n\n' |
| 57 | + |
| 58 | + if (options.nameSpace) { |
| 59 | + let nameSpaceVal = _.isString(options.nameSpace) ? options.nameSpace : options.nameSpace.nameSpace |
| 60 | + out += 'namespace ' + nameSpaceVal + '; \n\n' |
| 61 | + if (options.nameSpace.use && options.nameSpace.use.length) { |
| 62 | + _.forEach(options.nameSpace.use, use => { |
| 63 | + out += 'use ' + use + ';\n' |
| 64 | + }) |
| 65 | + } |
| 66 | + } |
| 67 | + out += 'class ' + options.phpClassName + ' {\n' |
| 68 | + |
| 69 | + _.forEach(obj, (list, name) => { |
| 70 | + out += arrayToPhpStatic(list, name) |
| 71 | + }) |
| 72 | + |
| 73 | + out += '\n}\n' |
| 74 | + return out |
| 75 | + } |
| 76 | + |
| 77 | + var mkOutputDir = function(dir) { |
| 78 | + // Make webpack output directory if it doesn't already exist |
| 79 | + try { |
| 80 | + fs.mkdirSync(dir) |
| 81 | + } catch (err) { |
| 82 | + // If it does exist, don't worry unless there's another error |
| 83 | + if (err.code !== 'EEXIST') throw err |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + compiler.plugin('emit', function(compilation, callback) { |
| 88 | + var stats = compilation.getStats().toJson() |
| 89 | + var toInclude = [] |
| 90 | + |
| 91 | + // Flatten the chunks (lists of files) to one list |
| 92 | + for (var chunkName in stats.assetsByChunkName) { |
| 93 | + var asset = stats.assetsByChunkName[chunkName] |
| 94 | + |
| 95 | + if (typeof asset === 'string') { |
| 96 | + toInclude.push(asset) |
| 97 | + } else if (Array.isArray(asset)) { |
| 98 | + toInclude = _.union(toInclude, asset) |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + var out = objectToPhpClass({ |
| 103 | + jsFiles: getJsFiles(toInclude, options.path), |
| 104 | + cssFiles: getCssFiles(toInclude, options.path), |
| 105 | + }) |
| 106 | + |
| 107 | + // Write file using fs |
| 108 | + // Build directory if it doesn't exist |
| 109 | + var outPutPath = options.outPutPath ? options.outPutPath : compiler.options.output.path |
| 110 | + |
| 111 | + mkOutputDir(path.resolve(outPutPath)) |
| 112 | + fs.writeFileSync(path.join(outPutPath, options.phpFileName + '.php'), out) |
| 113 | + |
| 114 | + callback() |
| 115 | + }) |
| 116 | +} |
| 117 | + |
| 118 | +module.exports = PhpOutputPlugin |
0 commit comments