Skip to content

Commit

Permalink
Replace packer with rollup
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk authored and piscisaureus committed Jul 22, 2018
1 parent dff5c16 commit 845f721
Show file tree
Hide file tree
Showing 9 changed files with 615 additions and 3,451 deletions.
47 changes: 12 additions & 35 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -200,24 +200,27 @@ run_node("bundle") {
"js/runtime.ts",
"js/types.ts",
"js/util.ts",
"package.json", # The `browserslist` field controls Babel behavior.
"rollup.config.js",
"tsconfig.json",
]
outputs = [
out_dir + "main.js",
out_dir + "main.map",
out_dir + "main.js.map",
]
deps = [
":flatbufferjs",
":run_tsc",
]
args = [
"./node_modules/parcel-bundler/bin/cli.js",
"build",
"--log-level=1", # Only disply errors.
"--no-minify",
"--out-dir",
rebase_path(out_dir, root_build_dir),
"./node_modules/rollup/bin/rollup",
"-c",
rebase_path("rollup.config.js", root_build_dir),
"-i",
rebase_path("js/main.ts", root_build_dir),
"-o",
rebase_path(out_dir + "main.js", root_build_dir),
"--silent",
"--environment",
"BASEPATH:" + rebase_path(".", root_build_dir),
]
}

Expand All @@ -236,32 +239,6 @@ source_set("libdeno_nosnapshot") {
defines = [ "BUNDLE_LOCATION=\"$bundle_location\"" ]
}

# Due to bugs in Parcel we must run TSC independently in order to catch errors.
# https://github.com/parcel-bundler/parcel/issues/954
run_node("run_tsc") {
main = "js/main.ts"
tsconfig = "tsconfig.json"
out_dir = "$target_gen_dir/tsc_dist/"
sources = [
"js/msg_generated.ts",
main,
tsconfig,
]
outputs = [
out_dir + "/main.js",
]
deps = [
":flatbufferjs",
]
args = [
"./node_modules/typescript/bin/tsc",
"--project",
rebase_path(tsconfig, root_build_dir),
"--outDir",
rebase_path(out_dir, root_build_dir),
]
}

# Generates flatbuffer TypeScript code.
# TODO(ry) Ideally flatc output files should be written into
# target_gen_dir, but its difficult to get this working in a way that the
Expand Down
82 changes: 54 additions & 28 deletions js/assets.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,77 @@
// Copyright 2018 Ryan Dahl <[email protected]>
// All rights reserved. MIT License.

// This file is formatted as it is because we are using the fact that Parcel
// statically evaluates fs.readFileSync.
import { readFileSync } from "fs";
// tslint:disable-next-line:no-reference
/// <reference path="plugins.d.ts" />

// There is a rollup plugin that will inline any module ending with `!string`
// tslint:disable:max-line-length
import denoDts from "/js/deno.d.ts!string";
import libDts from "/third_party/node_modules/typescript/lib/lib.d.ts!string";
import libDomIterableDts from "/third_party/node_modules/typescript/lib/lib.dom.iterable.d.ts!string";
import libEs2015CollectionDts from "/third_party/node_modules/typescript/lib/lib.es2015.collection.d.ts!string";
import libEs2015CoreDts from "/third_party/node_modules/typescript/lib/lib.es2015.core.d.ts!string";
import libEs2015GeneratorDts from "/third_party/node_modules/typescript/lib/lib.es2015.generator.d.ts!string";
import libEs2015IterableDts from "/third_party/node_modules/typescript/lib/lib.es2015.iterable.d.ts!string";
import libEs2015PromiseDts from "/third_party/node_modules/typescript/lib/lib.es2015.promise.d.ts!string";
import libEs2015ProxyDts from "/third_party/node_modules/typescript/lib/lib.es2015.proxy.d.ts!string";
import libEs2015ReflectDts from "/third_party/node_modules/typescript/lib/lib.es2015.reflect.d.ts!string";
import libEs2015SymbolDts from "/third_party/node_modules/typescript/lib/lib.es2015.symbol.d.ts!string";
import libEs2015SymbolWellknownDts from "/third_party/node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts!string";
import libEs2016ArrayIncludeDts from "/third_party/node_modules/typescript/lib/lib.es2016.array.include.d.ts!string";
import libEs2017IntlDts from "/third_party/node_modules/typescript/lib/lib.es2017.intl.d.ts!string";
import libEs2017ObjectDts from "/third_party/node_modules/typescript/lib/lib.es2017.object.d.ts!string";
import libEs2017SharedmemoryDts from "/third_party/node_modules/typescript/lib/lib.es2017.sharedmemory.d.ts!string";
import libEs2017StringDts from "/third_party/node_modules/typescript/lib/lib.es2017.string.d.ts!string";
import libEs2017TypedarraysDts from "/third_party/node_modules/typescript/lib/lib.es2017.typedarrays.d.ts!string";
import libEs2018Dts from "/third_party/node_modules/typescript/lib/lib.es2018.d.ts!string";
import libEs2018PromiseDts from "/third_party/node_modules/typescript/lib/lib.es2018.promise.d.ts!string";
import libEs2018RegexpDts from "/third_party/node_modules/typescript/lib/lib.es2018.regexp.d.ts!string";
import libEsnextArrayDts from "/third_party/node_modules/typescript/lib/lib.esnext.array.d.ts!string";
import libEsnextAsynciterablesDts from "/third_party/node_modules/typescript/lib/lib.esnext.asynciterable.d.ts!string";
import libEsnextDts from "/third_party/node_modules/typescript/lib/lib.esnext.d.ts!string";
import typescriptDts from "/third_party/node_modules/typescript/lib/typescript.d.ts!string";

