Skip to content

Commit

Permalink
chore: add build for web
Browse files Browse the repository at this point in the history
  • Loading branch information
EqualMa committed Aug 14, 2020
1 parent 0b67f5e commit d92842c
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 62 deletions.
4 changes: 4 additions & 0 deletions .browserslistrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defaults
not IE 11
not IE_Mob 11
maintained node versions
7 changes: 1 addition & 6 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ module.exports = {
[
"@babel/env",
{
debug: true,
targets: {
node: "10",
},
useBuiltIns: "usage",
corejs: {
version: 3,
Expand All @@ -16,7 +12,6 @@ module.exports = {
],
],
plugins: [
//
"@babel/plugin-proposal-class-properties",
// "@babel/plugin-proposal-class-properties",
],
};
64 changes: 8 additions & 56 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,8 @@
import externals from "rollup-plugin-node-externals";
import typescript from "@rollup/plugin-typescript";
import babel from "@rollup/plugin-babel";
import generatePackageJson from "rollup-plugin-generate-package-json";
import { getPkgJsonBaseContents } from "./script-modules/gen-pkg";

import glob from "glob";
import path from "path";

const kvToObj = ([k, v]) => ({ [k]: v });

const entryFiles = Object.assign(
{},
...glob
.sync("src/*.ts")
.map((f) => [path.parse(f).name, f])
.map(kvToObj),
...glob
.sync("src/*/index.ts")
.map((f) => [path.basename(path.dirname(f)), f])
.map(kvToObj),
);

export default {
input: entryFiles,
output: [
{ dir: "dist", format: "cjs", sourcemap: true },
{ dir: "dist/es", format: "es", sourcemap: true },
],
plugins: [
// https://github.com/Septh/rollup-plugin-node-externals
externals({
builtins: true,
deps: true,
peerDeps: true,
optDeps: true,
devDeps: false,
}),
// https://github.com/rollup/plugins/tree/master/packages/typescript
typescript({
tsconfig: "tsconfig.prod.json",
sourceMap: true,
inlineSources: true,
}),
// https://github.com/rollup/plugins/tree/master/packages/babel
babel({
extensions: [".js", ".jsx", ".es6", ".es", ".mjs", ".ts", ".tsx"],
babelHelpers: "bundled",
}),
// https://github.com/vladshcherbin/rollup-plugin-generate-package-json
generatePackageJson({
baseContents: getPkgJsonBaseContents,
outputFolder: "dist",
}),
],
};
import nodeConfig from "./script-modules/rollup-node";
import bundleConfig from "./script-modules/rollup-bundle";

export default [
//
nodeConfig,
bundleConfig,
];
33 changes: 33 additions & 0 deletions script-modules/rollup-bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { compilePlugins } from "./rollup-common";
import { terser } from "rollup-plugin-terser";

const GLOBAL_NAMESPACE = "ChineseNumbering";

const FORMATS = [
//
"es",
"iife",
"umd",
];

function genOutputs(format) {
// https://rollupjs.org/guide/en/#outputname
const name = format === "es" ? undefined : GLOBAL_NAMESPACE;

return [true, false].map((min) => ({
format,
sourcemap: true,
file: `dist/bundle/${format}${min ? ".min" : ""}.js`,
name,
plugins: min ? [terser()] : undefined,
}));
}

export default {
input: "src/index.ts",
output: FORMATS.map(genOutputs).flat(1),
plugins: [
//
...compilePlugins(),
],
};
18 changes: 18 additions & 0 deletions script-modules/rollup-common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import typescript from "@rollup/plugin-typescript";
import babel from "@rollup/plugin-babel";

export function compilePlugins() {
return [
// https://github.com/rollup/plugins/tree/master/packages/typescript
typescript({
tsconfig: "tsconfig.prod.json",
sourceMap: true,
inlineSources: true,
}),
// https://github.com/rollup/plugins/tree/master/packages/babel
babel({
extensions: [".js", ".jsx", ".es6", ".es", ".mjs", ".ts", ".tsx"],
babelHelpers: "bundled",
}),
];
}
47 changes: 47 additions & 0 deletions script-modules/rollup-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import externals from "rollup-plugin-node-externals";
import generatePackageJson from "rollup-plugin-generate-package-json";
import { compilePlugins } from "./rollup-common";
import { getPkgJsonBaseContents } from "./gen-pkg";

import glob from "glob";
import path from "path";

const kvToObj = ([k, v]) => ({ [k]: v });

const entryFiles = Object.assign(
{},
...glob
.sync("src/*.ts")
.map((f) => [path.parse(f).name, f])
.map(kvToObj),
...glob
.sync("src/*/index.ts")
.map((f) => [path.basename(path.dirname(f)), f])
.map(kvToObj),
);

const chunkFileNames = "_chunks/[name]-[hash].js";

export default {
input: entryFiles,
output: [
{ dir: "dist", format: "cjs", sourcemap: true, chunkFileNames },
{ dir: "dist/es", format: "es", sourcemap: true, chunkFileNames },
],
plugins: [
// https://github.com/Septh/rollup-plugin-node-externals
externals({
builtins: true,
deps: true,
peerDeps: true,
optDeps: true,
devDeps: false,
}),
...compilePlugins(),
// https://github.com/vladshcherbin/rollup-plugin-generate-package-json
generatePackageJson({
baseContents: getPkgJsonBaseContents,
outputFolder: "dist",
}),
],
};

0 comments on commit d92842c

Please sign in to comment.