From e581c68b05a8063075bff026258f5a130b874879 Mon Sep 17 00:00:00 2001 From: victorbjelkholm Date: Wed, 3 Oct 2018 14:09:27 +0200 Subject: [PATCH] fix: generate sourcemap for minified build Also added a tiny little acceptance test for easier testing with aegir-test-repo License: MIT Signed-off-by: Victor Bjelkholm --- package.json | 1 + src/build/browser.js | 21 +++++++++----- src/config/webpack/index.js | 26 ++++++++++++++---- test/acceptance.sh | 55 +++++++++++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 13 deletions(-) create mode 100755 test/acceptance.sh diff --git a/package.json b/package.json index a5a6bdd56..f6b1d8f67 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "lint": "node cli.js lint", "test:node": "cross-env AEGIR_TEST=hello node cli.js test -t node --files 'test/**/*.spec.js'", "test:browser": "cross-env AEGIR_TEST=hello node cli.js test -t browser webworker --files test/browser.spec.js", + "test:acceptance": "./test/acceptance.sh", "test": "npm run test:node && npm run test:browser", "coverage": "cross-env AEGIR_TEST=hello node cli.js coverage -t node", "watch": "cross-env AEGIR_TEST=hello node cli.js test -t node --watch", diff --git a/src/build/browser.js b/src/build/browser.js index 3d29674dd..7a6601744 100644 --- a/src/build/browser.js +++ b/src/build/browser.js @@ -35,23 +35,30 @@ function writeStats (ctx) { function minify (ctx, task) { const minifiedPath = path.join(process.cwd(), 'dist', 'index.min.js') + const mapPath = path.join(process.cwd(), 'dist', 'index.min.js.map') return fs.readFile(path.join(process.cwd(), 'dist', 'index.js')) .then((code) => { const result = Uglify.minify(code.toString(), { mangle: true, - compress: { unused: false } + compress: { unused: false }, + sourceMap: { + filename: 'index.min.js', + url: 'index.min.js.map' + } }) if (result.error) { throw result.error } - return result.code + return result }) - .then((minified) => { - return fs.writeFile( - minifiedPath, - minified - ) + .then(({code, map}) => { + return fs.writeFile(mapPath, map).then(() => { + return fs.writeFile( + minifiedPath, + code + ) + }) }) .then(() => fs.stat(minifiedPath)) .then((stats) => { diff --git a/src/config/webpack/index.js b/src/config/webpack/index.js index d0267419e..d4217f02b 100644 --- a/src/config/webpack/index.js +++ b/src/config/webpack/index.js @@ -10,9 +10,23 @@ const utils = require('../../utils') const base = require('./base') const user = require('../user')() -function webpackConfig (userEnv) { - const env = userEnv || 'production' - debug('Running webpack with userEnv', userEnv, 'ends up being', env) +const devtool = { + 'development': 'source-map', + 'production': 'source-map', + 'test': 'inline-source-map' +} + +const getDevtool = (env) => { + const d = devtool[env] + if (!d) { + const jsonStr = JSON.stringify(devtool, null, 2) + throw new Error(`Could not find devtool for '${env}' in ${jsonStr}`) + } + return d +} + +function webpackConfig (env) { + debug('Running webpack with env', env) return utils.getPkg().then((pkg) => { const libraryName = utils.getLibraryName(pkg.name) @@ -27,14 +41,14 @@ function webpackConfig (userEnv) { } else { environment.TEST_BROWSER_JS = JSON.stringify('') } - const sourcemap = env === 'test' ? 'inline-source-map' : 'source-map' + const mode = env === 'production' ? 'production' : 'development' return merge(base, { - mode: env === 'production' ? 'production' : 'development', + mode: mode, entry: [ entry ], - devtool: sourcemap, + devtool: getDevtool(env), output: { filename: path.basename(entry), library: libraryName, diff --git a/test/acceptance.sh b/test/acceptance.sh new file mode 100755 index 000000000..fc081f7af --- /dev/null +++ b/test/acceptance.sh @@ -0,0 +1,55 @@ +#! /usr/bin/env bash + +# Acceptance test for making sure aegir works as we want it to + +set -e + +# Debug +# set -x + +echo "## Creating a link to current AEgir" +npm link + +echo "## Creating test directory" +TEST_DIR=$(mktemp -d) + +cd $TEST_DIR + +echo "## Cloning aegir-test-repo" +git clone https://github.com/ipfs/aegir-test-repo + +cd aegir-test-repo + +echo "## Installing dependencies for aegir-test-repo" +npm install + +echo "## Linking current AEgir into aegir-test-repo" +npm link aegir + +echo "## Running build command" +npm run build + +echo "## Making sure right files were created" + +if [ ! -d "dist" ]; then + echo "'dist/' directory wasn't created correctly " + exit 1 +fi + +if [ ! -e "dist/index.js" ]; then + echo "'dist/index.js' file wasn't created correctly " + exit 1 +fi + +if [ ! -e "dist/index.min.js" ]; then + echo "'dist/index.min.js' file wasn't created correctly " + exit 1 +fi + +if [ ! -e "dist/index.min.js.map" ]; then + echo "'dist/index.min.js.map' file wasn't created correctly " + exit 1 +fi + +echo "## Cleaning up" +rm -rf $TEST_DIR