Skip to content

Instantly share code, notes, and snippets.

@Terkea
Created October 10, 2022 11:07
Show Gist options
  • Save Terkea/3aa76355906fb125aafdf844b9bcdea8 to your computer and use it in GitHub Desktop.
Save Terkea/3aa76355906fb125aafdf844b9bcdea8 to your computer and use it in GitHub Desktop.
esbuild
const envPlugin = {
name: "env",
setup(build) {
// Intercept import paths called "env" so esbuild doesn't attempt
// to map them to a file system location. Tag them with the "env-ns"
// namespace to reserve them for this plugin.
build.onResolve({ filter: /^env$/ }, (args) => ({
path: args.path,
namespace: "env-ns",
}));
// Load paths tagged with the "env-ns" namespace and behave as if
// they point to a JSON file containing the environment variables.
build.onLoad({ filter: /.*/, namespace: "env-ns" }, () => ({
contents: JSON.stringify(process.env),
loader: "json",
}));
},
};
const copyStaticFiles = (options = {}) => ({
name: "copy-static-files",
setup(build) {
let src = options.src || "./static";
let dest = options.dest || "../public";
build.onEnd(() =>
fs.cpSync(src, dest, {
dereference: options.dereference || true,
errorOnExist: options.errorOnExist || false,
filter: options.filter || filter,
force: options.force || true,
preserveTimestamps: options.preserveTimestamps || true,
recursive: options.recursive || true,
})
);
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment