Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ES module source transforms #29924

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions doc/api/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,17 @@ With the list of module exports provided upfront, the `execute` function will
then be called at the exact point of module evaluation order for that module
in the import tree.

### Transform hook

This hook is called only for modules that return `format: 'module'` from
the `resolve` hook.

```js
export async function transformSource(url, source) {
return source.replace(/'original'/, "'replacement'");
}
```

## Resolution Algorithm

### Features
Expand Down
18 changes: 17 additions & 1 deletion lib/internal/modules/esm/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ class Loader {
// an object with the same keys as `exports`, whose values are get/set
// functions for the actual exported values.
this._dynamicInstantiate = undefined;
// These hooks are called FILO when resolve(...).format is 'module' and
// has the signature
// (code : string, url : string) -> Promise<code : string>
this._transformSource = [];
// The index for assigning unique URLs to anonymous module evaluation
this.evalIndex = 0;
}
Expand Down Expand Up @@ -133,14 +137,26 @@ class Loader {
return module.getNamespace();
}

hook({ resolve, dynamicInstantiate }) {
async transformSource(url, code) {
for (const transformFn of this._transformSource) {
code = await transformFn(url, code);
}

return code;
}

hook({ resolve, dynamicInstantiate, transformSource }) {
// Use .bind() to avoid giving access to the Loader instance when called.
if (resolve !== undefined)
this._resolve = FunctionPrototype.bind(resolve, null);
if (dynamicInstantiate !== undefined) {
this._dynamicInstantiate =
FunctionPrototype.bind(dynamicInstantiate, null);
}
if (transformSource !== undefined) {
// this.transformSource protects `this`, no need to bind.
this._transformSource.push(transformSource);
}
}

async getModuleJob(specifier, parentURL) {
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/modules/esm/translators.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async function importModuleDynamically(specifier, { url }) {

// Strategy for loading a standard JavaScript module
translators.set('module', async function moduleStrategy(url) {
const source = `${await getSource(url)}`;
const source = await this.transformSource(url, `${await getSource(url)}`);
maybeCacheSourceMap(url, source);
debug(`Translating StandardModule ${url}`);
const module = new ModuleWrap(source, url);
Expand Down
10 changes: 10 additions & 0 deletions test/es-module/test-esm-transform-source.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Flags: --experimental-modules --experimental-loader ./test/fixtures/es-module-loaders/transform-loader.mjs
/* eslint-disable node-core/require-common-first, node-core/required-modules */
import {
foo,
bar
} from '../fixtures/es-module-loaders/module-named-exports.mjs';
import assert from 'assert';

assert.strictEqual(foo, 'transformed-foo');
assert.strictEqual(bar, 'transformed-bar');
11 changes: 11 additions & 0 deletions test/fixtures/es-module-loaders/transform-loader.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { promisify } from 'util';

const delay = promisify(setTimeout);

export async function transformSource(url, source) {
await delay(50);

return source
.replace(/'bar'/, "'transformed-bar'")
.replace(/'foo'/, "'transformed-foo'");
}