Skip to content

Commit f2f2552

Browse files
committed
add possibility to change node dirname option dynamically
1 parent 2a675c0 commit f2f2552

File tree

3 files changed

+8332
-59
lines changed

3 files changed

+8332
-59
lines changed

src/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module.exports = {
2828
externals: ["knex", "sharp"],
2929
// Set default file extensions to use the raw-loader with
3030
rawFileExtensions: ["pem", "txt"],
31-
sourceType: "module" // "script" | "module" | "unambiguous"
31+
sourceType: "module", // "script" | "module" | "unambiguous"
32+
nodeDirname: false
3233
}
3334
};

src/webpack.config.js

Lines changed: 60 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const forceExclude = config.options.forceExclude;
3838
const ignorePackages = config.options.ignorePackages;
3939
const rawFileExtensions = config.options.rawFileExtensions;
4040
const sourceType = config.options.sourceType;
41+
const nodeDirname = config.options.nodeDirname;
4142
const fixPackages = convertListToObject(config.options.fixPackages);
4243
const tsConfigPath = path.resolve(servicePath, config.options.tsConfig);
4344

@@ -51,8 +52,9 @@ const ENABLE_CACHING = isLocal ? config.options.caching : false;
5152

5253
// Handle the "all" option in externals
5354
// And add the forceExclude packages to it because they shouldn't be Webpacked
54-
const computedExternals = (
55-
externals === "all" ? [nodeExternals()] : externals
55+
const computedExternals = (externals === "all"
56+
? [nodeExternals()]
57+
: externals
5658
).concat(forceExclude);
5759

5860
const extensions = [
@@ -64,7 +66,7 @@ const extensions = [
6466
".ts",
6567
".tsx",
6668
".graphql",
67-
".gql",
69+
".gql"
6870
];
6971

7072
// If tsConfig is specified and not found, throw an error
@@ -125,7 +127,7 @@ function babelLoader() {
125127
"@babel/plugin-transform-runtime",
126128
"@babel/plugin-proposal-class-properties",
127129
"@babel/plugin-proposal-optional-chaining",
128-
"@babel/plugin-proposal-nullish-coalescing-operator",
130+
"@babel/plugin-proposal-nullish-coalescing-operator"
129131
];
130132

131133
if (ENABLE_SOURCE_MAPS) {
@@ -146,19 +148,19 @@ function babelLoader() {
146148
require.resolve("@babel/preset-env"),
147149
{
148150
targets: {
149-
node: nodeVersion,
150-
},
151-
},
152-
],
153-
],
154-
},
151+
node: nodeVersion
152+
}
153+
}
154+
]
155+
]
156+
}
155157
};
156158
}
157159

158160
function esbuildLoader(loader) {
159161
const options = {
160162
loader,
161-
target: esbuildNodeVersion,
163+
target: esbuildNodeVersion
162164
};
163165

164166
if (ENABLE_TYPESCRIPT) {
@@ -167,7 +169,7 @@ function esbuildLoader(loader) {
167169

168170
return {
169171
loader: "esbuild-loader",
170-
options,
172+
options
171173
};
172174
}
173175

@@ -179,16 +181,16 @@ function tsLoader() {
179181
configFile: tsConfigPath,
180182
experimentalWatchApi: true,
181183
// Don't check types if ForTsChecker is enabled
182-
transpileOnly: ENABLE_TSCHECKER,
183-
},
184+
transpileOnly: ENABLE_TSCHECKER
185+
}
184186
};
185187
}
186188

