Last active
June 13, 2019 11:38
-
-
Save pyrsmk/7cd085f2b5ea403ae99c313f902f3665 to your computer and use it in GitHub Desktop.
Systèmes de build : Just
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { task, series, logger } = require('just-task') | |
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 lint = options => { | |
const eslint = new CLIEngine() | |
const formatter = eslint.getFormatter() | |
const { results } = eslint.executeOnFiles([options.glob]) | |
if (results.reduce((value, item) => value + item.errorCount, 0)) { | |
logger.info(formatter(results)) | |
throw new Error('Linter has found errors') | |
} else if (results.reduce((value, item) => value + item.warningCount, 0)) { | |
logger.info(formatter(results)) | |
} | |
} | |
const bundle = async options => { | |
await rollup.rollup({ | |
input: options.input, | |
plugins: [ | |
typescript({ useTsconfigDeclarationDir: true }), | |
prepack(), | |
], | |
}).then(bundler => { | |
bundler.write({ | |
file: options.output, | |
format: 'umd', | |
name: options.name, | |
}) | |
}) | |
} | |
const minify = async options => { | |
const params = {} | |
if ('sourcemap' in options && 'filename' in options) { | |
params.sourceMap = { | |
filename: options.filename, | |
url: 'inline', | |
} | |
} | |
await new Promise((resolve, reject) => { | |
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() | |
}) | |
}) | |
}) | |
} | |
task('lint', () => lint({ | |
glob: 'src/**', | |
})) | |
task('bundle', () => bundle({ | |
input: 'src/Toast.ts', | |
output: 'dist/toast.js', | |
name: 'toast', | |
})) | |
task('minify', () => minify({ | |
from: 'dist/toast.js', | |
to: 'dist/toast.min.js', | |
})) | |
task('minify-dev', () => minify({ | |
from: 'dist/toast.js', | |
to: 'tests/lib/toast.min.js', | |
sourcemap: true, | |
filename: 'toast.js', | |
})) | |
task('build', series( | |
'lint', | |
'bundle', | |
'minify', | |
'minify-dev', | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment