Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions bin/styleguidist.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,21 @@ function commandBuild() {
verbose('Webpack config:', compiler.options);

// Custom error reporting
compiler.plugin('done', function(stats) {
function customErrorBuild(stats) {
const messages = formatWebpackMessages(stats.toJson({}, true));
const hasErrors = printAllErrorsAndWarnings(messages, stats.compilation);
if (hasErrors) {
process.exit(1);
}
});
}

if (compiler.hooks) {
// webpack 4
compiler.hooks.done.tap('rsgCustomErrorBuild', customErrorBuild);
} else {
// webpack 3
compiler.plugin('done', customErrorBuild);
}
}

function commandServer() {
Expand Down Expand Up @@ -146,13 +154,13 @@ function commandServer() {
verbose('Webpack config:', compiler.options);

// Show message when webpack is recompiling the bundle
compiler.plugin('invalid', function() {
function invalidServer() {
console.log();
spinner = ora('Compiling...').start();
});
}

// Custom error reporting
compiler.plugin('done', function(stats) {
function customErrorServer(stats) {
if (spinner) {
spinner.stop();
}
Expand All @@ -164,7 +172,17 @@ function commandServer() {
}

printAllErrorsAndWarnings(messages, stats.compilation);
});
}

if (compiler.hooks) {
// webpack 4
compiler.hooks.invalid.tap('rsgInvalidServer', invalidServer);
compiler.hooks.done.tap('rsgCustomErrorServer', customErrorServer);
} else {
// webpack 3
compiler.plugin('invalid', invalidServer);
compiler.plugin('done', customErrorServer);
}
}

function commandHelp() {
Expand Down
11 changes: 9 additions & 2 deletions scripts/utils/StyleguidistOptionsPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@ class StyleguidistOptionsPlugin {
}

plugin(compilation) {
compilation.plugin('normal-module-loader', (context, module) => {
const pluginFunc = (context, module) => {
if (!module.resource) {
return;
}
context._styleguidist = this.options;
});
};
if (compilation.hooks) {
// Webpack 4
compilation.hooks.normalModuleLoader.tap('StyleguidistOptionsPlugin', pluginFunc);
} else {
// Webpack 3
compilation.plugin('normal-module-loader', pluginFunc);
}
}

apply(compiler) {
Expand Down
25 changes: 25 additions & 0 deletions scripts/utils/__tests__/StyleguidistOptionsPlugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,31 @@ it('should attach Styleguidist config object to webpack context', () => {
expect(context._styleguidist).toEqual(options);
});

it('should attach Styleguidist config when webpack 4 is used', () => {
const context = {};
const compiler = {
hooks: {
compilation: {
tap: (name, callback) => {
callback({
hooks: {
normalModuleLoader: {
tap: (name, compilationCallback) => {
compilationCallback(context, { resource: 'pizza' });
},
},
},
});
},
},
},
};
const plugin = new StyleguidistOptionsPlugin(options);
plugin.apply(compiler);

expect(context._styleguidist).toEqual(options);
});

it('should do nothing when resource is empty', () => {
const context = {};
const compiler = {
Expand Down