// prettier-ignore
export const assetSourceCode: { [key: string]: string } = {
"deno.d.ts": readFileSync(__dirname + "/deno.d.ts", "utf8"),
"lib.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.d.ts", "utf8"),
"deno.d.ts": denoDts,
"lib.d.ts": libDts,
//"lib.dom.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.dom.d.ts", "utf8"),
"lib.dom.iterable.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.dom.iterable.d.ts", "utf8"),
"lib.es2015.collection.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.es2015.collection.d.ts", "utf8"),
"lib.es2015.core.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.es2015.core.d.ts", "utf8"),
"lib.dom.iterable.d.ts": libDomIterableDts,
"lib.es2015.collection.d.ts": libEs2015CollectionDts,
"lib.es2015.core.d.ts": libEs2015CoreDts,
//"lib.es2015.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.es2015.d.ts", "utf8"),
"lib.es2015.generator.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.es2015.generator.d.ts", "utf8"),
"lib.es2015.iterable.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.es2015.iterable.d.ts", "utf8"),
"lib.es2015.promise.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.es2015.promise.d.ts", "utf8"),
"lib.es2015.proxy.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.es2015.proxy.d.ts", "utf8"),
"lib.es2015.reflect.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.es2015.reflect.d.ts", "utf8"),
"lib.es2015.symbol.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.es2015.symbol.d.ts", "utf8"),
"lib.es2015.symbol.wellknown.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts", "utf8"),
"lib.es2016.array.include.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.es2016.array.include.d.ts", "utf8"),
"lib.es2015.generator.d.ts": libEs2015GeneratorDts,
"lib.es2015.iterable.d.ts": libEs2015IterableDts,
"lib.es2015.promise.d.ts": libEs2015PromiseDts,
"lib.es2015.proxy.d.ts": libEs2015ProxyDts,
"lib.es2015.reflect.d.ts": libEs2015ReflectDts,
"lib.es2015.symbol.d.ts": libEs2015SymbolDts,
"lib.es2015.symbol.wellknown.d.ts": libEs2015SymbolWellknownDts,
"lib.es2016.array.include.d.ts": libEs2016ArrayIncludeDts,
//"lib.es2016.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.es2016.d.ts", "utf8"),
//"lib.es2016.full.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.es2016.full.d.ts", "utf8"),
//"lib.es2017.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.es2017.d.ts", "utf8"),
//"lib.es2017.full.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.es2017.full.d.ts", "utf8"),
"lib.es2017.intl.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.es2017.intl.d.ts", "utf8"),
"lib.es2017.object.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.es2017.object.d.ts", "utf8"),
"lib.es2017.sharedmemory.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.es2017.sharedmemory.d.ts", "utf8"),
"lib.es2017.string.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.es2017.string.d.ts", "utf8"),
"lib.es2017.typedarrays.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.es2017.typedarrays.d.ts", "utf8"),
"lib.es2018.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.es2018.d.ts", "utf8"),
"lib.es2017.intl.d.ts": libEs2017IntlDts,
"lib.es2017.object.d.ts": libEs2017ObjectDts,
"lib.es2017.sharedmemory.d.ts": libEs2017SharedmemoryDts,
"lib.es2017.string.d.ts": libEs2017StringDts,
"lib.es2017.typedarrays.d.ts": libEs2017TypedarraysDts,
"lib.es2018.d.ts": libEs2018Dts,
//"lib.es2018.full.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.es2018.full.d.ts", "utf8"),
"lib.es2018.promise.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.es2018.promise.d.ts", "utf8"),
"lib.es2018.regexp.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.es2018.regexp.d.ts", "utf8"),
"lib.es2018.promise.d.ts": libEs2018PromiseDts,
"lib.es2018.regexp.d.ts": libEs2018RegexpDts,
//"lib.es5.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.es5.d.ts", "utf8"),
//"lib.es6.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.es6.d.ts", "utf8"),
"lib.esnext.array.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.esnext.array.d.ts", "utf8"),
"lib.esnext.asynciterable.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.esnext.asynciterable.d.ts", "utf8"),
"lib.esnext.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.esnext.d.ts", "utf8"),
"lib.esnext.array.d.ts": libEsnextArrayDts,
"lib.esnext.asynciterable.d.ts": libEsnextAsynciterablesDts,
"lib.esnext.d.ts": libEsnextDts,
//"lib.esnext.full.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.esnext.full.d.ts", "utf8"),
//"lib.scripthost.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.scripthost.d.ts", "utf8"),
//"lib.webworker.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/lib.webworker.d.ts", "utf8"),
//"protocol.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/protocol.d.ts", "utf8"),
//"tsserverlibrary.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/tsserverlibrary.d.ts", "utf8"),
"typescript.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/typescript.d.ts", "utf8"),
"typescript.d.ts": typescriptDts,
//"typescriptServices.d.ts": readFileSync(__dirname + "/../third_party/node_modules/typescript/lib/typescriptServices.d.ts", "utf8"),
};
8 changes: 3 additions & 5 deletions js/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import { deno as fbs } from "./msg_generated";
import { assert, log } from "./util";
import * as runtime from "./runtime";

