2019-01-21 14:03:30 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2018-11-04 19:26:15 -05:00
|
|
|
// @ts-check
|
2018-10-19 17:15:29 -04:00
|
|
|
import * as fs from "fs";
|
2018-07-21 21:14:52 -04:00
|
|
|
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";
|
2018-07-27 02:51:19 -04:00
|
|
|
import typescriptPlugin from "rollup-plugin-typescript2";
|
2018-07-21 21:14:52 -04:00
|
|
|
import { createFilter } from "rollup-pluginutils";
|
2019-02-18 18:43:02 -05:00
|
|
|
import replace from "rollup-plugin-replace";
|
2018-07-27 02:51:19 -04:00
|
|
|
import typescript from "typescript";
|
2018-07-21 21:14:52 -04:00
|
|
|
|
2018-10-19 17:15:29 -04:00
|
|
|
const mockPath = path.resolve(__dirname, "js/mock_builtin.js");
|
|
|
|
const tsconfig = path.resolve(__dirname, "tsconfig.json");
|
|
|
|
const typescriptPath = path.resolve(
|
|
|
|
__dirname,
|
|
|
|
"third_party/node_modules/typescript/lib/typescript.js"
|
|
|
|
);
|
2019-03-30 14:45:36 -04:00
|
|
|
const gnArgs = fs.readFileSync("gen/cli/gn_args.txt", "utf-8").trim();
|
2018-07-21 21:14:52 -04:00
|
|
|
|
2018-07-25 23:07:50 -04:00
|
|
|
// We will allow generated modules to be resolvable by TypeScript based on
|
|
|
|
// the current build path
|
|
|
|
const tsconfigOverride = {
|
|
|
|
compilerOptions: {
|
|
|
|
paths: {
|
2018-10-19 17:15:29 -04:00
|
|
|
"*": ["*", path.resolve("*")]
|
2018-07-25 23:07:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-11-04 19:26:15 -05:00
|
|
|
/** 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`.
|
|
|
|
* @param {any} param0
|
|
|
|
*/
|
|
|
|
function strings(
|
|
|
|
{ include, exclude } = { include: undefined, exclude: undefined }
|
|
|
|
) {
|
2018-07-21 21:14:52 -04:00
|
|
|
if (!include) {
|
|
|
|
throw new Error("include option must be passed");
|
|
|
|
}
|
|
|
|
|
|
|
|
const filter = createFilter(include, exclude);
|
|
|
|
|
|
|
|
return {
|
|
|
|
name: "strings",
|
|
|
|
|
2018-08-07 16:27:31 -04:00
|
|
|
/**
|
|
|
|
* @param {string} importee
|
|
|
|
*/
|
2018-07-21 21:14:52 -04:00
|
|
|
resolveId(importee) {
|
|
|
|
if (importee.endsWith("!string")) {
|
2018-08-07 16:27:31 -04:00
|
|
|
// strip the `!string` from `importee`
|
|
|
|
importee = importee.slice(0, importee.lastIndexOf("!string"));
|
|
|
|
if (!importee.startsWith("gen/")) {
|
2018-09-01 10:45:26 -04:00
|
|
|
// this is a static asset which is located relative to the root of
|
|
|
|
// the source project
|
2018-10-19 17:15:29 -04:00
|
|
|
return path.resolve(path.join(__dirname, importee));
|
2018-08-07 16:27:31 -04:00
|
|
|
}
|
2018-09-01 10:45:26 -04:00
|
|
|
// this is an asset which has been generated, therefore it will be
|
|
|
|
// located within the build path
|
|
|
|
return path.resolve(path.join(process.cwd(), importee));
|
2018-07-21 21:14:52 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-08-07 16:27:31 -04:00
|
|
|
/**
|
|
|
|
* @param {any} code
|
|
|
|
* @param {string} id
|
|
|
|
*/
|
2018-07-21 21:14:52 -04:00
|
|
|
transform(code, id) {
|
|
|
|
if (filter(id)) {
|
|
|
|
return {
|
2018-10-11 17:23:13 -04:00
|
|
|
code: `export default ${JSON.stringify(code)};`,
|
2018-07-21 21:14:52 -04:00
|
|
|
map: { mappings: "" }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-10-02 12:25:49 -04:00
|
|
|
const archNodeToDeno = {
|
|
|
|
x64: "x64"
|
|
|
|
};
|
|
|
|
const osNodeToDeno = {
|
|
|
|
win32: "win",
|
|
|
|
darwin: "mac",
|
|
|
|
linux: "linux"
|
|
|
|
};
|
|
|
|
|
2018-07-25 23:07:50 -04:00
|
|
|
// This plugin resolves at bundle time any generated resources that are
|
|
|
|
// in the build path under `gen` and specified with a MID starting with `gen/`.
|
|
|
|
// The plugin assumes that the MID needs to have the `.ts` extension appended.
|
|
|
|
function resolveGenerated() {
|
|
|
|
return {
|
|
|
|
name: "resolve-msg-generated",
|
|
|
|
resolveId(importee) {
|
2019-03-30 14:45:36 -04:00
|
|
|
if (importee.startsWith("gen/cli/msg_generated")) {
|
2018-10-19 17:15:29 -04:00
|
|
|
return path.resolve(`${importee}.ts`);
|
2018-07-25 23:07:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-10-19 17:15:29 -04:00
|
|
|
function generateDepFile({ outputFile, sourceFiles = [], configFiles = [] }) {
|
2018-11-04 19:26:15 -05:00
|
|
|
let timestamp = new Date();
|
2018-10-19 17:15:29 -04:00
|
|
|
|
|
|
|
// Save the depfile just before the node process exits.
|
|
|
|
process.once("beforeExit", () =>
|
|
|
|
writeDepFile({ outputFile, sourceFiles, configFiles, timestamp })
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
name: "depfile",
|
|
|
|
load(sourceFile) {
|
|
|
|
// The 'globals' plugin adds generated files that don't exist on disk.
|
|
|
|
// Don't add them to the depfile.
|
|
|
|
if (/^[0-9a-f]{30}$/.test(sourceFile)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sourceFiles.push(sourceFile);
|
|
|
|
// Remember the time stamp that we last resolved a dependency.
|
|
|
|
// We'll set the last modified time of the depfile to that.
|
|
|
|
timestamp = new Date();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function writeDepFile({ outputFile, sourceFiles, configFiles, timestamp }) {
|
|
|
|
const buildDir = process.cwd();
|
|
|
|
const outputDir = path.dirname(outputFile);
|
|
|
|
|
|
|
|
// Assert that the discovered bundle inputs are files that exist on disk.
|
|
|
|
sourceFiles.forEach(f => fs.accessSync(f));
|
|
|
|
// Since we also want to rebuild the bundle if rollup configuration or the the
|
|
|
|
// tooling changes (e.g. when typescript is updated), add the currently loaded
|
|
|
|
// node.js modules to the list of dependencies.
|
|
|
|
let inputs = [...sourceFiles, ...configFiles, ...Object.keys(require.cache)];
|
|
|
|
// Deduplicate the list of inputs.
|
|
|
|
inputs = Array.from(new Set(inputs.map(f => path.resolve(f))));
|
|
|
|
// Turn filenames into relative paths and format/escape them for a Makefile.
|
|
|
|
inputs = inputs.map(formatPath);
|
|
|
|
|
|
|
|
// Build a list of output filenames and normalize those too.
|
|
|
|
const depFile = path.join(
|
|
|
|
outputDir,
|
|
|
|
path.basename(outputFile, path.extname(outputFile)) + ".d"
|
|
|
|
);
|
|
|
|
const outputs = [outputFile, depFile].map(formatPath);
|
|
|
|
|
|
|
|
// Generate depfile contents.
|
|
|
|
const depFileContent = [
|
|
|
|
...outputs.map(filename => `${filename}: ` + inputs.join(" ") + "\n\n"),
|
|
|
|
...inputs.map(filename => `${filename}:\n`)
|
|
|
|
].join("");
|
|
|
|
|
|
|
|
// Since we're writing the depfile when node's "beforeExit" hook triggers,
|
|
|
|
// it's getting written _after_ the regular outputs are saved to disk.
|
|
|
|
// Therefore, after writing the depfile, reset its timestamps to when we last
|
|
|
|
// discovered a dependency, which was certainly before the bundle was built.
|
|
|
|
fs.writeFileSync(depFile, depFileContent);
|
|
|
|
fs.utimesSync(depFile, timestamp, timestamp);
|
|
|
|
|
|
|
|
// Renders path to make it suitable for a depfile.
|
|
|
|
function formatPath(filename) {
|
|
|
|
// Make the path relative to the root build directory.
|
|
|
|
filename = path.relative(buildDir, filename);
|
|
|
|
// Use forward slashes on Windows.
|
|
|
|
if (process.platform === "win32") {
|
|
|
|
filename = filename.replace(/\\/g, "/");
|
|
|
|
}
|
|
|
|
// Escape spaces with a backslash. This is what rust and clang do too.
|
|
|
|
filename = filename.replace(/ /g, "\\ ");
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-21 21:14:52 -04:00
|
|
|
export default function makeConfig(commandOptions) {
|
|
|
|
return {
|
|
|
|
output: {
|
|
|
|
format: "iife",
|
|
|
|
name: "denoMain",
|
2018-11-04 19:26:15 -05:00
|
|
|
sourcemap: true,
|
|
|
|
sourcemapExcludeSources: true
|
2018-07-21 21:14:52 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
plugins: [
|
2019-03-06 16:54:58 -05:00
|
|
|
// inject build and version info
|
2019-02-18 18:43:02 -05:00
|
|
|
replace({
|
2019-03-06 16:54:58 -05:00
|
|
|
ROLLUP_REPLACE_TS_VERSION: typescript.version,
|
|
|
|
ROLLUP_REPLACE_ARCH: archNodeToDeno[process.arch],
|
|
|
|
ROLLUP_REPLACE_OS: osNodeToDeno[process.platform],
|
|
|
|
ROLLUP_REPLACE_GN_ARGS: gnArgs
|
2019-02-18 18:43:02 -05:00
|
|
|
}),
|
|
|
|
|
2018-07-21 21:14:52 -04:00
|
|
|
// 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
|
|
|
|
}),
|
|
|
|
|
2018-08-07 16:27:31 -04:00
|
|
|
// Provides inlining of file contents for `js/assets.ts`
|
|
|
|
strings({
|
|
|
|
include: [
|
|
|
|
"*.d.ts",
|
|
|
|
`${__dirname}/**/*.d.ts`,
|
|
|
|
`${process.cwd()}/**/*.d.ts`
|
|
|
|
]
|
|
|
|
}),
|
|
|
|
|
2018-07-25 23:07:50 -04:00
|
|
|
// Resolves any resources that have been generated at build time
|
|
|
|
resolveGenerated(),
|
|
|
|
|
2018-07-21 21:14:52 -04:00
|
|
|
// Allows rollup to resolve modules based on Node.js resolution
|
2019-03-07 08:53:56 -05:00
|
|
|
nodeResolve(),
|
2018-07-21 21:14:52 -04:00
|
|
|
|
|
|
|
// Allows rollup to import CommonJS modules
|
|
|
|
commonjs({
|
|
|
|
namedExports: {
|
|
|
|
// Static analysis of `typescript.js` does detect the exports properly, therefore
|
2018-09-06 10:05:14 -04:00
|
|
|
// rollup requires them to be explicitly defined to make them available in the
|
|
|
|
// bundle
|
2018-07-21 21:14:52 -04:00
|
|
|
[typescriptPath]: [
|
2019-04-29 10:58:31 -04:00
|
|
|
"convertCompilerOptionsFromJson",
|
2018-07-21 21:14:52 -04:00
|
|
|
"createLanguageService",
|
2019-05-20 12:06:57 -04:00
|
|
|
"createProgram",
|
|
|
|
"createSourceFile",
|
|
|
|
"getPreEmitDiagnostics",
|
2019-02-10 18:19:31 -05:00
|
|
|
"formatDiagnostics",
|
2018-07-21 21:14:52 -04:00
|
|
|
"formatDiagnosticsWithColorAndContext",
|
2019-04-29 10:58:31 -04:00
|
|
|
"parseConfigFileTextToJson",
|
2019-02-15 11:05:29 -05:00
|
|
|
"version",
|
2019-05-20 12:06:57 -04:00
|
|
|
"CompilerHost",
|
2019-02-15 11:05:29 -05:00
|
|
|
"Extension",
|
2018-07-21 21:14:52 -04:00
|
|
|
"ModuleKind",
|
2018-09-06 10:05:14 -04:00
|
|
|
"ScriptKind",
|
2018-07-21 21:14:52 -04:00
|
|
|
"ScriptSnapshot",
|
2019-02-15 11:05:29 -05:00
|
|
|
"ScriptTarget"
|
2018-07-21 21:14:52 -04:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
2018-07-27 02:51:19 -04:00
|
|
|
typescriptPlugin({
|
2018-07-25 23:07:50 -04:00
|
|
|
// The build script is invoked from `out/:target` so passing an absolute file path is needed
|
2018-07-21 21:14:52 -04:00
|
|
|
tsconfig,
|
|
|
|
|
2018-07-25 23:07:50 -04:00
|
|
|
// This provides any overrides to the `tsconfig.json` that are needed to bundle
|
|
|
|
tsconfigOverride,
|
|
|
|
|
2018-07-27 02:51:19 -04:00
|
|
|
// This provides the locally configured version of TypeScript instead of the plugins
|
|
|
|
// default version
|
|
|
|
typescript,
|
|
|
|
|
2018-07-21 21:14:52 -04:00
|
|
|
// By default, the include path only includes the cwd and below, need to include the root of the project
|
2018-07-25 23:07:50 -04:00
|
|
|
// and build path to be passed to this plugin. This is different front tsconfig.json include
|
|
|
|
include: ["*.ts", `${__dirname}/**/*.ts`, `${process.cwd()}/**/*.ts`],
|
2018-07-21 21:14:52 -04:00
|
|
|
|
|
|
|
// d.ts files are not bundled and by default like include, it only includes the cwd and below
|
2018-07-26 11:01:17 -04:00
|
|
|
exclude: [
|
|
|
|
"*.d.ts",
|
|
|
|
`${__dirname}/**/*.d.ts`,
|
|
|
|
`${process.cwd()}/**/*.d.ts`
|
|
|
|
]
|
2018-07-21 21:14:52 -04:00
|
|
|
}),
|
|
|
|
|
|
|
|
// 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.
|
2018-10-19 17:15:29 -04:00
|
|
|
globals(),
|
|
|
|
|
|
|
|
generateDepFile({
|
|
|
|
outputFile: commandOptions.o,
|
|
|
|
configFiles: [commandOptions.c, tsconfig]
|
|
|
|
})
|
2018-07-21 21:14:52 -04:00
|
|
|
]
|
|
|
|
};
|
|
|
|
}
|