Skip to content

Commit

Permalink
fix: generate sourcemap for minified build
Browse files Browse the repository at this point in the history
Also added a tiny little acceptance test for easier testing with
aegir-test-repo

License: MIT
Signed-off-by: Victor Bjelkholm <[email protected]>
  • Loading branch information
victorb committed Oct 3, 2018
1 parent af41034 commit e581c68
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 13 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
21 changes: 14 additions & 7 deletions src/build/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
26 changes: 20 additions & 6 deletions src/config/webpack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down
55 changes: 55 additions & 0 deletions test/acceptance.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e581c68

Please sign in to comment.