diff --git a/packages/compat/src/v1-app.ts b/packages/compat/src/v1-app.ts index 10ade69c9..db93f9372 100644 --- a/packages/compat/src/v1-app.ts +++ b/packages/compat/src/v1-app.ts @@ -477,7 +477,7 @@ export default class V1App { }; let nestedInput = buildFunnel(this.combinedStyles(addonTrees), { destDir: 'app/styles' }); - let styles = this.preprocessors.preprocessCss(nestedInput, 'app/styles', '/assets', options); + let styles = this.preprocessors.preprocessCss(nestedInput, '/app/styles', '/assets', options); return new AddToTree(styles, outputPath => { let addonMeta: AddonMeta = { diff --git a/tests/fixtures/preprocess-addon/index.js b/tests/fixtures/preprocess-addon/index.js new file mode 100644 index 000000000..a23cc849a --- /dev/null +++ b/tests/fixtures/preprocess-addon/index.js @@ -0,0 +1,46 @@ +'use strict'; + +const Filter = require('broccoli-persistent-filter'); +const funnel = require('broccoli-funnel'); +const { join } = require('path').posix; + +class Awk extends Filter { + constructor(inputNode, searchReplaceObj) { + super(inputNode, {}); + this.searchReplaceObj = searchReplaceObj; + } + + processString(content) { + let modifiedContent = content; + + Object.entries(this.searchReplaceObj).forEach(([search, replace]) => { + modifiedContent = modifiedContent.replace(search, replace); + }); + + return modifiedContent; + } +} + +module.exports = { + name: require('./package').name, + + setupPreprocessorRegistry(type, registry) { + registry.add('css', { + name: this.name, + ext: 'css', + toTree: (tree, inputPath, outputPath) => { + return funnel(new Awk(tree, { '%%%': inputPath === '/app/styles' ? 'red' : 'blue' }), { + getDestinationPath(relativePath) { + let relativePathWithPrefix = `/${relativePath}`; + + if (relativePathWithPrefix === `${inputPath}/app.css`) { + return join(outputPath, 'app-template.css'); + } + + return join(outputPath, relativePathWithPrefix.replace(inputPath, '')); + }, + }); + }, + }); + }, +}; diff --git a/tests/scenarios/package.json b/tests/scenarios/package.json index 0277b47ae..3a67a0bb5 100644 --- a/tests/scenarios/package.json +++ b/tests/scenarios/package.json @@ -23,27 +23,30 @@ "bootstrap": "^4.3.1", "broccoli-funnel": "^3.0.5", "broccoli-merge-trees": "^3.0.2", + "broccoli-persistent-filter": "^3.1.2", "ember-bootstrap": "^4.6.3", - "ember-cli-latest": "npm:ember-cli@latest", - "ember-cli-beta": "npm:ember-cli@beta", "ember-cli-3.16": "npm:ember-cli@~3.16.0", "ember-cli-3.20": "npm:ember-cli@~3.20.0", "ember-cli-3.24": "npm:ember-cli@~3.24.0", + "ember-cli-beta": "npm:ember-cli@beta", + "ember-cli-latest": "npm:ember-cli@latest", "ember-cli-fastboot": "^2.2.3", + "ember-composable-helpers": "^4.4.1", "ember-data-3.16": "npm:ember-data@~3.16.0", "ember-data-3.20": "npm:ember-data@~3.20.0", "ember-data-3.24": "npm:ember-data@~3.24.0", "ember-data-latest": "npm:ember-data@latest", + "ember-engines": "^0.8.16", + "ember-inline-svg": "^0.2.1", "ember-source-latest": "npm:ember-source@latest", "ember-source-beta": "npm:ember-source@beta", "ember-source-3.16": "npm:ember-source@~3.16.0", "ember-source-3.20": "npm:ember-source@~3.20.0", "ember-source-3.24": "npm:ember-source@~3.24.0", - "fastboot-addon": "link:../../test-packages/fastboot-addon", - "ember-engines": "^0.8.16", - "ember-inline-svg": "^0.2.1", - "ember-composable-helpers": "^4.4.1", - "ember-truth-helpers": "^3.0.0" + "ember-source-beta": "npm:ember-source@beta", + "ember-source-latest": "npm:ember-source@latest", + "ember-truth-helpers": "^3.0.0", + "fastboot-addon": "link:../../test-packages/fastboot-addon" }, "volta": { "node": "14.16.1", diff --git a/tests/scenarios/preprocess-test.ts b/tests/scenarios/preprocess-test.ts new file mode 100644 index 000000000..824f498bb --- /dev/null +++ b/tests/scenarios/preprocess-test.ts @@ -0,0 +1,40 @@ +import { appScenarios } from './scenarios'; +import { PreparedApp, Project } from 'scenario-tester'; +import QUnit from 'qunit'; +import merge from 'lodash/merge'; +import { loadFromFixtureData } from './helpers'; +import { dirname, join } from 'path'; +import fs from 'fs'; +const { module: Qmodule, test } = QUnit; + +appScenarios + .map('preprocess', project => { + let preprocessAddon = Project.fromDir(dirname(require.resolve('../addon-template/package.json')), { + linkDeps: true, + }); + + merge(preprocessAddon.files, loadFromFixtureData('preprocess-addon')); + preprocessAddon.linkDependency('broccoli-funnel', { baseDir: __dirname }); + preprocessAddon.linkDependency('broccoli-persistent-filter', { baseDir: __dirname }); + preprocessAddon.pkg.name = 'preprocess-addon'; + + project.addDevDependency(preprocessAddon); + merge(project.files, { + app: { + styles: { + 'app.css': `body { background: %%%; }`, + }, + }, + }); + }) + .forEachScenario(scenario => { + Qmodule(scenario.name, function () { + test(`yarn test`, async function (assert) { + let app: PreparedApp = await scenario.prepare(); + await app.execute('node ./node_modules/ember-cli/bin/ember b'); + + const data = fs.readFileSync(join(app.dir, 'dist', 'assets', 'app-template.css'), 'utf8'); + assert.equal(data, 'body { background: red; }'); + }); + }); + });