2017-06-19 16:35:05 +08:00
|
|
|
/* global describe, it */
|
2017-01-18 21:53:20 +08:00
|
|
|
"use strict";
|
2013-02-14 00:00:07 +08:00
|
|
|
|
2017-01-18 21:53:20 +08:00
|
|
|
const path = require("path");
|
|
|
|
const webpack = require("../lib/webpack");
|
2013-02-14 00:00:07 +08:00
|
|
|
|
2017-01-18 21:53:20 +08:00
|
|
|
describe("NodeTemplatePlugin", () => {
|
|
|
|
|
|
|
|
it("should compile and run a simple module", (done) => {
|
2013-02-14 00:00:07 +08:00
|
|
|
webpack({
|
2017-11-21 17:41:01 +08:00
|
|
|
mode: "production",
|
2013-02-14 00:00:07 +08:00
|
|
|
context: path.join(__dirname, "fixtures", "nodetest"),
|
|
|
|
target: "node",
|
|
|
|
output: {
|
2018-02-25 08:20:45 +08:00
|
|
|
path: path.join(__dirname, "js", "NodeTemplatePlugin"),
|
2013-02-14 00:00:07 +08:00
|
|
|
filename: "result.js",
|
2013-06-28 20:20:11 +08:00
|
|
|
chunkFilename: "[hash].result.[id].js",
|
|
|
|
library: "abc",
|
|
|
|
libraryTarget: "commonjs",
|
2013-02-14 00:00:07 +08:00
|
|
|
},
|
2017-12-13 23:05:21 +08:00
|
|
|
entry: "./entry"
|
2017-01-18 21:53:20 +08:00
|
|
|
}, (err, stats) => {
|
2013-02-14 00:00:07 +08:00
|
|
|
if(err) return err;
|
2018-01-24 23:00:43 +08:00
|
|
|
expect(stats.hasErrors()).toBe(false);
|
|
|
|
expect(stats.hasWarnings()).toBe(false);
|
2017-11-16 14:06:30 +08:00
|
|
|
// eslint-disable-next-line node/no-missing-require
|
2018-02-25 08:20:45 +08:00
|
|
|
const result = require("./js/NodeTemplatePlugin/result").abc;
|
2018-01-24 23:00:43 +08:00
|
|
|
expect(result.nextTick).toBe(process.nextTick);
|
|
|
|
expect(result.fs).toBe(require("fs"));
|
2017-01-18 21:53:20 +08:00
|
|
|
result.loadChunk(456, (chunk) => {
|
2018-01-24 23:00:43 +08:00
|
|
|
expect(chunk).toBe(123);
|
2017-01-18 21:53:20 +08:00
|
|
|
result.loadChunk(567, (chunk) => {
|
2018-01-24 23:00:43 +08:00
|
|
|
expect(chunk).toEqual({
|
2015-08-09 18:42:43 +08:00
|
|
|
a: 1
|
|
|
|
});
|
2013-02-14 00:00:07 +08:00
|
|
|
done();
|
|
|
|
});
|
2013-06-28 20:20:11 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-01-18 21:53:20 +08:00
|
|
|
it("should compile and run a simple module in single mode", (done) => {
|
2013-06-28 20:20:11 +08:00
|
|
|
webpack({
|
2017-11-21 17:41:01 +08:00
|
|
|
mode: "production",
|
2013-06-28 20:20:11 +08:00
|
|
|
context: path.join(__dirname, "fixtures", "nodetest"),
|
|
|
|
target: "node",
|
|
|
|
output: {
|
2018-02-25 08:20:45 +08:00
|
|
|
path: path.join(__dirname, "js", "NodeTemplatePluginSingle"),
|
2013-06-28 20:20:11 +08:00
|
|
|
filename: "result2.js",
|
|
|
|
chunkFilename: "[hash].result2.[id].js",
|
|
|
|
library: "def",
|
|
|
|
libraryTarget: "umd",
|
2016-02-27 06:50:04 +08:00
|
|
|
auxiliaryComment: "test"
|
2013-06-28 20:20:11 +08:00
|
|
|
},
|
|
|
|
entry: "./entry",
|
2013-12-18 06:21:49 +08:00
|
|
|
plugins: [
|
2015-08-09 18:42:43 +08:00
|
|
|
new webpack.optimize.LimitChunkCountPlugin({
|
|
|
|
maxChunks: 1
|
2017-12-13 23:05:21 +08:00
|
|
|
})
|
2013-12-18 06:21:49 +08:00
|
|
|
]
|
2017-01-18 21:53:20 +08:00
|
|
|
}, (err, stats) => {
|
2013-06-28 20:20:11 +08:00
|
|
|
if(err) return err;
|
2018-01-24 23:00:43 +08:00
|
|
|
expect(stats.hasErrors()).toBe(false);
|
2017-11-16 14:06:30 +08:00
|
|
|
// eslint-disable-next-line node/no-missing-require
|
2018-02-25 08:20:45 +08:00
|
|
|
const result = require("./js/NodeTemplatePluginSingle/result2");
|
2018-01-24 23:00:43 +08:00
|
|
|
expect(result.nextTick).toBe(process.nextTick);
|
|
|
|
expect(result.fs).toBe(require("fs"));
|
2017-01-18 21:53:20 +08:00
|
|
|
const sameTick = true;
|
|
|
|
result.loadChunk(456, (chunk) => {
|
2018-01-24 23:00:43 +08:00
|
|
|
expect(chunk).toBe(123);
|
|
|
|
expect(sameTick).toBe(true);
|
2017-01-18 21:53:20 +08:00
|
|
|
result.loadChunk(567, (chunk) => {
|
2018-01-24 23:00:43 +08:00
|
|
|
expect(chunk).toEqual({
|
2015-08-09 18:42:43 +08:00
|
|
|
a: 1
|
|
|
|
});
|
2013-06-28 20:20:11 +08:00
|
|
|
done();
|
|
|
|
});
|
2013-02-14 00:00:07 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|