GoogleChromeLabs / tooling.report

tooling.report a quick way to determine the best build tool for your next web project, or if tooling migration is worth it, or how to adopt a tool's best practice into your existing configuration and code base.
https://tooling.report
Apache License 2.0
850 stars 49 forks source link

New test: Pureness hints #272

Open jakearchibald opened 4 years ago

jakearchibald commented 4 years ago

This would be a tree-shaking test.

Something like:

main.js

import { bar } from './lib.js';
console.log(bar);

lib.js

function createDatabaseConnection() {
  console.log('connecting!');
  return {};
}

export const foo = createDatabaseConnection();
export const bar = 'bar';

Where the output wouldn't contain foo. The solution may require a pureness hint. WDYT @surma?

jakearchibald commented 4 years ago

Rollup solution, which uses a /*#__PURE__*/ hint.

surma commented 4 years ago

I make use of these pureness hints in rollup-plugin-assemblyscript to supply conveniences exports that can get tree-shookanized when unused:

const wasmUrl = /* injected by Rollup */;
const modulePromise = /*@__PURE__*/compileStreaming(fetch(wasmUrl));
const instancePromise = /*@__PURE__*/modulePromise.then(module => WebAssembly.instantiate(module, {}));

export {wasmUrl, modulePromise, instancePromise};

So I agree, this should be a subtest for treeshaking.

AjayPoshak commented 4 years ago

I am doing this.