2020-11-17 20:26:24 +02:00
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const CopyPlugin = require('copy-webpack-plugin');
|
|
|
|
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-17 20:26:24 +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-17 20:26:24 +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}`);
|
|
|
|
}
|
|
|
|
|
2021-01-03 15:21:48 +02:00
|
|
|
const rootDir = path.resolve(__dirname);
|
|
|
|
const distDir = path.resolve(rootDir, 'dist');
|
|
|
|
const srcDir = path.resolve(rootDir, 'src');
|
2020-11-17 20:26:24 +02:00
|
|
|
const manifestPath = `${srcDir}/manifest.json`;
|
|
|
|
const manifest = readManifest(manifestPath);
|
|
|
|
const archiveFilePath = path.resolve(__dirname, `${manifest.id}.jpl`);
|
|
|
|
|
2020-11-23 19:06:52 +02:00
|
|
|
fs.removeSync(distDir);
|
|
|
|
|
2021-01-03 15:21:48 +02:00
|
|
|
const baseConfig = {
|
2020-11-17 20:26:24 +02:00
|
|
|
mode: 'production',
|
|
|
|
target: 'node',
|
|
|
|
module: {
|
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
test: /\.tsx?$/,
|
|
|
|
use: 'ts-loader',
|
|
|
|
exclude: /node_modules/,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2021-01-03 15:21:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const pluginConfig = Object.assign({}, baseConfig, {
|
|
|
|
entry: './src/index.ts',
|
2020-11-17 20:26:24 +02:00
|
|
|
resolve: {
|
|
|
|
alias: {
|
|
|
|
api: path.resolve(__dirname, 'api'),
|
|
|
|
},
|
|
|
|
extensions: ['.tsx', '.ts', '.js'],
|
|
|
|
},
|
|
|
|
output: {
|
|
|
|
filename: 'index.js',
|
|
|
|
path: distDir,
|
|
|
|
},
|
2021-01-03 15:21:48 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const lastStepConfig = {
|
2020-11-17 20:26:24 +02:00
|
|
|
plugins: [
|
|
|
|
new CopyPlugin({
|
|
|
|
patterns: [
|
|
|
|
{
|
|
|
|
from: '**/*',
|
|
|
|
context: path.resolve(__dirname, 'src'),
|
|
|
|
to: path.resolve(__dirname, 'dist'),
|
|
|
|
globOptions: {
|
|
|
|
ignore: [
|
2021-01-03 15:21:48 +02:00
|
|
|
// All TypeScript files are compiled to JS and
|
|
|
|
// already copied into /dist so we don't copy them.
|
2020-11-17 20:26:24 +02:00
|
|
|
'**/*.ts',
|
|
|
|
'**/*.tsx',
|
2021-01-03 15:21:48 +02:00
|
|
|
|
|
|
|
// Currently we don't support JS files for the main
|
|
|
|
// plugin script. We support it for content scripts,
|
|
|
|
// but theyr should be declared in manifest.json,
|
|
|
|
// and then they are also compiled and copied to
|
|
|
|
// /dist. So wse also don't need to copy JS files.
|
|
|
|
'**/*.js',
|
2020-11-17 20:26:24 +02:00
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
new WebpackOnBuildPlugin(function() {
|
|
|
|
createPluginArchive(distDir, archiveFilePath);
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
};
|
2021-01-03 15:21:48 +02:00
|
|
|
|
|
|
|
const contentScriptConfig = Object.assign({}, baseConfig, {
|
|
|
|
resolve: {
|
|
|
|
alias: {
|
|
|
|
api: path.resolve(__dirname, 'api'),
|
|
|
|
},
|
|
|
|
extensions: ['.tsx', '.ts', '.js'],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
function resolveContentScriptPaths(name) {
|
|
|
|
if (['.js', '.ts', '.tsx'].includes(path.extname(name).toLowerCase())) {
|
|
|
|
throw new Error(`Content script path must not include file extension: ${name}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const pathsToTry = [
|
|
|
|
`./src/${name}.ts`,
|
|
|
|
`${'./src/' + '/'}${name}.js`,
|
|
|
|
];
|
|
|
|
|
|
|
|
for (const pathToTry of pathsToTry) {
|
|
|
|
if (fs.pathExistsSync(`${rootDir}/${pathToTry}`)) {
|
|
|
|
return {
|
|
|
|
entry: pathToTry,
|
|
|
|
output: {
|
|
|
|
filename: `${name}.js`,
|
|
|
|
path: distDir,
|
|
|
|
library: 'default',
|
|
|
|
libraryTarget: 'commonjs',
|
|
|
|
libraryExport: 'default',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(`Could not find content script "${name}" at locations ${JSON.stringify(pathsToTry)}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createContentScriptConfigs() {
|
|
|
|
if (!manifest.content_scripts) return [];
|
|
|
|
|
|
|
|
const output = [];
|
|
|
|
|
|
|
|
for (const contentScriptName of manifest.content_scripts) {
|
|
|
|
const scriptPaths = resolveContentScriptPaths(contentScriptName);
|
|
|
|
output.push(Object.assign({}, contentScriptConfig, {
|
|
|
|
entry: scriptPaths.entry,
|
|
|
|
output: scriptPaths.output,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
const exportedConfigs = [pluginConfig].concat(createContentScriptConfigs());
|
|
|
|
|
|
|
|
exportedConfigs[exportedConfigs.length - 1] = Object.assign({}, exportedConfigs[exportedConfigs.length - 1], lastStepConfig);
|
|
|
|
|
|
|
|
module.exports = exportedConfigs;
|