refactor: avoid config deduping via global (it has not been working since Marko 4 anyway and is probably bad)

This commit is contained in:
Dylan Piercey 2023-03-24 08:58:07 -07:00 committed by Dylan Piercey
parent 1fe71a5020
commit c41cda53c8

View File

@ -1,157 +1,149 @@
import fs from "fs";
import { getRootPackage } from "lasso-package-root";
let config;
const globalThis = typeof window === "undefined" ? global : window;
const MARKO_CONFIG_KEY = Symbol("Default Marko Compiler Config");
const config = {
// The default output mode for compiled templates
output: "html",
if (globalThis[MARKO_CONFIG_KEY]) {
config = globalThis[MARKO_CONFIG_KEY];
} else {
config = globalThis[MARKO_CONFIG_KEY] = {
// The default output mode for compiled templates
output: "html",
// Override the runtimeid used when calling `marko/components.init` in the `hydrate` output.
runtimeId: null,
// Override the runtimeid used when calling `marko/components.init` in the `hydrate` output.
runtimeId: null,
/**
* Remove all typescript types from the output.
* By default, the compiler will remove types from the output if the
* `output` option is not `source` or `migrate`.
*/
stripTypes: undefined,
/**
* Remove all typescript types from the output.
* By default, the compiler will remove types from the output if the
* `output` option is not `source` or `migrate`.
*/
stripTypes: undefined,
// Have Marko provide the final AST in the compile result.
ast: false,
// Have Marko provide the final AST in the compile result.
ast: false,
// Set the false to have Marko not generate the final code string, useful if just reading metadata or AST.
code: true,
// Set the false to have Marko not generate the final code string, useful if just reading metadata or AST.
code: true,
/**
* Whether the version should be written to the template as a comment e.g.
* // Compiled using marko@x.x.x - DO NOT EDIT
*/
writeVersionComment: true,
/**
* Whether the version should be written to the template as a comment e.g.
* // Compiled using marko@x.x.x - DO NOT EDIT
*/
writeVersionComment: true,
/**
* Whether unrecognized tags should be ignored or not. This flag will
* be enabled by default when compiling XML.
*/
ignoreUnrecognizedTags: false,
/**
* Whether unrecognized tags should be ignored or not. This flag will
* be enabled by default when compiling XML.
*/
ignoreUnrecognizedTags: false,
/**
* Whether source maps should be output with the compiled templates.
* When `true` a `map` property will be available on the compile result.
* When `"inline"` the sourcemap will be inlined as a comment in the output code.
* When `"both"` both of the above will be used.
*/
sourceMaps: false,
/**
* Whether source maps should be output with the compiled templates.
* When `true` a `map` property will be available on the compile result.
* When `"inline"` the sourcemap will be inlined as a comment in the output code.
* When `"both"` both of the above will be used.
*/
sourceMaps: false,
/**
* This option inlines all of the meta data in the template.
* You can also access this metadata via `compile(...).meta`.
* This API is sticking around for compatibility purposes.
*/
meta: false,
/**
* This option inlines all of the meta data in the template.
* You can also access this metadata via `compile(...).meta`.
* This API is sticking around for compatibility purposes.
*/
meta: false,
/**
* Allows configuring Marko to compile to different runtimes.
*/
translator: (() => {
const translatorReg = /^(@marko\/|marko-)translator-/;
let translator;
let pkg;
/**
* Allows configuring Marko to compile to different runtimes.
*/
translator: (() => {
const translatorReg = /^(@marko\/|marko-)translator-/;
let translator;
let pkg;
try {
pkg = getRootPackage(process.cwd());
// eslint-disable-next-line no-empty
} catch {}
try {
pkg = getRootPackage(process.cwd());
// eslint-disable-next-line no-empty
} catch {}
if (pkg) {
for (const name in pkg.dependencies) {
if (translatorReg.test(name)) {
if (translator && translator !== name) {
return;
}
translator = name;
if (pkg) {
for (const name in pkg.dependencies) {
if (translatorReg.test(name)) {
if (translator && translator !== name) {
return;
}
}
for (const name in pkg.peerDependencies) {
if (translatorReg.test(name)) {
if (translator && translator !== name) {
return;
}
translator = name;
}
}
for (const name in pkg.devDependencies) {
if (translatorReg.test(name)) {
if (translator && translator !== name) {
return;
}
translator = name;
}
translator = name;
}
}
return translator || "@marko/translator-default";
})(),
for (const name in pkg.peerDependencies) {
if (translatorReg.test(name)) {
if (translator && translator !== name) {
return;
}
/**
* Use a different file system object, eg webpacks CachedInputFileSystem or lasso-caching-fs
*/
fileSystem: fs,
/**
* By default Marko 5 outputs esm, you can optionally specify commonjs.
*
* Valid options: esm | cjs
*/
modules: "esm",
translator = name;
}
}
/**
* Enables production mode optimizations if true, or not if false.
* If left as undefined checks for env === "production".
*/
optimize: undefined,
for (const name in pkg.devDependencies) {
if (translatorReg.test(name)) {
if (translator && translator !== name) {
return;
}
/**
* This option should be set if `hydrate` output is specified.
* Maps a virtual dependency to a resolved file which can be implemented
* for specific bundlers.
*/
resolveVirtualDependency: null,
translator = name;
}
}
}
/**
* Compiling a Marko template may require other (used) Marko templates to compile.
* To prevent compiling templates more than once, most of the compilation is cached.
*
* The default cache strategy is to clear the cache on every macrotask.
* If the default cache is overwritten it is up to the user to determine when the
* cache is cleared.
*/
cache: new Map(),
return translator || "@marko/translator-default";
})(),
/**
* A regexp or function that receives an import path that matches file types known to be client side assets.
*/
hydrateIncludeImports:
/\.(css|less|s[ac]ss|styl|png|jpe?g|gif|svg|ico|webp|avif|mp4|webm|ogg|mp3|wav|flac|aac|woff2?|eot|ttf|otf)$/,
/**
* Use a different file system object, eg webpacks CachedInputFileSystem or lasso-caching-fs
*/
fileSystem: fs,
/**
* By default Marko 5 outputs esm, you can optionally specify commonjs.
*
* Valid options: esm | cjs
*/
modules: "esm",
/**
* Set to true in order to bring in the hot module replacement runtime.
*/
hot: false
};
/**
* Enables production mode optimizations if true, or not if false.
* If left as undefined checks for env === "production".
*/
optimize: undefined,
if (process.env.MARKO_CONFIG) {
Object.assign(config, JSON.parse(process.env.MARKO_CONFIG));
}
/**
* This option should be set if `hydrate` output is specified.
* Maps a virtual dependency to a resolved file which can be implemented
* for specific bundlers.
*/
resolveVirtualDependency: null,
/**
* Compiling a Marko template may require other (used) Marko templates to compile.
* To prevent compiling templates more than once, most of the compilation is cached.
*
* The default cache strategy is to clear the cache on every macrotask.
* If the default cache is overwritten it is up to the user to determine when the
* cache is cleared.
*/
cache: new Map(),
/**
* A regexp or function that receives an import path that matches file types known to be client side assets.
*/
hydrateIncludeImports:
/\.(css|less|s[ac]ss|styl|png|jpe?g|gif|svg|ico|webp|avif|mp4|webm|ogg|mp3|wav|flac|aac|woff2?|eot|ttf|otf)$/,
/**
* Set to true in order to bring in the hot module replacement runtime.
*/
hot: false
};
if (process.env.MARKO_CONFIG) {
Object.assign(config, JSON.parse(process.env.MARKO_CONFIG));
}
export default config;