Skip to content

Commit

Permalink
Fix duplicate queries.json file generation
Browse files Browse the repository at this point in the history
Closes #145
  • Loading branch information
MaximStockmans authored May 29, 2024
1 parent 4b007de commit 165f3fb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
3 changes: 3 additions & 0 deletions bin/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ if (args.h || args.help || args._.length > 1) {
// Override the baseURL in the webpack config
webpackConfig.baseURL.replace = baseURL;

// Override the buildContext in the webpack config
webpackConfig.buildContext.dir = destinationPath;

for (const entry of webpackConfig) {
entry.mode = mode;
if (entry.output) {
Expand Down
25 changes: 25 additions & 0 deletions plugins/MoveFilePlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const fs = require('fs');

class MoveFilePlugin {

/**
* Create a new instance of the MoveFilePlugin.
* This plugin expects two functions as arguments, because the paths will be different at runtime.
* @param getSource method to retrieve the source file path
* @param getDestination method to retrieve the destination file path
*/
constructor(getSource, getDestination) {
this.getSource = getSource;
this.getDestination = getDestination;
}

apply(compiler) {
compiler.hooks.afterEmit.tap('MoveFilePlugin', (compilation) => {
if (fs.existsSync(this.getSource())) {
fs.renameSync(this.getSource(), this.getDestination());
}
});
}
}

module.exports = MoveFilePlugin;
19 changes: 15 additions & 4 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const path = require('path');
const webpack = require('webpack');
const MoveFilePlugin = require('./plugins/MoveFilePlugin');

// First check if we can load Comunica form cwd, if not, fallback to the default
let pathToComunica;
Expand All @@ -13,7 +14,7 @@ catch {
comunicaOverride = false;
}

// Make this an object so we can mutate it from the top level of the config
// Make this an object, so we can mutate it from the top level of the config
// and have the options propagated to the plugins
const baseURL = {
search: '<%= baseURL %>',
Expand All @@ -23,6 +24,12 @@ const baseURL = {
flags: 'g'
}

// Make this an object, so we can mutate it from the top level of the config
// and have the options propagated to the plugins
const buildContext = {
dir: 'build',
}

module.exports = [
{
entry: [
Expand All @@ -41,7 +48,6 @@ module.exports = [
path.join(__dirname, './images/sparql.png'),
path.join(__dirname, './favicon.ico'),
path.join(__dirname, './solid-client-id.jsonld'),
path.join(process.cwd(), './queries.json'),
],
output: {
filename: 'scripts/ldf-client-ui.min.js',
Expand All @@ -53,6 +59,11 @@ module.exports = [
jQuery: path.join(__dirname, '/deps/jquery-2.1.0.js'),
}),
new webpack.NormalModuleReplacementPlugin(/^comunica-packagejson$/, (process.platform === 'win32' ? '' : '!!json-loader!') + path.join(pathToComunica, '../../package.json')),
// Include the generated queries.json file by moving it from the current working directory (where it was generated) to the build path.
new MoveFilePlugin(
() => path.join(process.cwd(), 'queries.json'),
() => path.join(__dirname, `${buildContext.dir}/queries.json`)
),
],
module: {
rules: [
Expand Down Expand Up @@ -155,6 +166,6 @@ module.exports = [
},
];

// Export the baseURL object so we can mutated it
// in the generate script
// Export the baseURL and buildContext objects, so we can mutate it in the generate script
module.exports.baseURL = baseURL;
module.exports.buildContext = buildContext;

0 comments on commit 165f3fb

Please sign in to comment.