const globalEval = eval;
const window = globalEval("this");

let cmdIdCounter = 0;
function assignCmdId(): number {
// TODO(piscisaureus) Safely re-use so they don't overflow.
Expand All @@ -27,7 +24,8 @@ function startMsg(cmdId: number): Uint8Array {
return builder.asUint8Array();
}

window["denoMain"] = () => {
/* tslint:disable-next-line:no-default-export */
export default function denoMain() {
// First we send an empty "Start" message to let the privlaged side know we
// are ready. The response should be a "StartRes" message containing the CLI
// argv and other info.
Expand Down Expand Up @@ -61,4 +59,4 @@ window["denoMain"] = () => {
const inputFn = argv[1];
const mod = runtime.resolveModule(inputFn, `${cwd}/`);
mod.compileAndRun();
};
}
1 change: 1 addition & 0 deletions js/mock_builtin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default undefined;
7 changes: 7 additions & 0 deletions js/plugins.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// This allows TypeScript to resolve any modules that end with `!string`
// as there is a rollup plugin that will take any mids ending with `!string`
// and return them as a string to rollup for inlining
declare module "*!string" {
const value: string;
export default value;
}
16 changes: 11 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@
"@types/flatbuffers": "^1.9.0",
"@types/source-map-support": "^0.4.1",
"flatbuffers": "^1.9.0",
"parcel-bundler": "^1.8.1",
"prettier": "^1.13.7",
"rollup": "^0.63.2",
"rollup-pluginutils": "^2.3.0",
"rollup-plugin-alias": "^1.4.0",
"rollup-plugin-analyzer": "^2.1.0",
"rollup-plugin-commonjs": "^9.1.3",
"rollup-plugin-node-globals": "^1.2.1",
"rollup-plugin-node-resolve": "^3.3.0",
"rollup-plugin-string": "^2.0.2",
"rollup-plugin-typescript2": "^0.16.1",
"source-map-support": "^0.5.6",
"tslint": "^5.10.0",
"tslint-eslint-rules": "^5.3.1",
"tslint-no-circular-imports": "^0.5.0",
"typescript": "2.8.3"
},
"browserslist": [
"chrome 69"
]
}
}
141 changes: 141 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import path from "path";
import alias from "rollup-plugin-alias";
import { plugin as analyze } from "rollup-plugin-analyzer";
import commonjs from "rollup-plugin-commonjs";
import globals from "rollup-plugin-node-globals";
import nodeResolve from "rollup-plugin-node-resolve";
import typescript from "rollup-plugin-typescript2";
import { createFilter } from "rollup-pluginutils";

