Skip to content

Commit 79f9f4f

Browse files
committed
support embroider static
1 parent e72479c commit 79f9f4f

14 files changed

+14405
-29
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Syntax:
3535
{{import "{ fn }" from "@ember/helper"}}
3636
{{import myHelper from 'ui/helper'}}
3737
{{import my-mod from 'ui/modifiers'}}
38-
{{import style from './styles.scoped.scss'}}
38+
{{import style from './styles.module.scss'}}
3939
{{import BasicDropdown from 'ember-basic-dropdown/components/basic-dropdown'}}
4040
{{import SameDropdown from 'ember-basic-dropdown/components/basic-dropdown'}}
4141
@@ -90,7 +90,7 @@ packagerOptions: {
9090
],
9191
},
9292
{
93-
test: /\.scoped\.scss$/i,
93+
test: /\.module\.scss$/i,
9494
use: [
9595
{ loader: 'style-loader' },
9696
{ loader: 'css-loader', options: {

addon.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ module.exports = {
4848
const appOptions = this.app && this.app.options;
4949
const addonOptions = parentOptions || appOptions || {};
5050

51-
return addonOptions['ember-template-imports'] || {
51+
return addonOptions['ember-hbs-imports'] || {
5252
style: {
5353
extension: 'scss',
5454
plugins: {
@@ -91,14 +91,17 @@ module.exports = {
9191
const VersionChecker = require('ember-cli-version-checker');
9292
const checker = new VersionChecker(this);
9393
const ember = checker.for('ember-source');
94+
const addonOptions = this._getAddonOptions();
9495
const options = {
95-
styleExtension: this._getAddonOptions().style.extension,
96+
styleExtension: addonOptions.style.extension,
9697
root: path.join(this.project.root, ...(isDummy ? ['tests','dummy'] : [])),
9798
failOnMissingImport: false,
9899
failOnBadImport: false,
99100
namespace: isDummy ? 'dummy' : name,
100101
imports: {},
101-
useModifierHelperHelpers: ember.isAbove('v3.27.0-beta.2')
102+
useModifierHelperHelpers: ember.isAbove('v3.27.0-beta.2'),
103+
useHelperWrapper: !ember.isAbove('v3.27.0-beta.2'),
104+
embroiderStatic: addonOptions.embroiderStatic,
102105
}
103106
this._getBabelOptions().plugins.splice(0, 0, [require.resolve('./lib/hbs-imports-babel-plugin'), options]);
104107
this._super.included.call(this, arguments);
@@ -123,7 +126,7 @@ module.exports = {
123126
_scopedStyles(tree, namespace, outputFile) {
124127
const config = this._getAddonOptions().style;
125128
outputFile = outputFile || 'pod-styles.' + config.extension
126-
tree = new Funnel(tree, { include: [ '**/*.scoped.' + config.extension ] });
129+
tree = new Funnel(tree, { include: [ '**/*.module.' + config.extension ] });
127130
tree = new StylesRewriter(tree, {
128131
namespace,
129132
extension: config.extension,
@@ -138,7 +141,7 @@ module.exports = {
138141
// eslint-disable-next-line @typescript-eslint/no-this-alias
139142
const self = this;
140143
registry.add('template', {
141-
name: 'ember-template-imports',
144+
name: 'ember-hbs-imports',
142145
ext: 'hbs',
143146
before: ['ember-cli-htmlbars'],
144147
toTree: (tree) => {
@@ -148,14 +151,17 @@ module.exports = {
148151
const VersionChecker = require('ember-cli-version-checker');
149152
const checker = new VersionChecker(this);
150153
const ember = checker.for('ember-source');
154+
const addonOptions = this._getAddonOptions();
151155
const options = {
152-
styleExtension: this._getAddonOptions().style.extension,
156+
styleExtension: addonOptions.style.extension,
153157
root: path.join(this.project.root, ...(isDummy ? ['tests','dummy'] : [])),
154158
failOnMissingImport: false,
155159
failOnBadImport: false,
156160
namespace: isDummy ? 'dummy' : name,
157161
imports: this.imports,
158-
useModifierHelperHelpers: ember.isAbove('v3.27.0-beta.2')
162+
useModifierHelperHelpers: ember.isAbove('v3.27.0-beta.2'),
163+
useHelperWrapper: !ember.isAbove('v3.27.0-beta.2'),
164+
embroiderStatic: addonOptions.embroiderStatic,
159165
}
160166
tree = new TemplateImportProcessor(tree, options);
161167
return tree;

ember-cli-build.js

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const EmberAddon = require('ember-cli/lib/broccoli/ember-addon');
44

55
module.exports = function(defaults) {
66
const app = new EmberAddon(defaults, {
7-
sassOptions: { implementation: require('node-sass') }
87
});
98

109
/*

index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import 'ember-source/types/stable'

lib/StylesRewriter.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ module.exports = class StylesRewriter extends BroccoliFilter {
2323
this.options.extension = this.options.extension || 'scss';
2424
this.options.before = this.options.before || [];
2525
this.options.after = this.options.after || [];
26-
this.extensions = [ 'scoped.' + this.options.extension ];
27-
this.targetExtension = 'scoped.' + this.options.extension;
26+
this.extensions = [ 'module.' + this.options.extension ];
27+
this.targetExtension = 'module.' + this.options.extension;
2828
}
2929

3030
cacheKeyProcessString(string, relativePath) {
@@ -43,7 +43,7 @@ module.exports = class StylesRewriter extends BroccoliFilter {
4343
if (relativePath.endsWith('pod-styles.scss')) {
4444
return contents;
4545
}
46-
if (relativePath.endsWith('scoped.scss')) {
46+
if (relativePath.endsWith('module.scss')) {
4747
const plugins = [...this.options.before]
4848
plugins.push(rewriterPlugin({
4949
filename: relativePath,

lib/hbs-imports-babel-plugin.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import ImportProcessor from './import-processor';
22
import p from 'path';
33
import type * as BabelCoreNamespace from '@babel/core';
44
import type * as BabelTypesNamespace from '@babel/types';
5-
import type { NodePath } from '@babel/traverse';
65
import { PluginObj } from '@babel/core';
76
import { V8IntrinsicIdentifier } from '@babel/types';
87

@@ -39,7 +38,7 @@ module.exports = function hbsImports({ types: t }: { types: BabelTypes}) {
3938
if (!fileName) return;
4039
const importedStyles = allImports && [...allImports.others]
4140
.filter(x => x.endsWith('.scss'))
42-
.map(x => x.replace(new RegExp('^'+ImportProcessor.options.namespace + '\/'), ''))
41+
.map(x => x.replace(new RegExp('^'+ImportProcessor.options.namespace + '/'), ''))
4342
.map(x => p.relative(p.dirname(fileName), p.join(ImportProcessor.options.root, x)))
4443
.map(x => x.startsWith('.') ? x : `./${x}`);
4544
importedStyles?.forEach((s) => {

lib/import-processor.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ const importProcessors = {
8787
useHelperWrapper: true,
8888
useSafeImports: true,
8989
extendImportPathForNamedImports: true,
90+
embroiderStatic: true,
9091
messageFormat: 'json'
9192
},
9293
glimmer,
@@ -188,8 +189,11 @@ const importProcessors = {
188189
const components = imported.info.components;
189190
const helpers = imported.info.helpers;
190191
const modifiers = imported.info.modifiers;
192+
const options = this.options;
193+
191194
function findImport(name: string) {
192195
return imports.find((imp) => {
196+
if (options.embroiderStatic && imp.shouldLookInFile) return false;
193197
if (imp.isStyle) {
194198
return name.split('.')[0] === imp.localName;
195199
}
@@ -355,11 +359,13 @@ const importProcessors = {
355359
}
356360

357361
const createComponentLetBlockExpr = (comp: [key: string, info: {path: string}]) => {
358-
return importProcessors.glimmer.preprocess(`{{#let (component "${comp[1].path}") as |${comp[0]}|}}{{/let}}`).body[0] as glimmer.AST.BlockStatement;
362+
const path = (options.embroiderStatic ? '__hbs__import_' : '') + comp[1].path.replace(/\\/g, '/');
363+
return importProcessors.glimmer.preprocess(`{{#let (component "${path}") as |${comp[0]}|}}{{/let}}`).body[0] as glimmer.AST.BlockStatement;
359364
};
360365
const handleHelper = (helper: { nodes: PathExpression[], resolvedPath: string }) => {
366+
const path = (options.embroiderStatic ? '__hbs__import_' : '') + helper.resolvedPath.replace(/\\/g, '/');
361367
if (this.options.useModifierHelperHelpers) {
362-
let lookup = `"${helper.resolvedPath}"`;
368+
let lookup = `"${path}"`;
363369
if (this.options.useHelperWrapper) {
364370
lookup = `(ember-hbs-imports/helpers/lookup-helper this "${helper.resolvedPath}")`;
365371
}
@@ -371,8 +377,9 @@ const importProcessors = {
371377
}
372378
};
373379
const handleModifier = (modifier: { nodes: PathExpression[], resolvedPath: string }) => {
380+
const path = (options.embroiderStatic ? '__hbs__import_' : '') + modifier.resolvedPath.replace(/\\/g, '/');
374381
if (this.options.useModifierHelperHelpers) {
375-
let lookup = `"${modifier.resolvedPath}"`;
382+
let lookup = `"${path}"`;
376383
if (this.options.useHelperWrapper) {
377384
lookup = `(ember-hbs-imports/helpers/lookup-modifier this "${modifier.resolvedPath}")`;
378385
}

package.json

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ember-hbs-imports",
3-
"version": "0.5.41",
3+
"version": "1.0.0",
44
"repository": {
55
"type": "git",
66
"url": "https://github.com/patricklx/ember-hbs-imports"
@@ -26,9 +26,10 @@
2626
"test:all": "ember try:each"
2727
},
2828
"dependencies": {
29+
"@babel/core": "^7.23.9",
30+
"@babel/types": "^7.23.9",
2931
"@glimmer/reference": "^0.84.2",
3032
"@glimmer/syntax": "^0.84.2",
31-
"@types/ember__runloop": "^4.0.1",
3233
"broccoli-asset-rev": "^2.7.0",
3334
"broccoli-concat": "^4.2.4",
3435
"broccoli-funnel": "^3.0.3",
@@ -48,9 +49,7 @@
4849
"@commitlint/config-conventional": "^7.1.2",
4950
"@ember/optional-features": "^2.0.0",
5051
"@ember/test-helpers": "^2.7.0",
51-
"@types/ember": "^4.0.0",
52-
"@types/babel__traverse": "^7.18.3",
53-
"@types/babel__core": "^7.18.3",
52+
"@types/node": "^18.15.3",
5453
"@typescript-eslint/eslint-plugin": "^3.1.0",
5554
"@typescript-eslint/parser": "^3.1.0",
5655
"ember-auto-import": "^2.4.1",
@@ -61,26 +60,26 @@
6160
"ember-cli-sass": "^10.0.1",
6261
"ember-cli-sri": "^2.1.1",
6362
"ember-template-lint": "^4.6.0",
64-
"ember-cli-uglify": "^2.1.0",
63+
"ember-cli-terser": "^4.0.2",
6564
"ember-disable-prototype-extensions": "^1.1.3",
6665
"ember-load-initializers": "^2.1.2",
6766
"ember-maybe-import-regenerator": "^0.1.6",
6867
"ember-modifier": "^3.2.7",
6968
"ember-resolver": "^10.1.0",
70-
"ember-source": "^4.3.0",
69+
"ember-source": "^5.6.0",
7170
"ember-source-channel-url": "^1.1.0",
7271
"ember-try": "^1.0.0",
7372
"eslint": "^7.12.1",
7473
"eslint-plugin-ember": "^5.2.0",
7574
"eslint-plugin-node": "^7.0.1",
7675
"husky": "^1.3.1",
7776
"loader.js": "^4.7.0",
78-
"node-sass": "^5.0.0",
7977
"ember-qunit": "^5.1.5",
8078
"qunit": "^2.19.1",
8179
"semantic-release": "^15.13.2",
8280
"typescript": "^4.0.0",
83-
"webpack": "^5.0.0"
81+
"webpack": "^5.0.0",
82+
"sass": "^1.71.1"
8483
},
8584
"resolutions": {
8685
"minimist": "^1.2.5",

0 commit comments

Comments
 (0)