Skip to content

Commit 0851b1d

Browse files
authored
Merge pull request #215 from Lyrkan/fix-json-output
Fix output when using "--json" or "-j" option
2 parents bcfbbfd + 3966989 commit 0851b1d

File tree

4 files changed

+74
-24
lines changed

4 files changed

+74
-24
lines changed

bin/encore.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ if (runtimeConfig.useDevServer) {
4545

4646
return require('webpack-dev-server/bin/webpack-dev-server');
4747
} else {
48-
console.log('Running webpack ...');
49-
console.log();
48+
if (!runtimeConfig.outputJson) {
49+
console.log('Running webpack ...');
50+
console.log();
51+
}
5052

5153
return require('webpack/bin/webpack');
5254
}

lib/config-generator.js

+30-22
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,15 @@ class ConfigGenerator {
243243

244244
notifierPluginUtil(plugins, this.webpackConfig);
245245

246-
const friendlyErrorPlugin = friendlyErrorPluginUtil(this.webpackConfig);
247-
plugins.push({
248-
plugin: friendlyErrorPlugin,
249-
priority: PluginPriorities.FriendlyErrorsWebpackPlugin
250-
});
246+
if (!this.webpackConfig.runtimeConfig.outputJson) {
247+
const friendlyErrorPlugin = friendlyErrorPluginUtil(this.webpackConfig);
248+
plugins.push({
249+
plugin: friendlyErrorPlugin,
250+
priority: PluginPriorities.FriendlyErrorsWebpackPlugin
251+
});
251252

252-
assetOutputDisplay(plugins, this.webpackConfig, friendlyErrorPlugin);
253+
assetOutputDisplay(plugins, this.webpackConfig, friendlyErrorPlugin);
254+
}
253255

254256
this.webpackConfig.plugins.forEach(function(plugin) {
255257
plugins.push(plugin);
@@ -274,22 +276,28 @@ class ConfigGenerator {
274276
buildStatsConfig() {
275277
// try to silence as much as possible: the output is rarely helpful
276278
// this still doesn't remove all output
277-
return {
278-
hash: false,
279-
version: false,
280-
timings: false,
281-
assets: false,
282-
chunks: false,
283-
maxModules: 0,
284-
modules: false,
285-
reasons: false,
286-
children: false,
287-
source: false,
288-
errors: false,
289-
errorDetails: false,
290-
warnings: false,
291-
publicPath: false,
292-
};
279+
let stats = {};
280+
281+
if (!this.webpackConfig.runtimeConfig.outputJson) {
282+
stats = {
283+
hash: false,
284+
version: false,
285+
timings: false,
286+
assets: false,
287+
chunks: false,
288+
maxModules: 0,
289+
modules: false,
290+
reasons: false,
291+
children: false,
292+
source: false,
293+
errors: false,
294+
errorDetails: false,
295+
warnings: false,
296+
publicPath: false,
297+
};
298+
}
299+
300+
return stats;
293301
}
294302

295303
buildDevServerConfig() {

lib/config/parse-runtime.js

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module.exports = function(argv, cwd) {
2424
runtimeConfig.command = argv._[0];
2525
runtimeConfig.useDevServer = false;
2626
runtimeConfig.useHotModuleReplacement = false;
27+
runtimeConfig.outputJson = false;
2728

2829
switch (runtimeConfig.command) {
2930
case 'dev':
@@ -68,6 +69,10 @@ module.exports = function(argv, cwd) {
6869
runtimeConfig.helpRequested = true;
6970
}
7071

72+
if (argv.j || argv.json) {
73+
runtimeConfig.outputJson = true;
74+
}
75+
7176
runtimeConfig.babelRcFileExists = (typeof resolveRc(require('fs'), runtimeConfig.context)) !== 'undefined';
7277

7378
return runtimeConfig;

test/bin/encore.js

+35
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
const chai = require('chai');
1313
chai.use(require('chai-fs'));
14+
const expect = chai.expect;
1415
const path = require('path');
1516
const testSetup = require('../../lib/test/setup');
1617
const fs = require('fs-extra');
@@ -88,4 +89,38 @@ export default config;
8889
done();
8990
});
9091
});
92+
93+
it('Smoke test using the --json option', (done) => {
94+
testSetup.emptyTmpDir();
95+
const testDir = testSetup.createTestAppDir();
96+
97+
fs.writeFileSync(
98+
path.join(testDir, 'webpack.config.js'),
99+
`
100+
const Encore = require('../../index.js');
101+
Encore
102+
.setOutputPath('build/')
103+
.setPublicPath('/build')
104+
.addEntry('main', './js/no_require')
105+
;
106+
107+
module.exports = Encore.getWebpackConfig();
108+
`
109+
);
110+
111+
const binPath = path.resolve(__dirname, '../', '../', 'bin', 'encore.js');
112+
exec(`node ${binPath} dev --json --context=${testDir}`, { cwd: testDir }, (err, stdout, stderr) => {
113+
if (err) {
114+
throw new Error(`Error executing encore: ${err} ${stderr} ${stdout}`);
115+
}
116+
117+
const parsedOutput = JSON.parse(stdout);
118+
119+
expect(parsedOutput).to.be.an('object');
120+
expect(parsedOutput.modules).to.be.an('array');
121+
expect(parsedOutput.modules.length).to.equal(1);
122+
123+
done();
124+
});
125+
});
91126
});

0 commit comments

Comments
 (0)