-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathbuild.default.import-node-modules.spec.js
More file actions
230 lines (199 loc) · 8.62 KB
/
Copy pathbuild.default.import-node-modules.spec.js
File metadata and controls
230 lines (199 loc) · 8.62 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
* Use Case
* Run Greenwood with and loading different references to node_module types to ensure proper support.
* Sets prerender: true to validate the functionality.
*
* User Result
* Should generate a bare bones Greenwood build without erroring.
*
* User Command
* greenwood build
*
* User Config
* None
*
* User Workspace
* src/
* pages/
* index.html
* scripts/
* main.js
* styles/
* theme.css
*/
import chai from "chai";
import fs from "node:fs";
import glob from "glob-promise";
import { JSDOM } from "jsdom";
import path from "node:path";
import {
getOutputTeardownFiles,
getDependencyFiles,
HASH_REGEX,
} from "../../../../../test/utils.js";
import { Runner } from "gallinago";
import { fileURLToPath } from "node:url";
const expect = chai.expect;
describe("Build Greenwood With FIXED: ", function () {
const LABEL = "Importing packages from node modules FIXED";
const cliPath = path.join(process.cwd(), "packages/cli/src/bin.js");
const outputPath = fileURLToPath(new URL(".", import.meta.url));
let runner;
before(function () {
this.context = {
publicDir: path.join(outputPath, "public"),
};
runner = new Runner();
});
describe(LABEL, function () {
let dom;
before(async function () {
// this package has a known issue with import.meta.resolve
// in that it has no main, module, or exports so it has to be hoisted
// at least for this current version, as well as for testing relative ../node_modules references
// https://unpkg.com/browse/font-awesome@4.7.0/package.json
// https://github.com/FortAwesome/Font-Awesome/pull/19041
const fontAwesomePackageJson = await getDependencyFiles(
`${process.cwd()}/node_modules/font-awesome/package.json`,
`${outputPath}/node_modules/font-awesome/`,
);
const fontAwesomeCssFiles = await getDependencyFiles(
`${process.cwd()}/node_modules/font-awesome/css/*`,
`${outputPath}/node_modules/font-awesome/css/`,
);
const fontAwesomeFontFiles = await getDependencyFiles(
`${process.cwd()}/node_modules/font-awesome/fonts/*`,
`${outputPath}/node_modules/font-awesome/fonts/`,
);
try {
await runner.setup(outputPath, [
...fontAwesomePackageJson,
...fontAwesomeCssFiles,
...fontAwesomeFontFiles,
]);
} catch (error) {
console.error("Caught busy error in setup", JSON.stringify(error, null, 2));
throw error;
}
await new Promise((resolve) => setTimeout(resolve, 3000));
try {
await runner.runCommand(cliPath, "build");
} catch (error) {
console.error("Caught busy error in build", JSON.stringify(error, null, 2));
throw error;
}
dom = await JSDOM.fromFile(path.resolve(this.context.publicDir, "index.html"));
});
describe('<script src="..."> tag in the <head> tag', function () {
it('should have one <script src="..."> tag for main.js loaded in the <head> tag', function () {
const scriptTags = dom.window.document.querySelectorAll("head > script[src]");
const mainScriptTags = Array.prototype.slice.call(scriptTags).filter((script) => {
return /main.*.js/.test(script.src);
});
expect(mainScriptTags.length).to.be.equal(1);
});
it("should have the total expected number of .js file in the output directory", async function () {
expect(await glob.promise(path.join(this.context.publicDir, "*.js"))).to.have.lengthOf(3);
});
it("should have the expected main.js file in the output directory", async function () {
expect(await glob.promise(path.join(this.context.publicDir, "main.*.js"))).to.have.lengthOf(
1,
);
});
});
describe("<script> tag with inline code in the <head> tag", function () {
it("should have one <script> tag with inline code loaded in the <head> tag", function () {
const scriptTagsInline = Array.from(
dom.window.document.querySelectorAll("head > script:not([src])"),
).filter((tag) => !tag.getAttribute("data-gwd"));
expect(scriptTagsInline.length).to.be.equal(1);
});
it("should have the expected lit related files in the output directory", async function () {
expect(await glob.promise(path.join(this.context.publicDir, "lit*.js"))).to.have.lengthOf(
1,
);
});
it("should have the expected inline node_modules content in the first inline script", async function () {
const inlineScriptTag = Array.from(
dom.window.document.querySelectorAll("head > script:not([src])"),
).filter((tag) => !tag.getAttribute("data-gwd"))[0];
expect(inlineScriptTag.textContent.replace(/\n/g, "")).to.match(
new RegExp(
`import"\\/${HASH_REGEX}\\.${HASH_REGEX}\\.js";import"\\/lit-html\\.${HASH_REGEX}\\.js";\\/\\/# sourceMappingURL=${HASH_REGEX}\\.${HASH_REGEX}\\.js\\.map`,
),
);
});
});
describe('<script src="..."> with reference to node_modules/ path in the <head> tag', function () {
it('should have one <script src="..."> tag for lit-html loaded in the <head> tag', function () {
const scriptTagsInline = dom.window.document.querySelectorAll("head > script[src]");
const litScriptTags = Array.prototype.slice.call(scriptTagsInline).filter((script) => {
return /lit-.*.js/.test(script.src);
});
expect(litScriptTags.length).to.be.equal(1);
});
it("should have the expected lit-html.js files in the output directory", async function () {
expect(
await glob.promise(path.join(this.context.publicDir, "lit-html.*.js")),
).to.have.lengthOf(1);
});
});
describe('<link rel="stylesheet" href="..."> with reference to node_modules/ path in the <head> tag', function () {
it('should have one <link href="..."> tag in the <head> tag', function () {
const linkTags = dom.window.document.querySelectorAll('head > link[rel="stylesheet"]');
const prismLinkTag = Array.prototype.slice.call(linkTags).filter((link) => {
return /prism-tomorrow.*.css/.test(link.href);
});
expect(prismLinkTag.length).to.be.equal(1);
});
it("should have the expected prism.css file in the output directory", async function () {
expect(
await glob.promise(path.join(this.context.publicDir, "prism-tomorrow.*.css")),
).to.have.lengthOf(1);
});
});
describe('<link rel="stylesheet" href="..."> with reference to node_modules with bare @import paths in the <head> tag', function () {
it('should have one <link href="..."> tag in the <head> tag', function () {
const linkTags = dom.window.document.querySelectorAll('head > link[rel="stylesheet"]');
const themeLinkTag = Array.prototype.slice.call(linkTags).filter((link) => {
return /theme.*.css/.test(link.href);
});
expect(themeLinkTag.length).to.be.equal(1);
});
it("should have the expected theme.css file in the output directory with the expected content", async function () {
const themeFile = await glob.promise(
path.join(this.context.publicDir, "styles/theme.*.css"),
);
const contents = fs.readFileSync(themeFile[0], "utf-8");
expect(themeFile).to.have.lengthOf(1);
expect(
contents.indexOf(
":root,:host{--spectrum-global-animation-linear:cubic-bezier(0, 0, 1, 1);",
),
).to.equal(0);
});
});
describe('<link rel="stylesheet" href="..."> with reference to transient relative node_modules url(...) references', function () {
it("should have the expected number of font files referenced in vendor CSS file in the output directory", async function () {
expect(
await glob.promise(
path.join(this.context.publicDir, "node-modules/font-awesome/fonts/*"),
),
).to.have.lengthOf(5);
});
it("should have the expected url link for the bundled font-awesome file", async function () {
const themeFile = await glob.promise(
path.join(this.context.publicDir, "styles/theme.*.css"),
);
const contents = fs.readFileSync(themeFile[0], "utf-8");
const contentsRegex = new RegExp(
`@font-face\\s*\\{font-family:'FontAwesome';src:url\\('/node-modules/font-awesome/fonts/fontawesome-webfont\\.${HASH_REGEX}\\.eot\\?v=4\\.7\\.0'\\);`,
);
expect(contents).to.match(contentsRegex);
});
});
});
after(async function () {
await runner.teardown(getOutputTeardownFiles(outputPath));
});
});