Skip to content

Commit 9e42dd6

Browse files
committed
allow to pass AST directly to webpack
1 parent 122e7dc commit 9e42dd6

7 files changed

Lines changed: 43 additions & 3 deletions

File tree

lib/NormalModule.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,16 @@ class NormalModule extends Module {
195195

196196
const resourceBuffer = result.resourceBuffer;
197197
const source = result.result[0];
198-
const sourceMap = result.result[1];
198+
const sourceMap = result.result.length >= 1 ? result.result[1] : null;
199+
const extraInfo = result.result.length >= 2 ? result.result[2] : null;
199200

200201
if(!Buffer.isBuffer(source) && typeof source !== "string") {
201202
const error = new ModuleBuildError(this, new Error("Final loader didn't return a Buffer or String"));
202203
return callback(error);
203204
}
204205

205206
this._source = this.createSource(asString(source), resourceBuffer, sourceMap);
207+
this._ast = typeof extraInfo === "object" && extraInfo !== null && extraInfo.webpackAST !== undefined ? extraInfo.webpackAST : null;
206208
return callback();
207209
});
208210
}
@@ -217,6 +219,7 @@ class NormalModule extends Module {
217219
this.error = error;
218220
this.errors.push(this.error);
219221
this._source = new RawSource("throw new Error(" + JSON.stringify(this.error.message) + ");");
222+
this._ast = null;
220223
}
221224

222225
applyNoParseRule(rule, content) {
@@ -264,6 +267,7 @@ class NormalModule extends Module {
264267
this.buildTimestamp = Date.now();
265268
this.built = true;
266269
this._source = null;
270+
this._ast = null;
267271
this.error = null;
268272
this.errors.length = 0;
269273
this.warnings.length = 0;
@@ -289,7 +293,7 @@ class NormalModule extends Module {
289293
}
290294

291295
try {
292-
this.parser.parse(this._source.source(), {
296+
this.parser.parse(this._ast || this._source.source(), {
293297
current: this,
294298
module: this,
295299
compilation: compilation,

lib/Parser.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1376,7 +1376,11 @@ class Parser extends Tapable {
13761376

13771377
parse(source, initialState) {
13781378
let ast;
1379-
const comments = [];
1379+
let comments = [];
1380+
if(typeof source === "object" && source !== null) {
1381+
ast = source;
1382+
comments = source.comments;
1383+
}
13801384
for(let i = 0, len = POSSIBLE_AST_OPTIONS.length; i < len; i++) {
13811385
if(!ast) {
13821386
try {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"use strict";
2+
3+
const acorn = require("acorn-dynamic-import").default;
4+
5+
module.exports = function(source) {
6+
const comments = [];
7+
const ast = acorn.parse(source, {
8+
ranges: true,
9+
locations: true,
10+
ecmaVersion: 2017,
11+
sourceType: "module",
12+
plugins: {
13+
dynamicImport: true
14+
},
15+
onComment: comments
16+
});
17+
18+
// change something to test if it's really used
19+
ast.body[0].expression.right.arguments[0].value = "./ok";
20+
ast.body[0].expression.right.arguments[0].raw = "\"./ok\"";
21+
22+
ast.comments = comments;
23+
this.callback(null, source, null, {
24+
webpackAST: ast
25+
});
26+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
it("should be able to process AST from loader", function() {
2+
require("./ast-loader!./module").should.be.eql("ok");
3+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require("./wrong");
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "ok";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "wrong";

0 commit comments

Comments
 (0)