@gouvfr/dsfr-alchemist 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ Le contenu de ce dépôt est placé sous licence MIT License, à l'exception de la fonte Marianne.
2
+ Voir les [conditions générales d'utilisation](doc/legal/cgu.md)
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ of this software and associated documentation files (the "Software"), to deal
6
+ in the Software without restriction, including without limitation the rights
7
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the Software is
9
+ furnished to do so, subject to the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included in all
12
+ copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ SOFTWARE.
21
+
package/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # 🇫🇷 Outil de compilation du Système de Design de l’État
2
+
3
+ [![GitHub release](https://img.shields.io/github/v/release/gouvernementFR/dsfr.svg)](https://GitHub.com/gouvernementFR/dsfr/releases/) [![Generic badge](https://img.shields.io/badge/npm-yellow.svg)](https://www.npmjs.com/package/@gouvfr/dsfr-alchemist) [![Generic badge](https://img.shields.io/badge/license-grey.svg)](https://github.com/GouvernementFR/dsfr/blob/main/LICENSE.md) [![Npm package monthly downloads](https://badgen.net/npm/dm/@gouvfr/dsfr-alchemist)](https://npmjs.com/package/@gouvfr/dsfr-alchemist)
4
+
5
+ Le Système de Design de l’État (ci-après, le **DSFR**) est un ensemble de composants web HTML, CSS et Javascript pour faciliter le travail des équipes projets des sites Internet publics, et créer des interfaces numériques de qualité et accessibles.
6
+
7
+ Le module `dsfr-alchemist` est un outil permettant de compiler les feuilles de style et les scripts du DSFR. Il est destiné à un usage interne par les équipes du Service d'Information du Gouvernement (SIG).
8
+
9
+ ## Licence et droit d'utilisation
10
+
11
+ Le contenu de ce projet est placé sous licence MIT License. Voir [LICENSE.md](https://github.com/GouvernementFR/dsfr/blob/main/LICENSE.md).
package/index.js ADDED
@@ -0,0 +1,4 @@
1
+ import { ScriptCompiler } from './src/script/script-compiler.js';
2
+ import { StyleCompiler } from './src/style/style-compiler.js';
3
+
4
+ export { ScriptCompiler, StyleCompiler };
package/package.json ADDED
@@ -0,0 +1,62 @@
1
+ {
2
+ "name": "@gouvfr/dsfr-alchemist",
3
+ "version": "1.0.0",
4
+ "description": "Le module `dsfr-alchemist` est un outil permettant de compiler les feuilles de style et les scripts du DSFR.",
5
+ "keywords": [
6
+ "Système de Design de l'État",
7
+ "gouvfr",
8
+ "dsfr",
9
+ "compiler"
10
+ ],
11
+ "homepage": "https://github.com/GouvernementFR/dsfr/tree/main/modules/alchemist",
12
+ "bugs": {
13
+ "url": "https://github.com/GouvernementFR/dsfr/issues"
14
+ },
15
+ "license": "SEE LICENSE IN LICENSE.md",
16
+ "author": {
17
+ "name": "Service d'Information du Gouvernement",
18
+ "email": "maxime.beaugrand@pm.gouv.fr",
19
+ "url": "https://www.systeme-de-design.gouv.fr/"
20
+ },
21
+ "contributors": [
22
+ {
23
+ "name": "Bertrand Larrieu",
24
+ "email": "bertrand.larrieu@actimage.com"
25
+ },
26
+ {
27
+ "name": "Keryan Sanié",
28
+ "email": "keryan.sanie@actimage.com"
29
+ },
30
+ {
31
+ "name": "Baptiste Zeller",
32
+ "email": "baptiste.zeller@actimage.com"
33
+ }
34
+ ],
35
+ "repository": {
36
+ "type": "git",
37
+ "url": "git+ssh://git@github.com/GouvernementFR/dsfr.git",
38
+ "directory": "./modules/shared/compiler"
39
+ },
40
+ "publishConfig": {
41
+ "access": "public"
42
+ },
43
+ "type": "module",
44
+ "engines": {
45
+ "node": ">=22.14.0"
46
+ },
47
+ "packageManager": "yarn@4.5.2+sha224.c2e2e9ed3cdadd6ec250589b3393f71ae56d5ec297af11cec1eba3b4",
48
+ "files": [
49
+ "index.js",
50
+ "src/",
51
+ "../../../legal/*",
52
+ "../../../SECURITY.md",
53
+ "../../../LICENSE.md"
54
+ ],
55
+ "main": "./index.js",
56
+ "dependencies": {
57
+ "@gouvfr/dsfr-forge": "=1.0.0",
58
+ "esbuild": "^0.24.2",
59
+ "lightningcss": "^1.29.1",
60
+ "sass": "^1.79.3"
61
+ }
62
+ }
@@ -0,0 +1,40 @@
1
+ import { log } from '@gouvfr/dsfr-forge';
2
+ import * as esbuild from 'esbuild';
3
+ import fs from 'fs'
4
+
5
+ class ScriptCompiler {
6
+ async compile (src, dest, filename, options = { minify: false, sourceMap: false, banner: undefined}) {
7
+ const { minify, sourceMap , banner} = options;
8
+
9
+ await this._build(src, dest, filename, false, sourceMap, banner);
10
+
11
+ if (minify) {
12
+ await this._build(src, dest, filename, true, sourceMap, banner);
13
+ }
14
+ }
15
+
16
+ async _build (src, dest, filename, minify = false, sourceMap = false, banner = undefined) {
17
+ const appendix = minify ? '.min.js' : '.js';
18
+ const bannerOpt = banner ? { js: banner } : undefined;
19
+
20
+ const options = {
21
+ entryPoints: [src],
22
+ bundle: true,
23
+ minify: minify,
24
+ format: 'esm',
25
+ sourcemap: sourceMap,
26
+ outfile: `${dest}/${filename}${appendix}`,
27
+ banner: bannerOpt
28
+ };
29
+
30
+ try {
31
+ await esbuild.build(options);
32
+ log.file(options.outfile, `${fs.statSync(options.outfile)?.size} bytes`);
33
+ }
34
+ catch (error) {
35
+ log.error(error);
36
+ }
37
+ }
38
+ }
39
+
40
+ export { ScriptCompiler };
@@ -0,0 +1,100 @@
1
+ import * as sass from 'sass';
2
+ import path from 'path';
3
+ import * as url from 'node:url'
4
+ import { createFile, log } from '@gouvfr/dsfr-forge'
5
+ import { transform } from 'lightningcss';
6
+
7
+ class StyleCompiler {
8
+ _getDestFile (dest, filename, minify = false, sourceMap = false) {
9
+ const appendices = ['css'];
10
+ if (minify) appendices.unshift('min');
11
+ if (sourceMap) appendices.push('map');
12
+ const appendix = appendices.join('.');
13
+ return `${dest}/${filename}.${appendix}`;
14
+ }
15
+
16
+ async compile (src, dest, filename, options = { minify: false, sourceMap: false, banner: undefined, loadPaths: [] }) {
17
+ const { minify, sourceMap, banner, loadPaths } = options;
18
+
19
+ const outputOptions = {
20
+ outFile: this._getDestFile(dest, filename),
21
+ style: 'expanded',
22
+ loadPaths: [
23
+ '.',
24
+ ...loadPaths,
25
+ ]
26
+ };
27
+
28
+ if (sourceMap) {
29
+ outputOptions.sourceMap = true;
30
+ outputOptions.sourceMapIncludeSources = true;
31
+ }
32
+
33
+ const output= await this._compileSass(src, outputOptions);
34
+
35
+ const css = banner ? `${banner}\n\n${output.css}`: output.css;
36
+
37
+ let outputSourceMap;
38
+
39
+ if (output.sourceMap) {
40
+ const relativeSourceMap = {
41
+ version: output.sourceMap.version,
42
+ sources: output.sourceMap.sources.map(source => path.relative(process.cwd(), url.fileURLToPath(source))),
43
+ mappings: output.sourceMap.mappings,
44
+ file: output.sourceMap.file,
45
+ sourceRoot: output.sourceMap.sourceRoot,
46
+ names: output.sourceMap.names,
47
+ sourcesContent: output.sourceMap.sourcesContent
48
+ };
49
+ outputSourceMap = JSON.stringify(relativeSourceMap);
50
+ }
51
+
52
+ await this._create(dest, filename, false, outputSourceMap, css);
53
+
54
+ if (minify) await this._minify(dest, filename, true, outputSourceMap, css);
55
+ }
56
+
57
+ async _compileSass (path, options) {
58
+ try {
59
+ return await sass.compileAsync(path, options);
60
+ } catch (e) {
61
+ log.error(e.message);
62
+ try {
63
+ process.kill(0);
64
+ } catch (e) {
65
+ return null;
66
+ }
67
+ }
68
+ }
69
+
70
+ async _minify (dest, filename, minify, sourceMap, css) {
71
+ const options = {
72
+ filename: filename,
73
+ code: Buffer.from(css),
74
+ minify: true
75
+ };
76
+
77
+ if (sourceMap) {
78
+ options.inputSourceMap = sourceMap;
79
+ options.sourceMap = true;
80
+ }
81
+
82
+ let { code, map } = transform(options);
83
+
84
+ await this._create(dest, filename, minify, map, code);
85
+ }
86
+
87
+ async _create (dest, filename, minify, sourceMap, css) {
88
+ const appendices = ['css'];
89
+ if (minify) appendices.unshift('min');
90
+ const appendix = appendices.join('.');
91
+ const to = `${dest}/${filename}.${appendix}`;
92
+
93
+ const size = createFile(to, css, true);
94
+ log.file(to, `${size} bytes`);
95
+
96
+ if (sourceMap) createFile(`${to}.map`, sourceMap, true);
97
+ }
98
+ }
99
+
100
+ export { StyleCompiler };