diff --git a/config/webpack/environment.js b/config/webpack/environment.js index 2409460e2..d3a91f5fc 100644 --- a/config/webpack/environment.js +++ b/config/webpack/environment.js @@ -1,9 +1,39 @@ const { environment } = require('@rails/webpacker') const { VueLoaderPlugin } = require('vue-loader') const vue = require('./loaders/vue') +const { execSync } = require('child_process'); +const { basename, resolve } = require('path'); +const { readdirSync } = require('fs'); environment.loaders.delete('nodeModules') environment.plugins.prepend('VueLoaderPlugin', new VueLoaderPlugin()) environment.loaders.prepend('vue', vue) -module.exports = environment +// Engine pack loading based on https://github.com/rails/webpacker/issues/348#issuecomment-635480949 +// Get paths to all engines' folders +const enginePaths = execSync('ls -d $PWD/addons/*').toString().split('\n').filter((p) => !!p); +enginePaths.forEach((path) => { + const packsFolderPath = `${path}/app/javascript/packs`; + + let entryFiles; + try { + entryFiles = readdirSync(packsFolderPath); + console.log(`Found packs in ${path}`); + } catch { + console.log(`No packs in ${path}`); + return; + } + + entryFiles.forEach((file) => { + // File name without .js + const name = basename(file, '.js'); + const entryPath = `${packsFolderPath}/${file}`; + + environment.entry.set(name, entryPath); + }); + + // Otherwise babel won't transpile the file + environment.loaders.get('babel').include.push(`${path}/app/javascript`); +}); + +module.exports = environment; diff --git a/engines_yarn_install.js b/engines_yarn_install.js new file mode 100644 index 000000000..649321b56 --- /dev/null +++ b/engines_yarn_install.js @@ -0,0 +1,11 @@ +#!/usr/bin/env node + +const { spawn, execSync } = require('child_process'); +const enginePaths = execSync('ls -d $PWD/addons/*').toString().split('\n').filter((p) => !!p); +enginePaths.forEach(enginePath => { + spawn('yarn', ['install'], { + env: process.env, + cwd: enginePath, + stdio: 'inherit' + }); +}); diff --git a/package.json b/package.json index bce6e8615..313c595dc 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "flow": "flow", "lint": "eslint ." }, + "postinstall": "./engines_yarn_install.js", "devDependencies": { "@babel/core": "^7.0.0", "@babel/plugin-transform-react-jsx-source": "^7.0.0",