-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
modern.js
121 lines (107 loc) · 3.67 KB
/
modern.js
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
/*
* This file is based on @vue/cli-service (MIT) ModernModePlugin
* https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/webpack/ModernModePlugin.js
*/
import EventEmitter from 'events'
import HtmlWebpackPlugin from 'html-webpack-plugin'
import { safariNoModuleFix } from '@nuxt/utils'
const legacyTemplateTags = {}
const legacyTemplateWatcher = new EventEmitter()
export default class ModernModePlugin {
constructor ({ targetDir, isModernBuild, noUnsafeInline }) {
this.targetDir = targetDir
this.isModernBuild = isModernBuild
this.noUnsafeInline = noUnsafeInline
}
apply (compiler) {
if (!this.isModernBuild) {
this.applyLegacy(compiler)
} else {
this.applyModern(compiler)
}
}
getLegacyTemplateTags (name) {
return new Promise((resolve) => {
const tags = legacyTemplateTags[name]
if (tags) {
return resolve(tags)
}
return legacyTemplateWatcher.once(name, () => {
const tags = legacyTemplateTags[name]
return tags && resolve(tags)
})
})
}
applyLegacy (compiler) {
const ID = 'nuxt-legacy-bundle'
compiler.hooks.compilation.tap(ID, (compilation) => {
HtmlWebpackPlugin.getHooks(compilation).alterAssetTagGroups.tap(ID, (data) => {
HtmlWebpackPlugin.getHooks(compilation).afterEmit.tap(ID, ({ outputName }) => {
// get stats, write to disk
legacyTemplateTags[data.plugin.options.filename] = data.bodyTags
legacyTemplateWatcher.emit(outputName)
})
return data
})
})
}
applyModern (compiler) {
const ID = 'nuxt-modern-bundle'
compiler.hooks.compilation.tap(ID, (compilation) => {
HtmlWebpackPlugin.getHooks(compilation).alterAssetTagGroups.tapPromise(ID, async (data) => {
// use <script type="module"> for modern assets
data.bodyTags.forEach((tag) => {
if (tag.tagName === 'script' && tag.attributes) {
tag.attributes.type = 'module'
}
})
// use <link rel="modulepreload"> instead of <link rel="preload">
// for modern assets
data.headTags.forEach((tag) => {
if (tag.tagName === 'link' &&
tag.attributes.rel === 'preload' &&
tag.attributes.as === 'script') {
tag.attributes.rel = 'modulepreload'
}
})
// inject links for legacy assets as <script nomodule>
const fileName = data.plugin.options.filename
const legacyScriptTags = (await this.getLegacyTemplateTags(fileName))
.filter(a => a.tagName === 'script' && a.attributes)
for (const a of legacyScriptTags) {
a.attributes.nomodule = true
data.bodyTags.push(a)
}
if (this.noUnsafeInline) {
// inject the fix as an external script
const safariFixFilename = 'safari-nomodule-fix.js'
const safariFixPath = legacyScriptTags[0].attributes.src
.split('/')
.slice(0, -1)
.concat([safariFixFilename])
.join('/')
compilation.assets[safariFixFilename] = {
source: () => Buffer.from(safariNoModuleFix),
size: () => Buffer.byteLength(safariNoModuleFix)
}
data.bodyTags.push({
tagName: 'script',
closeTag: true,
attributes: {
src: safariFixPath
}
})
} else {
// inject Safari 10 nomodule fix
data.bodyTags.push({
tagName: 'script',
closeTag: true,
innerHTML: safariNoModuleFix
})
}
delete legacyTemplateTags[fileName]
return data
})
})
}
}