fix(webpack): Add etcher.js to webpack build

Change-type: patch
Signed-off-by: Lorenzo Alberto Maria Ambrosi <lorenzoa@resin.io>
This commit is contained in:
Lorenzo Alberto Maria Ambrosi 2018-08-10 15:50:38 +02:00
parent ea834f6778
commit c0ec74bbb7
5 changed files with 101 additions and 43 deletions

View File

@ -81,7 +81,13 @@ const createMainWindow = () => {
event.preventDefault()
})
mainWindow.loadURL(`file://${path.join(__dirname, 'app', 'index.html')}`)
const dir = __dirname.split(path.sep).pop()
if (dir === 'generated') {
mainWindow.loadURL(`file://${path.join(__dirname, '..', 'lib', 'gui', 'app', 'index.html')}`)
} else {
mainWindow.loadURL(`file://${path.join(__dirname, 'app', 'index.html')}`)
}
}
electron.app.on('window-all-closed', electron.app.quit)

View File

@ -29,5 +29,5 @@
if (process.env.ELECTRON_RUN_AS_NODE || process.env.ATOM_SHELL_INTERNAL_RUN_AS_NODE) {
require('./gui/modules/child-writer')
} else {
require('./gui/etcher')
require('../generated/etcher')
}

View File

@ -30,7 +30,7 @@
"concourse-build-electron": "make webpack",
"concourse-test": "npm test",
"concourse-test-electron": "npm test",
"concourse-test-node-cli": "make lint-js lint-cpp lint-spell test-cli"
"concourse-test-node-cli": "make webpack lint-js lint-cpp lint-spell test-cli"
},
"author": "Resin Inc. <hello@etcher.io>",
"license": "Apache-2.0",

View File

@ -75,4 +75,5 @@ else
export TARGET_ARCH="$ARGV_ARCHITECTURE"
make info
make electron-develop
make webpack
fi

View File

@ -16,52 +16,14 @@
'use strict'
const _ = require('lodash')
const webpack = require('webpack')
const path = require('path')
const SimpleProgressWebpackPlugin = require('simple-progress-webpack-plugin')
const nodeExternals = require('webpack-node-externals')
module.exports = {
const commonConfig = {
target: 'electron-main',
node: {
__dirname: true,
__filename: true
},
entry: {
gui: path.join(__dirname, 'lib', 'gui', 'app', 'app.js')
},
output: {
path: path.join(__dirname, 'generated'),
filename: '[name].js'
},
externals: [
nodeExternals(),
(context, request, callback) => {
// eslint-disable-next-line lodash/prefer-lodash-method
const absoluteContext = path.resolve(context)
const absoluteNodeModules = path.resolve('node_modules')
// We shouldn't rewrite any node_modules import paths
// eslint-disable-next-line lodash/prefer-lodash-method
if (!path.relative(absoluteNodeModules, absoluteContext).startsWith('..')) {
return callback()
}
// We want to keep the SDK code outside the GUI bundle.
// This piece of code allows us to run the GUI directly
// on the tree (for testing purposes) or inside a generated
// bundle (for production purposes), by translating
// relative require paths within the bundle.
if (/\/(sdk|shared)/i.test(request) || /package\.json$/.test(request)) {
const output = path.join(__dirname, 'generated')
const dirname = path.join(context, request)
const relative = path.relative(output, dirname)
return callback(null, `commonjs ${path.join('..', '..', relative)}`)
}
return callback()
}
],
module: {
rules: [
{
@ -96,3 +58,92 @@ module.exports = {
})
]
}
const guiConfig = _.assign({
node: {
__dirname: true,
__filename: true
},
externals: [
nodeExternals(),
(context, request, callback) => {
// eslint-disable-next-line lodash/prefer-lodash-method
const absoluteContext = path.resolve(context)
const absoluteNodeModules = path.resolve('node_modules')
// We shouldn't rewrite any node_modules import paths
// eslint-disable-next-line lodash/prefer-lodash-method
if (!path.relative(absoluteNodeModules, absoluteContext).startsWith('..')) {
return callback()
}
// We want to keep the SDK code outside the GUI bundle.
// This piece of code allows us to run the GUI directly
// on the tree (for testing purposes) or inside a generated
// bundle (for production purposes), by translating
// relative require paths within the bundle.
if (/\/(sdk|shared)/i.test(request) || /package\.json$/.test(request)) {
const output = path.join(__dirname, 'generated')
const dirname = path.join(context, request)
const relative = path.relative(output, dirname)
return callback(null, `commonjs ${path.join('..', '..', relative)}`)
}
return callback()
}
],
entry: {
gui: path.join(__dirname, 'lib', 'gui', 'app', 'app.js')
},
output: {
path: path.join(__dirname, 'generated'),
filename: '[name].js'
}
}, commonConfig)
const etcherConfig = _.assign({
node: {
__dirname: false,
__filename: true
},
externals: [
nodeExternals(),
(context, request, callback) => {
// eslint-disable-next-line lodash/prefer-lodash-method
const absoluteContext = path.resolve(context)
const absoluteNodeModules = path.resolve('node_modules')
// We shouldn't rewrite any node_modules import paths
// eslint-disable-next-line lodash/prefer-lodash-method
if (!path.relative(absoluteNodeModules, absoluteContext).startsWith('..')) {
return callback()
}
// We want to keep the SDK code outside the GUI bundle.
// This piece of code allows us to run the GUI directly
// on the tree (for testing purposes) or inside a generated
// bundle (for production purposes), by translating
// relative require paths within the bundle.
if (/\/(sdk|shared)/i.test(request) || /package\.json$/.test(request)) {
const output = path.join(__dirname, 'generated')
const dirname = path.join(context, request)
const relative = path.relative(output, dirname)
return callback(null, `commonjs ${path.join('..', 'lib', relative)}`)
}
return callback()
}
],
entry: {
etcher: path.join(__dirname, 'lib', 'gui', 'etcher.js')
},
output: {
path: path.join(__dirname, 'generated'),
filename: '[name].js'
}
}, commonConfig)
module.exports = [
guiConfig,
etcherConfig
]