Skip to content

Commit

Permalink
add possibility to change node dirname option dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
BenedyktT committed Sep 30, 2021
1 parent 2a675c0 commit f2f2552
Show file tree
Hide file tree
Showing 3 changed files with 8,332 additions and 59 deletions.
3 changes: 2 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = {
externals: ["knex", "sharp"],
// Set default file extensions to use the raw-loader with
rawFileExtensions: ["pem", "txt"],
sourceType: "module" // "script" | "module" | "unambiguous"
sourceType: "module", // "script" | "module" | "unambiguous"
nodeDirname: false
}
};
118 changes: 60 additions & 58 deletions src/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const forceExclude = config.options.forceExclude;
const ignorePackages = config.options.ignorePackages;
const rawFileExtensions = config.options.rawFileExtensions;
const sourceType = config.options.sourceType;
const nodeDirname = config.options.nodeDirname;
const fixPackages = convertListToObject(config.options.fixPackages);
const tsConfigPath = path.resolve(servicePath, config.options.tsConfig);

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

// Handle the "all" option in externals
// And add the forceExclude packages to it because they shouldn't be Webpacked
const computedExternals = (
externals === "all" ? [nodeExternals()] : externals
const computedExternals = (externals === "all"
? [nodeExternals()]
: externals
).concat(forceExclude);

const extensions = [
Expand All @@ -64,7 +66,7 @@ const extensions = [
".ts",
".tsx",
".graphql",
".gql",
".gql"
];

// If tsConfig is specified and not found, throw an error
Expand Down Expand Up @@ -125,7 +127,7 @@ function babelLoader() {
"@babel/plugin-transform-runtime",
"@babel/plugin-proposal-class-properties",
"@babel/plugin-proposal-optional-chaining",
"@babel/plugin-proposal-nullish-coalescing-operator",
"@babel/plugin-proposal-nullish-coalescing-operator"
];

if (ENABLE_SOURCE_MAPS) {
Expand All @@ -146,19 +148,19 @@ function babelLoader() {
require.resolve("@babel/preset-env"),
{
targets: {
node: nodeVersion,
},
},
],
],
},
node: nodeVersion
}
}
]
]
}
};
}

function esbuildLoader(loader) {
const options = {
loader,
target: esbuildNodeVersion,
target: esbuildNodeVersion
};

if (ENABLE_TYPESCRIPT) {
Expand All @@ -167,7 +169,7 @@ function esbuildLoader(loader) {

return {
loader: "esbuild-loader",
options,
options
};
}

Expand All @@ -179,16 +181,16 @@ function tsLoader() {
configFile: tsConfigPath,
experimentalWatchApi: true,
// Don't check types if ForTsChecker is enabled
transpileOnly: ENABLE_TSCHECKER,
},
transpileOnly: ENABLE_TSCHECKER
}
};
}

function loaders() {
const jsRule = {
test: /\.js$/,
exclude: /node_modules/,
use: [ENABLE_ESBUILD ? esbuildLoader("jsx") : babelLoader()],
use: [ENABLE_ESBUILD ? esbuildLoader("jsx") : babelLoader()]
};

const loaders = {
Expand All @@ -199,13 +201,13 @@ function loaders() {
include: /node_modules/,
type: "javascript/auto",
resolve: {
fullySpecified: false,
},
fullySpecified: false
}
},
{
test: /\.(graphql|gql)$/,
exclude: /node_modules/,
loader: "graphql-tag/loader",
loader: "graphql-tag/loader"
},
{
test: /\.css$/,
Expand All @@ -214,10 +216,10 @@ function loaders() {
{
loader: "css-loader",
options: {
importLoaders: 1,
},
},
],
importLoaders: 1
}
}
]
},
{
test: /\.s[ac]ss$/i,
Expand All @@ -226,19 +228,19 @@ function loaders() {
{
loader: "css-loader",
options: {
importLoaders: 1,
},
importLoaders: 1
}
},
{
loader: "sass-loader",
options: {
implementation: require("sass"),
},
},
],
implementation: require("sass")
}
}
]
},
{ test: /\.gif|\.svg|\.png|\.jpg|\.jpeg$/, loader: "ignore-loader" },
],
{ test: /\.gif|\.svg|\.png|\.jpg|\.jpeg$/, loader: "ignore-loader" }
]
};

if (ENABLE_TYPESCRIPT) {
Expand All @@ -249,22 +251,22 @@ function loaders() {
[
path.resolve(servicePath, "node_modules"),
path.resolve(servicePath, ".serverless"),
path.resolve(servicePath, ".webpack"),
],
],
path.resolve(servicePath, ".webpack")
]
]
};

loaders.rules.push(tsRule);
}

if (rawFileExtensions && rawFileExtensions.length) {
const rawFileRegex = `${rawFileExtensions
.map((rawFileExt) => `\\.${rawFileExt}`)
.map(rawFileExt => `\\.${rawFileExt}`)
.join("|")}$`;

loaders.rules.push({
test: new RegExp(rawFileRegex),
loader: "raw-loader",
loader: "raw-loader"
});
}

Expand All @@ -278,14 +280,14 @@ function plugins() {
const forkTsCheckerWebpackOptions = {
typescript: {
configFile: tsConfigPath,
build: true,
},
build: true
}
};

if (ENABLE_LINTING) {
forkTsCheckerWebpackOptions.eslint = {
files: path.join(servicePath, "**/*.ts"),
options: { cwd: servicePath, baseConfig: tsEslintConfig },
options: { cwd: servicePath, baseConfig: tsEslintConfig }
};
}

Expand All @@ -297,7 +299,7 @@ function plugins() {
new ESLintPlugin({
context: servicePath,
baseConfig: jsEslintConfig,
extensions: "js",
extensions: "js"
})
);

Expand All @@ -307,7 +309,7 @@ function plugins() {
new ESLintPlugin({
context: servicePath,
baseConfig: tsEslintConfig,
extensions: ["ts"],
extensions: ["ts"]
})
);
}
Expand All @@ -316,22 +318,22 @@ function plugins() {
if (copyFiles) {
plugins.push(
new CopyWebpackPlugin({
patterns: copyFiles.map(function (data) {
patterns: copyFiles.map(function(data) {
return {
to: data.to,
context: servicePath,
from: path.join(servicePath, data.from),
from: path.join(servicePath, data.from)
};
}),
})
})
);

// Copy file permissions
const buildFiles = [];
copyFiles.forEach(function (data) {
copyFiles.forEach(function(data) {
const entries = fastGlob.sync([data.from]);
// loop through each file matched by fg
entries.forEach(function (entry) {
entries.forEach(function(entry) {
// get source file stat
const stat = fs.statSync(path.resolve(servicePath, entry));
const { serverless } = slsw.lib;
Expand All @@ -342,13 +344,13 @@ function plugins() {
for (let key in serverless.service.functions) {
buildFiles.push({
fileMode: statModeToOctal(stat.mode),
path: path.resolve(data.to, `.webpack/${key}`, entry),
path: path.resolve(data.to, `.webpack/${key}`, entry)
});
}
} else {
buildFiles.push({
fileMode: statModeToOctal(stat.mode),
path: path.resolve(data.to, ".webpack/service", entry),
path: path.resolve(data.to, ".webpack/service", entry)
});
}
});
Expand All @@ -359,7 +361,7 @@ function plugins() {
if (concatText) {
const concatTextConfig = {};

concatText.map(function (data) {
concatText.map(function(data) {
concatTextConfig.files = data.files || null;
concatTextConfig.name = data.name || null;
concatTextConfig.outputPath = data.outputPath || null;
Expand All @@ -380,7 +382,7 @@ function plugins() {
for (let i = 0, l = ignorePackages.length; i < l; i++) {
plugins.push(
new webpack.IgnorePlugin({
resourceRegExp: new RegExp("^" + ignorePackages[i] + "$"),
resourceRegExp: new RegExp("^" + ignorePackages[i] + "$")
})
);
}
Expand All @@ -399,7 +401,7 @@ function resolvePlugins() {
plugins.push(
new TsconfigPathsPlugin({
configFile: tsConfigPath,
extensions: extensions,
extensions: extensions
})
);
}
Expand All @@ -426,7 +428,7 @@ module.exports = {
mode: isLocal ? "development" : "production",
performance: {
// Turn off size warnings for entry points
hints: false,
hints: false
},
resolve: {
// Performance
Expand All @@ -436,7 +438,7 @@ module.exports = {
// First start by looking for modules in the plugin's node_modules
// before looking inside the project's node_modules.
modules: [path.resolve(__dirname, "node_modules"), "node_modules"],
plugins: resolvePlugins(),
plugins: resolvePlugins()
},
// Add loaders
module: loaders(),
Expand All @@ -446,18 +448,18 @@ module.exports = {
nodeEnv: false,
splitChunks: false,
removeEmptyChunks: false,
removeAvailableModules: false,
removeAvailableModules: false
}
: {
nodeEnv: false,
minimizer: [
new ESBuildMinifyPlugin({
target: esbuildNodeVersion,
}),
],
target: esbuildNodeVersion
})
]
},
plugins: plugins(),
node: {
__dirname: false,
},
__dirname: nodeDirname
}
};
Loading

0 comments on commit f2f2552

Please sign in to comment.