@ampproject/rollup-plugin-closure-compiler
Advanced tools
Comparing version 0.3.1 to 0.4.0
@@ -18,53 +18,34 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const google_closure_compiler_1 = require("google-closure-compiler"); | ||
const temp_write_1 = require("temp-write"); | ||
const fs_1 = require("fs"); | ||
function defaultCompileOptions(outputOptions) { | ||
// Defaults for Rollup Projects are slightly different than Closure Compiler defaults. | ||
// - Users of Rollup tend to transpile their code before handing it to a minifier, | ||
// so no transpile is default. | ||
// - When Rollup output is set to "es" it is expected the code will live in a ES Module, | ||
// so safely be more aggressive in minification. | ||
// - When Rollup is configured to output an iife, ensure Closure Compiler does not | ||
// mangle the name of the iife wrapper. | ||
let options = { | ||
language_out: 'NO_TRANSPILE', | ||
assume_function_wrapper: outputOptions.format === 'es' ? true : false, | ||
warning_level: 'QUIET', | ||
}; | ||
if (outputOptions.format === 'iife' && outputOptions.name) { | ||
options['externs'] = temp_write_1.sync(`function ${outputOptions.name}(){}`); | ||
} | ||
return options; | ||
} | ||
exports.defaultCompileOptions = defaultCompileOptions; | ||
function closureCompiler(compileOptions = {}) { | ||
const fs = require("fs"); | ||
const util_1 = require("util"); | ||
const compiler_1 = require("./compiler"); | ||
const options_1 = require("./options"); | ||
const transforms_1 = require("./transforms"); | ||
const readFile = util_1.promisify(fs.readFile); | ||
/** | ||
* Transform the tree-shaken code from Rollup with Closure Compiler (with derived configuration and transforms) | ||
* @param compileOptions Closure Compiler compilation options from Rollup configuration. | ||
* @param transforms Transforms to apply to source followin Closure Compiler completion. | ||
* @param code Source to compile. | ||
* @param outputOptions Rollup Output Options. | ||
* @return Closure Compiled form of the Rollup Chunk | ||
*/ | ||
exports.transformChunk = async (transforms, requestedCompileOptions, sourceCode, outputOptions) => { | ||
const code = await transforms_1.preCompilation(sourceCode, outputOptions, transforms); | ||
const [compileOptions, mapFile] = options_1.default(requestedCompileOptions, outputOptions, code, transforms); | ||
return compiler_1.default(compileOptions, transforms).then(async (code) => { | ||
return { code, map: JSON.parse(await readFile(mapFile, 'utf8')) }; | ||
}, (error) => { | ||
throw error; | ||
}); | ||
}; | ||
function closureCompiler(requestedCompileOptions = {}) { | ||
let transforms; | ||
return { | ||
name: 'closure-compiler', | ||
transformBundle: function (code, outputOptions) { | ||
const temp = { | ||
js: temp_write_1.sync(code), | ||
map: temp_write_1.sync(''), | ||
}; | ||
compileOptions = Object.assign(defaultCompileOptions(outputOptions), compileOptions, { | ||
js: temp.js, | ||
create_source_map: temp.map, | ||
}); | ||
const compile = new Promise(function (resolve, reject) { | ||
new google_closure_compiler_1.compiler(compileOptions).run(function (exitCode, stdOut, stdErr) { | ||
if (exitCode !== 0) { | ||
reject(new Error(`Google Closure Compiler exit ${exitCode}: ${stdErr}`)); | ||
} | ||
else { | ||
resolve(stdOut); | ||
} | ||
}); | ||
}); | ||
return compile.then(stdOut => { | ||
const sourceMap = JSON.parse(fs_1.readFileSync(temp.map, 'utf8')); | ||
return { code: stdOut, map: sourceMap }; | ||
}, (error) => { | ||
throw error; | ||
}); | ||
load() { | ||
transforms = transforms || transforms_1.createTransforms(this); | ||
}, | ||
transform: async (code) => transforms_1.deriveFromInputSource(code, transforms), | ||
transformChunk: async (code, outputOptions, chunk) => await exports.transformChunk(transforms, requestedCompileOptions, code, outputOptions), | ||
}; | ||
@@ -71,0 +52,0 @@ } |
{ | ||
"name": "@ampproject/rollup-plugin-closure-compiler", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "Rollup + Google Closure Compiler", | ||
@@ -12,3 +12,2 @@ "main": "dist/index.js", | ||
"engines": { | ||
"yarn": ">=1", | ||
"node": ">=8" | ||
@@ -21,5 +20,6 @@ }, | ||
"scripts": { | ||
"pretest": "yarn build", | ||
"test": "ava test/*.js", | ||
"build": "rimraf dist && tsc -p tsconfig.json" | ||
"pretest": "npm run build", | ||
"test": "ava test/**/*.js", | ||
"build": "rimraf dist && tsc -p tsconfig.json", | ||
"lint": "tslint -c tslint.json -p tsconfig.json" | ||
}, | ||
@@ -31,6 +31,7 @@ "dependencies": { | ||
"devDependencies": { | ||
"@types/estree": "^0.0.39", | ||
"@types/google-closure-compiler": "^0.0.18", | ||
"@types/temp-write": "^3.3.0", | ||
"ava": "^0.25.0", | ||
"husky": "1.0.0-rc.9", | ||
"ava": "1.0.0-beta.6", | ||
"husky": "1.0.0-rc.13", | ||
"lint-staged": "^7.2.0", | ||
@@ -40,3 +41,3 @@ "prettier": "^1.13.5", | ||
"rollup": "^0.62.0", | ||
"sourcemap-validator": "^1.1.0", | ||
"tslint": "^5.10.0", | ||
"typescript": "^2.9.2" | ||
@@ -46,2 +47,3 @@ }, | ||
"*.ts": [ | ||
"npm run lint --fix", | ||
"prettier --config .prettierrc --write", | ||
@@ -53,6 +55,6 @@ "git add" | ||
"hooks": { | ||
"pre-commit": "lint-staged", | ||
"pre-push": "yarn test" | ||
"pre-push": "npm run test", | ||
"pre-commit": "lint-staged" | ||
} | ||
} | ||
} |
107
src/index.ts
@@ -17,70 +17,57 @@ /** | ||
import { compiler, CompileOptions } from 'google-closure-compiler'; | ||
import { sync } from 'temp-write'; | ||
import { readFileSync } from 'fs'; | ||
import { OutputOptions, RawSourceMap, Plugin } from 'rollup'; | ||
import { CompileOptions } from 'google-closure-compiler'; | ||
import * as fs from 'fs'; | ||
import { promisify } from 'util'; | ||
import { OutputOptions, RawSourceMap, Plugin, OutputChunk } from 'rollup'; | ||
import compiler from './compiler'; | ||
import options from './options'; | ||
import { preCompilation, createTransforms, deriveFromInputSource } from './transforms'; | ||
import { Transform } from './types'; | ||
export function defaultCompileOptions(outputOptions: OutputOptions): CompileOptions { | ||
// Defaults for Rollup Projects are slightly different than Closure Compiler defaults. | ||
// - Users of Rollup tend to transpile their code before handing it to a minifier, | ||
// so no transpile is default. | ||
// - When Rollup output is set to "es" it is expected the code will live in a ES Module, | ||
// so safely be more aggressive in minification. | ||
// - When Rollup is configured to output an iife, ensure Closure Compiler does not | ||
// mangle the name of the iife wrapper. | ||
const readFile = promisify(fs.readFile); | ||
let options: CompileOptions = { | ||
language_out: 'NO_TRANSPILE', | ||
assume_function_wrapper: outputOptions.format === 'es' ? true : false, | ||
warning_level: 'QUIET', | ||
}; | ||
if (outputOptions.format === 'iife' && outputOptions.name) { | ||
options['externs'] = sync(`function ${outputOptions.name}(){}`); | ||
} | ||
/** | ||
* Transform the tree-shaken code from Rollup with Closure Compiler (with derived configuration and transforms) | ||
* @param compileOptions Closure Compiler compilation options from Rollup configuration. | ||
* @param transforms Transforms to apply to source followin Closure Compiler completion. | ||
* @param code Source to compile. | ||
* @param outputOptions Rollup Output Options. | ||
* @return Closure Compiled form of the Rollup Chunk | ||
*/ | ||
export const transformChunk = async ( | ||
transforms: Array<Transform>, | ||
requestedCompileOptions: CompileOptions, | ||
sourceCode: string, | ||
outputOptions: OutputOptions, | ||
): Promise<{ code: string; map: RawSourceMap } | void> => { | ||
const code = await preCompilation(sourceCode, outputOptions, transforms); | ||
const [compileOptions, mapFile] = options( | ||
requestedCompileOptions, | ||
outputOptions, | ||
code, | ||
transforms, | ||
); | ||
return options; | ||
} | ||
return compiler(compileOptions, transforms).then( | ||
async code => { | ||
return { code, map: JSON.parse(await readFile(mapFile, 'utf8')) }; | ||
}, | ||
(error: Error) => { | ||
throw error; | ||
}, | ||
); | ||
}; | ||
export default function closureCompiler(compileOptions: CompileOptions = {}): Plugin { | ||
export default function closureCompiler(requestedCompileOptions: CompileOptions = {}): Plugin { | ||
let transforms: Array<Transform>; | ||
return { | ||
name: 'closure-compiler', | ||
transformBundle: function( | ||
code: string, | ||
outputOptions: OutputOptions, | ||
): Promise<{ code: string; map: RawSourceMap } | void> { | ||
const temp = { | ||
js: sync(code), | ||
map: sync(''), | ||
}; | ||
compileOptions = Object.assign(defaultCompileOptions(outputOptions), compileOptions, { | ||
js: temp.js, | ||
create_source_map: temp.map, | ||
}); | ||
const compile: Promise<string> = new Promise(function(resolve, reject) { | ||
new compiler(compileOptions).run(function( | ||
exitCode: number, | ||
stdOut: string, | ||
stdErr: string, | ||
) { | ||
if (exitCode !== 0) { | ||
reject(new Error(`Google Closure Compiler exit ${exitCode}: ${stdErr}`)); | ||
} else { | ||
resolve(stdOut); | ||
} | ||
}); | ||
}); | ||
return compile.then( | ||
stdOut => { | ||
const sourceMap: RawSourceMap = JSON.parse(readFileSync(temp.map, 'utf8')); | ||
return { code: stdOut, map: sourceMap }; | ||
}, | ||
(error: Error) => { | ||
throw error; | ||
}, | ||
); | ||
load() { | ||
transforms = transforms || createTransforms(this); | ||
}, | ||
transform: async (code: string) => deriveFromInputSource(code, transforms), | ||
transformChunk: async (code: string, outputOptions: OutputOptions, chunk: OutputChunk) => | ||
await transformChunk(transforms, requestedCompileOptions, code, outputOptions), | ||
}; | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
269786
47
6898
11
6
1