-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathscripts-webpack-plugin.ts
166 lines (139 loc) · 4.88 KB
/
scripts-webpack-plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// tslint:disable
// TODO: cleanup this file, it's copied as is from Angular CLI.
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { Compiler, loader } from 'webpack';
import { CachedSource, ConcatSource, OriginalSource, RawSource, Source } from 'webpack-sources';
import { interpolateName } from 'loader-utils';
import * as path from 'path';
const Chunk = require('webpack/lib/Chunk');
const EntryPoint = require('webpack/lib/Entrypoint');
export interface ScriptsWebpackPluginOptions {
name: string;
sourceMap: boolean;
scripts: string[];
filename: string;
basePath: string;
}
interface ScriptOutput {
filename: string;
source: CachedSource;
}
function addDependencies(compilation: any, scripts: string[]): void {
for (const script of scripts) {
compilation.fileDependencies.add(script);
}
}
function hook(compiler: any, action: (compilation: any, callback: (err?: Error) => void) => void) {
compiler.hooks.thisCompilation.tap('scripts-webpack-plugin', (compilation: any) => {
compilation.hooks.additionalAssets.tapAsync(
'scripts-webpack-plugin',
(callback: (err?: Error) => void) => action(compilation, callback),
);
});
}
export class ScriptsWebpackPlugin {
private _lastBuildTime?: number;
private _cachedOutput?: ScriptOutput;
constructor(private options: Partial<ScriptsWebpackPluginOptions> = {}) { }
shouldSkip(compilation: any, scripts: string[]): boolean {
if (this._lastBuildTime == undefined) {
this._lastBuildTime = Date.now();
return false;
}
for (let i = 0; i < scripts.length; i++) {
const scriptTime = compilation.fileTimestamps.get(scripts[i]);
if (!scriptTime || scriptTime > this._lastBuildTime) {
this._lastBuildTime = Date.now();
return false;
}
}
return true;
}
private _insertOutput(compilation: any, { filename, source }: ScriptOutput, cached = false) {
const chunk = new Chunk(this.options.name);
chunk.rendered = !cached;
chunk.id = this.options.name;
chunk.ids = [chunk.id];
chunk.files.push(filename);
const entrypoint = new EntryPoint(this.options.name);
entrypoint.pushChunk(chunk);
chunk.addGroup(entrypoint);
compilation.entrypoints.set(this.options.name, entrypoint);
compilation.chunks.push(chunk);
compilation.assets[filename] = source;
}
apply(compiler: Compiler): void {
if (!this.options.scripts || this.options.scripts.length === 0) {
return;
}
const scripts = this.options.scripts
.filter(script => !!script)
.map(script => path.resolve(this.options.basePath || '', script));
hook(compiler, (compilation, callback) => {
if (this.shouldSkip(compilation, scripts)) {
if (this._cachedOutput) {
this._insertOutput(compilation, this._cachedOutput, true);
}
addDependencies(compilation, scripts);
callback();
return;
}
const sourceGetters = scripts.map(fullPath => {
return new Promise<Source>((resolve, reject) => {
compilation.inputFileSystem.readFile(fullPath, (err: Error, data: Buffer) => {
if (err) {
reject(err);
return;
}
const content = data.toString();
let source;
if (this.options.sourceMap) {
// TODO: Look for source map file (for '.min' scripts, etc.)
let adjustedPath = fullPath;
if (this.options.basePath) {
adjustedPath = path.relative(this.options.basePath, fullPath);
}
source = new OriginalSource(content, adjustedPath);
} else {
source = new RawSource(content);
}
resolve(source);
});
});
});
Promise.all(sourceGetters)
.then(sources => {
const concatSource = new ConcatSource();
sources.forEach(source => {
concatSource.add(source);
concatSource.add('\n;');
});
const combinedSource = new CachedSource(concatSource);
const filename = interpolateName(
{ resourcePath: 'scripts.js' } as loader.LoaderContext,
this.options.filename as string,
{ content: combinedSource.source() },
);
const output = { filename, source: combinedSource };
this._insertOutput(compilation, output);
this._cachedOutput = output;
addDependencies(compilation, scripts);
callback();
})
.catch((err: Error) => callback(err));
});
}
}