Skip to content

Commit

Permalink
cli(init): use extractMiniCSSPlugin (#363)
Browse files Browse the repository at this point in the history
* cli(init): skip redundant question

* cli(init): use mini-css-extract-plugin
  • Loading branch information
jevancc authored and ematipico committed Apr 10, 2018
1 parent ab16b4f commit 1077003
Showing 1 changed file with 54 additions and 51 deletions.
105 changes: 54 additions & 51 deletions lib/generators/init-generator.js
Expand Up @@ -149,9 +149,7 @@ module.exports = class InitGenerator extends Generator {
]);
})
.then(stylingTypeAnswer => {
if (!this.isProd) {
ExtractUseProps = [];
}
ExtractUseProps = [];
switch (stylingTypeAnswer["stylingType"]) {
case "SASS":
this.dependencies.push(
Expand All @@ -162,20 +160,20 @@ module.exports = class InitGenerator extends Generator {
);
regExpForStyles = `${new RegExp(/\.(scss|css)$/)}`;
if (this.isProd) {
ExtractUseProps = `
use: [{
loader: "css-loader",
ExtractUseProps.push(
{
loader: "'css-loader'",
options: {
sourceMap: true
}
}, {
loader: "sass-loader",
},
{
loader: "'sass-loader'",
options: {
sourceMap: true
}
}],
fallback: "style-loader"
`;
}
);
} else {
ExtractUseProps.push(
{
Expand All @@ -199,20 +197,20 @@ module.exports = class InitGenerator extends Generator {
"css-loader"
);
if (this.isProd) {
ExtractUseProps = `
use: [{
loader: "css-loader",
ExtractUseProps.push(
{
loader: "'css-loader'",
options: {
sourceMap: true
}
}, {
loader: "less-loader",
},
{
loader: "'less-loader'",
options: {
sourceMap: true
}
}],
fallback: "style-loader"
`;
}
);
} else {
ExtractUseProps.push(
{
Expand Down Expand Up @@ -246,28 +244,26 @@ module.exports = class InitGenerator extends Generator {
);
regExpForStyles = `${new RegExp(/\.css$/)}`;
if (this.isProd) {
ExtractUseProps = `
use: [{
loader: "style-loader"
},{
loader: "css-loader",
ExtractUseProps.push(
{
loader: "'css-loader'",
options: {
sourceMap: true,
importLoaders: 1
}
}, {
loader: "postcss-loader",
},
{
loader: "'postcss-loader'",
options: {
plugins: function () {
plugins: `function () {
return [
precss,
autoprefixer
];
}
}`
}
}],
fallback: "style-loader"
`;
}
);
} else {
ExtractUseProps.push(
{
Expand Down Expand Up @@ -298,15 +294,14 @@ module.exports = class InitGenerator extends Generator {
this.dependencies.push("style-loader", "css-loader");
regExpForStyles = `${new RegExp(/\.css$/)}`;
if (this.isProd) {
ExtractUseProps = `
use: [{
loader: "css-loader",
ExtractUseProps.push(
{
loader: "'css-loader'",
options: {
sourceMap: true
}
}],
fallback: "style-loader"
`;
}
);
} else {
ExtractUseProps.push(
{
Expand All @@ -326,42 +321,50 @@ module.exports = class InitGenerator extends Generator {
}
})
.then(() => {
// Ask if the user wants to use extractPlugin
return this.prompt([
Input(
"extractPlugin",
"If you want to bundle your CSS files, what will you name the bundle? (press enter to skip)"
)
]);
if (this.isProd) {
// Ask if the user wants to use extractPlugin
return this.prompt([
Input(
"extractPlugin",
"If you want to bundle your CSS files, what will you name the bundle? (press enter to skip)"
)
]);
}
})
.then(extractPluginAnswer => {
const cssBundleName = extractPluginAnswer["extractPlugin"];
if (regExpForStyles) {
if (this.isProd) {
const cssBundleName = extractPluginAnswer["extractPlugin"];
this.configuration.config.topScope.push(tooltip.cssPlugin());
// TODO: Replace with regular version once v.4 is out
this.dependencies.push("extract-text-webpack-plugin@next");
this.dependencies.push("mini-css-extract-plugin");

if (cssBundleName.length !== 0) {
this.configuration.config.webpackOptions.plugins.push(
`new ExtractTextPlugin('${cssBundleName}.[contentHash].css')`
// TODO: use [contenthash] after it is supported
`new MiniCssExtractPlugin({ filename:'${cssBundleName}.[chunkhash].css' })`
);
} else {
this.configuration.config.webpackOptions.plugins.push(
"new ExtractTextPlugin('style.css')"
"new MiniCssExtractPlugin({ filename:'style.css' })"
);
}

ExtractUseProps.unshift(
{
loader: "MiniCssExtractPlugin.loader"
}
);

const moduleRulesObj = {
test: regExpForStyles,
use: `ExtractTextPlugin.extract({ ${ExtractUseProps} })`
use: ExtractUseProps
};

this.configuration.config.webpackOptions.module.rules.push(
moduleRulesObj
);
this.configuration.config.topScope.push(
"const ExtractTextPlugin = require('extract-text-webpack-plugin');",
"const MiniCssExtractPlugin = require('mini-css-extract-plugin');",
"\n"
);
} else {
Expand Down

0 comments on commit 1077003

Please sign in to comment.