Skip to content

Commit

Permalink
Re-support pods
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed May 21, 2024
1 parent 0c5595a commit 548b5ca
Show file tree
Hide file tree
Showing 87 changed files with 252 additions and 1,616 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ Setup webpack:
```js
// ember-cli-build.js
module.exports = async function (defaults) {
const { appJsUnplugin } = await import('ember-scoped-css/build');

const app = new EmberApp(defaults, { /* ... */ });

const { Webpack } = require('@embroider/webpack');
Expand All @@ -70,7 +68,6 @@ module.exports = async function (defaults) {
packagerOptions: {
// css loaders for live reloading css
webpackConfig: {
plugins: [appJsUnplugin.webpack({ appDir: __dirname })],
module: {
rules: [
// css loaders for production
Expand Down
50 changes: 50 additions & 0 deletions ember-scoped-css-compat/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const hashObj = require('hash-obj');
const fs = require('fs');

const {
default: ScopedCssPreprocessor,
Expand Down Expand Up @@ -57,6 +58,7 @@ module.exports = {

this._setupCSSPlugins(registry, options);
this._setupHBSPlugins(registry, options);
this._setupJSPlugins(registry, options);
},

_setupCSSPlugins(registry, options) {
Expand All @@ -72,6 +74,10 @@ module.exports = {
registry.add('css', this.outputStylePreprocessor);
},

_setupJSPlugins(registry, options) {
registry.add('js', new JSPreprocessor(options));
},

_setupHBSPlugins(registry, options) {
let plugin = this._buildHBSPlugin(options);

Expand Down Expand Up @@ -112,3 +118,47 @@ function cacheKeyForConfig(config) {
// and those css files are not imported into js... so... no cache for us
return `ember-scoped-css-${configHash}-${Math.random()}`;
}

const stew = require('broccoli-stew');
const babel = require('@babel/core');

class JSPreprocessor {
constructor(options) {
this.options = options;
}

toTree(tree) {
let updated = stew.map(tree, '**/*.{js}', (string, relativePath) => {
console.log({ string, relativePath });

let cssPath = relativePath.replace(/\.js$/, '.css');

if (fs.existsSync(cssPath)) {
/**
* This is slow to cause a whole parse again,
* so we only want to do it when we know we have a CSS file.
*
* The whole babel situation with embroider (pre v4) and classic ember,
* is... just a mess. So we're skirting around it and inserting our code
* as early as possible.
*/
let result = babel.transformSync('code', {
plugins: [
[
require.resolve('ember-scoped-css/babel-plugin'),
{
...this.options,
},
],
],
});

return result.code;
}

return string;
});

return updated;
}
}
1 change: 1 addition & 0 deletions ember-scoped-css-compat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@glimmer/tracking": "^1.1.2",
"@nullvoxpopuli/eslint-configs": "^4.0.0",
"broccoli-asset-rev": "^3.0.0",
"broccoli-stew": "^3.0.0",
"concurrently": "^8.2.2",
"ember-auto-import": "^2.7.2",
"ember-cli": "~5.8.1",
Expand Down
161 changes: 0 additions & 161 deletions ember-scoped-css/src/build/app-js-unplugin.js

This file was deleted.

3 changes: 1 addition & 2 deletions ember-scoped-css/src/build/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import appJsUnplugin from './app-js-unplugin.js';
import scopedCssUnplugin from './scoped-css-unplugin.js';

export { appJsUnplugin, scopedCssUnplugin };
export { scopedCssUnplugin };
89 changes: 6 additions & 83 deletions ember-scoped-css/src/build/template-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@
* @typedef {import('@glimmer/syntax').ASTPluginEnvironment} ASTPluginEnvironment
*
*/
import path from 'node:path';

import { existsSync } from 'fs';

import { getCSSInfo } from '../lib/css/utils.js';
import { appPath, findWorkspacePath } from '../lib/generateAbsolutePathHash.js';
import { generateRelativePathHash } from '../lib/generateRelativePathHash.js';
import { fixFilename } from '../lib/path/template-transform-paths.js';
import {
appPath,
cssPathFor,
hashFromModulePath,
isRelevantFile,
withoutExtension,
} from '../lib/path-utils.js';
} from '../lib/path/utils.js';
import { templatePlugin } from '../lib/rewriteHbs.js';

const noopPlugin = {
Expand All @@ -37,12 +34,12 @@ export function createPlugin(config) {
return noopPlugin;
}

let absolutePath = fixFilename(env);
let absolutePath = fixFilename(env.filename);
let modulePath = appPath(absolutePath);

let cssPath = cssPathFor(absolutePath);
let info = getCSSInfo(cssPath);
let postfix = generateRelativePathHash(modulePath);
let postfix = hashFromModulePath(modulePath);

if (!info) {
return noopPlugin;
Expand Down Expand Up @@ -77,77 +74,3 @@ export function createPlugin(config) {
};
};
}

/**
* template plugins do not hand us the correct file path.
* additionally, we may not be able to rely on this data in the future,
* so this functions acts as a means of normalizing _whatever_ we're given
* in the future.
*
* @param {ASTPluginEnvironment} env
* @returns {string} the absolute path to the file
*/
function fixFilename(env) {
let fileName = env.filename;
let workspace = findWorkspacePath(fileName);

/**
* ember-source 5.8:
* - the filename looks like an absolute path, but swapped out the 'app' part of the path
* with the module name, so the file paths never exist on disk
*/
if (!fileName.includes('/app/')) {
let maybeModule = fileName.replace(workspace, '');
let [maybeScope, ...rest] = maybeModule.split('/').filter(Boolean);
let parts = rest;

if (maybeScope.startsWith('@')) {
let [, ...rester] = rest;

parts = rester;
}

let relative = path.join(...parts);

/**
* We don't actually know if this file is an app.
* it could be an addon (v1 or v2)
*
* So here we log to see if we have unhandled situations.
*/
let candidatePath = path.join(workspace, 'app', relative);

let resolved = findCandidate(candidatePath);

if (resolved) {
return resolved;
}
}

console.debug(`[ScopedCSS]: Failed to handle ${fileName}`);

// Fallback to what the plugin system gives us.
// This may be wrong, and if wrong, reveals
// unhandled scenarios with the file names in the plugin infra
return fileName;
}

const COMPILES_TO_JS = ['.hbs', '.gjs', '.gts'];

function findCandidate(filePath) {
if (existsSync(filePath)) {
return filePath;
}

let withoutExt = withoutExtension(filePath);

for (let ext of COMPILES_TO_JS) {
let candidatePath = withoutExt + ext;

if (existsSync(candidatePath)) {
return candidatePath;
}
}

return null;
}
Loading

0 comments on commit 548b5ca

Please sign in to comment.