187189
function loaders() {
188190
const jsRule = {
189191
test: /\.js$/,
190192
exclude: /node_modules/,
191-
use: [ENABLE_ESBUILD ? esbuildLoader("jsx") : babelLoader()],
193+
use: [ENABLE_ESBUILD ? esbuildLoader("jsx") : babelLoader()]
192194
};
193195

194196
const loaders = {
@@ -199,13 +201,13 @@ function loaders() {
199201
include: /node_modules/,
200202
type: "javascript/auto",
201203
resolve: {
202-
fullySpecified: false,
203-
},
204+
fullySpecified: false
205+
}
204206
},
205207
{
206208
test: /\.(graphql|gql)$/,
207209
exclude: /node_modules/,
208-
loader: "graphql-tag/loader",
210+
loader: "graphql-tag/loader"
209211
},
210212
{
211213
test: /\.css$/,
@@ -214,10 +216,10 @@ function loaders() {
214216
{
215217
loader: "css-loader",
216218
options: {
217-
importLoaders: 1,
218-
},
219-
},
220-
],
219+
importLoaders: 1
220+
}
221+
}
222+
]
221223
},
222224
{
223225
test: /\.s[ac]ss$/i,
@@ -226,19 +228,19 @@ function loaders() {
226228
{
227229
loader: "css-loader",
228230
options: {
229-
importLoaders: 1,
230-
},
231+
importLoaders: 1
232+
}
231233
},
232234
{
233235
loader: "sass-loader",
234236
options: {
235-
implementation: require("sass"),
236-
},
237-
},
238-
],
237+
implementation: require("sass")
238+
}
239+
}
240+
]
239241
},
240-
{ test: /\.gif|\.svg|\.png|\.jpg|\.jpeg$/, loader: "ignore-loader" },
241-
],
242+
{ test: /\.gif|\.svg|\.png|\.jpg|\.jpeg$/, loader: "ignore-loader" }
243+
]
242244
};
243245

244246
if (ENABLE_TYPESCRIPT) {
@@ -249,22 +251,22 @@ function loaders() {
249251
[
250252
path.resolve(servicePath, "node_modules"),
251253
path.resolve(servicePath, ".serverless"),
252-
path.resolve(servicePath, ".webpack"),
253-
],
254-
],
254+
path.resolve(servicePath, ".webpack")
255+
]
256+
]
255257
};
256258

257259
loaders.rules.push(tsRule);
258260
}
259261

260262
if (rawFileExtensions && rawFileExtensions.length) {
261263
const rawFileRegex = `${rawFileExtensions
262-
.map((rawFileExt) => `\\.${rawFileExt}`)
264+
.map(rawFileExt => `\\.${rawFileExt}`)
263265
.join("|")}$`;
264266

265267
loaders.rules.push({
266268
test: new RegExp(rawFileRegex),
267-
loader: "raw-loader",
269+
loader: "raw-loader"
268270
});
269271
}
270272

@@ -278,14 +280,14 @@ function plugins() {
278280
const forkTsCheckerWebpackOptions = {
279281
typescript: {
280282
configFile: tsConfigPath,
281-
build: true,
282-
},
283+
build: true
284+
}
283285
};
284286

285287
if (ENABLE_LINTING) {
286288
forkTsCheckerWebpackOptions.eslint = {
287289
files: path.join(servicePath, "**/*.ts"),
288-
options: { cwd: servicePath, baseConfig: tsEslintConfig },
290+
options: { cwd: servicePath, baseConfig: tsEslintConfig }
289291
};
290292
}
291293

@@ -297,7 +299,7 @@ function plugins() {
297299
new ESLintPlugin({
298300
context: servicePath,
299301
baseConfig: jsEslintConfig,
300-
extensions: "js",
302+
extensions: "js"
301303
})
302304
);
303305