const mockPath = path.join(__dirname, "js", "mock_builtin");
const tsconfig = path.join(__dirname, "tsconfig.json");
const typescriptPath = `${
process.env.BASEPATH
}/third_party/node_modules/typescript/lib/typescript.js`;

// this is a rollup plugin which will look for imports ending with `!string` and resolve
// them with a module that will inline the contents of the file as a string. Needed to
// support `js/assets.ts`.
function strings({ include, exclude } = {}) {
if (!include) {
throw new Error("include option must be passed");
}

const filter = createFilter(include, exclude);

return {
name: "strings",

resolveId(importee) {
if (importee.endsWith("!string")) {
return path.resolve(
path.join(
process.env.BASEPATH,
importee.slice(0, importee.lastIndexOf("!string"))
)
);
}
},

transform(code, id) {
if (filter(id)) {
return {
code: `export default ${JSON.stringify(code)};`,
map: { mappings: "" }
};
}
}
};
}

export default function makeConfig(commandOptions) {
return {
output: {
format: "iife",
name: "denoMain",
sourcemap: true
},

plugins: [
// would prefer to use `rollup-plugin-virtual` to inject the empty module, but there
// is an issue with `rollup-plugin-commonjs` which causes errors when using the
// virtual plugin (see: rollup/rollup-plugin-commonjs#315), this means we have to use
// a physical module to substitute
alias({
fs: mockPath,
path: mockPath,
os: mockPath,
crypto: mockPath,
buffer: mockPath,
module: mockPath
}),

// Allows rollup to resolve modules based on Node.js resolution
nodeResolve({
jsnext: true,
main: true
}),

// Allows rollup to import CommonJS modules
commonjs({
namedExports: {
// Static analysis of `typescript.js` does detect the exports properly, therefore
// rollup requires them to be explicitly defined to avoid generating warnings
[typescriptPath]: [
"createLanguageService",
"formatDiagnosticsWithColorAndContext",
"ModuleKind",
"ScriptSnapshot",
"ScriptTarget",
"version"
]
}
}),

typescript({
// The build script is invoked from `out/Target` and so config is located alongside this file
tsconfig,

// By default, the include path only includes the cwd and below, need to include the root of the project
// to be passed to this plugin. This is different front tsconfig.json include
include: ["*.ts", `${__dirname}/**/*.ts`],

// d.ts files are not bundled and by default like include, it only includes the cwd and below
exclude: ["*.d.ts", `${__dirname}/**/*.d.ts`]
}),

// Provides inlining of file contents for `js/assets.ts`
strings({
include: ["*.d.ts", `${__dirname}/**/*.d.ts`]
}),

// Provide some concise information about the bundle
analyze({
skipFormatted: true,
onAnalysis({
bundleSize,
bundleOrigSize,
bundleReduction,
moduleCount
}) {
if (!commandOptions.silent) {
console.log(
`Bundle size: ${Math.round((bundleSize / 1000000) * 100) / 100}Mb`
);
console.log(
`Original size: ${Math.round((bundleOrigSize / 1000000) * 100) /
100}Mb`
);
console.log(`Reduction: ${bundleReduction}%`);
console.log(`Module count: ${moduleCount}`);
}
}
}),

// source-map-support, which is required by TypeScript to support source maps, requires Node.js Buffer
// implementation. This needs to come at the end of the plugins because of the impact it has on
// the existing runtime environment, which breaks other plugins and features of the bundler.
globals()
]
};
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"compilerOptions": {
"allowJs": true,
"module": "commonjs",
"module": "esnext",
"noImplicitAny": true,
"sourceMap": true,
"removeComments": true,
"preserveConstEnums": true,
"target": "es2017",
"moduleResolution": "node",
"noImplicitReturns": true,
"pretty": true,
"noFallthroughCasesInSwitch": true,
Expand Down
Loading

0 comments on commit 845f721

Please sign in to comment.