ember-scoped-css 0.5.0 → 0.7.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.
Files changed (35) hide show
  1. package/dist/app-css-loader.cjs +10 -10
  2. package/dist/app-dependency-loader.cjs +1 -1
  3. package/index.js +2 -8
  4. package/package.json +6 -1
  5. package/src/app-css-livereload-loader.js +2 -2
  6. package/src/app-css-loader.js +4 -4
  7. package/src/app-dependency-loader.js +1 -1
  8. package/src/app-js-unplugin.js +4 -4
  9. package/src/app-scopedcss-webpack.js +5 -5
  10. package/src/babel-plugin-scoped-class.js +1 -1
  11. package/src/{getPostfix.js → lib/generateAbsolutePathHash.js} +1 -1
  12. package/src/{replaceScopedClass.js → lib/replaceScopedClass.js} +2 -2
  13. package/src/scoped-css-unplugin.js +116 -0
  14. package/src/scopedClass.js +2 -2
  15. package/src/template-lint/{scoped-class-helper-plugin.js → plugin.js} +1 -1
  16. package/test/{getPostfix.js → generateHash.js} +5 -5
  17. package/test/getClassesTagsFromCss.js +1 -1
  18. package/test/rewriteCss.js +1 -1
  19. package/test/{scoped-class-helper-lint.js → template-lint-rule.js} +1 -1
  20. package/src/addon-css-rollup.js +0 -86
  21. package/src/addon-hbs-rollup.js +0 -46
  22. package/src/addon-js-unplugin.js +0 -65
  23. package/src/addon-rewritecss-rollup.js +0 -31
  24. /package/src/{findCssInJs.js → lib/findCssInJs.js} +0 -0
  25. /package/src/{fsExists.js → lib/fsExists.js} +0 -0
  26. /package/src/{generateHash.js → lib/generateRelativePathHash.js} +0 -0
  27. /package/src/{getClassesTagsFromCss.js → lib/getClassesTagsFromCss.js} +0 -0
  28. /package/src/{getFiles.js → lib/getFiles.js} +0 -0
  29. /package/src/{getImportedCssFiles.js → lib/getImportedCssFiles.js} +0 -0
  30. /package/src/{isInsideGlobal.js → lib/isInsideGlobal.js} +0 -0
  31. /package/src/{renameClass.js → lib/renameClass.js} +0 -0
  32. /package/src/{replaceGlimmerAst.js → lib/replaceGlimmerAst.js} +0 -0
  33. /package/src/{replaceHbsInJs.js → lib/replaceHbsInJs.js} +0 -0
  34. /package/src/{rewriteCss.js → lib/rewriteCss.js} +0 -0
  35. /package/src/{rewriteHbs.js → lib/rewriteHbs.js} +0 -0