@@ -307,7 +309,7 @@ function plugins() {
307309
new ESLintPlugin({
308310
context: servicePath,
309311
baseConfig: tsEslintConfig,
310-
extensions: ["ts"],
312+
extensions: ["ts"]
311313
})
312314
);
313315
}
@@ -316,22 +318,22 @@ function plugins() {
316318
if (copyFiles) {
317319
plugins.push(
318320
new CopyWebpackPlugin({
319-
patterns: copyFiles.map(function (data) {
321+
patterns: copyFiles.map(function(data) {
320322
return {
321323
to: data.to,
322324
context: servicePath,
323-
from: path.join(servicePath, data.from),
325+
from: path.join(servicePath, data.from)
324326
};
325-
}),
327+
})
326328
})
327329
);
328330

329331
// Copy file permissions
330332
const buildFiles = [];
331-
copyFiles.forEach(function (data) {
333+
copyFiles.forEach(function(data) {
332334
const entries = fastGlob.sync([data.from]);
333335
// loop through each file matched by fg
334-
entries.forEach(function (entry) {
336+
entries.forEach(function(entry) {
335337
// get source file stat
336338
const stat = fs.statSync(path.resolve(servicePath, entry));
337339
const { serverless } = slsw.lib;
@@ -342,13 +344,13 @@ function plugins() {
342344
for (let key in serverless.service.functions) {
343345
buildFiles.push({
344346
fileMode: statModeToOctal(stat.mode),
345-
path: path.resolve(data.to, `.webpack/${key}`, entry),
347+
path: path.resolve(data.to, `.webpack/${key}`, entry)
346348
});
347349
}
348350
} else {
349351
buildFiles.push({
350352
fileMode: statModeToOctal(stat.mode),
351-
path: path.resolve(data.to, ".webpack/service", entry),
353+
path: path.resolve(data.to, ".webpack/service", entry)
352354
});
353355
}
354356
});
@@ -359,7 +361,7 @@ function plugins() {
359361
if (concatText) {
360362
const concatTextConfig = {};
361363

362-
concatText.map(function (data) {
364+
concatText.map(function(data) {
363365
concatTextConfig.files = data.files || null;
364366
concatTextConfig.name = data.name || null;
365367
concatTextConfig.outputPath = data.outputPath || null;
@@ -380,7 +382,7 @@ function plugins() {
380382
for (let i = 0, l = ignorePackages.length; i < l; i++) {
381383
plugins.push(
382384
new webpack.IgnorePlugin({
383-
resourceRegExp: new RegExp("^" + ignorePackages[i] + "$"),
385+
resourceRegExp: new RegExp("^" + ignorePackages[i] + "$")
384386
})
385387
);
386388
}
@@ -399,7 +401,7 @@ function resolvePlugins() {
399401
plugins.push(
400402
new TsconfigPathsPlugin({
401403
configFile: tsConfigPath,
402-
extensions: extensions,
404+
extensions: extensions
403405
})
404406
);
405407
}
@@ -426,7 +428,7 @@ module.exports = {
426428
mode: isLocal ? "development" : "production",
427429
performance: {
428430
// Turn off size warnings for entry points
429-
hints: false,
431+
hints: false
430432
},
431433
resolve: {
432434
// Performance
@@ -436,7 +438,7 @@ module.exports = {
436438
// First start by looking for modules in the plugin's node_modules
437439
// before looking inside the project's node_modules.
438440
modules: [path.resolve(__dirname, "node_modules"), "node_modules"],
439-
plugins: resolvePlugins(),
441+
plugins: resolvePlugins()
440442
},
441443
// Add loaders
442444
module: loaders(),
@@ -446,18 +448,18 @@ module.exports = {
446448
nodeEnv: false,
447449
splitChunks: false,
448450
removeEmptyChunks: false,
449-
removeAvailableModules: false,
451+
removeAvailableModules: false
450452
}
451453
: {
452454
nodeEnv: false,
453455
minimizer: [
454456
new ESBuildMinifyPlugin({
455-
target: esbuildNodeVersion,
456-
}),
457-
],
457+
target: esbuildNodeVersion
458+
})
459+
]
458460
},
459461
plugins: plugins(),
460462
node: {
461-
__dirname: false,
462-
},
463+
__dirname: nodeDirname
464+
}
463465
};

0 commit comments

Comments
 (0)