Skip to content

Commit 22c07dd

Browse files
authored
Merge pull request #197 from elastic-coders/set-location-option-automatically
Set offline location parameter automatically.
2 parents c5841bd + 3e69a83 commit 22c07dd

4 files changed

Lines changed: 41 additions & 11 deletions

File tree

index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const wpwatch = require('./lib/wpwatch');
99
const cleanup = require('./lib/cleanup');
1010
const run = require('./lib/run');
1111
const prepareLocalInvoke = require('./lib/prepareLocalInvoke');
12+
const prepareOfflineInvoke = require('./lib/prepareOfflineInvoke');
1213
const packExternalModules = require('./lib/packExternalModules');
1314
const packageModules = require('./lib/packageModules');
1415
const lib = require('./lib');
@@ -39,7 +40,8 @@ class ServerlessWebpack {
3940
run,
4041
packExternalModules,
4142
packageModules,
42-
prepareLocalInvoke
43+
prepareLocalInvoke,
44+
prepareOfflineInvoke
4345
);
4446

4547
this.commands = {
@@ -125,12 +127,12 @@ class ServerlessWebpack {
125127
.then(() => BbPromise.reject(new this.serverless.classes.Error('serve has been removed. Use serverless-offline instead.'))),
126128

127129
'before:offline:start': () => BbPromise.bind(this)
128-
.then(this.validate)
130+
.then(this.prepareOfflineInvoke)
129131
.then(this.compile)
130132
.then(this.wpwatch),
131133

132134
'before:offline:start:init': () => BbPromise.bind(this)
133-
.then(this.validate)
135+
.then(this.prepareOfflineInvoke)
134136
.then(this.compile)
135137
.then(this.wpwatch),
136138

lib/prepareOfflineInvoke.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
const _ = require('lodash');
4+
const path = require('path');
5+
6+
/**
7+
* Special settings for use with serverless-offline.
8+
*/
9+
10+
module.exports = {
11+
prepareOfflineInvoke() {
12+
13+
// Use service packaging for compile
14+
_.set(this.serverless, 'service.package.individually', false);
15+
16+
return this.validate()
17+
.then(() => {
18+
// Set offline location automatically if not set manually
19+
if (!this.options.location && !_.get(this.serverless, 'service.custom.serverless-offline.location')) {
20+
_.set(this.serverless, 'service.custom.serverless-offline.location',
21+
path.relative(this.serverless.config.servicePath, path.join(this.webpackOutputPath, 'service'))
22+
);
23+
}
24+
return null;
25+
});
26+
}
27+
};

lib/validate.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ const _ = require('lodash');
1515
const preferredExtensions = [
1616
'.js',
1717
'.ts',
18-
'.jsx'
18+
'.jsx',
19+
'.tsx'
1920
];
2021

2122
module.exports = {

lib/wpwatch.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ module.exports = {
99

1010
const compiler = webpack(this.webpackConfig);
1111
compiler.watch({}, (err, stats) => {
12-
if (err) {
13-
throw err;
14-
}
12+
if (err) {
13+
throw err;
14+
}
1515

16-
if (stats) {
17-
console.log(stats.toString());
18-
}
19-
});
16+
if (stats) {
17+
console.log(stats.toString()); // eslint-disable-line no-console
18+
}
19+
});
2020

2121
return BbPromise.resolve();
2222
},

0 commit comments

Comments
 (0)