Skip to content

Commit 59f534f

Browse files
committed
build: refactor to es modules
1 parent 7b178ed commit 59f534f

5 files changed

Lines changed: 65 additions & 59 deletions

File tree

.github/workflows/build-deploy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ jobs:
3939
git config user.name "igniteui-deploy"
4040
git add --all
4141
if [ -n "$(git status --porcelain)" ]; then git commit -m "Samples for ${{ github.ref_name }}"; fi
42-
git push -fq
42+
git push -fq
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
var path = require("path"),
2-
cheerio = require("cheerio"),
3-
through = require("through2"),
4-
File = require('vinyl');
1+
import cheerio from 'cheerio';
2+
import { dirname, join, posix } from "path";
3+
import { obj } from "through2";
4+
import File from 'vinyl';
55
/**
66
* Extracts HTML, JS and optionally CSS into separate files in a fiddle folder
77
* and generates an embed json config file
88
*/
9-
module.exports = function(options) {
9+
export default function(options) {
1010

1111
var replaceSrc = function ($) {
1212
var attr = this.name === "script" ? "src" : "href",
@@ -64,25 +64,25 @@ module.exports = function(options) {
6464

6565
var processStream = function(file, encoding, next){
6666
var contents, stream, $,
67-
basePath = path.dirname(file.path),
67+
basePath = dirname(file.path),
6868
relativePath = basePath.split("HTMLSamples").pop().replace(/\\/g, "/"),
6969
originalPath = "HTMLSamples" + file.originalPath.split("HTMLSamples").pop().replace(/\\/g, "/"),
7070
htmlFile, jsFile, cssFile,
7171
html = [], js = "", css = "",
7272
resultStr = file.lang === "ja" ? options.strings.resultJA : options.strings.resultEN,
7373
embed = {
7474
//use original file path for source link
75-
"srcUrlPattern" : path.posix.join("/${owner}/${repo}-src/blob/", options.version, originalPath),
75+
"srcUrlPattern" : posix.join("/${owner}/${repo}-src/blob/", options.version, originalPath),
7676
"embed": [{
7777
"label": "JS",
78-
"path": path.posix.join(options.version, relativePath, "fiddle/demo.js")
78+
"path": posix.join(options.version, relativePath, "fiddle/demo.js")
7979
},{
8080
"label": "HTML",
81-
"path": path.posix.join(options.version, relativePath, "fiddle/demo.html")
81+
"path": posix.join(options.version, relativePath, "fiddle/demo.html")
8282
}, {
8383
"type": "htmlpage",
8484
"label": resultStr,
85-
"url": options.liveUrl + path.posix.join("/" + options.version, relativePath, "/index.html")
85+
"url": options.liveUrl + posix.join("/" + options.version, relativePath, "/index.html")
8686
}]
8787
};
8888

@@ -118,7 +118,7 @@ module.exports = function(options) {
118118

119119
htmlFile = new File({
120120
base: file.base,
121-
path: path.join(basePath, "fiddle", "demo.html"),
121+
path: join(basePath, "fiddle", "demo.html"),
122122
contents: Buffer.from(html.join("\r\n"))
123123
});
124124

@@ -138,7 +138,7 @@ module.exports = function(options) {
138138
}
139139
jsFile = new File({
140140
base: file.base,
141-
path: path.join(basePath, "fiddle", "demo.js"),
141+
path: join(basePath, "fiddle", "demo.js"),
142142
contents: Buffer.from(js)
143143
});
144144

@@ -149,13 +149,13 @@ module.exports = function(options) {
149149
if (css.length) {
150150
cssFile = new File({
151151
base: file.base,
152-
path: path.join(basePath, "fiddle", "demo.css"),
152+
path: join(basePath, "fiddle", "demo.css"),
153153
contents: Buffer.from(unindentTrim(css))
154154
});
155155
stream.push(cssFile);
156156
embed.embed.splice(2, 0, {
157157
"label": "CSS",
158-
"path": path.posix.join(options.version, relativePath, "fiddle/demo.css")
158+
"path": posix.join(options.version, relativePath, "fiddle/demo.css")
159159
});
160160
}
161161

@@ -166,13 +166,13 @@ module.exports = function(options) {
166166
stream.push(jsFile);
167167
stream.push(new File({
168168
base: file.base,
169-
path: path.join(basePath, ".gh-embed.json"),
169+
path: join(basePath, ".gh-embed.json"),
170170
contents: Buffer.from(JSON.stringify(embed, null, 4))
171171
}));
172172

173173
stream.push(file);
174174
next();
175175
};
176176

177-
return through.obj(processStream);
178-
};
177+
return obj(processStream);
178+
};
Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,50 @@
1-
var path = require("path"),
2-
through = require("through2"),
3-
File = require('vinyl');
1+
import { createRequire } from "module";
2+
import { basename, dirname, extname, join, sep } from "path";
3+
import { obj } from "through2";
4+
import File from 'vinyl';
5+
6+
const require = createRequire(import.meta.url);
47

58
/**
69
* Localize contents of file (resources, localization strings) and push both versions back to stream
710
*/
8-
module.exports = function(options) {
11+
export default function(_options) {
912

1013
var processStream = function(file, encoding, next){
1114
var contentsEN, contentsJA, jaFile, token,
12-
basePath = path.dirname(file.path),
13-
fileName = path.basename(file.path, path.extname(file.path)),
15+
basePath = dirname(file.path),
16+
fileName = basename(file.path, extname(file.path)),
1417
enStrings, jaStrings,
1518
stream = this;
1619
contentsEN = contentsJA = file.contents.toString(encoding);
1720

18-
enStrings = require(path.join(basePath, "strings-en.json"));
19-
jaStrings = require(path.join(basePath, "strings-ja.json"));
21+
enStrings = require(join(basePath, "strings-en.json"));
22+
jaStrings = require(join(basePath, "strings-ja.json"));
2023
// combine with shared strings
21-
Object.assign(enStrings, require(path.join(basePath, "../strings-en.json")));
22-
Object.assign(jaStrings, require(path.join(basePath, "../strings-ja.json")));
24+
Object.assign(enStrings, require(join(basePath, "../strings-en.json")));
25+
Object.assign(jaStrings, require(join(basePath, "../strings-ja.json")));
2326

24-
for (key in enStrings) {
27+
for (const key in enStrings) {
2528
token = new RegExp("\\$\\$\\(" + key + "\\)", "g");
2629
contentsEN = contentsEN.replace(token, enStrings[key]);
2730
}
2831

29-
for (key in jaStrings) {
32+
for (const key in jaStrings) {
3033
token = new RegExp("\\$\\$\\(" + key + "\\)", "g");
3134
contentsJA = contentsJA.replace(token, jaStrings[key]);
3235
}
3336

3437
// save original path, because hystory stack won't be available for the newly created JA file:
3538
file.originalPath = file.history[0];
3639
// change out path, HTMLSamples must remain in path to keep relative correct for dest
37-
file.path = path.join(basePath.replace("HTMLSamples", "HTMLSamples" + path.sep + "EN"), fileName, "index.html");
40+
file.path = join(basePath.replace("HTMLSamples", "HTMLSamples" + sep + "EN"), fileName, "index.html");
3841
file.contents = Buffer.from(contentsEN, encoding);
3942
file.lang = "en";
4043

4144
// replace JA
4245
jaFile = new File({
4346
base: file.base,
44-
path: path.join(basePath.replace("HTMLSamples", "HTMLSamples" + path.sep + "JA"), fileName, "index.html"),
47+
path: join(basePath.replace("HTMLSamples", "HTMLSamples" + sep + "JA"), fileName, "index.html"),
4548
contents: Buffer.from(contentsJA)
4649
});
4750
jaFile.lang = "ja";
@@ -52,5 +55,5 @@ module.exports = function(options) {
5255
next();
5356
};
5457

55-
return through.obj(processStream);
56-
};
58+
return obj(processStream);
59+
};
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
var fs = require("fs"),
2-
cheerio = require("cheerio"),
3-
through = require("through2");
1+
import cheerio from 'cheerio';
2+
import { existsSync } from "fs";
3+
import { obj } from "through2";
44

55
/**
66
* Replace resource strings (src, href) and igLoader sources. Adds Japanese locale script for JA files.
77
*/
8-
module.exports = function (options) {
8+
export default function (options) {
99

1010
/**
1111
* Replaces %%resource%% links based on configuration. Swaps to Japanese data-file if available for localized files.
@@ -23,7 +23,7 @@ module.exports = function (options) {
2323
if (lang === "ja" && src.indexOf("../data-files") !== -1) {
2424
// check if respective japanese files is available:
2525
dataFile = src.split("../data-files/").pop();
26-
if (fs.existsSync("./data-files-ja/" + dataFile)) {
26+
if (existsSync("./data-files-ja/" + dataFile)) {
2727
src = src.replace("/data-files/", "/data-files-ja/");
2828
}
2929
}
@@ -88,5 +88,5 @@ module.exports = function (options) {
8888
next();
8989
};
9090

91-
return through.obj(processStream);
92-
};
91+
return obj(processStream);
92+
};

gulpfile.js renamed to gulpfile.mjs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1-
var gulp = require("gulp"),
2-
localize = require("./build/localizeFiles.js"),
3-
updateResources = require("./build/updateResources.js"),
4-
createFiddleFiles = require("./build/createFiddleFiles.js");
5-
6-
var argv = require('minimist')(process.argv.slice(2));
7-
var version = argv["ignite-version"]; //should be passed as --ignite-version ${BRANCH}
8-
9-
var dist = "./dist",
10-
copySrc = [
11-
"./css/**/*",
12-
"./js/**/*",
13-
"./images/**/*",
14-
"./data-files/**/*",
15-
"./data-files-ja/**/*"
16-
],
17-
config = require("./build/config.json");
1+
import gulp from "gulp";
2+
import minimist from "minimist";
3+
import { createRequire } from "module";
4+
import createFiddleFiles from "./build/createFiddleFiles.mjs";
5+
import localize from "./build/localizeFiles.mjs";
6+
import updateResources from "./build/updateResources.mjs";
7+
8+
const require = createRequire(import.meta.url);
9+
const argv = minimist(process.argv.slice(2));
10+
let version = argv["ignite-version"]; //should be passed as --ignite-version ${BRANCH}
11+
12+
let dist = "./dist";
13+
const copySrc = [
14+
"./css/**/*",
15+
"./js/**/*",
16+
"./images/**/*",
17+
"./data-files/**/*",
18+
"./data-files-ja/**/*"
19+
];
20+
const config = require("./build/config.json");
1821

1922
// Command line overrides:
2023
if (argv["ignite-ui"]) {
@@ -51,4 +54,4 @@ gulp.task("copy-static-files", () => {
5154
.pipe(gulp.dest("./dist"));
5255
});
5356

54-
gulp.task("build-samples", gulp.series("process-files", "copy-static-files"));
57+
gulp.task("build-samples", gulp.series("process-files", "copy-static-files"));

0 commit comments

Comments
 (0)