Skip to content

Commit 729d65f

Browse files
committed
build: add webpack
1 parent a4f0b93 commit 729d65f

File tree

5 files changed

+200
-4
lines changed

5 files changed

+200
-4
lines changed

package.json

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
"src"
1313
],
1414
"scripts": {
15-
"lint": "eslint src/**/*.js",
16-
"start": "node src/index.js"
15+
"build": "webpack --env.mode=prod",
16+
"lint": "eslint statik/**/*.js",
17+
"start": "webpack-dev-server --env.mode=dev"
1718
},
1819
"repository": {
1920
"type": "git",
@@ -22,12 +23,35 @@
2223
"bugs": {
2324
"url": "https://github.com/statikstack/html-stylus-javascript-webpack/issues"
2425
},
25-
"dependencies": {},
26+
"dependencies": {
27+
"normalize.css": "8.0.0"
28+
},
2629
"devDependencies": {
30+
"autoprefixer": "8.6.5",
31+
"babel-core": "6.26.3",
32+
"babel-loader": "7.1.5",
33+
"babel-preset-env": "1.7.0",
34+
"clean-webpack-plugin": "0.1.19",
35+
"css-loader": "1.0.0",
2736
"eslint": "5.1.0",
2837
"eslint-config-prettier": "2.9.0",
2938
"eslint-plugin-prettier": "2.6.2",
30-
"prettier": "1.13.7"
39+
"file-loader": "1.1.11",
40+
"html-loader": "0.5.5",
41+
"html-webpack-plugin": "3.2.0",
42+
"image-webpack-loader": "4.3.1",
43+
"mini-css-extract-plugin": "0.4.1",
44+
"optimize-css-assets-webpack-plugin": "5.0.0",
45+
"postcss-loader": "2.1.6",
46+
"prettier": "1.13.7",
47+
"style-loader": "0.21.0",
48+
"stylus": "0.54.5",
49+
"stylus-loader": "3.0.2",
50+
"uglifyjs-webpack-plugin": "1.2.7",
51+
"webpack": "4.16.1",
52+
"webpack-cli": "3.0.8",
53+
"webpack-dev-server": "3.1.4",
54+
"webpack-merge": "4.1.3"
3155
},
3256
"license": "MIT"
3357
}

webpack.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = (env) => {
2+
const config = require(`./webpack/webpack.${env.mode}`);
3+
4+
return config;
5+
};

webpack/webpack.common.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const CleanWebpackPlugin = require('clean-webpack-plugin');
2+
const HtmlWebpackPlugin = require('html-webpack-plugin');
3+
const path = require('path');
4+
5+
module.exports = {
6+
entry: {
7+
vendor: './statik/scripts/vendor.js',
8+
app: './statik/scripts/app.js'
9+
},
10+
output: {
11+
filename: '[name].js',
12+
path: path.resolve('dist')
13+
},
14+
module: {
15+
rules: [
16+
{
17+
test: /\.html$/,
18+
use: 'html-loader'
19+
},
20+
{
21+
test: /\.js$/,
22+
exclude: /node_modules/,
23+
use: {
24+
loader: 'babel-loader',
25+
options: {
26+
presets: ['env']
27+
}
28+
}
29+
},
30+
{
31+
test: /\.(woff2?)$/,
32+
use: [
33+
{
34+
loader: 'file-loader',
35+
options: {
36+
name: '[name].[ext]',
37+
useRelativePath: true
38+
}
39+
}
40+
]
41+
},
42+
{
43+
test: /\.(gif|jpg|png|svg|ico)$/,
44+
use: [
45+
{
46+
loader: 'file-loader',
47+
options: {
48+
name: '[name].[ext]',
49+
useRelativePath: true
50+
}
51+
}
52+
]
53+
}
54+
]
55+
},
56+
plugins: [
57+
new CleanWebpackPlugin(['dist'], { root: path.resolve() }),
58+
new HtmlWebpackPlugin({
59+
template: './statik/index.html'
60+
})
61+
]
62+
};

webpack/webpack.dev.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const merge = require('webpack-merge');
2+
const path = require('path');
3+
4+
const common = require('./webpack.common');
5+
6+
module.exports = merge.smart(common, {
7+
mode: 'development',
8+
devtool: 'eval',
9+
module: {
10+
rules: [
11+
{
12+
test: /\.(css|styl)$/,
13+
use: ['style-loader', 'css-loader', 'stylus-loader']
14+
}
15+
]
16+
},
17+
devServer: {
18+
contentBase: path.resolve(__dirname, 'dist'),
19+
open: true,
20+
port: 8080
21+
}
22+
});

webpack/webpack.prod.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
const autoprefixer = require('autoprefixer');
2+
const merge = require('webpack-merge');
3+
const webpack = require('webpack');
4+
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
5+
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
6+
const UglifyJsWebpackPlugin = require('uglifyjs-webpack-plugin');
7+
8+
const common = require('./webpack.common');
9+
10+
module.exports = merge.smart(common, {
11+
mode: 'production',
12+
devtool: 'source-map',
13+
output: {
14+
filename: '[name].[chunkhash].js'
15+
},
16+
module: {
17+
rules: [
18+
{
19+
test: /\.html$/,
20+
use: {
21+
loader: 'html-loader',
22+
options: {
23+
minimize: true
24+
}
25+
}
26+
},
27+
{
28+
test: /\.(css|styl)$/,
29+
use: [
30+
MiniCssExtractPlugin.loader,
31+
{
32+
loader: 'css-loader',
33+
options: { importLoaders: 1 }
34+
},
35+
{
36+
loader: 'postcss-loader',
37+
options: {
38+
plugins: () => [autoprefixer()]
39+
}
40+
},
41+
{
42+
loader: 'stylus-loader'
43+
}
44+
]
45+
},
46+
{
47+
test: /\.(gif|jpg|png|svg|ico)$/,
48+
use: 'image-webpack-loader'
49+
}
50+
]
51+
},
52+
optimization: {
53+
runtimeChunk: 'single',
54+
splitChunks: {
55+
chunks: 'all',
56+
name: true,
57+
cacheGroups: {
58+
vendor: {
59+
test: /[\\/]node_modules[\\/]/,
60+
name: 'vendors',
61+
chunks: 'all'
62+
}
63+
}
64+
},
65+
minimizer: [
66+
new OptimizeCSSAssetsPlugin({
67+
cssProcessorOptions: { discardUnused: false }
68+
})
69+
]
70+
},
71+
plugins: [
72+
new MiniCssExtractPlugin({
73+
filename: '[name].[chunkhash].css'
74+
}),
75+
new UglifyJsWebpackPlugin({
76+
sourceMap: true
77+
}),
78+
new webpack.HashedModuleIdsPlugin(),
79+
new webpack.DefinePlugin({
80+
'process.env.NODE_ENV': JSON.stringify('production')
81+
})
82+
]
83+
});

0 commit comments

Comments
 (0)