Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update config webpack for services #340

Merged
merged 2 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .bundlemonrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
{
"path": "app-mespapiers.<hash>.min.css"
},
{
"path": "services/**/*.js"
},
{
"path": "vendors/mespapiers.<hash>.js"
},
Expand Down
5 changes: 4 additions & 1 deletion app.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const merge = require('webpack-merge')
const config = [require('cozy-scripts/config/webpack.bundle.default.js')]
const config = [
require('cozy-scripts/config/webpack.bundle.default.js'),
require('./app.services')
]

const extraConfig = {
resolve: {
Expand Down
105 changes: 105 additions & 0 deletions app.services.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
const path = require('path')
const fs = require('fs')
const webpack = require('webpack')
const {
target,
eslintFix,
getFilename
} = require('cozy-scripts/config/webpack.vars')
const pathCS = require('cozy-scripts/utils/paths')

const SRC_DIR = path.resolve(__dirname, 'src')

const serviceDir = path.resolve(SRC_DIR, './targets/services/')
const servicesPaths = fs.existsSync(serviceDir)
? fs.readdirSync(serviceDir)
: []

const servicesEntries = {}
servicesPaths.forEach(file => {
if (!file.match(/^[^.]*.js$/)) return
const filename = file.match(/^([^.]*).js$/)[1]
servicesEntries[filename] = path.resolve(path.join(serviceDir, file))
})

const config = {
__mergeStrategy: {
smart: false,
strategy: {
plugins: 'replace',
output: 'replace',
entry: 'replace',
optimization: 'replace',
module: 'replace',
externals: 'replace'
}
},
entry: servicesEntries,
output: {
path: pathCS.appServicesBuild(),
filename: `${getFilename(false)}.js`
},
target: 'node',
optimization: {
minimize: false
},
devtool: false,
externals: [], // reset externals property
module: {
rules: [
{
enforce: 'pre',
test: /\.js$/,
loader: require.resolve('cozy-scripts/node_modules/eslint-loader'),
exclude: /node_modules/,
options: {
extends: ['cozy-app'],
fix: eslintFix,
emitWarning: true
}
},
{
test: /\.js$/,
exclude: /(node_modules|cozy-(bar|client-js))/,
loader: require.resolve('cozy-scripts/node_modules/babel-loader'),
options: {
cacheDirectory: 'cozy-scripts/node_modules/.cache/babel-loader/node',
babelrc: false,
presets: [['cozy-app', { node: true, react: false }]]
}
}
],
// Dynamic requires produce warnings in webpack. Some of our dependencies
// use them for features we do not use, so we can disable them.
// More information : https://gitlab.cozycloud.cc/labs/cozy-bank/merge_requests/197#note_4018
exprContextRegExp: /$^/,
exprContextCritical: false
},
resolve: {
alias: {
// We are building with target: node as webpack options. This causes webpack
// to consider the "module" entrypoint from node-fetch. This does not work properly
// as require('node-fetch') returns a module object (with the default property).
// Here, we force the resolution to take the commonJS file.
// TODO See if it is necessary to integrate in cozy-scripts
'node-fetch': 'node-fetch/lib/index.js',
// Unminified Handlebars uses `require.extensions` and this causes
// warnings on Webpack. We should think of a way to precompile
// our Handlebars template. At the moment it is not possible
// since we pass helpers at runtime.
handlebars: 'handlebars/dist/handlebars.min.js'
}
},
plugins: [
new webpack.DefinePlugin({
__TARGET__: JSON.stringify('services')
})
]
}

/* We don't build services if no services and if on mobile build */
const addServicesConfig =
target === 'browser' && Object.keys(servicesEntries).length

// only for browser target (services are usable only on cozy-stack)
module.exports = addServicesConfig ? { multiple: { services: config } } : {}