2020-10-21 01:23:55 +02:00
|
|
|
const path = require('path');
|
2020-11-18 12:17:27 +02:00
|
|
|
const fs = require('fs-extra');
|
2020-10-21 01:23:55 +02:00
|
|
|
const CopyPlugin = require('copy-webpack-plugin');
|
2020-11-18 12:17:27 +02:00
|
|
|
const WebpackOnBuildPlugin = require('on-build-webpack');
|
|
|
|
const tar = require('tar');
|
|
|
|
const glob = require('glob');
|
|
|
|
|
|
|
|
function readManifest(manifestPath) {
|
|
|
|
const content = fs.readFileSync(manifestPath, 'utf8');
|
|
|
|
const output = JSON.parse(content);
|
|
|
|
if (!output.id) throw new Error(`Manifest plugin ID is not set in ${manifestPath}`);
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
function createPluginArchive(sourceDir, destPath) {
|
2020-12-11 15:28:59 +02:00
|
|
|
const distFiles = glob.sync(`${sourceDir}/**/*`, { nodir: true })
|
2020-11-18 12:17:27 +02:00
|
|
|
.map(f => f.substr(sourceDir.length + 1));
|
|
|
|
|
2020-11-23 19:06:52 +02:00
|
|
|
if (!distFiles.length) {
|
|
|
|
// Usually means there's an error, which is going to be printed by
|
|
|
|
// webpack
|
|
|
|
console.info('Plugin archive was not created because the "dist" directory is empty');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-18 12:17:27 +02:00
|
|
|
fs.removeSync(destPath);
|
|
|
|
|
|
|
|
tar.create(
|
|
|
|
{
|
|
|
|
strict: true,
|
|
|
|
portable: true,
|
|
|
|
file: destPath,
|
|
|
|
cwd: sourceDir,
|
|
|
|
sync: true,
|
|
|
|
},
|
|
|
|
distFiles
|
|
|
|
);
|
|
|
|
|
|
|
|
console.info(`Plugin archive has been created in ${destPath}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const distDir = path.resolve(__dirname, 'dist');
|
|
|
|
const srcDir = path.resolve(__dirname, 'src');
|
|
|
|
const manifestPath = `${srcDir}/manifest.json`;
|
|
|
|
const manifest = readManifest(manifestPath);
|
|
|
|
const archiveFilePath = path.resolve(__dirname, `${manifest.id}.jpl`);
|
2020-10-21 01:23:55 +02:00
|
|
|
|
2020-11-23 19:06:52 +02:00
|
|
|
fs.removeSync(distDir);
|
|
|
|
|
2020-10-21 01:23:55 +02:00
|
|
|
module.exports = {
|
|
|
|
mode: 'production',
|
|
|
|
entry: './src/index.ts',
|
|
|
|
target: 'node',
|
|
|
|
module: {
|
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
test: /\.tsx?$/,
|
|
|
|
use: 'ts-loader',
|
|
|
|
exclude: /node_modules/,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
resolve: {
|
|
|
|
alias: {
|
2020-11-15 16:18:46 +02:00
|
|
|
api: path.resolve(__dirname, 'api'),
|
2020-10-21 01:23:55 +02:00
|
|
|
},
|
2020-11-15 16:18:46 +02:00
|
|
|
extensions: ['.tsx', '.ts', '.js'],
|
2020-10-21 01:23:55 +02:00
|
|
|
},
|
|
|
|
output: {
|
|
|
|
filename: 'index.js',
|
2020-11-18 12:17:27 +02:00
|
|
|
path: distDir,
|
2020-10-21 01:23:55 +02:00
|
|
|
},
|
|
|
|
plugins: [
|
|
|
|
new CopyPlugin({
|
|
|
|
patterns: [
|
|
|
|
{
|
2020-11-15 16:18:46 +02:00
|
|
|
from: '**/*',
|
2020-10-21 01:23:55 +02:00
|
|
|
context: path.resolve(__dirname, 'src'),
|
|
|
|
to: path.resolve(__dirname, 'dist'),
|
|
|
|
globOptions: {
|
|
|
|
ignore: [
|
|
|
|
'**/*.ts',
|
|
|
|
'**/*.tsx',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}),
|
2020-11-18 12:17:27 +02:00
|
|
|
new WebpackOnBuildPlugin(function() {
|
|
|
|
createPluginArchive(distDir, archiveFilePath);
|
|
|
|
}),
|
2020-10-21 01:23:55 +02:00
|
|
|
],
|
|
|
|
};
|