Skip to content

Commit 32aadbf

Browse files
author
Angular Builds
committed
5def2de1b fix(@angular-devkit/architect): correctly handle ESM builders
1 parent 1541ab9 commit 32aadbf

File tree

6 files changed

+56
-44
lines changed

6 files changed

+56
-44
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"typings": "src/index.d.ts",
88
"builders": "builders.json",
99
"dependencies": {
10-
"@angular-devkit/architect": "github:angular/angular-devkit-architect-builds#919b56971",
10+
"@angular-devkit/architect": "github:angular/angular-devkit-architect-builds#5def2de1b",
1111
"rxjs": "6.6.7"
1212
},
1313
"peerDependencies": {

src/utils.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8+
import { Compilation, Configuration } from 'webpack';
89
export interface EmittedFiles {
910
id?: string;
1011
name?: string;
@@ -13,4 +14,5 @@ export interface EmittedFiles {
1314
asset?: boolean;
1415
extension: string;
1516
}
16-
export declare function getEmittedFiles(compilation: import('webpack').Compilation): EmittedFiles[];
17+
export declare function getEmittedFiles(compilation: Compilation): EmittedFiles[];
18+
export declare function getWebpackConfig(configPath: string): Promise<Configuration>;

src/utils.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ var __importStar = (this && this.__importStar) || function (mod) {
2626
return result;
2727
};
2828
Object.defineProperty(exports, "__esModule", { value: true });
29-
exports.getEmittedFiles = void 0;
29+
exports.getWebpackConfig = exports.getEmittedFiles = void 0;
30+
const fs_1 = require("fs");
3031
const path = __importStar(require("path"));
32+
const url_1 = require("url");
3133
function getEmittedFiles(compilation) {
3234
var _a;
3335
const files = [];
@@ -59,3 +61,49 @@ function getEmittedFiles(compilation) {
5961
return files;
6062
}
6163
exports.getEmittedFiles = getEmittedFiles;
64+
/**
65+
* This uses a dynamic import to load a module which may be ESM.
66+
* CommonJS code can load ESM code via a dynamic import. Unfortunately, TypeScript
67+
* will currently, unconditionally downlevel dynamic import into a require call.
68+
* require calls cannot load ESM code and will result in a runtime error. To workaround
69+
* this, a Function constructor is used to prevent TypeScript from changing the dynamic import.
70+
* Once TypeScript provides support for keeping the dynamic import this workaround can
71+
* be dropped.
72+
*
73+
* @param modulePath The path of the module to load.
74+
* @returns A Promise that resolves to the dynamically imported module.
75+
*/
76+
function loadEsmModule(modulePath) {
77+
return new Function('modulePath', `return import(modulePath);`)(modulePath);
78+
}
79+
async function getWebpackConfig(configPath) {
80+
if (!(0, fs_1.existsSync)(configPath)) {
81+
throw new Error(`Webpack configuration file ${configPath} does not exist.`);
82+
}
83+
switch (path.extname(configPath)) {
84+
case '.mjs':
85+
// Load the ESM configuration file using the TypeScript dynamic import workaround.
86+
// Once TypeScript provides support for keeping the dynamic import this workaround can be
87+
// changed to a direct dynamic import.
88+
return (await loadEsmModule((0, url_1.pathToFileURL)(configPath))).default;
89+
case '.cjs':
90+
return require(configPath);
91+
default:
92+
// The file could be either CommonJS or ESM.
93+
// CommonJS is tried first then ESM if loading fails.
94+
try {
95+
return require(configPath);
96+
}
97+
catch (e) {
98+
if (e.code === 'ERR_REQUIRE_ESM') {
99+
// Load the ESM configuration file using the TypeScript dynamic import workaround.
100+
// Once TypeScript provides support for keeping the dynamic import this workaround can be
101+
// changed to a direct dynamic import.
102+
return (await loadEsmModule((0, url_1.pathToFileURL)(configPath)))
103+
.default;
104+
}
105+
throw e;
106+
}
107+
}
108+
}
109+
exports.getWebpackConfig = getWebpackConfig;

src/webpack-dev-server/index.js

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,6 @@
66
* Use of this source code is governed by an MIT-style license that can be
77
* found in the LICENSE file at https://angular.io/license
88
*/
9-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
10-
if (k2 === undefined) k2 = k;
11-
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
12-
}) : (function(o, m, k, k2) {
13-
if (k2 === undefined) k2 = k;
14-
o[k2] = m[k];
15-
}));
16-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
17-
Object.defineProperty(o, "default", { enumerable: true, value: v });
18-
}) : function(o, v) {
19-
o["default"] = v;
20-
});
21-
var __importStar = (this && this.__importStar) || function (mod) {
22-
if (mod && mod.__esModule) return mod;
23-
var result = {};
24-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
25-
__setModuleDefault(result, mod);
26-
return result;
27-
};
289
var __importDefault = (this && this.__importDefault) || function (mod) {
2910
return (mod && mod.__esModule) ? mod : { "default": mod };
3011
};
@@ -99,5 +80,5 @@ function runWebpackDevServer(config, context, options = {}) {
9980
exports.runWebpackDevServer = runWebpackDevServer;
10081
exports.default = (0, architect_1.createBuilder)((options, context) => {
10182
const configPath = (0, path_1.resolve)(context.workspaceRoot, options.webpackConfig);
102-
return (0, rxjs_1.from)(Promise.resolve().then(() => __importStar(require(configPath)))).pipe((0, operators_1.switchMap)(({ default: config }) => runWebpackDevServer(config, context)));
83+
return (0, rxjs_1.from)((0, utils_1.getWebpackConfig)(configPath)).pipe((0, operators_1.switchMap)((config) => runWebpackDevServer(config, context)));
10384
});

src/webpack/index.js

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,6 @@
66
* Use of this source code is governed by an MIT-style license that can be
77
* found in the LICENSE file at https://angular.io/license
88
*/
9-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
10-
if (k2 === undefined) k2 = k;
11-
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
12-
}) : (function(o, m, k, k2) {
13-
if (k2 === undefined) k2 = k;
14-
o[k2] = m[k];
15-
}));
16-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
17-
Object.defineProperty(o, "default", { enumerable: true, value: v });
18-
}) : function(o, v) {
19-
o["default"] = v;
20-
});
21-
var __importStar = (this && this.__importStar) || function (mod) {
22-
if (mod && mod.__esModule) return mod;
23-
var result = {};
24-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
25-
__setModuleDefault(result, mod);
26-
return result;
27-
};
289
var __importDefault = (this && this.__importDefault) || function (mod) {
2910
return (mod && mod.__esModule) ? mod : { "default": mod };
3011
};
@@ -104,5 +85,5 @@ function runWebpack(config, context, options = {}) {
10485
exports.runWebpack = runWebpack;
10586
exports.default = (0, architect_1.createBuilder)((options, context) => {
10687
const configPath = (0, path_1.resolve)(context.workspaceRoot, options.webpackConfig);
107-
return (0, rxjs_1.from)(Promise.resolve().then(() => __importStar(require(configPath)))).pipe((0, operators_1.switchMap)(({ default: config }) => runWebpack(config, context)));
88+
return (0, rxjs_1.from)((0, utils_1.getWebpackConfig)(configPath)).pipe((0, operators_1.switchMap)((config) => runWebpack(config, context)));
10889
});

uniqueId

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Mon Jan 24 2022 15:17:58 GMT+0000 (Coordinated Universal Time)
1+
Mon Jan 24 2022 20:13:24 GMT+0000 (Coordinated Universal Time)

0 commit comments

Comments
 (0)