Copy static files, when running on Vercel, Netlify, etc

This commit is contained in:
Alicia Sykes 2024-04-19 23:09:41 +01:00
parent 6a6fcd4be9
commit 7f011878b4

View File

@ -1,13 +1,26 @@
/** /**
* Global config for the main Vue app. ES7 not supported here. * Dashy is built using Vue (2). This is the main Vue and Webpack configuration
* See docs for all config options: https://cli.vuejs.org/config *
* User Configurable Options:
* - NODE_ENV: Sets the app mode (production, development, test).
* - BASE_URL: Root URL for the app deployment (defaults to '/').
* - INTEGRITY: Enables SRI, set to 'true' to activate.
* - USER_DATA_DIR: Sets an alternative dir for user data (defaults ./user-data).
* - IS_DOCKER: Indicates if running in a Docker container.
* - IS_SERVER: Indicates if running as a server (as opposed to static build).
*
* Documentation:
* - Vue CLI Config options: https://cli.vuejs.org/config
* - For Dashy docs, see the repo: https://github.com/lissy93/dashy
*
* Note: ES7 syntax is not supported in this configuration context.
* Licensed under the MIT License, (C) Alicia Sykes 2024 (see LICENSE for details).
*/ */
const fs = require('fs');
const path = require('path'); const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin'); const CopyWebpackPlugin = require('copy-webpack-plugin');
// Get app mode: production, development or test // Get app mode: production, development, or test
const mode = process.env.NODE_ENV || 'production'; const mode = process.env.NODE_ENV || 'production';
// Get current version // Get current version
@ -22,33 +35,22 @@ const publicPath = process.env.BASE_URL || '/';
// Should enable Subresource Integrity (SRI) on link and script tags // Should enable Subresource Integrity (SRI) on link and script tags
const integrity = process.env.INTEGRITY === 'true'; const integrity = process.env.INTEGRITY === 'true';
// When deploying as a static site, we must ensure user-data is copied over // If neither env vars are set, then it's a static build
class ConditionalCopyPlugin { const isServer = process.env.IS_DOCKER || process.env.IS_SERVER || false;
constructor(options) {
this.options = options;
}
apply(compiler) { // Use copy-webpack-plugin to copy user-data to dist IF not running as a server
compiler.hooks.beforeRun.tapAsync('ConditionalCopyPlugin', (_compilation, callback) => { const plugins = !isServer ? [
const targetDir = path.resolve(compiler.options.output.path, this.options.to); new CopyWebpackPlugin({
if (!fs.existsSync(targetDir)) { patterns: [
new CopyWebpackPlugin({ !isServer ? { from: './user-data', to: './' } : {},
patterns: [ ],
{ from: path.resolve(__dirname, this.options.from), to: targetDir }, }),
], ] : [];
}).apply(compiler);
}
callback();
});
}
}
// Webpack Config // Webpack Config
const configureWebpack = { const configureWebpack = {
mode, mode,
plugins: [ plugins,
new ConditionalCopyPlugin({ from: 'user-data', to: 'user-data' }),
],
module: { module: {
rules: [ rules: [
{ test: /.svg$/, loader: 'vue-svg-loader' }, { test: /.svg$/, loader: 'vue-svg-loader' },