Skip to content

Commit c1b652a

Browse files
committed
feat : add webpack-php-output
1 parent 1be6f1b commit c1b652a

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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')), // 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')), // 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

webpack.config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const CopyWebpackPlugin = require('copy-webpack-plugin')
44
const ManifestPlugin = require('webpack-manifest-plugin')
55
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
66
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin')
7+
const PhpOutputPlugin = require('./src/js/vendor/webpack-php-output')
78
const SoundsPlugin = require('sounds-webpack-plugin')
89
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
910
const WebpackProgressOraPlugin = require('webpack-progress-ora-plugin')
@@ -137,6 +138,15 @@ const webpackConfig = {
137138
to: 'img/sample/',
138139
},
139140
]),
141+
new PhpOutputPlugin({
142+
devServer: false, // false or string with server entry point, e.g: app.js or
143+
outPutPath: path.resolve(__dirname, 'dist/'), // false for default webpack path of pass string to specify
144+
assetsPathPrefix: '',
145+
phpClassName: 'WebpackBuiltFiles', //
146+
phpFileName: 'WebpackBuiltFiles',
147+
nameSpace: false, // false {nameSpace: 'name', use: ['string'] or empty property or don't pass "use" property}
148+
path: '',
149+
}),
140150
new WebpackProgressOraPlugin(),
141151
],
142152
}

0 commit comments

Comments
 (0)