mirror of
https://github.com/discourse/discourse.git
synced 2025-02-14 00:12:45 +08:00
![David Taylor](/assets/img/avatar_default.png)
Previously, accessing the Rails app directly in development mode would give you assets from our 'legacy' Ember asset pipeline. The only way to run with Ember CLI assets was to run ember-cli as a proxy. This was quite limiting when working on things which are bypassed when using the ember-cli proxy (e.g. changes to `application.html.erb`). Also, since `ember-auto-import` introduced chunking, visiting `/theme-qunit` under Ember CLI was failing to include all necessary chunks. This commit teaches Sprockets about our Ember CLI assets so that they can be used in development mode, and are automatically collected up under `/public/assets` during `assets:precompile`. As a bonus, this allows us to remove all the custom manifest modification from `assets:precompile`. The key changes are: - Introduce a shared `EmberCli.enabled?` helper - When ember-cli is enabled, add ember-cli `/dist/assets` as the top-priority Rails asset directory - Have ember-cli output a `chunks.json` manifest, and teach `preload_script` to read it and append the correct chunks to their associated `afterFile` - Remove most custom ember-cli logic from the `assets:precompile` step. Instead, rely on Rails to take care of pulling the 'precompiled' assets into the `public/assets` directory. Move the 'renaming' logic to runtime, so it can be used in development mode as well. - Remove fingerprinting from `ember-cli-build`, and allow Rails to take care of things Long-term, we may want to replace Sprockets with the lighter-weight Propshaft. The changes made in this commit have been made with that long-term goal in mind. tldr: when you visit the rails app directly, you'll now be served the current ember-cli assets. To keep these up-to-date make sure either `ember serve`, or `ember build --watch` is running. If you really want to load the old non-ember-cli assets, then you should start the server with `EMBER_CLI_PROD_ASSETS=0`. (the legacy asset pipeline will be removed very soon)
139 lines
4.2 KiB
JavaScript
139 lines
4.2 KiB
JavaScript
"use strict";
|
|
|
|
const EmberApp = require("ember-cli/lib/broccoli/ember-app");
|
|
const resolve = require("path").resolve;
|
|
const mergeTrees = require("broccoli-merge-trees");
|
|
const concat = require("broccoli-concat");
|
|
const prettyTextEngine = require("./lib/pretty-text-engine");
|
|
const { createI18nTree } = require("./lib/translation-plugin");
|
|
const discourseScss = require("./lib/discourse-scss");
|
|
const funnel = require("broccoli-funnel");
|
|
|
|
module.exports = function (defaults) {
|
|
let discourseRoot = resolve("../../../..");
|
|
let vendorJs = discourseRoot + "/vendor/assets/javascripts/";
|
|
|
|
const isProduction = EmberApp.env().includes("production");
|
|
const isTest = EmberApp.env().includes("test");
|
|
|
|
let app = new EmberApp(defaults, {
|
|
autoRun: false,
|
|
"ember-qunit": {
|
|
insertContentForTestBody: false,
|
|
},
|
|
sourcemaps: {
|
|
// There seems to be a bug with brocolli-concat when sourcemaps are disabled
|
|
// that causes the `app.import` statements below to fail in production mode.
|
|
// This forces the use of `fast-sourcemap-concat` which works in production.
|
|
enabled: true,
|
|
},
|
|
autoImport: {
|
|
forbidEval: true,
|
|
},
|
|
fingerprint: {
|
|
// Handled by Rails asset pipeline
|
|
enabled: false,
|
|
},
|
|
SRI: {
|
|
// We don't use SRI in Rails. Disable here to match:
|
|
enabled: false,
|
|
},
|
|
|
|
"ember-cli-terser": {
|
|
enabled: isProduction,
|
|
exclude: [
|
|
"**/test-*.js",
|
|
"**/core-tests*.js",
|
|
"**/highlightjs/*",
|
|
"**/javascripts/*",
|
|
],
|
|
},
|
|
|
|
// We need to build tests in prod for theme tests
|
|
tests: true,
|
|
});
|
|
|
|
// Patching a private method is not great, but there's no other way for us to tell
|
|
// Ember CLI that we want the tests alone in a package without helpers/fixtures, since
|
|
// we re-use those in the theme tests.
|
|
app._defaultPackager.packageApplicationTests = function (tree) {
|
|
let appTestTrees = []
|
|
.concat(
|
|
this.packageEmberCliInternalFiles(),
|
|
this.packageTestApplicationConfig(),
|
|
tree
|
|
)
|
|
.filter(Boolean);
|
|
|
|
appTestTrees = mergeTrees(appTestTrees, {
|
|
overwrite: true,
|
|
annotation: "TreeMerger (appTestTrees)",
|
|
});
|
|
|
|
let tests = concat(appTestTrees, {
|
|
inputFiles: ["**/tests/**/*-test.js"],
|
|
headerFiles: ["vendor/ember-cli/tests-prefix.js"],
|
|
footerFiles: ["vendor/ember-cli/app-config.js"],
|
|
outputFile: "/assets/core-tests.js",
|
|
annotation: "Concat: Core Tests",
|
|
sourceMapConfig: false,
|
|
});
|
|
|
|
let testHelpers = concat(appTestTrees, {
|
|
inputFiles: [
|
|
"**/tests/test-boot-ember-cli.js",
|
|
"**/tests/helpers/**/*.js",
|
|
"**/tests/fixtures/**/*.js",
|
|
"**/tests/setup-tests.js",
|
|
],
|
|
outputFile: "/assets/test-helpers.js",
|
|
annotation: "Concat: Test Helpers",
|
|
sourceMapConfig: false,
|
|
});
|
|
|
|
if (isTest) {
|
|
return mergeTrees([
|
|
tests,
|
|
testHelpers,
|
|
discourseScss(`${discourseRoot}/app/assets/stylesheets`, "testem.scss"),
|
|
]);
|
|
} else {
|
|
return mergeTrees([tests, testHelpers]);
|
|
}
|
|
};
|
|
|
|
// WARNING: We should only import scripts here if they are not in NPM.
|
|
// For example: our very specific version of bootstrap-modal.
|
|
app.import(vendorJs + "bootbox.js");
|
|
app.import(vendorJs + "bootstrap-modal.js");
|
|
app.import(vendorJs + "caret_position.js");
|
|
app.import("node_modules/ember-source/dist/ember-template-compiler.js", {
|
|
type: "test",
|
|
});
|
|
app.import(discourseRoot + "/app/assets/javascripts/polyfills.js");
|
|
|
|
app.import(
|
|
discourseRoot +
|
|
"/app/assets/javascripts/discourse/public/assets/scripts/module-shims.js"
|
|
);
|
|
|
|
return mergeTrees([
|
|
createI18nTree(discourseRoot, vendorJs),
|
|
app.toTree(),
|
|
funnel(`${discourseRoot}/public/javascripts`, { destDir: "javascripts" }),
|
|
funnel(`${vendorJs}/highlightjs`, {
|
|
files: ["highlight-test-bundle.min.js"],
|
|
destDir: "assets/highlightjs",
|
|
}),
|
|
concat(mergeTrees([app.options.adminTree]), {
|
|
outputFile: `assets/admin.js`,
|
|
}),
|
|
prettyTextEngine(vendorJs, "discourse-markdown"),
|
|
concat("public/assets/scripts", {
|
|
outputFile: `assets/start-discourse.js`,
|
|
headerFiles: [`start-app.js`],
|
|
inputFiles: [`discourse-boot.js`],
|
|
}),
|
|
]);
|
|
};
|