From 592513dd475872a7e789f6c6058728a43cb283be Mon Sep 17 00:00:00 2001 From: daniel-j Date: Mon, 12 Mar 2018 22:03:58 +0100 Subject: [PATCH] Update build system --- .gitignore | 8 +-- bin/fimfic2epub | 12 ++-- extension/Info.plist | 2 +- extension/global.html | 4 +- extension/manifest.json | 5 +- gulpfile.babel.js | 138 ++++++++++++++++++++++++---------------- package.json | 23 ++++--- packchrome.sh | 2 +- webpack.config.babel.js | 47 +++++++++++--- 9 files changed, 148 insertions(+), 93 deletions(-) diff --git a/.gitignore b/.gitignore index 120700d..3e2c0ea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,13 +1,11 @@ .DS_Store node_modules/ -extension/fimfic2epub.js -extension/eventPage.js -extension/*.js.map +extension/build/ extension.crx extension.pem extension.xpi extension.zip fimfic2epub.safariextension/ -fimfic2epub.js -fimfic2epub.js.map +dist/ +build/ *.epub diff --git a/bin/fimfic2epub b/bin/fimfic2epub index f2fe542..2c3728a 100755 --- a/bin/fimfic2epub +++ b/bin/fimfic2epub @@ -1,13 +1,9 @@ #!/usr/bin/env node -// Fix for mithril -const noop = () => {} -global.window = { - document: { createDocumentFragment: noop }, - history: { pushState: noop } -} +// use a mock DOM so we can run mithril on the server +require('mithril/test-utils/browserMock')(global) -const FimFic2Epub = require('../fimfic2epub') +const FimFic2Epub = require('../dist/fimfic2epub') const fs = require('fs') const STORY_ID = process.argv[2] @@ -22,9 +18,11 @@ if (outputStdout) { } ffc.on('progress', (percent, status) => { + /* if (status) { console.log('fimfic2epub: ' + status) } + */ }) ffc.fetchAll() diff --git a/extension/Info.plist b/extension/Info.plist index 1ba6a06..309049b 100644 --- a/extension/Info.plist +++ b/extension/Info.plist @@ -27,7 +27,7 @@ End - fimfic2epub.js + build/fimfic2epub.js Whitelist diff --git a/extension/global.html b/extension/global.html index da7019b..8845d25 100644 --- a/extension/global.html +++ b/extension/global.html @@ -1,10 +1,10 @@ - Background page + fimfic2epub background page - + diff --git a/extension/manifest.json b/extension/manifest.json index 807ae4a..6f4b939 100644 --- a/extension/manifest.json +++ b/extension/manifest.json @@ -4,21 +4,20 @@ "name": "fimfic2epub", "short_name": "fimfic2epub", "description": "Improved EPUB exporter for Fimfiction", - "version": "1.6.5", "icons": { "128": "icon-128.png" }, "background": { - "scripts": ["eventPage.js"], + "scripts": ["build/eventPage.js"], "persistent": false }, "content_scripts": [ { "matches": ["https://www.fimfiction.net/*", "http://www.fimfiction.net/*"], - "js": ["fimfic2epub.js"], + "js": ["build/fimfic2epub.js"], "css": ["inject.css"] } ], diff --git a/gulpfile.babel.js b/gulpfile.babel.js index 47d2774..124e9a7 100644 --- a/gulpfile.babel.js +++ b/gulpfile.babel.js @@ -7,11 +7,14 @@ import Sequence from 'run-sequence' import watch from 'gulp-watch' import lazypipe from 'lazypipe' import filter from 'gulp-filter' +import merge from 'merge-stream' +import change from 'gulp-change' +import rename from 'gulp-rename' import jsonedit from 'gulp-json-editor' import zip from 'gulp-zip' -import { execFile, exec } from 'child_process' +// import { execFile, exec } from 'child_process' // script import standard from 'gulp-standard' @@ -20,44 +23,33 @@ import webpackConfig from './webpack.config.babel.js' const sequence = Sequence.use(gulp) -const inProduction = process.env.NODE_ENV === 'production' || process.argv.indexOf('-p') !== -1 +// const inProduction = process.env.NODE_ENV === 'production' || process.argv.indexOf('-p') !== -1 let watchOpts = { readDelay: 500, - verbose: true + verbose: true, + read: false } -webpackConfig.forEach((c) => { - if (inProduction) { - c.plugins.push(new webpack.optimize.ModuleConcatenationPlugin()) - c.plugins.push(new webpack.LoaderOptionsPlugin({ - minimize: true, - debug: false - })) - /* - c.plugins.push(new webpack.optimize.UglifyJsPlugin({ - compress: { - warnings: false, - screw_ie8: true - }, - comments: false, - mangle: { - screw_ie8: true - }, - screw_ie8: true, - sourceMap: !!c.devtool - })) - */ - } - c.plugins.push(new webpack.DefinePlugin({ - FIMFIC2EPUB_VERSION: JSON.stringify(require('./package.json').version) - })) +let packageVersion = require('./package.json').version + +let webpackDefines = new webpack.DefinePlugin({ + FIMFIC2EPUB_VERSION: JSON.stringify(packageVersion) }) -const wpCompiler = webpack(webpackConfig) +webpackConfig.forEach((c) => { + c.plugins.push(webpackDefines) +}) + +let wpCompiler = webpack(webpackConfig) function webpackTask (callback) { - // run webpack + if (webpackDefines.definitions.FIMFIC2EPUB_VERSION !== JSON.stringify(packageVersion)) { + webpackDefines.definitions.FIMFIC2EPUB_VERSION = JSON.stringify(packageVersion) + wpCompiler = webpack(webpackConfig) + } + + // run webpack compiler wpCompiler.run(function (err, stats) { if (err) throw new gutil.PluginError('webpack', err) gutil.log('[webpack]', stats.toString({ @@ -71,6 +63,16 @@ function webpackTask (callback) { }) } +function convertFontAwesomeVars (contents) { + let vars = {} + let matchVar = /\$fa-var-(.*?): "\\(.*?)";/g + let ma + for (;(ma = matchVar.exec(contents));) { + vars[ma[1]] = String.fromCharCode(parseInt(ma[2], 16)) + } + return JSON.stringify(vars) +} + let lintPipe = lazypipe() .pipe(filter, ['**/*', '!src/lib/**/*']) .pipe(standard) @@ -78,21 +80,25 @@ let lintPipe = lazypipe() // Cleanup task gulp.task('clean', () => del([ - 'extension/fimfic2epub.js', - 'extension/eventPage.js', - 'extension/*.js.map', - 'fimfic2epub.js', - 'fimfic2epub.js.map', + 'build/', + 'extension/build/', + 'dist/', 'extension.zip', 'extension.xpi', 'extension.crx', 'fimfic2epub.safariextension/' ])) +gulp.task('version', (done) => { + delete require.cache[require.resolve('./package.json')] + packageVersion = require('./package.json').version + done() +}) + // Main tasks -gulp.task('webpack', webpackTask) +gulp.task('webpack', ['version', 'fontawesome'], webpackTask) gulp.task('watch:webpack', () => { - return watch(['src/**/*.js', 'src/**/*.styl'], watchOpts, function () { + return watch(['src/**/*.js', 'src/**/*.styl', './package.json'], watchOpts, () => { return sequence('webpack') }) }) @@ -101,8 +107,8 @@ gulp.task('lint', () => { return gulp.src(['gulpfile.babel.js', 'webpack.config.babel.js', 'src/**/*.js', 'bin/fimfic2epub']).pipe(lintPipe()) }) gulp.task('watch:lint', () => { - return watch(['src/**/*.js', 'gulpfile.babel.js', 'webpack.config.babel.js', 'bin/fimfic2epub'], watchOpts, function (file) { - gulp.src(file.path).pipe(lintPipe()) + return watch(['src/**/*.js', 'gulpfile.babel.js', 'webpack.config.babel.js', 'bin/fimfic2epub'], watchOpts, (file) => { + return gulp.src(file.path).pipe(lintPipe()) }) }) @@ -113,20 +119,38 @@ gulp.task('default', (done) => { // Watch task gulp.task('watch', (done) => { - sequence('default', ['watch:lint', 'watch:webpack'], done) + sequence('default', ['watch:lint', 'watch:pack', 'watch:webpack'], done) }) -// creates extensions for chrome and firefox +gulp.task('fontawesome', () => { + let copy = gulp.src('node_modules/font-awesome/fonts/fontawesome-webfont.ttf') + .pipe(gulp.dest('extension/build/fonts/')) + let codes = gulp.src('node_modules/font-awesome/scss/_variables.scss') + .pipe(change(convertFontAwesomeVars)) + .pipe(rename({ + basename: 'font-awesome-codes', + extname: '.json', + dirname: '' + })) + .pipe(gulp.dest('build/')) + return merge(copy, codes) +}) gulp.task('pack', (done) => { - sequence(['pack:firefox', 'pack:chrome', 'pack:safari'], done) + sequence(['pack:firefox', 'pack:chrome'], done) +}) +gulp.task('watch:pack', () => { + return watch(['extension/**/*', '!extension/build/**/*'], watchOpts, () => { + return sequence('pack') + }) }) -gulp.task('pack:firefox', () => { - let manifest = filter('extension/manifest.json', {restore: true}) +gulp.task('pack:firefox', ['version'], () => { + const manifest = filter('extension/manifest.json', {restore: true}) return gulp.src('extension/**/*') .pipe(manifest) - .pipe(jsonedit(function (json) { + .pipe(jsonedit((json) => { + json.version = packageVersion if (json.content_scripts) { // tweak the manifest so Firefox can read it json.applications = { @@ -143,20 +167,23 @@ gulp.task('pack:firefox', () => { .pipe(gulp.dest('./')) }) -gulp.task('pack:chrome', (done) => { - execFile('./packchrome.sh', [], (error, stdout, stderr) => { - // gutil.log('[pack:chrome]', stdout) - if (error) { - done(new gutil.PluginError('pack:chrome', stderr, {showStack: false})) - return - } - done() - }) +gulp.task('pack:chrome', ['version'], (done) => { + const manifest = filter('extension/manifest.json', {restore: true}) + + return gulp.src('extension/**/*') + .pipe(manifest) + .pipe(jsonedit({ + version: packageVersion + })) + .pipe(manifest.restore) + .pipe(zip('extension.zip')) + .pipe(gulp.dest('./')) }) +/* gulp.task('pack:safari', (done) => { exec('rm -rf fimfic2epub.safariextension/; cp -r extension/ fimfic2epub.safariextension', [], (error, stdout, stderr) => { - // gutil.log('[pack:chrome]', stdout) + // gutil.log('[pack:safari]', stdout) if (error || stderr) { done(new gutil.PluginError('pack:safari', stderr, {showStack: false})) return @@ -164,3 +191,4 @@ gulp.task('pack:safari', (done) => { done() }) }) +*/ diff --git a/package.json b/package.json index 48895b8..93b1ffc 100644 --- a/package.json +++ b/package.json @@ -8,15 +8,15 @@ "url": "https://github.com/daniel-j/fimfic2epub.git" }, "scripts": { - "build": "gulp -p" + "build": "NODE_ENV=production gulp", + "dev": "NODE_ENV=development gulp" }, "bin": { "fimfic2epub": "./bin/fimfic2epub" }, - "main": "fimfic2epub.js", + "main": "dist/fimfic2epub.js", "files": [ - "fimfic2epub.js", - "fimfic2epub.js.map", + "dist/", "bin/" ], "dependencies": { @@ -40,28 +40,33 @@ "devDependencies": { "autosize": "^4.0.0", "babel-core": "^6.26.0", - "babel-loader": "^7.1.2", + "babel-loader": "^7.1.4", + "babel-preset-env": "^1.6.1", "babel-preset-es2015": "^6.14.0", "babel-preset-node6": "^11.0.0", "babel-register": "^6.26.0", "del": "^3.0.0", "es6-event-emitter": "^1.10.2", - "exports-loader": "^0.6.3", + "exports-loader": "^0.7.0", "file-saver": "^1.3.2", + "font-awesome": "4.7.0", "gulp": "^3.9.1", + "gulp-change": "^1.0.0", "gulp-filter": "^5.0.1", "gulp-json-editor": "^2.2.1", + "gulp-rename": "^1.2.2", "gulp-standard": "^10.1.1", "gulp-util": "^3.0.7", - "gulp-watch": "^4.3.9", + "gulp-watch": "^5.0.0", "gulp-zip": "^4.0.0", "lazypipe": "^1.0.1", + "merge-stream": "^1.0.1", "raw-loader": "^0.5.1", "run-sequence": "^2.2.0", - "standard": "^10.0.3", + "standard": "^11.0.0", "stylus": "^0.54.5", "stylus-loader": "^3.0.1", - "webpack": "^3.8.1", + "webpack": "^4.1.1", "webpack-node-externals": "^1.3.3" }, "standard": { diff --git a/packchrome.sh b/packchrome.sh index 46577bf..6579aae 100755 --- a/packchrome.sh +++ b/packchrome.sh @@ -4,7 +4,7 @@ CHROME=`command -v chrome || command -v chromium || command -v chromium-browser rm -f extension.crx -z=`cd extension && zip -Xr9D ../extension.zip .` +# z=`cd extension && zip -Xr9D ../extension.zip .` code=-1 diff --git a/webpack.config.babel.js b/webpack.config.babel.js index c1b1afa..4408b07 100644 --- a/webpack.config.babel.js +++ b/webpack.config.babel.js @@ -12,7 +12,7 @@ const bundleExtensionConfig = { output: { path: path.join(__dirname, '/'), - filename: './extension/[name].js' + filename: './extension/build/[name].js' }, module: { @@ -38,16 +38,27 @@ const bundleExtensionConfig = { modules: [ path.resolve('./src'), 'node_modules' - ], - alias: { - fs: require.resolve('./src/false.js') - } + ] + }, + + node: { + fs: 'empty' }, externals: ['request'], - plugins: [], - devtool: 'source-map' + plugins: [ + // new (require('webpack-bundle-analyzer').BundleAnalyzerPlugin)() + ], + performance: { + hints: false + }, + optimization: { + concatenateModules: inProduction, + minimize: inProduction + }, + devtool: inProduction ? 'nosources-source-map' : 'source-map', + mode: inProduction ? 'production' : 'development' } const bundleNpmModuleConfig = { @@ -55,7 +66,7 @@ const bundleNpmModuleConfig = { output: { path: path.join(__dirname, '/'), - filename: './fimfic2epub.js', + filename: './dist/fimfic2epub.js', libraryTarget: 'commonjs2' }, @@ -69,7 +80,11 @@ const bundleNpmModuleConfig = { exclude: /node_modules/, query: { sourceMaps: !inProduction, - presets: ['es2015'] + presets: [['env', { + targets: { + node: '8.0.0' + } + }]] } }, { @@ -87,10 +102,22 @@ const bundleNpmModuleConfig = { ] }, + node: { + __dirname: false + }, + externals: [nodeExternals({whitelist: ['es6-event-emitter', /^babel-runtime/]})], plugins: [], - devtool: 'source-map' + performance: { + hints: false + }, + optimization: { + concatenateModules: inProduction, + minimize: inProduction + }, + devtool: 'nosources-source-map', + mode: inProduction ? 'production' : 'development' } export default [bundleExtensionConfig, bundleNpmModuleConfig]