1
1
mirror of https://github.com/aelve/guide.git synced 2024-11-27 18:12:44 +03:00

Build server-side by webpack (#193)

+ make `webpack` configs DRY
This commit is contained in:
Jens Krause 2017-08-27 21:28:38 +02:00
parent de167957da
commit 4367331565
No known key found for this signature in database
GPG Key ID: 3B2FAFBCEFA5906D
4 changed files with 141 additions and 88 deletions

View File

@ -6,14 +6,16 @@
"keywords": [],
"scripts": {
"postinstall": "bower cache clean && bower install",
"clean": "rimraf dist && rimraf public/*.js && rimraf output",
"build:category-overview": "rimraf output/Main && pulp build --src-path ./client --main Guide.Client.CategoryOverview --include ./common -O > ./public/category-overview.js",
"build:category-detail": "rimraf output/Main && pulp build --src-path ./client --main Guide.Client.CategoryDetail --include ./common --no-check-main -O > ./public/category-detail.js",
"build:server": "rimraf output/Main && mkdir -p dist && pulp build --src-path ./server -O > ./dist/server.js",
"build:client": "npm run clean && webpack --config ./webpack.config.client.js --progress",
"clean": "rimraf output",
"build:client": "npm run clean && rimraf public/*.js && webpack --config ./webpack.config.client.js --progress",
"build:server": "npm run clean && rimraf dist && webpack --config ./webpack.config.server.js --progress",
"run:server": "PORT=3333 node dist/server.js",
"start": "npm run run:server",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"--comment--": "All following `build:*-pulp` scripts are for debugging only and will be removed in the future",
"build:server-pulp": "rimraf output/Main && mkdir -p dist && pulp build --src-path ./server --include ./common -O > ./dist/server.js",
"build:category-detail-pulp": "rimraf output/Main && pulp build --src-path ./client --main Guide.Client.CategoryDetail --include ./common --no-check-main -O > ./public/category-detail.js",
"build:category-overview-pulp": "rimraf output/Main && pulp build --src-path ./client --main Guide.Client.CategoryOverview --include ./common -O > ./public/category-overview.js"
},
"repository": {
"type": "git",

View File

@ -1,44 +1,13 @@
const spawn = require('child_process').spawn
const path = require('path')
const webpack = require('webpack')
const path = require('path');
const webpack = require('webpack');
const c = require('./webpack.config.common.js');
const NamedModulesPlugin = require('webpack/lib/NamedModulesPlugin');
const isProd = process.env.NODE_ENV === 'production'
const plugins = [
new webpack.ProgressPlugin(),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
'$PRODUCTION': isProd
}),
new NamedModulesPlugin(),
]
if (isProd) {
plugins.push(
new webpack.LoaderOptionsPlugin({
minimize: true,
debug: false
}),
new webpack.HashedModuleIdsPlugin({
hashFunction: 'sha256',
hashDigest: 'hex',
hashDigestLength: 20
})
)
} else {
plugins.push(
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin()
)
}
const config = {
module.exports = {
context: c.context,
entry: {
"category-overview": path.join(__dirname, 'client', 'Guide', 'Client', 'CategoryOverview-entry.js'),
"category-detail": path.join(__dirname, 'client', 'Guide', 'Client', 'CategoryDetail-entry.js'),
"category-detail": path.join(__dirname, 'client', 'Guide', 'Client', 'CategoryDetail-entry.js')
},
output: {
path: path.join(__dirname, '/public'),
@ -46,56 +15,16 @@ const config = {
filename: '[name].js',
pathinfo: true
},
plugins: plugins,
plugins: c.plugins,
module: {
loaders: [
{
test: /\.purs$/,
loader: 'purs-loader',
exclude: /node_modules/,
query: {
psc: 'psa',
src: [
path.join(__dirname, 'common', '**', '*.purs'),
path.join(__dirname, 'client', '**', '*.purs'),
path.join(__dirname, 'bower_components', 'purescript-*', 'src', '**', '*.purs')
],
bundle: isProd
}
}
],
loaders: c.loaders
},
resolveLoader: {
modules: [
path.join(__dirname, 'node_modules')
]
},
resolve: {
extensions: [ '.js', '.purs'],
alias: {
'react': 'preact-compat',
'react-dom': 'preact-compat',
'create-react-class': 'preact-compat/lib/create-react-class'
},
modules: [
'node_modules',
'bower_components'
],
extensions: ['.js', '.purs']
},
performance: { hints: false },
stats: {
hash: false,
timings: false,
version: false,
assets: false,
errors: true,
colors: false,
chunks: false,
children: false,
cached: false,
modules: false,
chunkModules: false
}
resolve: c.resolve,
performance: c.performance,
stats: c.stats
}
module.exports = config

View File

@ -0,0 +1,93 @@
const spawn = require('child_process').spawn
const path = require('path')
const webpack = require('webpack')
const NamedModulesPlugin = require('webpack/lib/NamedModulesPlugin');
const isProd = process.env.NODE_ENV === 'production'
const plugins = [
new webpack.ProgressPlugin(),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
'$PRODUCTION': isProd
}),
new NamedModulesPlugin(),
]
if (isProd) {
plugins.push(
new webpack.LoaderOptionsPlugin({
minimize: true,
debug: false
}),
new webpack.HashedModuleIdsPlugin({
hashFunction: 'sha256',
hashDigest: 'hex',
hashDigestLength: 20
})
)
} else {
plugins.push(
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin()
)
}
const loaders = [
{
test: /\.purs$/,
loader: 'purs-loader',
exclude: /node_modules/,
query: {
psc: 'psa',
src: [
path.join(__dirname, 'common', '**', '*.purs'),
path.join(__dirname, 'client', '**', '*.purs'),
path.join(__dirname, 'server', '**', '*.purs'),
path.join(__dirname, 'bower_components', 'purescript-*', 'src', '**', '*.purs')
],
bundle: isProd
}
}
]
const resolve = {
extensions: [ '.js', '.purs'],
alias: {
'react': 'preact-compat',
'react-dom': 'preact-compat',
'create-react-class': 'preact-compat/lib/create-react-class'
},
modules: [
'node_modules',
'bower_components'
],
extensions: ['.js', '.purs']
}
const stats = {
hash: false,
timings: false,
version: false,
assets: false,
errors: true,
colors: false,
chunks: false,
children: false,
cached: false,
modules: false,
chunkModules: false
}
const performance = { hints: false }
module.exports = {
plugins,
performance,
loaders,
resolve,
stats
}

View File

@ -0,0 +1,29 @@
const path = require('path');
const webpack = require('webpack');
const c = require('./webpack.config.common.js');
module.exports = {
target: 'node',
entry: {
"server": path.join(__dirname, 'server', 'entry.js')
},
output: {
path: path.join(__dirname, 'dist'),
publicPath: '/',
filename: '[name].js',
pathinfo: true
},
plugins: c.plugins,
module: {
loaders: c.loaders
},
resolveLoader: {
modules: [
path.join(__dirname, 'node_modules')
]
},
resolve: c.resolve,
performance: c.performance,
stats: c.stats
}