Skip to content

Instantly share code, notes, and snippets.

@pyrsmk
Last active June 13, 2019 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pyrsmk/ba371668b2cb89cea5d325a2fca56a02 to your computer and use it in GitHub Desktop.
Save pyrsmk/ba371668b2cb89cea5d325a2fca56a02 to your computer and use it in GitHub Desktop.
Systèmes de build : vanilla JS
const { CLIEngine } = require('eslint')
const rollup = require('rollup')
const typescript = require('rollup-plugin-typescript2')
const prepack = require('rollup-plugin-prepack-up')
const uglify = require('uglify-js')
const fs = require('fs')
const chalk = require('chalk')
const info = message => {
console.info(chalk.yellow(' * ') + chalk.green(message))
}
const error = message => {
if (message) {
console.error(chalk.yellow(' ! ') + chalk.red(message))
}
}
const lint = options => {
const eslint = new CLIEngine()
const formatter = eslint.getFormatter()
return new Promise((resolve, reject) => {
const { results } = eslint.executeOnFiles([options.glob])
if (results.reduce((value, item) => value + item.errorCount, 0)) {
console.log(formatter(results))
reject()
}
if (results.reduce((value, item) => value + item.warningCount, 0)) {
console.log(formatter(results))
}
resolve()
})
}
const bundle = options => new Promise((resolve, reject) => {
rollup.rollup({
input: options.input,
plugins: [
typescript({ useTsconfigDeclarationDir: true }),
prepack(),
],
}).then(bundler => {
bundler.write({
file: options.output,
format: 'umd',
name: options.name,
}).then(
resolve,
).catch(
reject,
)
}).catch(
reject
)
})
const minify = options => new Promise((resolve, reject) => {
const params = {}
if ('sourcemap' in options && 'filename' in options) {
params.sourceMap = {
filename: options.filename,
url: 'inline',
}
}
fs.readFile(options.from, 'utf8', (readError, data) => {
if (readError) reject(readError)
fs.writeFile(options.to, uglify.minify(data, params).code, writeError => {
if (writeError) reject(writeError)
resolve()
})
})
})
info('Linting TypeScript modules...')
lint({
glob: 'src/**',
}).then(() => {
info('Bundling TypeScript modules...')
bundle({
input: 'src/Toast.ts',
output: 'dist/toast.js',
name: 'toast',
}).then(() => {
info('Minifying production file...')
minify({
from: 'dist/toast.js',
to: 'dist/toast.min.js',
}).then(() => {
info('Minifying test file...')
minify({
from: 'dist/toast.js',
to: 'tests/lib/toast.min.js',
sourcemap: true,
filename: 'toast.js',
}).catch(reason => {
error(reason)
})
}).catch(reason => {
error(reason)
})
}).catch(reason => {
error(reason)
})
}).catch(reason => {
error(reason)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment