|
| 1 | +const { env } = require('process'); |
| 2 | + |
| 3 | +module.exports = (() => { |
| 4 | + let settings = {}; |
| 5 | + |
| 6 | + /* eslint-disable global-require,import/no-extraneous-dependencies */ |
| 7 | + try { |
| 8 | + const yaml = require('js-yaml'); |
| 9 | + const { readFileSync, readdirSync, lstatSync } = require('fs'); |
| 10 | + const { resolve } = require('path'); |
| 11 | + |
| 12 | + const railsEnv = env.RAILS_ENV || 'development'; |
| 13 | + const config = yaml.safeLoad(readFileSync(resolve('config/vue.yml'), 'utf8'))[railsEnv]; |
| 14 | + const root = resolve(__dirname); |
| 15 | + const po = (config.public_output_path || 'vue_assets').replace(/(^\/+|\/+$)/g, ''); |
| 16 | + const { manifestOutput, alias = {}, devServer = {} } = config; |
| 17 | + if (devServer.contentBase) { |
| 18 | + devServer.contentBase = resolve(root, devServer.contentBase); |
| 19 | + } |
| 20 | + const entry = {}; |
| 21 | + const assetRoot = resolve(root, 'app/assets/vue/views'); |
| 22 | + const findAllJsFiles = (path) => { |
| 23 | + readdirSync(path).forEach((fn) => { |
| 24 | + const filename = resolve(path, fn); |
| 25 | + const stat = lstatSync(filename); |
| 26 | + if (stat.isDirectory()) { |
| 27 | + findAllJsFiles(filename); |
| 28 | + } else if (stat.isFile() && fn.endsWith('.js')) { |
| 29 | + entry[filename.slice(assetRoot.length + 1, -3)] = filename; |
| 30 | + } |
| 31 | + }); |
| 32 | + }; |
| 33 | + findAllJsFiles(assetRoot); |
| 34 | + |
| 35 | + settings = { |
| 36 | + ...config, |
| 37 | + env: railsEnv, |
| 38 | + root, |
| 39 | + outputDir: resolve(root, 'public', po), |
| 40 | + publicPath: `/${po}/`, |
| 41 | + alias: Object.keys(alias).reduce((obj, key) => ({ |
| 42 | + ...obj, |
| 43 | + [key]: resolve(root, alias[key]), |
| 44 | + }), {}), |
| 45 | + manifestOutput: resolve(root, manifestOutput), |
| 46 | + devServer, |
| 47 | + entry, |
| 48 | + }; |
| 49 | + } catch (e) { |
| 50 | + const { execSync } = require('child_process'); |
| 51 | + |
| 52 | + settings = JSON.parse(execSync('bundle exec rake vue:json_config', { |
| 53 | + cwd: __dirname, |
| 54 | + encoding: 'utf8', |
| 55 | + })); |
| 56 | + } |
| 57 | + /* eslint-enable global-require,import/no-extraneous-dependencies */ |
| 58 | + |
| 59 | + return { |
| 60 | + ...settings, |
| 61 | + isProd: settings.env === 'production', |
| 62 | + }; |
| 63 | +})(); |
0 commit comments