forum-django/gulpfile.js

113 lines
4 KiB
JavaScript

import { deleteAsync } from 'del'
import GulpClient from 'gulp'
import logger from 'fancy-log'
import PluginError from 'plugin-error'
import webpackStream from 'webpack-stream'
import webpack from 'webpack'
import Server from 'webpack-dev-server'
import path from 'path'
import gulpEslint from 'gulp-eslint-new'
import { fileURLToPath } from 'url'
const sourceGlob = ['frontend/src/ts/**/*.js']
const _myFileName = fileURLToPath(import.meta.url)
const _myDirName = path.dirname(_myFileName)
// const htmlReplaceSkeleton = () =>
// GulpClient.src('frontend/src/index.html')
// .pipe(htmlreplace({
// loader: {
// src: null,
// tpl: bundleHelper.createHeader(),
// },
// }))
// .pipe(GulpClient.dest('frontend/dist/'))
// Cleanup task
GulpClient.task('clean', () => deleteAsync(['./frontend/dist/**/*']))
// Lint Task
if (process.argv.length < 4) {
GulpClient.task('lint', () => GulpClient.src(sourceGlob)
// gulpEslint() attaches the lint output to the "gulpEslint" property
// of the file object so it can be used by other modules.
.pipe(gulpEslint())
// gulpEslint.format() outputs the lint results to the console.
// Alternatively use gulpEslint.formatEach() (see Docs).
.pipe(gulpEslint.format())
// To have the process exit with an error code (1) on
// lint error, return the stream and pipe to failAfterError last.
.pipe(gulpEslint.failAfterError()))
} else if (process.argv[3] === '-n') {
const passedFiles = process.argv[4].split('\n')
GulpClient.task('lint', () => GulpClient.src(passedFiles)
// gulpEslint() attaches the lint output to the "gulpEslint" property
// of the file object so it can be used by other modules.
.pipe(gulpEslint())
// gulpEslint.format() outputs the lint results to the console.
// Alternatively use gulpEslint.formatEach() (see Docs).
.pipe(gulpEslint.format())
// To have the process exit with an error code (1) on
// lint error, return the stream and pipe to failAfterError last.
.pipe(gulpEslint.failAfterError()))
}
GulpClient.task('clean-lint', GulpClient.parallel(['clean', 'lint']))
GulpClient.task('webpack-prod', GulpClient.series('clean-lint', async (done) => {
const prodConfig = (await import('./frontend/webpack/config.prod.js')).default
GulpClient.src(sourceGlob)
.pipe(webpackStream(prodConfig, webpack))
.on('error', (error) => { done(error) })
.pipe(GulpClient.dest('frontend/dist/assets/'))
.on('end', done)
}))
GulpClient.task('webpack-dev', GulpClient.series(['clean-lint'], async (done) => {
const devConfig = (await import('./frontend/webpack/config.dev.js')).default
GulpClient.src(sourceGlob)
.pipe(webpackStream(devConfig, webpack))
.on('error', (error) => { done(error) })
.pipe(GulpClient.dest('frontend/dist/assets/'))
.on('end', done)
}))
GulpClient.task('build', GulpClient.series(['webpack-prod']))
GulpClient.task('build-dev', GulpClient.series(['clean', 'webpack-dev']))
GulpClient.task('webpack-dev-server', GulpClient.series(['clean'], async (done) => {
const config = (await import('./frontend/webpack/config.dev-server.js')).default
const compiler = webpack(config, () => {})
// @See https://webpack.js.org/configuration/dev-server/#usage-via-api
const devServerOptions = {
hot: true,
// host: '0.0.0.0',
host: 'localhost',
port: 3000,
compress: true,
historyApiFallback: true,
allowedHosts: 'all',
client: {
overlay: true,
progress: true,
},
headers: {
'Access-Control-Allow-Origin': '*',
},
static: {
directory: path.join(_myDirName, 'frontend', 'src', 'assets'),
}
}
// https://github.com/webpack/webpack-dev-server/issues/533#issuecomment-298202657
const server = new Server(devServerOptions, compiler)
server.startCallback((err) => {
if (err) throw new PluginError('webpack-dev-server', err)
// Server listening
logger('[webpack-dev-server]', `http://${devServerOptions.host}:3000/`)
})
done()
}))
GulpClient.task('default', GulpClient.series('clean-lint'))