Skip to content

Commit

Permalink
feat: add Deno-compatible build output
Browse files Browse the repository at this point in the history
Resolves #8
  • Loading branch information
kripod committed May 19, 2020
1 parent d6d6f50 commit e54209a
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 27 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@commitlint/cli": "^8.3.5",
"@commitlint/config-conventional": "^8.3.4",
"@rollup/plugin-node-resolve": "^7.1.3",
"@rollup/plugin-replace": "^2.3.2",
"@typescript-eslint/eslint-plugin": "^2.33.0",
"@typescript-eslint/parser": "^2.33.0",
"@wessberg/rollup-plugin-ts": "^1.2.24",
Expand Down Expand Up @@ -72,7 +73,7 @@
},
"bundlesize": [
{
"path": "./packages/otion/dist-*/*.min.*js",
"path": "./packages/otion/dist-deno/*.min.*js",
"maxSize": "3 kB"
}
]
Expand Down
2 changes: 1 addition & 1 deletion packages/example-nextjs/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as React from "react";
import options from "../otion.config";

export default function MyApp({ Component, pageProps }: AppProps): JSX.Element {
if (typeof document !== "undefined") {
if (typeof window !== "undefined") {
setup(options);
hydrate();
}
Expand Down
2 changes: 1 addition & 1 deletion packages/otion/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ import { hydrate, setup } from "otion";
import { sharedOptions } from "./server";

// Make sure to rehydrate only in browser environments
if (typeof document !== "undefined") {
if (typeof window !== "undefined") {
setup(options);
hydrate();
}
Expand Down
5 changes: 3 additions & 2 deletions packages/otion/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { getMainEntry, getServerEntry } from "../../rollup.config.base";
import { getMainEntries, getServerEntry } from "../../rollup.config.base";
import pkg from "./package.json";

export default [
getMainEntry(pkg),
...getMainEntries(pkg, "development"),
...getMainEntries(pkg, "production"),
getServerEntry(pkg, { input: "./src/server.ts" }),
];
2 changes: 1 addition & 1 deletion packages/otion/src/env.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export const isBrowser = typeof document !== "undefined";
export const isBrowser = typeof window !== "undefined";
export const isDev = process.env.NODE_ENV !== "production";
64 changes: 44 additions & 20 deletions rollup.config.base.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import resolve from "@rollup/plugin-node-resolve";
import replace from "@rollup/plugin-replace";
import ts from "@wessberg/rollup-plugin-ts";
import * as path from "path";
import { terser } from "rollup-plugin-terser";
Expand All @@ -17,38 +18,61 @@ function getOutputs(pkg, subpath) {
];
}

export const commonPlugins = [
ts({
transpiler: "babel",
cwd: path.join(__dirname, "../.."),
tsconfig: path.join(__dirname, "tsconfig.json"),
}),
];
const tsPlugin = ts({
transpiler: "babel",
cwd: path.join(__dirname, "../.."),
tsconfig: path.join(__dirname, "tsconfig.json"),
});

export function getMainEntry(pkg) {
const minifiedOutputs = getOutputs(pkg, ".");
export function getMainEntries(pkg, env) {
const nodeOutputs = getOutputs(pkg, ".");
if (env === "development") {
nodeOutputs.forEach((output) => {
// eslint-disable-next-line no-param-reassign
output.file = output.file.replace(".min.", ".");
});
}

const unminifiedOutputs = minifiedOutputs.map(({ file, ...rest }) => ({
...rest,
file: file.replace(".min.", "."),
}));
const denoOutput = {
...nodeOutputs[0],
file: nodeOutputs[0].file.replace(
"dist-node/esm/bundle",
`dist-deno/bundle.${env === "development" ? "dev" : "prod"}`,
),
};

return {
const commonLastPlugins = [
resolve({ browser: true }),
terser({ include: /\.min\.[^.]+$/ }),
];

const nodeEntry = {
input: "./src/index.ts",
output: [...unminifiedOutputs, ...minifiedOutputs],
output: nodeOutputs,
plugins: [tsPlugin, ...commonLastPlugins],
external: [/^@babel\/runtime\//],
};

const denoEntry = {
...nodeEntry,
output: denoOutput,
plugins: [
...commonPlugins,
resolve({ browser: true }),
terser({ include: /\.min\.[^.]+$/ }),
tsPlugin,
replace({
"process.env.NODE_ENV": JSON.stringify(env),
"typeof window": "typeof document",
}),
...commonLastPlugins,
],
external: [/^@babel\/runtime\//],
};

return [nodeEntry, denoEntry];
}

export function getServerEntry(pkg, options) {
return {
...options,
output: getOutputs(pkg, "./server"),
plugins: commonPlugins,
plugins: tsPlugin,
};
}
10 changes: 9 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2200,6 +2200,14 @@
is-module "^1.0.0"
resolve "^1.14.2"

"@rollup/plugin-replace@^2.3.2":
version "2.3.2"
resolved "https://registry.yarnpkg.com/@rollup/plugin-replace/-/plugin-replace-2.3.2.tgz#da4e0939047f793c2eb5eedfd6c271232d0a033f"
integrity sha512-KEEL7V2tMNOsbAoNMKg91l1sNXBDoiP31GFlqXVOuV5691VQKzKBh91+OKKOG4uQWYqcFskcjFyh1d5YnZd0Zw==
dependencies:
"@rollup/pluginutils" "^3.0.8"
magic-string "^0.25.5"

"@rollup/pluginutils@^3.0.8":
version "3.0.10"
resolved "https://registry.yarnpkg.com/@rollup/pluginutils/-/pluginutils-3.0.10.tgz#a659b9025920378494cd8f8c59fbf9b3a50d5f12"
Expand Down Expand Up @@ -9745,7 +9753,7 @@ macos-release@^2.2.0:
resolved "https://registry.yarnpkg.com/macos-release/-/macos-release-2.3.0.tgz#eb1930b036c0800adebccd5f17bc4c12de8bb71f"
integrity sha512-OHhSbtcviqMPt7yfw5ef5aghS2jzFVKEFyCJndQt2YpSQ9qRVSEv2axSJI1paVThEu+FFGs584h/1YhxjVqajA==

magic-string@^0.25.3, magic-string@^0.25.7:
magic-string@^0.25.3, magic-string@^0.25.5, magic-string@^0.25.7:
version "0.25.7"
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.7.tgz#3f497d6fd34c669c6798dcb821f2ef31f5445051"
integrity sha512-4CrMT5DOHTDk4HYDlzmwu4FVCcIYI8gauveasrdCu2IKIFOJ3f0v/8MDGJCDL9oD2ppz/Av1b0Nj345H9M+XIA==
Expand Down

0 comments on commit e54209a

Please sign in to comment.