functionalized webpack configs to provide environment variable access

This commit is contained in:
Mason
2019-10-18 12:36:40 -07:00
parent 8b7ff27529
commit 20c47a1027
10 changed files with 333 additions and 293 deletions
+3
View File
@@ -0,0 +1,3 @@
export default {
name: "development configs"
}
+3
View File
@@ -0,0 +1,3 @@
export default {
name: "production configs"
}
+3
View File
@@ -0,0 +1,3 @@
export default {
name: "unit testing configs"
}
+4
View File
@@ -11,3 +11,7 @@ export { standardInit } from "./standardInit";
export { initializations$, addInitialization, prependInitialization, clearInitQueue } from "./initQueue";
export { config$, set as setConfig, get as getConfig, getAppRoot } from "./loadConfig";
export { getRootFromIndexLink } from "./getRootFromIndexLink";
// Client-side configuration variables (based on environment)
import config from "config";
console.log("Configs:", config.name, config);
+25 -17
View File
@@ -2,24 +2,32 @@
* Base karma config, assumes chrome headless and webpack
*/
const webpackConfig = require("./webpack.config.unittest");
const webpackConfigFunc = require("./webpack.config.unittest");
module.exports = {
basePath: "../galaxy/scripts",
browsers: ["ChromeHeadlessNoSandbox"],
client: {
captureConsole: true
},
failOnEmptyTestSuite: false,
singleRun: true,
webpack: webpackConfig,
webpackMiddleware: {
noInfo: true
},
customLaunchers: {
ChromeHeadlessNoSandbox: {
base: "ChromeHeadless",
flags: ["--no-sandbox"]
module.exports = config => {
// Karma does not execute the webpack config function by default
const webpackConfig = webpackConfigFunc({}, {
mode: "testing"
});
return {
basePath: "../galaxy/scripts",
browsers: ["ChromeHeadlessNoSandbox"],
client: {
captureConsole: true
},
failOnEmptyTestSuite: false,
singleRun: true,
webpack: webpackConfig,
webpackMiddleware: {
noInfo: true
},
customLaunchers: {
ChromeHeadlessNoSandbox: {
base: "ChromeHeadless",
flags: ["--no-sandbox"]
}
}
}
}
+7 -6
View File
@@ -1,10 +1,10 @@
/**
* Runs mocha tests
*
*
* Individual files can be run by passing in a comma-delimited list
* of globs for the karma config like this:
*
* npm run test-watch watch-only="Tags.test.js,something.js,doodads.js"
*
* npm run test-watch watch-only="Tags.test.js,something.js,doodads.js"
*/
const baseKarmaConfig = require("./karma.config.base");
@@ -24,7 +24,7 @@ function getTestFiles() {
return patterns.map(pattern => ({ pattern, watched: true}));
}
// command line arg "watch-only" can be a list of file globs
// command line arg "watch-only" can be a list of file globs
// for karma to watch
function getUserTestGlobs() {
let userGlobs = process.argv.find(s => s.startsWith("watch-only"));
@@ -48,15 +48,16 @@ function checkGlobPrefix(glob) {
return glob.startsWith("**/") ? glob : `**/${glob}`;
}
module.exports = function (config) {
const baseConfig = baseKarmaConfig(config);
let files = [
"../../node_modules/@babel/polyfill/dist/polyfill.js",
...getTestFiles()
];
let settings = Object.assign({}, baseKarmaConfig, {
let settings = Object.assign({}, baseConfig, {
files,
preprocessors: {
"**/*.js": ["webpack"]
+9 -6
View File
@@ -1,6 +1,6 @@
/**
* Qunit Tests
*
*
* These legacy QUnit tests are interdependent and only execute as a single
* package. A flaw in the previous node environment parameter-checking in the
* old karma configs gave the illusion that they ran individually, but they do
@@ -12,6 +12,7 @@
const baseKarmaConfig = require("./karma.config.base");
const testFiles = [
"../../node_modules/@babel/polyfill/dist/polyfill.js",
"qunit/test.js"
];
@@ -19,14 +20,16 @@ const assets = [
"qunit/assets/*.css"
];
let preprocessors = testFiles.reduce((result, path) => {
result[path] = ["webpack"];
return result;
}, {});
module.exports = function (config) {
let preprocessors = testFiles.reduce((result, path) => {
result[path] = ["webpack"];
return result;
}, {});
const baseConfig = baseKarmaConfig(config);
let settings = Object.assign({}, baseKarmaConfig, {
let settings = Object.assign({}, baseConfig, {
files: testFiles.concat(assets),
preprocessors: preprocessors,
frameworks: ["polyfill", "qunit"],
+38 -32
View File
@@ -4,41 +4,47 @@
* TODO: Replace these tests with like... actual unit tests.
*/
let wpConfig = require("../webpack.config");
wpConfig.mode = "production";
wpConfig.entry = () => ({});
const baseConfig = require("../webpack.config");
const switchPlugin = (reMatcher, replacement) => {
module.exports = (env, argv) => {
let scrubLoader = (obj) =>
(typeof obj == "string" && obj.match(reMatcher))
? Object.assign({}, replacement)
: obj
// load base config
let wpConfig = baseConfig(env, argv);
wpConfig.mode = "production";
wpConfig.entry = () => ({});
return (rule) => {
if (rule.use && rule.use.length) {
rule.use = rule.use.map(scrubLoader);
const switchPlugin = (reMatcher, replacement) => {
let scrubLoader = (obj) =>
(typeof obj == "string" && obj.match(reMatcher))
? Object.assign({}, replacement)
: obj
return (rule) => {
if (rule.use && rule.use.length) {
rule.use = rule.use.map(scrubLoader);
}
return rule;
}
return rule;
}
// Remove MiniCssExtractPlugin loader references
// replace mini-css-extract plugin with basic style loader
// Honestly I'm not clear on why we process css at all for unit tests
const scrubLoaderRules = (module) => {
let rePluginMatch = /mini-css-extract-plugin/;
let styleLoader = { loader: "style-loader" };
let processor = switchPlugin(rePluginMatch, styleLoader);
module.rules = module.rules.map(processor);
}
// Remove mini-css-extract loader references
scrubLoaderRules(wpConfig.module);
// remove MiniCSSExtract Plugin
wpConfig.plugins = wpConfig.plugins.filter(p => {
return p.constructor.name != "MiniCssExtractPlugin";
});
return wpConfig;
}
// Remove MiniCssExtractPlugin loader references
// replace mini-css-extract plugin with basic style loader
// Honestly I'm not clear on why we process css at all for unit tests
const scrubLoaderRules = (module) => {
let rePluginMatch = /mini-css-extract-plugin/;
let styleLoader = { loader: "style-loader" };
let processor = switchPlugin(rePluginMatch, styleLoader);
module.rules = module.rules.map(processor);
}
// Remove mini-css-extract loader references
scrubLoaderRules(wpConfig.module);
// remove MiniCSSExtract Plugin
wpConfig.plugins = wpConfig.plugins.filter(p => {
return p.constructor.name != "MiniCssExtractPlugin";
});
module.exports = wpConfig;
+36 -35
View File
@@ -4,44 +4,45 @@
*/
const merge = require("webpack-merge");
const wpConfig = require("../webpack.config");
const baseConfig = require("../webpack.config");
wpConfig.mode = "development";
wpConfig.entry = () => ({});
module.exports = (env, argv) => {
const wpConfig = baseConfig(env, argv);
wpConfig.mode = "development";
wpConfig.entry = () => ({});
// Don't need any assets for unit testing
let ignoreAssetLoaders = {
rules:[
{
test: /\.(png|jpg|jpeg|gif|svg|woff|woff2|ttf|eot)(\?.*$|$)/,
loader: "ignore-loader"
},
{
test: /\.css$/,
loader: "ignore-loader"
},
{
test: /\.scss$/,
loader: "ignore-loader"
}
]
};
wpConfig.module = merge.smart(wpConfig.module, ignoreAssetLoaders);
// Don't need any assets for unit testing
// Using babel-plugin-rewire to handle dependency mocking since webpack 4
// exports immutable bindings for ES modules but we still need a way to
// overwrite dependencies during unit-testing.
let ignoreAssetLoaders = {
rules:[
{
test: /\.(png|jpg|jpeg|gif|svg|woff|woff2|ttf|eot)(\?.*$|$)/,
loader: "ignore-loader"
},
{
test: /\.css$/,
loader: "ignore-loader"
},
{
test: /\.scss$/,
loader: "ignore-loader"
wpConfig.module.rules = wpConfig.module.rules.map(rule => {
if (rule.loader == "babel-loader") {
rule.options.plugins.push("rewire");
}
]
};
return rule;
});
wpConfig.module = merge.smart(wpConfig.module, ignoreAssetLoaders);
// Using babel-plugin-rewire to handle dependency mocking since webpack 4
// exports immutable bindings for ES modules but we still need a way to
// overwrite dependencies during unit-testing.
wpConfig.module.rules = wpConfig.module.rules.map(rule => {
if (rule.loader == "babel-loader") {
rule.options.plugins.push("rewire");
}
return rule;
});
module.exports = wpConfig;
return wpConfig;
}
+205 -197
View File
@@ -11,211 +11,219 @@ const libsBase = path.join(scriptsBase, "libs");
const styleBase = path.join(__dirname, "galaxy/style");
const imageBase = path.join(__dirname, "../static/style");
let buildconfig = {
entry: {
login: ["polyfills", "bundleEntries", "entry/login"],
analysis: ["polyfills", "bundleEntries", "entry/analysis"],
admin: ["polyfills", "bundleEntries", "entry/admin"],
generic: ["polyfills", "bundleEntries", "entry/generic"]
},
output: {
path: path.join(__dirname, "../", "lib/galaxy/web/framework/static/scripts/bundled"),
publicPath: "/static/scripts/bundled/",
filename: "[name].bundled.js",
chunkFilename: "[name].chunk.js"
},
resolve: {
extensions: ["*", ".js", ".json", ".vue", ".scss"],
modules: [scriptsBase, "node_modules", styleBase, imageBase],
alias: {
jquery$: `${libsBase}/jquery.custom.js`,
jqueryVendor$: `${libsBase}/jquery/jquery.js`,
storemodern$: "store/dist/store.modern.js"
}
},
optimization: {
splitChunks: {
cacheGroups: {
styles: {
name: "base",
chunks: "all",
test: m => m.constructor.name == "CssModule",
priority: -5
},
libs: {
name: "libs",
test: /node_modules[\\/](?!(handsontable|pikaday|moment)[\\/])|galaxy\/scripts\/libs/,
chunks: "all",
priority: -10
module.exports = (env = {}, argv = {}) => {
// environment name based on -d, -p, webpack flag
const targetEnv = argv.mode || "development";
let buildconfig = {
entry: {
login: ["polyfills", "bundleEntries", "entry/login"],
analysis: ["polyfills", "bundleEntries", "entry/analysis"],
admin: ["polyfills", "bundleEntries", "entry/admin"],
generic: ["polyfills", "bundleEntries", "entry/generic"]
},
output: {
path: path.join(__dirname, "../", "lib/galaxy/web/framework/static/scripts/bundled"),
publicPath: "/static/scripts/bundled/",
filename: "[name].bundled.js",
chunkFilename: "[name].chunk.js"
},
resolve: {
extensions: ["*", ".js", ".json", ".vue", ".scss"],
modules: [scriptsBase, "node_modules", styleBase, imageBase],
alias: {
jquery$: `${libsBase}/jquery.custom.js`,
jqueryVendor$: `${libsBase}/jquery/jquery.js`,
storemodern$: "store/dist/store.modern.js",
// client-side application config
config$: path.join(__dirname, "galaxy", "config", targetEnv) + ".js"
}
},
optimization: {
splitChunks: {
cacheGroups: {
styles: {
name: "base",
chunks: "all",
test: m => m.constructor.name == "CssModule",
priority: -5
},
libs: {
name: "libs",
test: /node_modules[\\/](?!(handsontable|pikaday|moment)[\\/])|galaxy\/scripts\/libs/,
chunks: "all",
priority: -10
}
}
}
}
},
module: {
rules: [
{
test: /\.vue$/,
loader: "vue-loader"
},
{
test: /\.js$/,
// Pretty sure we don't want anything except node_modules here
exclude: [
/(node_modules\/(?!(handsontable)\/)|bower_components)/,
libsBase
],
loader: "babel-loader",
options: {
cacheDirectory: true,
cacheCompression: false,
presets: [
["@babel/preset-env", { modules: false }]
},
module: {
rules: [
{
test: /\.vue$/,
loader: "vue-loader"
},
{
test: /\.js$/,
// Pretty sure we don't want anything except node_modules here
exclude: [
/(node_modules\/(?!(handsontable)\/)|bower_components)/,
libsBase
],
plugins: [
"transform-vue-template",
"@babel/plugin-syntax-dynamic-import"
],
ignore: [
"i18n.js",
"utils/localization.js",
"nls/*"
]
}
},
{
test: `${libsBase}/jquery.custom.js`,
use: [
{
loader: "expose-loader",
options: "jQuery"
},
{
loader: "expose-loader",
options: "$"
}
]
},
{
test: require.resolve("underscore"),
use: [
{
loader: "expose-loader",
options: "_"
},
{
loader: "expose-loader",
options: "underscore"
}
]
},
{
test: /\.(png|jpg|jpeg|gif|svg|woff|woff2|ttf|eot)(\?.*$|$)/,
use: {
loader: "file-loader",
loader: "babel-loader",
options: {
outputPath: "assets",
publicPath: "../scripts/bundled/assets/"
cacheDirectory: true,
cacheCompression: false,
presets: [
["@babel/preset-env", { modules: false }]
],
plugins: [
"transform-vue-template",
"@babel/plugin-syntax-dynamic-import"
],
ignore: [
"i18n.js",
"utils/localization.js",
"nls/*"
]
}
}
},
// Alternative to setting window.bundleEntries
// Just import "bundleEntries" in any endpoint that needs
// access to these globals, or even-better, make
// more endpoints and skip the global altogether
{
test: `${scriptsBase}/bundleEntries`,
use: [
{
loader: "expose-loader",
options: "bundleEntries"
}
]
},
{
test: `${scriptsBase}/onload/loadConfig.js`,
use: [
{
loader: "expose-loader",
options: "config"
}
]
},
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: { sourceMap: true }
}
]
},
{
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: { sourceMap: true }
},
{
loader: "sass-loader",
options: {
sourceMap: true,
includePaths: [
"galaxy/style/scss",
path.resolve(__dirname, './node_modules')
]
},
{
test: `${libsBase}/jquery.custom.js`,
use: [
{
loader: "expose-loader",
options: "jQuery"
},
{
loader: "expose-loader",
options: "$"
}
]
},
{
test: require.resolve("underscore"),
use: [
{
loader: "expose-loader",
options: "_"
},
{
loader: "expose-loader",
options: "underscore"
}
]
},
{
test: /\.(png|jpg|jpeg|gif|svg|woff|woff2|ttf|eot)(\?.*$|$)/,
use: {
loader: "file-loader",
options: {
outputPath: "assets",
publicPath: "../scripts/bundled/assets/"
}
}
]
},
{
test: /\.(txt|tmpl)$/,
loader: "raw-loader"
}
]
},
node: {
setImmediate: false
},
resolveLoader: {
alias: {
// since we support both requirejs i18n and non-requirejs and both use a similar syntax,
// use an alias so we can just use one file
i18n: "amdi18n-loader"
}
},
plugins: [
// this plugin allows using the following keys/globals in scripts (w/o req'ing them first)
// and webpack will automagically require them in the bundle for you
new webpack.ProvidePlugin({
$: `${libsBase}/jquery.custom.js`,
jQuery: `${libsBase}/jquery.custom.js`,
_: "underscore",
Backbone: "backbone",
Galaxy: ["app", "monitor"]
}),
new VueLoaderPlugin(),
new MiniCssExtractPlugin({
filename: "[name].css",
sourceMap: true
}),
// https://github.com/webpack-contrib/mini-css-extract-plugin/issues/141
new OptimizeCssAssetsPlugin({
cssProcessorOptions: {
map: {
inline: false,
annotation: true
},
// Alternative to setting window.bundleEntries
// Just import "bundleEntries" in any endpoint that needs
// access to these globals, or even-better, make
// more endpoints and skip the global altogether
{
test: `${scriptsBase}/bundleEntries`,
use: [
{
loader: "expose-loader",
options: "bundleEntries"
}
]
},
{
test: `${scriptsBase}/onload/loadConfig.js`,
use: [
{
loader: "expose-loader",
options: "config"
}
]
},
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: { sourceMap: true }
}
]
},
{
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: { sourceMap: true }
},
{
loader: "sass-loader",
options: {
sourceMap: true,
includePaths: [
"galaxy/style/scss",
path.resolve(__dirname, './node_modules')
]
}
}
]
},
{
test: /\.(txt|tmpl)$/,
loader: "raw-loader"
}
]
},
node: {
setImmediate: false
},
resolveLoader: {
alias: {
// since we support both requirejs i18n and non-requirejs and both use a similar syntax,
// use an alias so we can just use one file
i18n: "amdi18n-loader"
}
}),
new DuplicatePackageCheckerPlugin()
]
};
},
plugins: [
// this plugin allows using the following keys/globals in scripts (w/o req'ing them first)
// and webpack will automagically require them in the bundle for you
new webpack.ProvidePlugin({
$: `${libsBase}/jquery.custom.js`,
jQuery: `${libsBase}/jquery.custom.js`,
_: "underscore",
Backbone: "backbone",
Galaxy: ["app", "monitor"]
}),
new VueLoaderPlugin(),
new MiniCssExtractPlugin({
filename: "[name].css",
sourceMap: true
}),
// https://github.com/webpack-contrib/mini-css-extract-plugin/issues/141
new OptimizeCssAssetsPlugin({
cssProcessorOptions: {
map: {
inline: false,
annotation: true
}
}
}),
new DuplicatePackageCheckerPlugin()
]
};
if (process.env.GXY_BUILD_SOURCEMAPS || process.env.NODE_ENV == "development") {
buildconfig.devtool = "source-map";
if (process.env.GXY_BUILD_SOURCEMAPS || process.env.NODE_ENV == "development") {
buildconfig.devtool = "source-map";
}
return buildconfig;
}
module.exports = buildconfig;