Skip to content

Commit

Permalink
Use new deprecation API for transform dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Jun 19, 2019
1 parent 58114bd commit 98cd72b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 46 deletions.
1 change: 0 additions & 1 deletion src/ast/nodes/MetaProperty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ export default class MetaProperty extends NodeBase {
const relativePath = normalize(relative(dirname(chunkId), fileName));
let replacement;
if (assetReferenceId !== null) {
// deprecated hook for assets
replacement = pluginDriver.hookFirstSync('resolveAssetUrl', [
{
assetFileName: fileName,
Expand Down
10 changes: 5 additions & 5 deletions src/utils/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,17 @@ export default function transform(
}
}
} else {
// assets emitted by transform are transformDependencies
// assets/chunks emitted by a transform hook need to be emitted again if the hook is skipped
if (emittedAssets.length) module.transformAssets = emittedAssets;
if (emittedChunks.length) module.transformChunks = emittedChunks;

if (result && typeof result === 'object' && Array.isArray(result.dependencies)) {
// not great, but a useful way to track this without assuming WeakMap
if (!(curPlugin as any).warnedTransformDependencies)
this.warn({
code: 'TRANSFORM_DEPENDENCIES_DEPRECATED',
message: `Returning "dependencies" from plugin transform hook is deprecated for using this.addWatchFile() instead.`
});
graph.warnDeprecation(
`Returning "dependencies" from the "transform" hook as done by plugin ${plugin.name} is deprecated. The "this.addWatchFile" plugin context function should be used instead.`,
true
);
(curPlugin as any).warnedTransformDependencies = true;
if (!transformDependencies) transformDependencies = [];
for (const dep of result.dependencies)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const path = require('path');

module.exports = {
description: 'marks transform dependencies as deprecated',
options: {
plugins: {
transform(code) {
return { code, dependencies: [] };
}
}
},
error: {
code: 'PLUGIN_ERROR',
id: path.resolve(__dirname, 'main.js'),
message:
'Returning "dependencies" from the "transform" hook as done by plugin at position 1 is deprecated. The "this.addWatchFile" plugin context function should be used instead.',
pluginCode: 'DEPRECATED_FEATURE'
}
};
11 changes: 11 additions & 0 deletions test/function/samples/deprecations/transform-dependencies/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const foo = {};

function doIt(x) {
if (foo[x]) {
return true;
}
foo[x] = true;
}

doIt('x');
assert.ok(doIt('x'), 'foo was not reassigned');
40 changes: 0 additions & 40 deletions test/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1299,46 +1299,6 @@ module.exports = input;
});
});

it('Warns when using deprecated transform dependencies in plugins', () => {
let warned = false;
const watcher = rollup.watch({
input: 'input',
output: {
file: 'asdf',
format: 'es'
},
onwarn(warning) {
warned = true;
assert.equal(warning.code, 'PLUGIN_WARNING');
assert.equal(warning.pluginCode, 'TRANSFORM_DEPENDENCIES_DEPRECATED');
assert.equal(
warning.message,
'Returning "dependencies" from plugin transform hook is deprecated for using this.addWatchFile() instead.'
);
// throw here to stop file system write
throw new Error('STOP');
},
plugins: [
loader({ input: `alert('hello')` }),
{
name: 'x',
transform(code) {
return { code, dependencies: [] };
}
}
]
});
return new Promise((resolve, reject) => {
watcher.on('event', evt => {
if (evt.code === 'END') resolve();
else if (evt.code === 'ERROR' || evt.code === 'FATAL') reject(evt.error);
});
}).catch(err => {
assert.equal(err.message, 'STOP');
assert.equal(warned, true);
});
});

it('assigns chunk IDs before creating outputBundle chunks', () => {
const chunks = [];
return rollup
Expand Down

0 comments on commit 98cd72b

Please sign in to comment.