From 94f603e50afd3affc4976a62f8c8f0a2a6c83366 Mon Sep 17 00:00:00 2001 From: Michelle Tilley Date: Tue, 1 Nov 2016 10:54:26 -0700 Subject: [PATCH] Pass package info to transpilers --- spec/package-transpilation-registry-spec.js | 14 ++++++++------ src/compile-cache.js | 4 ++-- src/package-transpilation-registry.js | 17 ++++++++++++++--- src/package.coffee | 2 +- 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/spec/package-transpilation-registry-spec.js b/spec/package-transpilation-registry-spec.js index cec52a949..310570c35 100644 --- a/spec/package-transpilation-registry-spec.js +++ b/spec/package-transpilation-registry-spec.js @@ -56,6 +56,8 @@ describe("PackageTranspilationRegistry", () => { const coffeeSpec = { glob: "*.coffee", transpiler: './transpiler-coffee', options: { type: 'coffee' } } const omgSpec = { glob: "*.omgwhatisthis", transpiler: './transpiler-omg', options: { type: 'omg' } } + const expectedMeta = { name: 'my-package', path: '/path/to', meta: { some: 'metadata' } } + const jsTranspiler = { transpile: (sourceCode, filePath, options) => { return {code: sourceCode + "-transpiler-js"} @@ -98,7 +100,7 @@ describe("PackageTranspilationRegistry", () => { throw new Error('bad transpiler path ' + spec.transpiler) }) - registry.addTranspilerConfigForPath('/path/to', 'my-package', [ + registry.addTranspilerConfigForPath('/path/to', 'my-package', { some: 'metadata' }, [ jsSpec, coffeeSpec, omgSpec ]) }) @@ -119,9 +121,9 @@ describe("PackageTranspilationRegistry", () => { spyOn(jsTranspiler, 'getCacheKeyData').andCallThrough() wrappedCompiler.getCachePath('source', missPath, jsSpec) - expect(jsTranspiler.getCacheKeyData).not.toHaveBeenCalled() + expect(jsTranspiler.getCacheKeyData).not.toHaveBeenCalledWith('source', missPath, jsSpec.options, expectedMeta) wrappedCompiler.getCachePath('source', hitPath, jsSpec) - expect(jsTranspiler.getCacheKeyData).toHaveBeenCalled() + expect(jsTranspiler.getCacheKeyData).toHaveBeenCalledWith('source', hitPath, jsSpec.options, expectedMeta) }) it('compiles files matching a glob with the associated transpiler, and the old one otherwise', () => { @@ -130,11 +132,11 @@ describe("PackageTranspilationRegistry", () => { spyOn(omgTranspiler, "transpile").andCallThrough() expect(wrappedCompiler.compile('source', hitPath)).toEqual('source-transpiler-js') - expect(jsTranspiler.transpile).toHaveBeenCalledWith('source', hitPath, jsSpec.options) + expect(jsTranspiler.transpile).toHaveBeenCalledWith('source', hitPath, jsSpec.options, expectedMeta) expect(wrappedCompiler.compile('source', hitPathCoffee)).toEqual('source-transpiler-coffee') - expect(coffeeTranspiler.transpile).toHaveBeenCalledWith('source', hitPathCoffee, coffeeSpec.options) + expect(coffeeTranspiler.transpile).toHaveBeenCalledWith('source', hitPathCoffee, coffeeSpec.options, expectedMeta) expect(wrappedCompiler.compile('source', hitNonStandardExt)).toEqual('source-transpiler-omg') - expect(omgTranspiler.transpile).toHaveBeenCalledWith('source', hitNonStandardExt, omgSpec.options) + expect(omgTranspiler.transpile).toHaveBeenCalledWith('source', hitNonStandardExt, omgSpec.options, expectedMeta) expect(wrappedCompiler.compile('source', missPath)).toEqual('source-original-compiler') expect(wrappedCompiler.compile('source', hitPathMissExt)).toEqual('source-original-compiler') diff --git a/src/compile-cache.js b/src/compile-cache.js index fed890d5c..9d4ec08f3 100644 --- a/src/compile-cache.js +++ b/src/compile-cache.js @@ -19,9 +19,9 @@ var COMPILERS = { '.coffee': packageTranspilationRegistry.wrapTranspiler(require('./coffee-script')) } -exports.addTranspilerConfigForPath = function (packagePath, packageName, config) { +exports.addTranspilerConfigForPath = function (packagePath, packageName, packageMeta, config) { packagePath = fs.realpathSync(packagePath) - packageTranspilationRegistry.addTranspilerConfigForPath(packagePath, packageName, config) + packageTranspilationRegistry.addTranspilerConfigForPath(packagePath, packageName, packageMeta, config) } exports.removeTranspilerConfigForPath = function (packagePath) { diff --git a/src/package-transpilation-registry.js b/src/package-transpilation-registry.js index 46509807f..b8a792339 100644 --- a/src/package-transpilation-registry.js +++ b/src/package-transpilation-registry.js @@ -18,9 +18,10 @@ class PackageTranspilationRegistry { this.transpilerPaths = {} } - addTranspilerConfigForPath (packagePath, packageName, config) { + addTranspilerConfigForPath (packagePath, packageName, packageMeta, config) { this.configByPackagePath[packagePath] = { name: packageName, + meta: packageMeta, path: packagePath, specs: config } @@ -115,7 +116,8 @@ class PackageTranspilationRegistry { .update(sourceCode, 'utf8') if (transpiler && transpiler.getCacheKeyData) { - const additionalCacheData = transpiler.getCacheKeyData(sourceCode, filePath, spec.options) + const meta = this.getMetadata(spec) + const additionalCacheData = transpiler.getCacheKeyData(sourceCode, filePath, spec.options, meta) hash.update(additionalCacheData, 'utf8') } @@ -126,7 +128,8 @@ class PackageTranspilationRegistry { const transpiler = this.getTranspiler(spec) if (transpiler) { - const result = transpiler.transpile(sourceCode, filePath, spec.options || {}) + const meta = this.getMetadata(spec) + const result = transpiler.transpile(sourceCode, filePath, spec.options || {}, meta) if (result === undefined || (result && result.code === undefined)) { return sourceCode } else if (result.code) { @@ -140,6 +143,14 @@ class PackageTranspilationRegistry { } } + getMetadata (spec) { + return { + name: spec._config.name, + path: spec._config.path, + meta: spec._config.meta + } + } + getTranspilerPath (spec) { Resolve = Resolve || require('resolve') return Resolve.sync(spec.transpiler, { diff --git a/src/package.coffee b/src/package.coffee index 6029c99ae..20236930b 100644 --- a/src/package.coffee +++ b/src/package.coffee @@ -254,7 +254,7 @@ class Package registerTranspilerConfig: -> if @metadata.atomTranspilers - CompileCache.addTranspilerConfigForPath(@path, @name, @metadata.atomTranspilers) + CompileCache.addTranspilerConfigForPath(@path, @name, @metadata, @metadata.atomTranspilers) unregisterTranspilerConfig: -> if @metadata.atomTranspilers