@@ -9608,7 +9608,7 @@ __export(app_css_loader_exports, {
9608
9608
  module.exports = __toCommonJS(app_css_loader_exports);
9609
9609
  var import_path2 = require("path");
9610
9610
 
9611
- // src/fsExists.js
9611
+ // src/lib/fsExists.js
9612
9612
  var import_promises = require("fs/promises");
9613
9613
  async function fsExists_default(path2) {
9614
9614
  try {
@@ -9619,20 +9619,20 @@ async function fsExists_default(path2) {
9619
9619
  }
9620
9620
  }
9621
9621
 
9622
- // src/generateHash.js
9622
+ // src/lib/generateRelativePathHash.js
9623
9623
  var import_blueimp_md5 = __toESM(require_md5(), 1);
9624
- function generateHash_default(relativePath) {
9624
+ function generateRelativePathHash_default(relativePath) {
9625
9625
  return "e" + (0, import_blueimp_md5.default)(relativePath).substring(0, 8);
9626
9626
  }
9627
9627
 
9628
- // src/getPostfix.js
9628
+ // src/lib/generateAbsolutePathHash.js
9629
9629
  var import_path = __toESM(require("path"), 1);
9630
- function getPostfix_default(cssFileName) {
9630
+ function generateAbsolutePathHash_default(cssFileName) {
9631
9631
  const relativePath = import_path.default.relative(process.cwd(), cssFileName);
9632
- return generateHash_default(relativePath);
9632
+ return generateRelativePathHash_default(relativePath);
9633
9633
  }
9634
9634
 
9635
- // src/rewriteCss.js
9635
+ // src/lib/rewriteCss.js
9636
9636
  var import_postcss_selector_parser = __toESM(require_dist(), 1);
9637
9637
 
9638
9638
  // ../node_modules/.pnpm/postcss@8.4.21/node_modules/postcss/lib/postcss.mjs
@@ -9663,7 +9663,7 @@ var Rule = import_postcss.default.Rule;
9663
9663
  var Root = import_postcss.default.Root;
9664
9664
  var Node = import_postcss.default.Node;
9665
9665
 
9666
- // src/isInsideGlobal.js
9666
+ // src/lib/isInsideGlobal.js
9667
9667
  function isInsideGlobal(node, func) {
9668
9668
  const parent = node.parent;
9669
9669
  if (!parent)
@@ -9673,7 +9673,7 @@ function isInsideGlobal(node, func) {
9673
9673
  return isInsideGlobal(parent, func);
9674
9674
  }
9675
9675
 
9676
- // src/rewriteCss.js
9676
+ // src/lib/rewriteCss.js
9677
9677
  function rewriteSelector(sel, postfix) {
9678
9678
  const transform = (selectors) => {
9679
9679
  selectors.walk((selector) => {
@@ -9721,7 +9721,7 @@ function rewriteCss(css, postfix, fileName) {
9721
9721
  async function app_css_loader_default(code) {
9722
9722
  const cssPath = this.resourcePath;
9723
9723
  const cssFileName = (0, import_path2.basename)(cssPath);
9724
- const postfix = getPostfix_default(cssPath);
9724
+ const postfix = generateAbsolutePathHash_default(cssPath);
9725
9725
  const hbsPath = cssPath.replace(".css", ".hbs");
9726
9726
  const gjsPath = cssPath.replace(".css", ".js");
9727
9727
  const [hbsExists, gjsExists] = await Promise.all([
@@ -23,7 +23,7 @@ __export(app_dependency_loader_exports, {
23
23
  });
24
24
  module.exports = __toCommonJS(app_dependency_loader_exports);
25
25
 
26
- // src/fsExists.js
26
+ // src/lib/fsExists.js
27
27
  var import_promises = require("fs/promises");
28
28
  async function fsExists_default(path) {
29
29
  try {
package/index.js CHANGED
@@ -1,17 +1,11 @@
1
- import addonJsUnplugin from './src/addon-js-unplugin.js';
2
- import addonCssRollup from './src/addon-css-rollup.js';
3
- import addonHbsRollup from './src/addon-hbs-rollup.js';
4
1
  import appJsUnplugin from './src/app-js-unplugin.js';
5
2
  import appCssUnplugin from './src/app-css-unplugin.js';
6
3
  import appScopedcssWebpack from './src/app-scopedcss-webpack.js';
7
- import addonRewritecssRollup from './src/addon-rewritecss-rollup.js';
4
+ import scopedCssUnplugin from './src/scoped-css-unplugin.js';
8
5
 
9
6
  export {
10
- addonJsUnplugin,
11
- addonCssRollup,
12
- addonHbsRollup,
7
+ scopedCssUnplugin,
13
8
  appJsUnplugin,
14
9
  appCssUnplugin,
15
10
  appScopedcssWebpack,
16
- addonRewritecssRollup,
17
11
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ember-scoped-css",
3
- "version": "0.5.0",
3
+ "version": "0.7.0",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "license": "ISC",
@@ -55,6 +55,11 @@
55
55
  "ember-template-lint": "^5.7.2",
56
56
  "webpack": "^5.0.0"
57
57
  },
58
+ "peerDependenciesMeta": {
59
+ "ember-teplate-lint": {
60
+ "optional": true
61
+ }
62
+ },
58
63
  "publishConfig": {
59
64
  "registry": "https://registry.npmjs.org"
60
65
  },
@@ -2,7 +2,7 @@ import { createUnplugin } from 'unplugin';
2
2
  import path from 'path';
3
3
  import { readFile } from 'fs/promises';
4
4
  import { Compilation } from 'webpack';
5
- import getPostfix from './getPostfix.js';
5
+ import generateHash from './lib/generateAbsolutePathHash.js';
6
6
 
7
7
  export default createUnplugin(({ loaders, htmlEntrypointInfo }) => {
8
8
  return {
@@ -41,7 +41,7 @@ export default createUnplugin(({ loaders, htmlEntrypointInfo }) => {
41
41
  css = await loader.bind({ resourcePath: cssPath })(css);
42
42
  }
43
43
  // random string; lenght is 8
44
- const postfix = getPostfix(path.basename(cssPath));
44
+ const postfix = generateHash(path.basename(cssPath));
45
45
 
46
46
  this.emitFile({
47
47
  type: 'asset',
@@ -1,14 +1,14 @@
1
1
  // import { createUnplugin } from 'unplugin';
2
2
  import { basename } from 'path';
3
- import fsExists from './fsExists.js';
4
- import getPostfix from './getPostfix.js';
5
- import rewriteCss from './rewriteCss.js';
3
+ import fsExists from './lib/fsExists.js';
4
+ import generateHash from './lib/generateAbsolutePathHash.js';
5
+ import rewriteCss from './lib/rewriteCss.js';
6
6
  // import path from 'path';
7
7
 
8
8
  export default async function (code) {
9
9
  const cssPath = this.resourcePath;
10
10
  const cssFileName = basename(cssPath);
11
- const postfix = getPostfix(cssPath);
11
+ const postfix = generateHash(cssPath);
12
12
 
13
13
  const hbsPath = cssPath.replace('.css', '.hbs');
14
14
  const gjsPath = cssPath.replace('.css', '.js');
@@ -1,4 +1,4 @@
1
- import fsExists from './fsExists.js';
1
+ import fsExists from './lib/fsExists.js';
2
2
 
3
3
  export default async function (source) {
4
4
  if (this.resourcePath.endsWith('.js')) {
@@ -1,8 +1,8 @@
1
1
  import { createUnplugin } from 'unplugin';
2
- import replaceGlimmerAst from './replaceGlimmerAst.js';
2
+ import replaceGlimmerAst from './lib/replaceGlimmerAst.js';
3
3
  import path from 'path';
4
- import getPostfix from './getPostfix.js';
5
- import getClassesTagsFromCss from './getClassesTagsFromCss.js';
4
+ import generateHash from './lib/generateAbsolutePathHash.js';
5
+ import getClassesTagsFromCss from './lib/getClassesTagsFromCss.js';
6
6
 
7
7
  function* iterateOpcodes(opcodes) {
8
8
  for (let instruction of opcodes) {
@@ -48,7 +48,7 @@ export default createUnplugin(({ appDir }) => {
48
48
 
49
49
  async transform(code, id) {
50
50
  const cssPath = id.replace(/(\.js)|(\.hbs)/, '.css');
51
- const postfix = getPostfix(cssPath);
51
+ const postfix = generateHash(cssPath);
52
52
 
53
53
  return await replaceGlimmerAst(code, id, (opcodes, css) => {
54
54
  const { classes, tags } = getClassesTagsFromCss(css);
@@ -1,10 +1,10 @@
1
1
  // import { RawSource } from 'webpack-sources';
2
- import rewriteCss from './rewriteCss.js';
2
+ import rewriteCss from './lib/rewriteCss.js';
3
3
  import { readFile, writeFile } from 'fs/promises';
4
4
  import path from 'path';
5
- import getPostfix from './getPostfix.js';
6
- import fsExists from './fsExists.js';
7
- import getFiles from './getFiles.js';
5
+ import generateHash from './lib/generateAbsolutePathHash.js';
6
+ import fsExists from './lib/fsExists.js';
7
+ import getFiles from './lib/getFiles.js';
8
8
 
9
9
  export default class {
10
10
  apply(compiler) {
@@ -44,7 +44,7 @@ export default class {
44
44
  continue;
45
45
  }
46
46
  const fileName = path.basename(file);
47
- const postfix = getPostfix(fileName);
47
+ const postfix = generateHash(fileName);
48
48
  const css = await readFile(file, 'utf-8');
49
49
  const rewrittenCss = rewriteCss(css, postfix, fileName);
50
50
 
@@ -1,4 +1,4 @@
1
- import replaceScopedClass from './replaceScopedClass.js';
1
+ import replaceScopedClass from './lib/replaceScopedClass.js';
2
2
 
3
3
  const scopedClass = () => {
4
4
  return {
@@ -1,4 +1,4 @@
1
- import generateHash from './generateHash.js';
1
+ import generateHash from './generateRelativePathHash.js';
2
2
  import path from 'path';
3
3
 
4
4
  export default function (cssFileName) {
@@ -1,12 +1,12 @@
1
1
  import recast from 'ember-template-recast';
2
2
  import renameClass from './renameClass.js';
3
- import getPostfix from './getPostfix.js';
3
+ import generateHash from './generateAbsolutePathHash.js';
4
4
 
5
5
  export default function (hbs, templatePath) {
6
6
  let ast = recast.parse(hbs);
7
7
  let stack = [];
8
8
  const cssPath = templatePath.replace(/(\.hbs)?\.js$/, '.css');
9
- const postfix = getPostfix(cssPath);
9
+ const postfix = generateHash(cssPath);
10
10
 
11
11
  recast.traverse(ast, {
12
12
  All: {
@@ -0,0 +1,116 @@
1
+ import { createUnplugin } from 'unplugin';
2
+ import { readFile } from 'fs/promises';
3
+ import path from 'path';
4
+ import getClassesTagsFromCss from './lib/getClassesTagsFromCss.js';
5
+ import generateHash from './lib/generateAbsolutePathHash.js';
6
+ import replaceHbsInJs from './lib/replaceHbsInJs.js';
7
+ import rewriteHbs from './lib/rewriteHbs.js';
8
+ import fsExists from './lib/fsExists.js';
9
+ import rewriteCss from './lib/rewriteCss.js';
10
+
11
+ function isJsFile(id) {
12
+ return id.endsWith('.js') || id.endsWith('.ts');
13
+ }
14
+
15
+ function isCssFile(id) {
16
+ return id.endsWith('.css');
17
+ }
18
+
19
+ async function transformJsFile(code, jsPath) {
20
+ const cssPath = jsPath.replace(/(\.hbs)?\.((js)|(ts))$/, '.css');
21
+ const cssFileName = path.basename(cssPath);
22
+
23
+ const cssExists = await fsExists(cssPath);
24
+ let css;
25
+ if (cssExists) {
26
+ css = await readFile(cssPath, 'utf8');
27
+ } else {
28
+ return {
29
+ code,
30
+ map: null,
31
+ };
32
+ }
33
+
34
+ // add css import for js and gjs files
35
+ code = `import './${cssFileName}';\n\n${code}`;
36
+
37
+ // rewrite hbs in js in case it is gjs file (for gjs files hbs is already in js file)
38
+
39
+ const rewrittenCode = replaceHbsInJs(code, (hbs) => {
40
+ const { classes, tags } = getClassesTagsFromCss(css);
41
+ const postfix = generateHash(cssPath);
42
+ const rewritten = rewriteHbs(hbs, classes, tags, postfix);
43
+ return rewritten;
44
+ });
45
+
46
+ return {
47
+ code: rewrittenCode,
48
+ map: null,
49
+ };
50
+ }
51
+
52
+ async function transformCssFile(code, id, emitFile) {
53
+ const jsPath = id.replace(/\.css$/, '.gjs');
54
+ const gtsPath = id.replace(/\.css$/, '.gts');
55
+ const hbsPath = id.replace(/\.css$/, '.hbs');
56
+
57
+ const [jsExists, gtsExists, hbsExists] = await Promise.all([
58
+ fsExists(jsPath),
59
+ fsExists(gtsPath),
60
+ fsExists(hbsPath),
61
+ ]);
62
+
63
+ if (jsExists || hbsExists || gtsExists) {
64
+ const postfix = generateHash(id);
65
+ code = rewriteCss(code, postfix, path.basename(id));
66
+ }
67
+
68
+ emitFile({
69
+ type: 'asset',
70
+ fileName: id.replace(path.join(process.cwd(), 'src/'), ''),
71
+ source: code,
72
+ });
73
+ return '';
74
+ }
75
+
76
+ export default createUnplugin(() => {
77
+ return {
78
+ name: 'ember-scoped-css-unplugin',
79
+
80
+ generateBundle(a, bundle) {
81
+ let cssFiles = [];
82
+ for (let asset in bundle) {
83
+ const cssAsset = asset.replace('js', 'css');
84
+ if (!asset.endsWith('js') || !bundle[cssAsset]) {
85
+ continue;
86
+ }
87
+
88
+ if (process.env.environment === 'development') {
89
+ cssFiles.push(bundle[cssAsset].source);
90
+ delete bundle[cssAsset];
91
+ } else {
92
+ // add import to js files
93
+ bundle[asset].code =
94
+ `import './${path.basename(asset.replace('.js', '.css'))}';\n` +
95
+ bundle[asset].code;
96
+ }
97
+ }
98
+
99
+ if (process.env.environment === 'development') {
100
+ this.emitFile({
101
+ type: 'asset',
102
+ fileName: 'scoped.css',
103
+ source: cssFiles.join('\n'),
104
+ });
105
+ }
106
+ },
107
+
108
+ transform(code, jsPath) {
109
+ if (isJsFile(jsPath)) {
110
+ return transformJsFile(code, jsPath);
111
+ } else if (isCssFile(jsPath)) {
112
+ return transformCssFile(code, jsPath, this.emitFile);
113
+ }
114
+ },
115
+ };
116
+ });
@@ -1,5 +1,5 @@
1
- import generateHash from './generateHash.js';
2
- import renameClass from './renameClass.js';
1
+ import generateHash from './lib/generateRelativePathHash.js';
2
+ import renameClass from './lib/renameClass.js';
3
3
 
4
4
  export function scopedClass(className, relativeCssPath) {
5
5
  return renameClass(className, generateHash(relativeCssPath));
@@ -31,7 +31,7 @@ class ScopedClassHelperRule extends Rule {
31
31
  }
32
32
 
33
33
  export default {
34
- name: 'scoped-class-helper-plugin',
34
+ name: 'scoped-css-plugin',
35
35
  rules: {
36
36
  'scoped-class-helper': ScopedClassHelperRule,
37
37
  },
@@ -1,21 +1,21 @@
1
1
  import { expect } from 'chai';
2
- import getPostfix from '../src/getPostfix.js';
2
+ import generateHash from '../src/lib/generateAbsolutePathHash.js';
3
3
 
4
- describe('getPostfix', function () {
4
+ describe('generateHash', function () {
5
5
  it('should return a string', function () {
6
- const postfix = getPostfix('foo.css');
6
+ const postfix = generateHash('foo.css');
7
7
 
8
8
  expect(postfix).to.be.a('string');
9
9
  });
10
10
 
11
11
  it('should return a string starting with "e"', function () {
12
- const postfix = getPostfix('foo.css');
12
+ const postfix = generateHash('foo.css');
13
13
 
14
14
  expect(postfix).to.match(/^e/);
15
15
  });
16
16
 
17
17
  it('should return a string of length 9', function () {
18
- const postfix = getPostfix('foo.css');
18
+ const postfix = generateHash('foo.css');
19
19
 
20
20
  expect(postfix).to.have.lengthOf(9);
21
21
  });
@@ -1,5 +1,5 @@
1
1
  import { expect } from 'chai';
2
- import getClassesTagsFromCss from '../src/getClassesTagsFromCss.js';
2
+ import getClassesTagsFromCss from '../src/lib/getClassesTagsFromCss.js';
3
3
 
4
4
  describe('rewriteCss', function () {
5
5
  it('should return classes and tags that are not in :global', function () {
@@ -1,5 +1,5 @@
1
1
  import { expect } from 'chai';
2
- import rewriteCss from '../src/rewriteCss.js';
2
+ import rewriteCss from '../src/lib/rewriteCss.js';
3
3
 
4
4
  describe('rewriteCss', function () {
5
5
  it('should rewrite css', function () {
@@ -1,5 +1,5 @@
1
1
  import { generateRuleTests } from 'ember-template-lint';
2
- import scopedClassHelperPlugin from '../src/template-lint/scoped-class-helper-plugin.js';
2
+ import scopedClassHelperPlugin from '../src/template-lint/plugin.js';
3
3
  import assert from 'assert';
4
4
 
5
5
  generateRuleTests({
@@ -1,86 +0,0 @@
1
- import path from 'path';
2
- import { readFile } from 'fs/promises';
3
-
4
- export default function () {
5
- return {
6
- name: 'addon-css-rollup',
7
-
8
- async resolveId(source, importer, options) {
9
- // catch emited css files
10
- if (source.endsWith('.css')) {
11
- const resolution = await this.resolve(source, importer, {
12
- ...options,
13
- skipSelf: true,
14
- });
15
-
16
- if (resolution) {
17
- return resolution;
18
- } else {
19
- const gjsCss = this.getModuleInfo(importer)?.meta?.gjsCss;
20
- if (gjsCss) {
21
- return {
22
- external: false,
23
- id: importer.replace(/\.js$/, '.css'),
24
- meta: {
25
- importer,
26
- gjsCss,
27
- },
28
- };
29
- }
30
- }
31
- }
32
- return null;
33
- },
34
-
35
- async load(id) {
36
- if (id.endsWith('.css')) {
37
- const gjsCss = this.getModuleInfo(id).meta.gjsCss;
38
- let css = gjsCss;
39
- if (!css) {
40
- css = await readFile(id, 'utf-8');
41
- }
42
-
43
- return css;
44
- }
45
- },
46
-
47
- async transform(code, id) {
48
- if (id.endsWith('.css')) {
49
- this.emitFile({
50
- type: 'asset',
51
- fileName: id.replace(path.join(process.cwd(), 'src/'), ''),
52
- source: code,
53
- });
54
- return '';
55
- }
56
- },
57
-
58
- generateBundle(a, bundle) {
59
- let cssFiles = [];
60
- for (let asset in bundle) {
61
- const cssAsset = asset.replace('js', 'css');
62
- if (!asset.endsWith('js') || !bundle[cssAsset]) {
63
- continue;
64
- }
65
-
66
- if (process.env.environment === 'development') {
67
- cssFiles.push(bundle[cssAsset].source);
68
- delete bundle[cssAsset];
69
- } else {
70
- // add import to js files
71
- bundle[asset].code =
72
- `import './${path.basename(asset.replace('.js', '.css'))}';\n` +
73
- bundle[asset].code;
74
- }
75
- }
76
-
77
- if (process.env.environment === 'development') {
78
- this.emitFile({
79
- type: 'asset',
80
- fileName: 'scoped.css',
81
- source: cssFiles.join('\n'),
82
- });
83
- }
84
- },
85
- };
86
- }
@@ -1,46 +0,0 @@
1
- import { readFile } from 'fs/promises';
2
- import getPostfix from './getPostfix.js';
3
- import replaceHbsInJs from './replaceHbsInJs.js';
4
- import getClassesTagsFromCss from './getClassesTagsFromCss.js';
5
- import rewriteHbs from './rewriteHbs.js';
6
- import fsExists from './fsExists.js';
7
-
8
- export default function rollupCssColocation() {
9
- return {
10
- name: 'addon-hbs-rollup',
11
-
12
- async transform(code, id) {
13
- if (id.endsWith('.hbs.js')) {
14
- const hbsPath = id.replace('.js', '');
15
- const cssPath = hbsPath.replace('.hbs', '.css');
16
-
17
- const cssExists = await fsExists(cssPath);
18
- if (cssExists) {
19
- // read the css file
20
- // TODO: get css from loader, because there are classes in imported css files; css can be stored in meta!!!!!
21
- // const resolution = await this.resolve(importPath, id);
22
- // resolution.meta.internalImport = true;
23
- // const importedCss = await this.load(resolution);
24
- const css = await readFile(cssPath, 'utf-8');
25
- const { classes, tags } = getClassesTagsFromCss(css);
26
-
27
- // generate unique postfix
28
- const postfix = getPostfix(cssPath);
29
-
30
- // rewrite the template
31
- const rewrittenHbsJs = replaceHbsInJs(code, (hbs) => {
32
- // add dependency to the css file
33
- this.addWatchFile(cssPath);
34
- return rewriteHbs(hbs, classes, tags, postfix);
35
- });
36
-
37
- return {
38
- code: rewrittenHbsJs,
39
- // this rollup plugin changes only the template string, so the code structure is the same
40
- map: null,
41
- };
42
- }
43
- }
44
- },
45
- };
46
- }
@@ -1,65 +0,0 @@
1
- import { createUnplugin } from 'unplugin';
2
- import { readFile } from 'fs/promises';
3
- import path from 'path';
4
- import getClassesTagsFromCss from './getClassesTagsFromCss.js';
5
- import getPostfix from './getPostfix.js';
6
- import replaceHbsInJs from './replaceHbsInJs.js';
7
- import rewriteHbs from './rewriteHbs.js';
8
- import fsExists from './fsExists.js';
9
- import findCssInJs from './findCssInJs.js';
10
- import recast from 'recast';
11
-
12
- export default createUnplugin(() => {
13
- return {
14
- name: 'addon-js-unplugin',
15
-
16
- transformInclude(id) {
17
- return id.endsWith('.js') || id.endsWith('.ts');
18
- },
19
-
20
- async transform(code, jsPath) {
21
- const cssPath = jsPath.replace(/(\.hbs)?\.((js)|(ts))$/, '.css');
22
- const cssFileName = path.basename(cssPath);
23
-
24
- const cssExists = await fsExists(cssPath);
25
- let css;
26
- if (cssExists) {
27
- css = await readFile(cssPath, 'utf8');
28
- } else {
29
- if (code.includes('__GLIMMER_STYLES')) {
30
- const result = findCssInJs(code, true);
31
- css = result.css;
32
- code = recast.print(result.ast).code;
33
- this.getModuleInfo(jsPath).meta.gjsCss = result.css;
34
-
35
- // TODO: generate changed source map. Implementation depends on implemented rollup plugin for style tag
36
- }
37
- }
38
-
39
- if (!css) {
40
- return {
41
- code,
42
- map: null,
43
- };
44
- }
45
-
46
- // add css import for js and gjs files
47
- code = `import './${cssFileName}';\n\n${code}`;
48
-
49
- // rewrite hbs in js in case it is gjs file (for gjs files hbs is already in js file)
50
- // for js components "@embroider/addon-dev/template-colocation-plugin", will add hbs to js later. So there is hbs plugin to rewrite hbs
51
-
52
- const rewrittenCode = replaceHbsInJs(code, (hbs) => {
53
- const { classes, tags } = getClassesTagsFromCss(css);
54
- const postfix = getPostfix(cssPath);
55
- const rewritten = rewriteHbs(hbs, classes, tags, postfix);
56
- return rewritten;
57
- });
58
-
59
- return {
60
- code: rewrittenCode,
61
- map: null,
62
- };
63
- },
64
- };
65
- });
@@ -1,31 +0,0 @@
1
- import path from 'path';
2
- import getPostfix from './getPostfix.js';
3
- import rewriteCss from './rewriteCss.js';
4
- import fsExists from './fsExists.js';
5
-
6
- export default function () {
7
- return {
8
- name: 'addon-rewritecss-rollup',
9
-
10
- async transform(code, id) {
11
- if (!id.endsWith('.css')) {
12
- return;
13
- }
14
- const postfix = getPostfix(id);
15
- const jsPath = id.replace(/\.css$/, '.gjs');
16
- const gtsPath = id.replace(/\.css$/, '.gts');
17
- const hbsPath = id.replace(/\.css$/, '.hbs');
18
-
19
- const [jsExists, gtsExists, hbsExists] = await Promise.all([
20
- fsExists(jsPath),
21
- fsExists(gtsPath),
22
- fsExists(hbsPath),
23
- ]);
24
-
25
- if (jsExists || hbsExists || gtsExists) {
26
- const rewritten = rewriteCss(code, postfix, path.basename(id));
27
- return rewritten;
28
- }
29
- },
30
- };
31
- }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes