Last active
September 12, 2019 00:47
-
-
Save n1xx1/fad44388085b4827fd0cbb77172b7215 to your computer and use it in GitHub Desktop.
Alt:V webpack configuration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const WrapperPlugin = require('wrapper-webpack-plugin'); | |
const path = require("path"); | |
const resolve = require("resolve"); | |
const notResolved = {}; | |
const CONFIG = module.exports = { | |
mode: "development", | |
devtool: "", | |
entry: { | |
client: path.resolve("./src/client/index.ts"), | |
server: path.resolve("./src/server/index.ts"), | |
}, | |
output: { | |
filename: "[name].mjs", | |
path: path.resolve(__dirname, "build"), | |
}, | |
resolve: { | |
extensions: [".ts", ".tsx", ".js", ".json"], | |
}, | |
externals: function(context, request, callback) { | |
resolve(request, { basedir: context }, (err) => { | |
if (err === null) { | |
callback(); | |
return; | |
} | |
for (const [id, path] of Object.entries(CONFIG.entry)) { | |
if (!isPathInside(context, path)) { | |
continue; | |
} | |
if (notResolved[id] === undefined) { | |
notResolved[id] = []; | |
} | |
if (notResolved[id].indexOf(request) === -1) { | |
notResolved[id].push(request); | |
} | |
callback(null, request, "root"); | |
return; | |
} | |
throw new Error("cannot resolve path: " + request); | |
}); | |
}, | |
module: { | |
rules: [ | |
{ | |
test: /\.tsx?$/, | |
include: path.join(__dirname, "./src/client"), | |
use: { | |
loader: "ts-loader", | |
options: { | |
instance: "client", | |
configFile: path.join(__dirname, "./src/client/tsconfig.json"), | |
}, | |
} | |
}, | |
{ | |
test: /\.tsx?$/, | |
include: path.join(__dirname, "./src/server"), | |
use: { | |
loader: "ts-loader", | |
options: { | |
instance: "server", | |
configFile: path.join(__dirname, "./src/server/tsconfig.json"), | |
}, | |
} | |
} | |
] | |
}, | |
plugins: [ | |
new WrapperPlugin({ | |
header: (filename) => { | |
for (const [id, path] of Object.entries(CONFIG.entry)) { | |
const outfile = CONFIG.output.filename.replace("[name]", id); | |
if (filename !== outfile) { | |
continue; | |
} | |
if (notResolved[id] !== undefined) { | |
return notResolved[id].map(x => `import * as ${x} from "${x}";`).join("\n"); | |
} | |
} | |
return ""; | |
}, | |
}), | |
], | |
}; | |
function isPathInside(parent, dir) { | |
const relative = path.relative(parent, dir); | |
return relative && !relative.startsWith('..') && !path.isAbsolute(relative); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment